blob: aa35ab9643ac2299e64339db38d607d45bfffdbf [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Sean Christopherson3de63472018-07-13 08:42:30 -0700134#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200193static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200195/* Storage for pre module init parameter parsing */
196static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197
198static const struct {
199 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200200 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200202 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
203 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
204 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
205 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
206 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
207 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200208};
209
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200210#define L1D_CACHE_ORDER 4
211static void *vmx_l1d_flush_pages;
212
213static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
214{
215 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200216 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200217
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200218 if (!enable_ept) {
219 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
220 return 0;
221 }
222
Yi Wangd806afa2018-08-16 13:42:39 +0800223 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
224 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200225
Yi Wangd806afa2018-08-16 13:42:39 +0800226 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
227 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
228 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
229 return 0;
230 }
231 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200232
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200233 /* If set to auto use the default l1tf mitigation method */
234 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
235 switch (l1tf_mitigation) {
236 case L1TF_MITIGATION_OFF:
237 l1tf = VMENTER_L1D_FLUSH_NEVER;
238 break;
239 case L1TF_MITIGATION_FLUSH_NOWARN:
240 case L1TF_MITIGATION_FLUSH:
241 case L1TF_MITIGATION_FLUSH_NOSMT:
242 l1tf = VMENTER_L1D_FLUSH_COND;
243 break;
244 case L1TF_MITIGATION_FULL:
245 case L1TF_MITIGATION_FULL_FORCE:
246 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
247 break;
248 }
249 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
250 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
251 }
252
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200253 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
254 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
255 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
256 if (!page)
257 return -ENOMEM;
258 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200259
260 /*
261 * Initialize each page with a different pattern in
262 * order to protect against KSM in the nested
263 * virtualization case.
264 */
265 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
266 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
267 PAGE_SIZE);
268 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200269 }
270
271 l1tf_vmx_mitigation = l1tf;
272
Thomas Gleixner895ae472018-07-13 16:23:22 +0200273 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
274 static_branch_enable(&vmx_l1d_should_flush);
275 else
276 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200277
Nicolai Stange427362a2018-07-21 22:25:00 +0200278 if (l1tf == VMENTER_L1D_FLUSH_COND)
279 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200280 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200281 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200282 return 0;
283}
284
285static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200286{
287 unsigned int i;
288
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200289 if (s) {
290 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200291 if (vmentry_l1d_param[i].for_parse &&
292 sysfs_streq(s, vmentry_l1d_param[i].option))
293 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200294 }
295 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200296 return -EINVAL;
297}
298
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200299static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
300{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200301 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200302
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303 l1tf = vmentry_l1d_flush_parse(s);
304 if (l1tf < 0)
305 return l1tf;
306
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200307 if (!boot_cpu_has(X86_BUG_L1TF))
308 return 0;
309
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200310 /*
311 * Has vmx_init() run already? If not then this is the pre init
312 * parameter parsing. In that case just store the value and let
313 * vmx_init() do the proper setup after enable_ept has been
314 * established.
315 */
316 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
317 vmentry_l1d_flush_param = l1tf;
318 return 0;
319 }
320
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200321 mutex_lock(&vmx_l1d_flush_mutex);
322 ret = vmx_setup_l1d_flush(l1tf);
323 mutex_unlock(&vmx_l1d_flush_mutex);
324 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200325}
326
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200327static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
328{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200329 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
330 return sprintf(s, "???\n");
331
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200332 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333}
334
335static const struct kernel_param_ops vmentry_l1d_flush_ops = {
336 .set = vmentry_l1d_flush_set,
337 .get = vmentry_l1d_flush_get,
338};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200339module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200340
Tianyu Lan877ad952018-07-19 08:40:23 +0000341enum ept_pointers_status {
342 EPT_POINTERS_CHECK = 0,
343 EPT_POINTERS_MATCH = 1,
344 EPT_POINTERS_MISMATCH = 2
345};
346
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700347struct kvm_vmx {
348 struct kvm kvm;
349
350 unsigned int tss_addr;
351 bool ept_identity_pagetable_done;
352 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000353
354 enum ept_pointers_status ept_pointers_match;
355 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700356};
357
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200358#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300359
Liran Alon392b2f22018-06-23 02:35:01 +0300360struct vmcs_hdr {
361 u32 revision_id:31;
362 u32 shadow_vmcs:1;
363};
364
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400365struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300366 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367 u32 abort;
368 char data[0];
369};
370
Nadav Har'Eld462b812011-05-24 15:26:10 +0300371/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700372 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
373 * and whose values change infrequently, but are not constant. I.e. this is
374 * used as a write-through cache of the corresponding VMCS fields.
375 */
376struct vmcs_host_state {
377 unsigned long cr3; /* May not match real cr3 */
378 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700379 unsigned long gs_base;
380 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700381
382 u16 fs_sel, gs_sel, ldt_sel;
383#ifdef CONFIG_X86_64
384 u16 ds_sel, es_sel;
385#endif
386};
387
388/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300389 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
390 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
391 * loaded on this CPU (so we can clear them if the CPU goes down).
392 */
393struct loaded_vmcs {
394 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700395 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300396 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200397 bool launched;
398 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700399 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100400 /* Support for vnmi-less CPUs */
401 int soft_vnmi_blocked;
402 ktime_t entry_time;
403 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100404 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300405 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700406 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300407};
408
Avi Kivity26bb0982009-09-07 11:14:12 +0300409struct shared_msr_entry {
410 unsigned index;
411 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200412 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300413};
414
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300415/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300416 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
417 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
418 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
419 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
420 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
421 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600422 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300423 * underlying hardware which will be used to run L2.
424 * This structure is packed to ensure that its layout is identical across
425 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700426 *
427 * IMPORTANT: Changing the layout of existing fields in this structure
428 * will break save/restore compatibility with older kvm releases. When
429 * adding new fields, either use space in the reserved padding* arrays
430 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300431 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300432typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300433struct __packed vmcs12 {
434 /* According to the Intel spec, a VMCS region must start with the
435 * following two fields. Then follow implementation-specific data.
436 */
Liran Alon392b2f22018-06-23 02:35:01 +0300437 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300438 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300439
Nadav Har'El27d6c862011-05-25 23:06:59 +0300440 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
441 u32 padding[7]; /* room for future expansion */
442
Nadav Har'El22bd0352011-05-25 23:05:57 +0300443 u64 io_bitmap_a;
444 u64 io_bitmap_b;
445 u64 msr_bitmap;
446 u64 vm_exit_msr_store_addr;
447 u64 vm_exit_msr_load_addr;
448 u64 vm_entry_msr_load_addr;
449 u64 tsc_offset;
450 u64 virtual_apic_page_addr;
451 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800452 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300453 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800454 u64 eoi_exit_bitmap0;
455 u64 eoi_exit_bitmap1;
456 u64 eoi_exit_bitmap2;
457 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800458 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300459 u64 guest_physical_address;
460 u64 vmcs_link_pointer;
461 u64 guest_ia32_debugctl;
462 u64 guest_ia32_pat;
463 u64 guest_ia32_efer;
464 u64 guest_ia32_perf_global_ctrl;
465 u64 guest_pdptr0;
466 u64 guest_pdptr1;
467 u64 guest_pdptr2;
468 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100469 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300470 u64 host_ia32_pat;
471 u64 host_ia32_efer;
472 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700473 u64 vmread_bitmap;
474 u64 vmwrite_bitmap;
475 u64 vm_function_control;
476 u64 eptp_list_address;
477 u64 pml_address;
478 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300479 /*
480 * To allow migration of L1 (complete with its L2 guests) between
481 * machines of different natural widths (32 or 64 bit), we cannot have
482 * unsigned long fields with no explict size. We use u64 (aliased
483 * natural_width) instead. Luckily, x86 is little-endian.
484 */
485 natural_width cr0_guest_host_mask;
486 natural_width cr4_guest_host_mask;
487 natural_width cr0_read_shadow;
488 natural_width cr4_read_shadow;
489 natural_width cr3_target_value0;
490 natural_width cr3_target_value1;
491 natural_width cr3_target_value2;
492 natural_width cr3_target_value3;
493 natural_width exit_qualification;
494 natural_width guest_linear_address;
495 natural_width guest_cr0;
496 natural_width guest_cr3;
497 natural_width guest_cr4;
498 natural_width guest_es_base;
499 natural_width guest_cs_base;
500 natural_width guest_ss_base;
501 natural_width guest_ds_base;
502 natural_width guest_fs_base;
503 natural_width guest_gs_base;
504 natural_width guest_ldtr_base;
505 natural_width guest_tr_base;
506 natural_width guest_gdtr_base;
507 natural_width guest_idtr_base;
508 natural_width guest_dr7;
509 natural_width guest_rsp;
510 natural_width guest_rip;
511 natural_width guest_rflags;
512 natural_width guest_pending_dbg_exceptions;
513 natural_width guest_sysenter_esp;
514 natural_width guest_sysenter_eip;
515 natural_width host_cr0;
516 natural_width host_cr3;
517 natural_width host_cr4;
518 natural_width host_fs_base;
519 natural_width host_gs_base;
520 natural_width host_tr_base;
521 natural_width host_gdtr_base;
522 natural_width host_idtr_base;
523 natural_width host_ia32_sysenter_esp;
524 natural_width host_ia32_sysenter_eip;
525 natural_width host_rsp;
526 natural_width host_rip;
527 natural_width paddingl[8]; /* room for future expansion */
528 u32 pin_based_vm_exec_control;
529 u32 cpu_based_vm_exec_control;
530 u32 exception_bitmap;
531 u32 page_fault_error_code_mask;
532 u32 page_fault_error_code_match;
533 u32 cr3_target_count;
534 u32 vm_exit_controls;
535 u32 vm_exit_msr_store_count;
536 u32 vm_exit_msr_load_count;
537 u32 vm_entry_controls;
538 u32 vm_entry_msr_load_count;
539 u32 vm_entry_intr_info_field;
540 u32 vm_entry_exception_error_code;
541 u32 vm_entry_instruction_len;
542 u32 tpr_threshold;
543 u32 secondary_vm_exec_control;
544 u32 vm_instruction_error;
545 u32 vm_exit_reason;
546 u32 vm_exit_intr_info;
547 u32 vm_exit_intr_error_code;
548 u32 idt_vectoring_info_field;
549 u32 idt_vectoring_error_code;
550 u32 vm_exit_instruction_len;
551 u32 vmx_instruction_info;
552 u32 guest_es_limit;
553 u32 guest_cs_limit;
554 u32 guest_ss_limit;
555 u32 guest_ds_limit;
556 u32 guest_fs_limit;
557 u32 guest_gs_limit;
558 u32 guest_ldtr_limit;
559 u32 guest_tr_limit;
560 u32 guest_gdtr_limit;
561 u32 guest_idtr_limit;
562 u32 guest_es_ar_bytes;
563 u32 guest_cs_ar_bytes;
564 u32 guest_ss_ar_bytes;
565 u32 guest_ds_ar_bytes;
566 u32 guest_fs_ar_bytes;
567 u32 guest_gs_ar_bytes;
568 u32 guest_ldtr_ar_bytes;
569 u32 guest_tr_ar_bytes;
570 u32 guest_interruptibility_info;
571 u32 guest_activity_state;
572 u32 guest_sysenter_cs;
573 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100574 u32 vmx_preemption_timer_value;
575 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300576 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800577 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300578 u16 guest_es_selector;
579 u16 guest_cs_selector;
580 u16 guest_ss_selector;
581 u16 guest_ds_selector;
582 u16 guest_fs_selector;
583 u16 guest_gs_selector;
584 u16 guest_ldtr_selector;
585 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800586 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300587 u16 host_es_selector;
588 u16 host_cs_selector;
589 u16 host_ss_selector;
590 u16 host_ds_selector;
591 u16 host_fs_selector;
592 u16 host_gs_selector;
593 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700594 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300595};
596
597/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700598 * For save/restore compatibility, the vmcs12 field offsets must not change.
599 */
600#define CHECK_OFFSET(field, loc) \
601 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
602 "Offset of " #field " in struct vmcs12 has changed.")
603
604static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300605 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700606 CHECK_OFFSET(abort, 4);
607 CHECK_OFFSET(launch_state, 8);
608 CHECK_OFFSET(io_bitmap_a, 40);
609 CHECK_OFFSET(io_bitmap_b, 48);
610 CHECK_OFFSET(msr_bitmap, 56);
611 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
612 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
613 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
614 CHECK_OFFSET(tsc_offset, 88);
615 CHECK_OFFSET(virtual_apic_page_addr, 96);
616 CHECK_OFFSET(apic_access_addr, 104);
617 CHECK_OFFSET(posted_intr_desc_addr, 112);
618 CHECK_OFFSET(ept_pointer, 120);
619 CHECK_OFFSET(eoi_exit_bitmap0, 128);
620 CHECK_OFFSET(eoi_exit_bitmap1, 136);
621 CHECK_OFFSET(eoi_exit_bitmap2, 144);
622 CHECK_OFFSET(eoi_exit_bitmap3, 152);
623 CHECK_OFFSET(xss_exit_bitmap, 160);
624 CHECK_OFFSET(guest_physical_address, 168);
625 CHECK_OFFSET(vmcs_link_pointer, 176);
626 CHECK_OFFSET(guest_ia32_debugctl, 184);
627 CHECK_OFFSET(guest_ia32_pat, 192);
628 CHECK_OFFSET(guest_ia32_efer, 200);
629 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
630 CHECK_OFFSET(guest_pdptr0, 216);
631 CHECK_OFFSET(guest_pdptr1, 224);
632 CHECK_OFFSET(guest_pdptr2, 232);
633 CHECK_OFFSET(guest_pdptr3, 240);
634 CHECK_OFFSET(guest_bndcfgs, 248);
635 CHECK_OFFSET(host_ia32_pat, 256);
636 CHECK_OFFSET(host_ia32_efer, 264);
637 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
638 CHECK_OFFSET(vmread_bitmap, 280);
639 CHECK_OFFSET(vmwrite_bitmap, 288);
640 CHECK_OFFSET(vm_function_control, 296);
641 CHECK_OFFSET(eptp_list_address, 304);
642 CHECK_OFFSET(pml_address, 312);
643 CHECK_OFFSET(cr0_guest_host_mask, 344);
644 CHECK_OFFSET(cr4_guest_host_mask, 352);
645 CHECK_OFFSET(cr0_read_shadow, 360);
646 CHECK_OFFSET(cr4_read_shadow, 368);
647 CHECK_OFFSET(cr3_target_value0, 376);
648 CHECK_OFFSET(cr3_target_value1, 384);
649 CHECK_OFFSET(cr3_target_value2, 392);
650 CHECK_OFFSET(cr3_target_value3, 400);
651 CHECK_OFFSET(exit_qualification, 408);
652 CHECK_OFFSET(guest_linear_address, 416);
653 CHECK_OFFSET(guest_cr0, 424);
654 CHECK_OFFSET(guest_cr3, 432);
655 CHECK_OFFSET(guest_cr4, 440);
656 CHECK_OFFSET(guest_es_base, 448);
657 CHECK_OFFSET(guest_cs_base, 456);
658 CHECK_OFFSET(guest_ss_base, 464);
659 CHECK_OFFSET(guest_ds_base, 472);
660 CHECK_OFFSET(guest_fs_base, 480);
661 CHECK_OFFSET(guest_gs_base, 488);
662 CHECK_OFFSET(guest_ldtr_base, 496);
663 CHECK_OFFSET(guest_tr_base, 504);
664 CHECK_OFFSET(guest_gdtr_base, 512);
665 CHECK_OFFSET(guest_idtr_base, 520);
666 CHECK_OFFSET(guest_dr7, 528);
667 CHECK_OFFSET(guest_rsp, 536);
668 CHECK_OFFSET(guest_rip, 544);
669 CHECK_OFFSET(guest_rflags, 552);
670 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
671 CHECK_OFFSET(guest_sysenter_esp, 568);
672 CHECK_OFFSET(guest_sysenter_eip, 576);
673 CHECK_OFFSET(host_cr0, 584);
674 CHECK_OFFSET(host_cr3, 592);
675 CHECK_OFFSET(host_cr4, 600);
676 CHECK_OFFSET(host_fs_base, 608);
677 CHECK_OFFSET(host_gs_base, 616);
678 CHECK_OFFSET(host_tr_base, 624);
679 CHECK_OFFSET(host_gdtr_base, 632);
680 CHECK_OFFSET(host_idtr_base, 640);
681 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
682 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
683 CHECK_OFFSET(host_rsp, 664);
684 CHECK_OFFSET(host_rip, 672);
685 CHECK_OFFSET(pin_based_vm_exec_control, 744);
686 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
687 CHECK_OFFSET(exception_bitmap, 752);
688 CHECK_OFFSET(page_fault_error_code_mask, 756);
689 CHECK_OFFSET(page_fault_error_code_match, 760);
690 CHECK_OFFSET(cr3_target_count, 764);
691 CHECK_OFFSET(vm_exit_controls, 768);
692 CHECK_OFFSET(vm_exit_msr_store_count, 772);
693 CHECK_OFFSET(vm_exit_msr_load_count, 776);
694 CHECK_OFFSET(vm_entry_controls, 780);
695 CHECK_OFFSET(vm_entry_msr_load_count, 784);
696 CHECK_OFFSET(vm_entry_intr_info_field, 788);
697 CHECK_OFFSET(vm_entry_exception_error_code, 792);
698 CHECK_OFFSET(vm_entry_instruction_len, 796);
699 CHECK_OFFSET(tpr_threshold, 800);
700 CHECK_OFFSET(secondary_vm_exec_control, 804);
701 CHECK_OFFSET(vm_instruction_error, 808);
702 CHECK_OFFSET(vm_exit_reason, 812);
703 CHECK_OFFSET(vm_exit_intr_info, 816);
704 CHECK_OFFSET(vm_exit_intr_error_code, 820);
705 CHECK_OFFSET(idt_vectoring_info_field, 824);
706 CHECK_OFFSET(idt_vectoring_error_code, 828);
707 CHECK_OFFSET(vm_exit_instruction_len, 832);
708 CHECK_OFFSET(vmx_instruction_info, 836);
709 CHECK_OFFSET(guest_es_limit, 840);
710 CHECK_OFFSET(guest_cs_limit, 844);
711 CHECK_OFFSET(guest_ss_limit, 848);
712 CHECK_OFFSET(guest_ds_limit, 852);
713 CHECK_OFFSET(guest_fs_limit, 856);
714 CHECK_OFFSET(guest_gs_limit, 860);
715 CHECK_OFFSET(guest_ldtr_limit, 864);
716 CHECK_OFFSET(guest_tr_limit, 868);
717 CHECK_OFFSET(guest_gdtr_limit, 872);
718 CHECK_OFFSET(guest_idtr_limit, 876);
719 CHECK_OFFSET(guest_es_ar_bytes, 880);
720 CHECK_OFFSET(guest_cs_ar_bytes, 884);
721 CHECK_OFFSET(guest_ss_ar_bytes, 888);
722 CHECK_OFFSET(guest_ds_ar_bytes, 892);
723 CHECK_OFFSET(guest_fs_ar_bytes, 896);
724 CHECK_OFFSET(guest_gs_ar_bytes, 900);
725 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
726 CHECK_OFFSET(guest_tr_ar_bytes, 908);
727 CHECK_OFFSET(guest_interruptibility_info, 912);
728 CHECK_OFFSET(guest_activity_state, 916);
729 CHECK_OFFSET(guest_sysenter_cs, 920);
730 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
731 CHECK_OFFSET(vmx_preemption_timer_value, 928);
732 CHECK_OFFSET(virtual_processor_id, 960);
733 CHECK_OFFSET(posted_intr_nv, 962);
734 CHECK_OFFSET(guest_es_selector, 964);
735 CHECK_OFFSET(guest_cs_selector, 966);
736 CHECK_OFFSET(guest_ss_selector, 968);
737 CHECK_OFFSET(guest_ds_selector, 970);
738 CHECK_OFFSET(guest_fs_selector, 972);
739 CHECK_OFFSET(guest_gs_selector, 974);
740 CHECK_OFFSET(guest_ldtr_selector, 976);
741 CHECK_OFFSET(guest_tr_selector, 978);
742 CHECK_OFFSET(guest_intr_status, 980);
743 CHECK_OFFSET(host_es_selector, 982);
744 CHECK_OFFSET(host_cs_selector, 984);
745 CHECK_OFFSET(host_ss_selector, 986);
746 CHECK_OFFSET(host_ds_selector, 988);
747 CHECK_OFFSET(host_fs_selector, 990);
748 CHECK_OFFSET(host_gs_selector, 992);
749 CHECK_OFFSET(host_tr_selector, 994);
750 CHECK_OFFSET(guest_pml_index, 996);
751}
752
753/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300754 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
755 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
756 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700757 *
758 * IMPORTANT: Changing this value will break save/restore compatibility with
759 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 */
761#define VMCS12_REVISION 0x11e57ed0
762
763/*
764 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
765 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
766 * current implementation, 4K are reserved to avoid future complications.
767 */
768#define VMCS12_SIZE 0x1000
769
770/*
Jim Mattson5b157062017-12-22 12:11:12 -0800771 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
772 * supported VMCS12 field encoding.
773 */
774#define VMCS12_MAX_FIELD_INDEX 0x17
775
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100776struct nested_vmx_msrs {
777 /*
778 * We only store the "true" versions of the VMX capability MSRs. We
779 * generate the "non-true" versions by setting the must-be-1 bits
780 * according to the SDM.
781 */
782 u32 procbased_ctls_low;
783 u32 procbased_ctls_high;
784 u32 secondary_ctls_low;
785 u32 secondary_ctls_high;
786 u32 pinbased_ctls_low;
787 u32 pinbased_ctls_high;
788 u32 exit_ctls_low;
789 u32 exit_ctls_high;
790 u32 entry_ctls_low;
791 u32 entry_ctls_high;
792 u32 misc_low;
793 u32 misc_high;
794 u32 ept_caps;
795 u32 vpid_caps;
796 u64 basic;
797 u64 cr0_fixed0;
798 u64 cr0_fixed1;
799 u64 cr4_fixed0;
800 u64 cr4_fixed1;
801 u64 vmcs_enum;
802 u64 vmfunc_controls;
803};
804
Jim Mattson5b157062017-12-22 12:11:12 -0800805/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300806 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
807 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
808 */
809struct nested_vmx {
810 /* Has the level1 guest done vmxon? */
811 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400812 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400813 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300814
815 /* The guest-physical address of the current VMCS L1 keeps for L2 */
816 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700817 /*
818 * Cache of the guest's VMCS, existing outside of guest memory.
819 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700820 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700821 */
822 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300823 /*
Liran Alon61ada742018-06-23 02:35:08 +0300824 * Cache of the guest's shadow VMCS, existing outside of guest
825 * memory. Loaded from guest memory during VM entry. Flushed
826 * to guest memory during VM exit.
827 */
828 struct vmcs12 *cached_shadow_vmcs12;
829 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300830 * Indicates if the shadow vmcs must be updated with the
831 * data hold by vmcs12
832 */
833 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100834 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300835
Jim Mattson8d860bb2018-05-09 16:56:05 -0400836 bool change_vmcs01_virtual_apic_mode;
837
Nadav Har'El644d7112011-05-25 23:12:35 +0300838 /* L2 must run next, and mustn't decide to exit to L1. */
839 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600840
841 struct loaded_vmcs vmcs02;
842
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300843 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600844 * Guest pages referred to in the vmcs02 with host-physical
845 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300846 */
847 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800848 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800849 struct page *pi_desc_page;
850 struct pi_desc *pi_desc;
851 bool pi_pending;
852 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100853
854 struct hrtimer preemption_timer;
855 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200856
857 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
858 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300859 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800860
Wanpeng Li5c614b32015-10-13 09:18:36 -0700861 u16 vpid02;
862 u16 last_vpid;
863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100864 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200865
866 /* SMM related state */
867 struct {
868 /* in VMX operation on SMM entry? */
869 bool vmxon;
870 /* in guest mode on SMM entry? */
871 bool guest_mode;
872 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300873};
874
Yang Zhang01e439b2013-04-11 19:25:12 +0800875#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800876#define POSTED_INTR_SN 1
877
Yang Zhang01e439b2013-04-11 19:25:12 +0800878/* Posted-Interrupt Descriptor */
879struct pi_desc {
880 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800881 union {
882 struct {
883 /* bit 256 - Outstanding Notification */
884 u16 on : 1,
885 /* bit 257 - Suppress Notification */
886 sn : 1,
887 /* bit 271:258 - Reserved */
888 rsvd_1 : 14;
889 /* bit 279:272 - Notification Vector */
890 u8 nv;
891 /* bit 287:280 - Reserved */
892 u8 rsvd_2;
893 /* bit 319:288 - Notification Destination */
894 u32 ndst;
895 };
896 u64 control;
897 };
898 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800899} __aligned(64);
900
Yang Zhanga20ed542013-04-11 19:25:15 +0800901static bool pi_test_and_set_on(struct pi_desc *pi_desc)
902{
903 return test_and_set_bit(POSTED_INTR_ON,
904 (unsigned long *)&pi_desc->control);
905}
906
907static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
908{
909 return test_and_clear_bit(POSTED_INTR_ON,
910 (unsigned long *)&pi_desc->control);
911}
912
913static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
914{
915 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
916}
917
Feng Wuebbfc762015-09-18 22:29:46 +0800918static inline void pi_clear_sn(struct pi_desc *pi_desc)
919{
920 return clear_bit(POSTED_INTR_SN,
921 (unsigned long *)&pi_desc->control);
922}
923
924static inline void pi_set_sn(struct pi_desc *pi_desc)
925{
926 return set_bit(POSTED_INTR_SN,
927 (unsigned long *)&pi_desc->control);
928}
929
Paolo Bonziniad361092016-09-20 16:15:05 +0200930static inline void pi_clear_on(struct pi_desc *pi_desc)
931{
932 clear_bit(POSTED_INTR_ON,
933 (unsigned long *)&pi_desc->control);
934}
935
Feng Wuebbfc762015-09-18 22:29:46 +0800936static inline int pi_test_on(struct pi_desc *pi_desc)
937{
938 return test_bit(POSTED_INTR_ON,
939 (unsigned long *)&pi_desc->control);
940}
941
942static inline int pi_test_sn(struct pi_desc *pi_desc)
943{
944 return test_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400948struct vmx_msrs {
949 unsigned int nr;
950 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
951};
952
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400953struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000954 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300955 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300956 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100957 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300958 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200959 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200960 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300961 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400962 int nmsrs;
963 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800964 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400965#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300966 u64 msr_host_kernel_gs_base;
967 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100969
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100970 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100971 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100972
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 u32 vm_entry_controls_shadow;
974 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200975 u32 secondary_exec_control;
976
Nadav Har'Eld462b812011-05-24 15:26:10 +0300977 /*
978 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
979 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700980 * guest (L2), it points to a different VMCS. loaded_cpu_state points
981 * to the VMCS whose state is loaded into the CPU registers that only
982 * need to be switched when transitioning to/from the kernel; a NULL
983 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984 */
985 struct loaded_vmcs vmcs01;
986 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700987 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300989 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400990 struct vmx_msrs guest;
991 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300992 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700993
Avi Kivity9c8cba32007-11-22 11:42:59 +0200994 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300995 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300996 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300997 struct kvm_segment segs[8];
998 } rmode;
999 struct {
1000 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001001 struct kvm_save_segment {
1002 u16 selector;
1003 unsigned long base;
1004 u32 limit;
1005 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001006 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001007 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001008 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001009 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001010
Andi Kleena0861c02009-06-08 17:37:09 +08001011 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001012
Yang Zhang01e439b2013-04-11 19:25:12 +08001013 /* Posted interrupt descriptor */
1014 struct pi_desc pi_desc;
1015
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001016 /* Support for a guest hypervisor (nested VMX) */
1017 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001018
1019 /* Dynamic PLE window. */
1020 int ple_window;
1021 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001022
Sean Christophersond264ee02018-08-27 15:21:12 -07001023 bool req_immediate_exit;
1024
Kai Huang843e4332015-01-28 10:54:28 +08001025 /* Support for PML */
1026#define PML_ENTITY_NUM 512
1027 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001028
Yunhong Jiang64672c92016-06-13 14:19:59 -07001029 /* apic deadline value in host tsc */
1030 u64 hv_deadline_tsc;
1031
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001032 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001033
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001034 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001035
Wanpeng Li74c55932017-11-29 01:31:20 -08001036 unsigned long host_debugctlmsr;
1037
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001038 /*
1039 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1040 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1041 * in msr_ia32_feature_control_valid_bits.
1042 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001043 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001044 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001045 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046};
1047
Avi Kivity2fb92db2011-04-27 19:42:18 +03001048enum segment_cache_field {
1049 SEG_FIELD_SEL = 0,
1050 SEG_FIELD_BASE = 1,
1051 SEG_FIELD_LIMIT = 2,
1052 SEG_FIELD_AR = 3,
1053
1054 SEG_FIELD_NR = 4
1055};
1056
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001057static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1058{
1059 return container_of(kvm, struct kvm_vmx, kvm);
1060}
1061
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1063{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001064 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065}
1066
Feng Wuefc64402015-09-18 22:29:51 +08001067static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1068{
1069 return &(to_vmx(vcpu)->pi_desc);
1070}
1071
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001072#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001073#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1075#define FIELD64(number, name) \
1076 FIELD(number, name), \
1077 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078
Abel Gordon4607c2d2013-04-18 14:35:55 +03001079
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001080static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001081#define SHADOW_FIELD_RO(x) x,
1082#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001083};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001084static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001085 ARRAY_SIZE(shadow_read_only_fields);
1086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001087static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001088#define SHADOW_FIELD_RW(x) x,
1089#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001091static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001092 ARRAY_SIZE(shadow_read_write_fields);
1093
Mathias Krause772e0312012-08-30 01:30:19 +02001094static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001095 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001096 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1098 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1099 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1100 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1101 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1102 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1103 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1104 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001105 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001106 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001107 FIELD(HOST_ES_SELECTOR, host_es_selector),
1108 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1109 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1110 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1111 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1112 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1113 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1114 FIELD64(IO_BITMAP_A, io_bitmap_a),
1115 FIELD64(IO_BITMAP_B, io_bitmap_b),
1116 FIELD64(MSR_BITMAP, msr_bitmap),
1117 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1118 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1119 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001120 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD64(TSC_OFFSET, tsc_offset),
1122 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1123 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001124 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001125 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001126 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001127 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1128 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1129 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1130 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001131 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001132 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1133 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001134 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001135 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1136 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1137 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1138 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1139 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1140 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1141 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1142 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1143 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1144 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001145 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1147 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1148 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1149 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1150 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1151 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1152 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1153 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1154 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1155 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1156 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1157 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1158 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1159 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1160 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1161 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1162 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1163 FIELD(TPR_THRESHOLD, tpr_threshold),
1164 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1165 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1166 FIELD(VM_EXIT_REASON, vm_exit_reason),
1167 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1168 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1169 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1170 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1171 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1172 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1173 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1174 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1175 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1176 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1177 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1178 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1179 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1180 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1181 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1182 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1183 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1184 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1185 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1186 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1187 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1188 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1189 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1190 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1191 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1192 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1193 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1194 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001195 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001196 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1197 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1198 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1199 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1200 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1201 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1202 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1203 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1204 FIELD(EXIT_QUALIFICATION, exit_qualification),
1205 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1206 FIELD(GUEST_CR0, guest_cr0),
1207 FIELD(GUEST_CR3, guest_cr3),
1208 FIELD(GUEST_CR4, guest_cr4),
1209 FIELD(GUEST_ES_BASE, guest_es_base),
1210 FIELD(GUEST_CS_BASE, guest_cs_base),
1211 FIELD(GUEST_SS_BASE, guest_ss_base),
1212 FIELD(GUEST_DS_BASE, guest_ds_base),
1213 FIELD(GUEST_FS_BASE, guest_fs_base),
1214 FIELD(GUEST_GS_BASE, guest_gs_base),
1215 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1216 FIELD(GUEST_TR_BASE, guest_tr_base),
1217 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1218 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1219 FIELD(GUEST_DR7, guest_dr7),
1220 FIELD(GUEST_RSP, guest_rsp),
1221 FIELD(GUEST_RIP, guest_rip),
1222 FIELD(GUEST_RFLAGS, guest_rflags),
1223 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1224 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1225 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1226 FIELD(HOST_CR0, host_cr0),
1227 FIELD(HOST_CR3, host_cr3),
1228 FIELD(HOST_CR4, host_cr4),
1229 FIELD(HOST_FS_BASE, host_fs_base),
1230 FIELD(HOST_GS_BASE, host_gs_base),
1231 FIELD(HOST_TR_BASE, host_tr_base),
1232 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1233 FIELD(HOST_IDTR_BASE, host_idtr_base),
1234 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1235 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1236 FIELD(HOST_RSP, host_rsp),
1237 FIELD(HOST_RIP, host_rip),
1238};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001239
1240static inline short vmcs_field_to_offset(unsigned long field)
1241{
Dan Williams085331d2018-01-31 17:47:03 -08001242 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1243 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001244 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001245
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001246 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001247 return -ENOENT;
1248
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001249 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001250 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001251 return -ENOENT;
1252
Linus Torvalds15303ba2018-02-10 13:16:35 -08001253 index = array_index_nospec(index, size);
1254 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001255 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001257 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001258}
1259
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001260static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1261{
David Matlack4f2777b2016-07-13 17:16:37 -07001262 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001263}
1264
Liran Alon61ada742018-06-23 02:35:08 +03001265static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1266{
1267 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1268}
1269
Peter Feiner995f00a2017-06-30 17:26:32 -07001270static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001271static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001272static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001273static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001274static void vmx_set_segment(struct kvm_vcpu *vcpu,
1275 struct kvm_segment *var, int seg);
1276static void vmx_get_segment(struct kvm_vcpu *vcpu,
1277 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001278static bool guest_state_valid(struct kvm_vcpu *vcpu);
1279static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001280static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001281static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1282static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1283static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1284 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001285static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001286static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1287 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1290static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001291/*
1292 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1293 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1294 */
1295static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Feng Wubf9f6ac2015-09-18 22:29:55 +08001297/*
1298 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1299 * can find which vCPU should be waken up.
1300 */
1301static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1302static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1303
Radim Krčmář23611332016-09-29 22:41:33 +02001304enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001305 VMX_VMREAD_BITMAP,
1306 VMX_VMWRITE_BITMAP,
1307 VMX_BITMAP_NR
1308};
1309
1310static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1311
Radim Krčmář23611332016-09-29 22:41:33 +02001312#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1313#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001314
Avi Kivity110312c2010-12-21 12:54:20 +02001315static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001316static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001317
Sheng Yang2384d2b2008-01-17 15:14:33 +08001318static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1319static DEFINE_SPINLOCK(vmx_vpid_lock);
1320
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 int size;
1323 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001324 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001326 u32 pin_based_exec_ctrl;
1327 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001328 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329 u32 vmexit_ctrl;
1330 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001331 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
Hannes Ederefff9e52008-11-28 17:02:06 +01001334static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001335 u32 ept;
1336 u32 vpid;
1337} vmx_capability;
1338
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339#define VMX_SEGMENT_FIELD(seg) \
1340 [VCPU_SREG_##seg] = { \
1341 .selector = GUEST_##seg##_SELECTOR, \
1342 .base = GUEST_##seg##_BASE, \
1343 .limit = GUEST_##seg##_LIMIT, \
1344 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1345 }
1346
Mathias Krause772e0312012-08-30 01:30:19 +02001347static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 unsigned selector;
1349 unsigned base;
1350 unsigned limit;
1351 unsigned ar_bytes;
1352} kvm_vmx_segment_fields[] = {
1353 VMX_SEGMENT_FIELD(CS),
1354 VMX_SEGMENT_FIELD(DS),
1355 VMX_SEGMENT_FIELD(ES),
1356 VMX_SEGMENT_FIELD(FS),
1357 VMX_SEGMENT_FIELD(GS),
1358 VMX_SEGMENT_FIELD(SS),
1359 VMX_SEGMENT_FIELD(TR),
1360 VMX_SEGMENT_FIELD(LDTR),
1361};
1362
Avi Kivity26bb0982009-09-07 11:14:12 +03001363static u64 host_efer;
1364
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001365static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1366
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001367/*
Brian Gerst8c065852010-07-17 09:03:26 -04001368 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001369 * away by decrementing the array size.
1370 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001372#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001373 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001375 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001378DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1379
1380#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1381
1382#define KVM_EVMCS_VERSION 1
1383
1384#if IS_ENABLED(CONFIG_HYPERV)
1385static bool __read_mostly enlightened_vmcs = true;
1386module_param(enlightened_vmcs, bool, 0444);
1387
1388static inline void evmcs_write64(unsigned long field, u64 value)
1389{
1390 u16 clean_field;
1391 int offset = get_evmcs_offset(field, &clean_field);
1392
1393 if (offset < 0)
1394 return;
1395
1396 *(u64 *)((char *)current_evmcs + offset) = value;
1397
1398 current_evmcs->hv_clean_fields &= ~clean_field;
1399}
1400
1401static inline void evmcs_write32(unsigned long field, u32 value)
1402{
1403 u16 clean_field;
1404 int offset = get_evmcs_offset(field, &clean_field);
1405
1406 if (offset < 0)
1407 return;
1408
1409 *(u32 *)((char *)current_evmcs + offset) = value;
1410 current_evmcs->hv_clean_fields &= ~clean_field;
1411}
1412
1413static inline void evmcs_write16(unsigned long field, u16 value)
1414{
1415 u16 clean_field;
1416 int offset = get_evmcs_offset(field, &clean_field);
1417
1418 if (offset < 0)
1419 return;
1420
1421 *(u16 *)((char *)current_evmcs + offset) = value;
1422 current_evmcs->hv_clean_fields &= ~clean_field;
1423}
1424
1425static inline u64 evmcs_read64(unsigned long field)
1426{
1427 int offset = get_evmcs_offset(field, NULL);
1428
1429 if (offset < 0)
1430 return 0;
1431
1432 return *(u64 *)((char *)current_evmcs + offset);
1433}
1434
1435static inline u32 evmcs_read32(unsigned long field)
1436{
1437 int offset = get_evmcs_offset(field, NULL);
1438
1439 if (offset < 0)
1440 return 0;
1441
1442 return *(u32 *)((char *)current_evmcs + offset);
1443}
1444
1445static inline u16 evmcs_read16(unsigned long field)
1446{
1447 int offset = get_evmcs_offset(field, NULL);
1448
1449 if (offset < 0)
1450 return 0;
1451
1452 return *(u16 *)((char *)current_evmcs + offset);
1453}
1454
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001455static inline void evmcs_touch_msr_bitmap(void)
1456{
1457 if (unlikely(!current_evmcs))
1458 return;
1459
1460 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1461 current_evmcs->hv_clean_fields &=
1462 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1463}
1464
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001465static void evmcs_load(u64 phys_addr)
1466{
1467 struct hv_vp_assist_page *vp_ap =
1468 hv_get_vp_assist_page(smp_processor_id());
1469
1470 vp_ap->current_nested_vmcs = phys_addr;
1471 vp_ap->enlighten_vmentry = 1;
1472}
1473
1474static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1475{
1476 /*
1477 * Enlightened VMCSv1 doesn't support these:
1478 *
1479 * POSTED_INTR_NV = 0x00000002,
1480 * GUEST_INTR_STATUS = 0x00000810,
1481 * APIC_ACCESS_ADDR = 0x00002014,
1482 * POSTED_INTR_DESC_ADDR = 0x00002016,
1483 * EOI_EXIT_BITMAP0 = 0x0000201c,
1484 * EOI_EXIT_BITMAP1 = 0x0000201e,
1485 * EOI_EXIT_BITMAP2 = 0x00002020,
1486 * EOI_EXIT_BITMAP3 = 0x00002022,
1487 */
1488 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1489 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1490 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1492 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1493 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1494 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1495
1496 /*
1497 * GUEST_PML_INDEX = 0x00000812,
1498 * PML_ADDRESS = 0x0000200e,
1499 */
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1501
1502 /* VM_FUNCTION_CONTROL = 0x00002018, */
1503 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1504
1505 /*
1506 * EPTP_LIST_ADDRESS = 0x00002024,
1507 * VMREAD_BITMAP = 0x00002026,
1508 * VMWRITE_BITMAP = 0x00002028,
1509 */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1511
1512 /*
1513 * TSC_MULTIPLIER = 0x00002032,
1514 */
1515 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1516
1517 /*
1518 * PLE_GAP = 0x00004020,
1519 * PLE_WINDOW = 0x00004022,
1520 */
1521 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1522
1523 /*
1524 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1525 */
1526 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1527
1528 /*
1529 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1530 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1531 */
1532 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1533 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1534
1535 /*
1536 * Currently unsupported in KVM:
1537 * GUEST_IA32_RTIT_CTL = 0x00002814,
1538 */
1539}
Tianyu Lan877ad952018-07-19 08:40:23 +00001540
1541/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1542static void check_ept_pointer_match(struct kvm *kvm)
1543{
1544 struct kvm_vcpu *vcpu;
1545 u64 tmp_eptp = INVALID_PAGE;
1546 int i;
1547
1548 kvm_for_each_vcpu(i, vcpu, kvm) {
1549 if (!VALID_PAGE(tmp_eptp)) {
1550 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1551 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1552 to_kvm_vmx(kvm)->ept_pointers_match
1553 = EPT_POINTERS_MISMATCH;
1554 return;
1555 }
1556 }
1557
1558 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1559}
1560
1561static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1562{
1563 int ret;
1564
1565 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1566
1567 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1568 check_ept_pointer_match(kvm);
1569
1570 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1571 ret = -ENOTSUPP;
1572 goto out;
1573 }
1574
1575 ret = hyperv_flush_guest_mapping(
1576 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1577
1578out:
1579 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1580 return ret;
1581}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001582#else /* !IS_ENABLED(CONFIG_HYPERV) */
1583static inline void evmcs_write64(unsigned long field, u64 value) {}
1584static inline void evmcs_write32(unsigned long field, u32 value) {}
1585static inline void evmcs_write16(unsigned long field, u16 value) {}
1586static inline u64 evmcs_read64(unsigned long field) { return 0; }
1587static inline u32 evmcs_read32(unsigned long field) { return 0; }
1588static inline u16 evmcs_read16(unsigned long field) { return 0; }
1589static inline void evmcs_load(u64 phys_addr) {}
1590static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001591static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001592#endif /* IS_ENABLED(CONFIG_HYPERV) */
1593
Jan Kiszka5bb16012016-02-09 20:14:21 +01001594static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1597 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001598 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1599}
1600
Jan Kiszka6f054852016-02-09 20:15:18 +01001601static inline bool is_debug(u32 intr_info)
1602{
1603 return is_exception_n(intr_info, DB_VECTOR);
1604}
1605
1606static inline bool is_breakpoint(u32 intr_info)
1607{
1608 return is_exception_n(intr_info, BP_VECTOR);
1609}
1610
Jan Kiszka5bb16012016-02-09 20:14:21 +01001611static inline bool is_page_fault(u32 intr_info)
1612{
1613 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001617{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001619}
1620
Liran Alon9e869482018-03-12 13:12:51 +02001621static inline bool is_gp_fault(u32 intr_info)
1622{
1623 return is_exception_n(intr_info, GP_VECTOR);
1624}
1625
Gui Jianfeng31299942010-03-15 17:29:09 +08001626static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001627{
1628 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1629 INTR_INFO_VALID_MASK)) ==
1630 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1631}
1632
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001633/* Undocumented: icebp/int1 */
1634static inline bool is_icebp(u32 intr_info)
1635{
1636 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1637 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1638}
1639
Gui Jianfeng31299942010-03-15 17:29:09 +08001640static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001641{
Sheng Yang04547152009-04-01 15:52:31 +08001642 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001643}
1644
Gui Jianfeng31299942010-03-15 17:29:09 +08001645static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001646{
Sheng Yang04547152009-04-01 15:52:31 +08001647 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001648}
1649
Paolo Bonzini35754c92015-07-29 12:05:37 +02001650static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001651{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001652 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001653}
1654
Gui Jianfeng31299942010-03-15 17:29:09 +08001655static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001656{
Sheng Yang04547152009-04-01 15:52:31 +08001657 return vmcs_config.cpu_based_exec_ctrl &
1658 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001659}
1660
Avi Kivity774ead32007-12-26 13:57:04 +02001661static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001662{
Sheng Yang04547152009-04-01 15:52:31 +08001663 return vmcs_config.cpu_based_2nd_exec_ctrl &
1664 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1665}
1666
Yang Zhang8d146952013-01-25 10:18:50 +08001667static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1668{
1669 return vmcs_config.cpu_based_2nd_exec_ctrl &
1670 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1671}
1672
Yang Zhang83d4c282013-01-25 10:18:49 +08001673static inline bool cpu_has_vmx_apic_register_virt(void)
1674{
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1677}
1678
Yang Zhangc7c9c562013-01-25 10:18:51 +08001679static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl &
1682 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1683}
1684
Sean Christopherson0b665d32018-08-14 09:33:34 -07001685static inline bool cpu_has_vmx_encls_vmexit(void)
1686{
1687 return vmcs_config.cpu_based_2nd_exec_ctrl &
1688 SECONDARY_EXEC_ENCLS_EXITING;
1689}
1690
Yunhong Jiang64672c92016-06-13 14:19:59 -07001691/*
1692 * Comment's format: document - errata name - stepping - processor name.
1693 * Refer from
1694 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1695 */
1696static u32 vmx_preemption_cpu_tfms[] = {
1697/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16980x000206E6,
1699/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1700/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1701/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17020x00020652,
1703/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17040x00020655,
1705/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1706/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1707/*
1708 * 320767.pdf - AAP86 - B1 -
1709 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1710 */
17110x000106E5,
1712/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17130x000106A0,
1714/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17150x000106A1,
1716/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17170x000106A4,
1718 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1719 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1720 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17210x000106A5,
1722};
1723
1724static inline bool cpu_has_broken_vmx_preemption_timer(void)
1725{
1726 u32 eax = cpuid_eax(0x00000001), i;
1727
1728 /* Clear the reserved bits */
1729 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001730 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001731 if (eax == vmx_preemption_cpu_tfms[i])
1732 return true;
1733
1734 return false;
1735}
1736
1737static inline bool cpu_has_vmx_preemption_timer(void)
1738{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001739 return vmcs_config.pin_based_exec_ctrl &
1740 PIN_BASED_VMX_PREEMPTION_TIMER;
1741}
1742
Yang Zhang01e439b2013-04-11 19:25:12 +08001743static inline bool cpu_has_vmx_posted_intr(void)
1744{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001745 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1746 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001747}
1748
1749static inline bool cpu_has_vmx_apicv(void)
1750{
1751 return cpu_has_vmx_apic_register_virt() &&
1752 cpu_has_vmx_virtual_intr_delivery() &&
1753 cpu_has_vmx_posted_intr();
1754}
1755
Sheng Yang04547152009-04-01 15:52:31 +08001756static inline bool cpu_has_vmx_flexpriority(void)
1757{
1758 return cpu_has_vmx_tpr_shadow() &&
1759 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001760}
1761
Marcelo Tosattie7997942009-06-11 12:07:40 -03001762static inline bool cpu_has_vmx_ept_execute_only(void)
1763{
Gui Jianfeng31299942010-03-15 17:29:09 +08001764 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001765}
1766
Marcelo Tosattie7997942009-06-11 12:07:40 -03001767static inline bool cpu_has_vmx_ept_2m_page(void)
1768{
Gui Jianfeng31299942010-03-15 17:29:09 +08001769 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001770}
1771
Sheng Yang878403b2010-01-05 19:02:29 +08001772static inline bool cpu_has_vmx_ept_1g_page(void)
1773{
Gui Jianfeng31299942010-03-15 17:29:09 +08001774 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001775}
1776
Sheng Yang4bc9b982010-06-02 14:05:24 +08001777static inline bool cpu_has_vmx_ept_4levels(void)
1778{
1779 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1780}
1781
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001782static inline bool cpu_has_vmx_ept_mt_wb(void)
1783{
1784 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1785}
1786
Yu Zhang855feb62017-08-24 20:27:55 +08001787static inline bool cpu_has_vmx_ept_5levels(void)
1788{
1789 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1790}
1791
Xudong Hao83c3a332012-05-28 19:33:35 +08001792static inline bool cpu_has_vmx_ept_ad_bits(void)
1793{
1794 return vmx_capability.ept & VMX_EPT_AD_BIT;
1795}
1796
Gui Jianfeng31299942010-03-15 17:29:09 +08001797static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001798{
Gui Jianfeng31299942010-03-15 17:29:09 +08001799 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001800}
1801
Gui Jianfeng31299942010-03-15 17:29:09 +08001802static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001803{
Gui Jianfeng31299942010-03-15 17:29:09 +08001804 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001805}
1806
Liran Aloncd9a4912018-05-22 17:16:15 +03001807static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1808{
1809 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1810}
1811
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001812static inline bool cpu_has_vmx_invvpid_single(void)
1813{
1814 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1815}
1816
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001817static inline bool cpu_has_vmx_invvpid_global(void)
1818{
1819 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1820}
1821
Wanpeng Li08d839c2017-03-23 05:30:08 -07001822static inline bool cpu_has_vmx_invvpid(void)
1823{
1824 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1825}
1826
Gui Jianfeng31299942010-03-15 17:29:09 +08001827static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001828{
Sheng Yang04547152009-04-01 15:52:31 +08001829 return vmcs_config.cpu_based_2nd_exec_ctrl &
1830 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001831}
1832
Gui Jianfeng31299942010-03-15 17:29:09 +08001833static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001834{
1835 return vmcs_config.cpu_based_2nd_exec_ctrl &
1836 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1837}
1838
Gui Jianfeng31299942010-03-15 17:29:09 +08001839static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001840{
1841 return vmcs_config.cpu_based_2nd_exec_ctrl &
1842 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1843}
1844
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001845static inline bool cpu_has_vmx_basic_inout(void)
1846{
1847 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1848}
1849
Paolo Bonzini35754c92015-07-29 12:05:37 +02001850static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001851{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001852 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001853}
1854
Gui Jianfeng31299942010-03-15 17:29:09 +08001855static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001856{
Sheng Yang04547152009-04-01 15:52:31 +08001857 return vmcs_config.cpu_based_2nd_exec_ctrl &
1858 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001859}
1860
Gui Jianfeng31299942010-03-15 17:29:09 +08001861static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001862{
1863 return vmcs_config.cpu_based_2nd_exec_ctrl &
1864 SECONDARY_EXEC_RDTSCP;
1865}
1866
Mao, Junjiead756a12012-07-02 01:18:48 +00001867static inline bool cpu_has_vmx_invpcid(void)
1868{
1869 return vmcs_config.cpu_based_2nd_exec_ctrl &
1870 SECONDARY_EXEC_ENABLE_INVPCID;
1871}
1872
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001873static inline bool cpu_has_virtual_nmis(void)
1874{
1875 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1876}
1877
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001878static inline bool cpu_has_vmx_wbinvd_exit(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_WBINVD_EXITING;
1882}
1883
Abel Gordonabc4fc52013-04-18 14:35:25 +03001884static inline bool cpu_has_vmx_shadow_vmcs(void)
1885{
1886 u64 vmx_msr;
1887 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1888 /* check if the cpu supports writing r/o exit information fields */
1889 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1890 return false;
1891
1892 return vmcs_config.cpu_based_2nd_exec_ctrl &
1893 SECONDARY_EXEC_SHADOW_VMCS;
1894}
1895
Kai Huang843e4332015-01-28 10:54:28 +08001896static inline bool cpu_has_vmx_pml(void)
1897{
1898 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1899}
1900
Haozhong Zhang64903d62015-10-20 15:39:09 +08001901static inline bool cpu_has_vmx_tsc_scaling(void)
1902{
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_TSC_SCALING;
1905}
1906
Bandan Das2a499e42017-08-03 15:54:41 -04001907static inline bool cpu_has_vmx_vmfunc(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl &
1910 SECONDARY_EXEC_ENABLE_VMFUNC;
1911}
1912
Sean Christopherson64f7a112018-04-30 10:01:06 -07001913static bool vmx_umip_emulated(void)
1914{
1915 return vmcs_config.cpu_based_2nd_exec_ctrl &
1916 SECONDARY_EXEC_DESC;
1917}
1918
Sheng Yang04547152009-04-01 15:52:31 +08001919static inline bool report_flexpriority(void)
1920{
1921 return flexpriority_enabled;
1922}
1923
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001924static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1925{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001926 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001927}
1928
Jim Mattsonf4160e42018-05-29 09:11:33 -07001929/*
1930 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1931 * to modify any valid field of the VMCS, or are the VM-exit
1932 * information fields read-only?
1933 */
1934static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1935{
1936 return to_vmx(vcpu)->nested.msrs.misc_low &
1937 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1938}
1939
Marc Orr04473782018-06-20 17:21:29 -07001940static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1941{
1942 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1943}
1944
1945static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1948 CPU_BASED_MONITOR_TRAP_FLAG;
1949}
1950
Liran Alonfa97d7d2018-07-18 14:07:59 +02001951static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1954 SECONDARY_EXEC_SHADOW_VMCS;
1955}
1956
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001957static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1958{
1959 return vmcs12->cpu_based_vm_exec_control & bit;
1960}
1961
1962static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1963{
1964 return (vmcs12->cpu_based_vm_exec_control &
1965 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1966 (vmcs12->secondary_vm_exec_control & bit);
1967}
1968
Jan Kiszkaf4124502014-03-07 20:03:13 +01001969static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1970{
1971 return vmcs12->pin_based_vm_exec_control &
1972 PIN_BASED_VMX_PREEMPTION_TIMER;
1973}
1974
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001975static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1976{
1977 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1978}
1979
1980static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1983}
1984
Nadav Har'El155a97a2013-08-05 11:07:16 +03001985static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1986{
1987 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1988}
1989
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001990static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1991{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001992 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001993}
1994
Bandan Dasc5f983f2017-05-05 15:25:14 -04001995static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1996{
1997 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1998}
1999
Wincy Vanf2b93282015-02-03 23:56:03 +08002000static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2001{
2002 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2003}
2004
Wanpeng Li5c614b32015-10-13 09:18:36 -07002005static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2006{
2007 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2008}
2009
Wincy Van82f0dd42015-02-03 23:57:18 +08002010static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2011{
2012 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2013}
2014
Wincy Van608406e2015-02-03 23:57:51 +08002015static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2016{
2017 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2018}
2019
Wincy Van705699a2015-02-03 23:58:17 +08002020static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2021{
2022 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2023}
2024
Bandan Das27c42a12017-08-03 15:54:42 -04002025static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2026{
2027 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2028}
2029
Bandan Das41ab9372017-08-03 15:54:43 -04002030static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2031{
2032 return nested_cpu_has_vmfunc(vmcs12) &&
2033 (vmcs12->vm_function_control &
2034 VMX_VMFUNC_EPTP_SWITCHING);
2035}
2036
Liran Alonf792d272018-06-23 02:35:05 +03002037static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2038{
2039 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2040}
2041
Jim Mattsonef85b672016-12-12 11:01:37 -08002042static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002043{
2044 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002045 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002046}
2047
Jan Kiszka533558b2014-01-04 18:47:20 +01002048static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2049 u32 exit_intr_info,
2050 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002051
Rusty Russell8b9cf982007-07-30 16:31:43 +10002052static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002053{
2054 int i;
2055
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002056 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002057 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002058 return i;
2059 return -1;
2060}
2061
Sheng Yang2384d2b2008-01-17 15:14:33 +08002062static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2063{
2064 struct {
2065 u64 vpid : 16;
2066 u64 rsvd : 48;
2067 u64 gva;
2068 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002069 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002070
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002071 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2072 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2073 : "memory");
2074 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002075}
2076
Sheng Yang14394422008-04-28 12:24:45 +08002077static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2078{
2079 struct {
2080 u64 eptp, gpa;
2081 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002082 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002083
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002084 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2085 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2086 : "memory");
2087 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002088}
2089
Avi Kivity26bb0982009-09-07 11:14:12 +03002090static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002091{
2092 int i;
2093
Rusty Russell8b9cf982007-07-30 16:31:43 +10002094 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002095 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002096 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002097 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002098}
2099
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100static void vmcs_clear(struct vmcs *vmcs)
2101{
2102 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002103 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002105 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2106 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2107 : "memory");
2108 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2110 vmcs, phys_addr);
2111}
2112
Nadav Har'Eld462b812011-05-24 15:26:10 +03002113static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2114{
2115 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002116 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2117 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002118 loaded_vmcs->cpu = -1;
2119 loaded_vmcs->launched = 0;
2120}
2121
Dongxiao Xu7725b892010-05-11 18:29:38 +08002122static void vmcs_load(struct vmcs *vmcs)
2123{
2124 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002125 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002126
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002127 if (static_branch_unlikely(&enable_evmcs))
2128 return evmcs_load(phys_addr);
2129
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002130 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2131 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2132 : "memory");
2133 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002134 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002135 vmcs, phys_addr);
2136}
2137
Dave Young2965faa2015-09-09 15:38:55 -07002138#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002139/*
2140 * This bitmap is used to indicate whether the vmclear
2141 * operation is enabled on all cpus. All disabled by
2142 * default.
2143 */
2144static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2145
2146static inline void crash_enable_local_vmclear(int cpu)
2147{
2148 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2149}
2150
2151static inline void crash_disable_local_vmclear(int cpu)
2152{
2153 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2154}
2155
2156static inline int crash_local_vmclear_enabled(int cpu)
2157{
2158 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2159}
2160
2161static void crash_vmclear_local_loaded_vmcss(void)
2162{
2163 int cpu = raw_smp_processor_id();
2164 struct loaded_vmcs *v;
2165
2166 if (!crash_local_vmclear_enabled(cpu))
2167 return;
2168
2169 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2170 loaded_vmcss_on_cpu_link)
2171 vmcs_clear(v->vmcs);
2172}
2173#else
2174static inline void crash_enable_local_vmclear(int cpu) { }
2175static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002176#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002177
Nadav Har'Eld462b812011-05-24 15:26:10 +03002178static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002180 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002181 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182
Nadav Har'Eld462b812011-05-24 15:26:10 +03002183 if (loaded_vmcs->cpu != cpu)
2184 return; /* vcpu migration can race with cpu offline */
2185 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002187 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002188 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002189
2190 /*
2191 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2192 * is before setting loaded_vmcs->vcpu to -1 which is done in
2193 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2194 * then adds the vmcs into percpu list before it is deleted.
2195 */
2196 smp_wmb();
2197
Nadav Har'Eld462b812011-05-24 15:26:10 +03002198 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002199 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200}
2201
Nadav Har'Eld462b812011-05-24 15:26:10 +03002202static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002203{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002204 int cpu = loaded_vmcs->cpu;
2205
2206 if (cpu != -1)
2207 smp_call_function_single(cpu,
2208 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002209}
2210
Junaid Shahidfaff8752018-06-29 13:10:05 -07002211static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2212{
2213 if (vpid == 0)
2214 return true;
2215
2216 if (cpu_has_vmx_invvpid_individual_addr()) {
2217 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2218 return true;
2219 }
2220
2221 return false;
2222}
2223
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002224static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002225{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002226 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002227 return;
2228
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002229 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002230 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002231}
2232
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002233static inline void vpid_sync_vcpu_global(void)
2234{
2235 if (cpu_has_vmx_invvpid_global())
2236 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2237}
2238
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002239static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002240{
2241 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002242 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002243 else
2244 vpid_sync_vcpu_global();
2245}
2246
Sheng Yang14394422008-04-28 12:24:45 +08002247static inline void ept_sync_global(void)
2248{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002249 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002250}
2251
2252static inline void ept_sync_context(u64 eptp)
2253{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002254 if (cpu_has_vmx_invept_context())
2255 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2256 else
2257 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002258}
2259
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002260static __always_inline void vmcs_check16(unsigned long field)
2261{
2262 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2263 "16-bit accessor invalid for 64-bit field");
2264 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2265 "16-bit accessor invalid for 64-bit high field");
2266 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2267 "16-bit accessor invalid for 32-bit high field");
2268 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2269 "16-bit accessor invalid for natural width field");
2270}
2271
2272static __always_inline void vmcs_check32(unsigned long field)
2273{
2274 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2275 "32-bit accessor invalid for 16-bit field");
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2277 "32-bit accessor invalid for natural width field");
2278}
2279
2280static __always_inline void vmcs_check64(unsigned long field)
2281{
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2283 "64-bit accessor invalid for 16-bit field");
2284 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2285 "64-bit accessor invalid for 64-bit high field");
2286 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2287 "64-bit accessor invalid for 32-bit field");
2288 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2289 "64-bit accessor invalid for natural width field");
2290}
2291
2292static __always_inline void vmcs_checkl(unsigned long field)
2293{
2294 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2295 "Natural width accessor invalid for 16-bit field");
2296 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2297 "Natural width accessor invalid for 64-bit field");
2298 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2299 "Natural width accessor invalid for 64-bit high field");
2300 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2301 "Natural width accessor invalid for 32-bit field");
2302}
2303
2304static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305{
Avi Kivity5e520e62011-05-15 10:13:12 -04002306 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307
Avi Kivity5e520e62011-05-15 10:13:12 -04002308 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2309 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 return value;
2311}
2312
Avi Kivity96304212011-05-15 10:13:13 -04002313static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002315 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002316 if (static_branch_unlikely(&enable_evmcs))
2317 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002318 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319}
2320
Avi Kivity96304212011-05-15 10:13:13 -04002321static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002323 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002324 if (static_branch_unlikely(&enable_evmcs))
2325 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002326 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327}
2328
Avi Kivity96304212011-05-15 10:13:13 -04002329static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002331 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002332 if (static_branch_unlikely(&enable_evmcs))
2333 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002334#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002335 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338#endif
2339}
2340
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002341static __always_inline unsigned long vmcs_readl(unsigned long field)
2342{
2343 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002344 if (static_branch_unlikely(&enable_evmcs))
2345 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002346 return __vmcs_readl(field);
2347}
2348
Avi Kivitye52de1b2007-01-05 16:36:56 -08002349static noinline void vmwrite_error(unsigned long field, unsigned long value)
2350{
2351 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2352 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2353 dump_stack();
2354}
2355
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002356static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002358 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002360 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2361 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002362 if (unlikely(error))
2363 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364}
2365
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002366static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002368 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002369 if (static_branch_unlikely(&enable_evmcs))
2370 return evmcs_write16(field, value);
2371
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002372 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373}
2374
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002378 if (static_branch_unlikely(&enable_evmcs))
2379 return evmcs_write32(field, value);
2380
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002381 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382}
2383
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002384static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002387 if (static_branch_unlikely(&enable_evmcs))
2388 return evmcs_write64(field, value);
2389
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002390 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002391#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002393 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394#endif
2395}
2396
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002398{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002399 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002400 if (static_branch_unlikely(&enable_evmcs))
2401 return evmcs_write64(field, value);
2402
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002403 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002404}
2405
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002407{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002408 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2409 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002410 if (static_branch_unlikely(&enable_evmcs))
2411 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2412
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2414}
2415
2416static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2417{
2418 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2419 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002420 if (static_branch_unlikely(&enable_evmcs))
2421 return evmcs_write32(field, evmcs_read32(field) | mask);
2422
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002423 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002424}
2425
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002426static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2427{
2428 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2429}
2430
Gleb Natapov2961e8762013-11-25 15:37:13 +02002431static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2432{
2433 vmcs_write32(VM_ENTRY_CONTROLS, val);
2434 vmx->vm_entry_controls_shadow = val;
2435}
2436
2437static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2438{
2439 if (vmx->vm_entry_controls_shadow != val)
2440 vm_entry_controls_init(vmx, val);
2441}
2442
2443static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2444{
2445 return vmx->vm_entry_controls_shadow;
2446}
2447
2448
2449static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2450{
2451 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2452}
2453
2454static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2455{
2456 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2457}
2458
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002459static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2460{
2461 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2462}
2463
Gleb Natapov2961e8762013-11-25 15:37:13 +02002464static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2465{
2466 vmcs_write32(VM_EXIT_CONTROLS, val);
2467 vmx->vm_exit_controls_shadow = val;
2468}
2469
2470static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2471{
2472 if (vmx->vm_exit_controls_shadow != val)
2473 vm_exit_controls_init(vmx, val);
2474}
2475
2476static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2477{
2478 return vmx->vm_exit_controls_shadow;
2479}
2480
2481
2482static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2483{
2484 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2485}
2486
2487static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2488{
2489 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2490}
2491
Avi Kivity2fb92db2011-04-27 19:42:18 +03002492static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2493{
2494 vmx->segment_cache.bitmask = 0;
2495}
2496
2497static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2498 unsigned field)
2499{
2500 bool ret;
2501 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2502
2503 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2504 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2505 vmx->segment_cache.bitmask = 0;
2506 }
2507 ret = vmx->segment_cache.bitmask & mask;
2508 vmx->segment_cache.bitmask |= mask;
2509 return ret;
2510}
2511
2512static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2513{
2514 u16 *p = &vmx->segment_cache.seg[seg].selector;
2515
2516 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2517 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2518 return *p;
2519}
2520
2521static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2522{
2523 ulong *p = &vmx->segment_cache.seg[seg].base;
2524
2525 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2526 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2527 return *p;
2528}
2529
2530static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2531{
2532 u32 *p = &vmx->segment_cache.seg[seg].limit;
2533
2534 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2535 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2536 return *p;
2537}
2538
2539static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2540{
2541 u32 *p = &vmx->segment_cache.seg[seg].ar;
2542
2543 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2544 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2545 return *p;
2546}
2547
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002548static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2549{
2550 u32 eb;
2551
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002552 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002553 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002554 /*
2555 * Guest access to VMware backdoor ports could legitimately
2556 * trigger #GP because of TSS I/O permission bitmap.
2557 * We intercept those #GP and allow access to them anyway
2558 * as VMware does.
2559 */
2560 if (enable_vmware_backdoor)
2561 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002562 if ((vcpu->guest_debug &
2563 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2564 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2565 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002566 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002567 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002568 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002569 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002570
2571 /* When we are running a nested L2 guest and L1 specified for it a
2572 * certain exception bitmap, we must trap the same exceptions and pass
2573 * them to L1. When running L2, we will only handle the exceptions
2574 * specified above if L1 did not want them.
2575 */
2576 if (is_guest_mode(vcpu))
2577 eb |= get_vmcs12(vcpu)->exception_bitmap;
2578
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002579 vmcs_write32(EXCEPTION_BITMAP, eb);
2580}
2581
Ashok Raj15d45072018-02-01 22:59:43 +01002582/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002583 * Check if MSR is intercepted for currently loaded MSR bitmap.
2584 */
2585static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2586{
2587 unsigned long *msr_bitmap;
2588 int f = sizeof(unsigned long);
2589
2590 if (!cpu_has_vmx_msr_bitmap())
2591 return true;
2592
2593 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2594
2595 if (msr <= 0x1fff) {
2596 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2597 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2598 msr &= 0x1fff;
2599 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2600 }
2601
2602 return true;
2603}
2604
2605/*
Ashok Raj15d45072018-02-01 22:59:43 +01002606 * Check if MSR is intercepted for L01 MSR bitmap.
2607 */
2608static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2609{
2610 unsigned long *msr_bitmap;
2611 int f = sizeof(unsigned long);
2612
2613 if (!cpu_has_vmx_msr_bitmap())
2614 return true;
2615
2616 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2617
2618 if (msr <= 0x1fff) {
2619 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2620 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2621 msr &= 0x1fff;
2622 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2623 }
2624
2625 return true;
2626}
2627
Gleb Natapov2961e8762013-11-25 15:37:13 +02002628static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2629 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002630{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002631 vm_entry_controls_clearbit(vmx, entry);
2632 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002633}
2634
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002635static int find_msr(struct vmx_msrs *m, unsigned int msr)
2636{
2637 unsigned int i;
2638
2639 for (i = 0; i < m->nr; ++i) {
2640 if (m->val[i].index == msr)
2641 return i;
2642 }
2643 return -ENOENT;
2644}
2645
Avi Kivity61d2ef22010-04-28 16:40:38 +03002646static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2647{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002648 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002649 struct msr_autoload *m = &vmx->msr_autoload;
2650
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002651 switch (msr) {
2652 case MSR_EFER:
2653 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002654 clear_atomic_switch_msr_special(vmx,
2655 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002656 VM_EXIT_LOAD_IA32_EFER);
2657 return;
2658 }
2659 break;
2660 case MSR_CORE_PERF_GLOBAL_CTRL:
2661 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002662 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002663 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2664 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2665 return;
2666 }
2667 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002668 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002669 i = find_msr(&m->guest, msr);
2670 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002671 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002672 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002673 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002674 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002675
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002676skip_guest:
2677 i = find_msr(&m->host, msr);
2678 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002679 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002680
2681 --m->host.nr;
2682 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002683 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002684}
2685
Gleb Natapov2961e8762013-11-25 15:37:13 +02002686static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2687 unsigned long entry, unsigned long exit,
2688 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2689 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002690{
2691 vmcs_write64(guest_val_vmcs, guest_val);
2692 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002693 vm_entry_controls_setbit(vmx, entry);
2694 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002695}
2696
Avi Kivity61d2ef22010-04-28 16:40:38 +03002697static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002698 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002699{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002700 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002701 struct msr_autoload *m = &vmx->msr_autoload;
2702
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002703 switch (msr) {
2704 case MSR_EFER:
2705 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002706 add_atomic_switch_msr_special(vmx,
2707 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002708 VM_EXIT_LOAD_IA32_EFER,
2709 GUEST_IA32_EFER,
2710 HOST_IA32_EFER,
2711 guest_val, host_val);
2712 return;
2713 }
2714 break;
2715 case MSR_CORE_PERF_GLOBAL_CTRL:
2716 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002717 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002718 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2719 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2720 GUEST_IA32_PERF_GLOBAL_CTRL,
2721 HOST_IA32_PERF_GLOBAL_CTRL,
2722 guest_val, host_val);
2723 return;
2724 }
2725 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002726 case MSR_IA32_PEBS_ENABLE:
2727 /* PEBS needs a quiescent period after being disabled (to write
2728 * a record). Disabling PEBS through VMX MSR swapping doesn't
2729 * provide that period, so a CPU could write host's record into
2730 * guest's memory.
2731 */
2732 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002733 }
2734
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002735 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002736 if (!entry_only)
2737 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002738
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002739 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002740 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002741 "Can't add msr %x\n", msr);
2742 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002743 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002744 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002745 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002746 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002747 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002748 m->guest.val[i].index = msr;
2749 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002750
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002751 if (entry_only)
2752 return;
2753
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002754 if (j < 0) {
2755 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002756 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002757 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002758 m->host.val[j].index = msr;
2759 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002760}
2761
Avi Kivity92c0d902009-10-29 11:00:16 +02002762static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002763{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002764 u64 guest_efer = vmx->vcpu.arch.efer;
2765 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002766
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002767 if (!enable_ept) {
2768 /*
2769 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2770 * host CPUID is more efficient than testing guest CPUID
2771 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2772 */
2773 if (boot_cpu_has(X86_FEATURE_SMEP))
2774 guest_efer |= EFER_NX;
2775 else if (!(guest_efer & EFER_NX))
2776 ignore_bits |= EFER_NX;
2777 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002778
Avi Kivity51c6cf62007-08-29 03:48:05 +03002779 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002780 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002781 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002782 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002783#ifdef CONFIG_X86_64
2784 ignore_bits |= EFER_LMA | EFER_LME;
2785 /* SCE is meaningful only in long mode on Intel */
2786 if (guest_efer & EFER_LMA)
2787 ignore_bits &= ~(u64)EFER_SCE;
2788#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002789
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002790 /*
2791 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2792 * On CPUs that support "load IA32_EFER", always switch EFER
2793 * atomically, since it's faster than switching it manually.
2794 */
2795 if (cpu_has_load_ia32_efer ||
2796 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002797 if (!(guest_efer & EFER_LMA))
2798 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002799 if (guest_efer != host_efer)
2800 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002801 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002802 else
2803 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002804 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002805 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002806 clear_atomic_switch_msr(vmx, MSR_EFER);
2807
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002808 guest_efer &= ~ignore_bits;
2809 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002810
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002811 vmx->guest_msrs[efer_offset].data = guest_efer;
2812 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2813
2814 return true;
2815 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002816}
2817
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002818#ifdef CONFIG_X86_32
2819/*
2820 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2821 * VMCS rather than the segment table. KVM uses this helper to figure
2822 * out the current bases to poke them into the VMCS before entry.
2823 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002824static unsigned long segment_base(u16 selector)
2825{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002826 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002827 unsigned long v;
2828
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002829 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002830 return 0;
2831
Thomas Garnier45fc8752017-03-14 10:05:08 -07002832 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002834 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002835 u16 ldt_selector = kvm_read_ldt();
2836
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 return 0;
2839
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002840 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002841 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002842 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002843 return v;
2844}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002845#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002846
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002847static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002848{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002850 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002851#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002852 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002853#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002854 unsigned long fs_base, gs_base;
2855 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002856 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002857
Sean Christophersond264ee02018-08-27 15:21:12 -07002858 vmx->req_immediate_exit = false;
2859
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002860 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002861 return;
2862
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002863 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002864 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002865
Avi Kivity33ed6322007-05-02 16:54:03 +03002866 /*
2867 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2868 * allow segment selectors with cpl > 0 or ti == 1.
2869 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002870 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002871
2872#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002873 savesegment(ds, host_state->ds_sel);
2874 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002875
2876 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002877 if (likely(is_64bit_mm(current->mm))) {
2878 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002879 fs_sel = current->thread.fsindex;
2880 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002881 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002882 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002883 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002884 savesegment(fs, fs_sel);
2885 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002886 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002887 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002888 }
2889
Paolo Bonzini4679b612018-09-24 17:23:01 +02002890 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002891#else
Sean Christophersone368b872018-07-23 12:32:41 -07002892 savesegment(fs, fs_sel);
2893 savesegment(gs, gs_sel);
2894 fs_base = segment_base(fs_sel);
2895 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002896#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002897
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002898 if (unlikely(fs_sel != host_state->fs_sel)) {
2899 if (!(fs_sel & 7))
2900 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2901 else
2902 vmcs_write16(HOST_FS_SELECTOR, 0);
2903 host_state->fs_sel = fs_sel;
2904 }
2905 if (unlikely(gs_sel != host_state->gs_sel)) {
2906 if (!(gs_sel & 7))
2907 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2908 else
2909 vmcs_write16(HOST_GS_SELECTOR, 0);
2910 host_state->gs_sel = gs_sel;
2911 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002912 if (unlikely(fs_base != host_state->fs_base)) {
2913 vmcs_writel(HOST_FS_BASE, fs_base);
2914 host_state->fs_base = fs_base;
2915 }
2916 if (unlikely(gs_base != host_state->gs_base)) {
2917 vmcs_writel(HOST_GS_BASE, gs_base);
2918 host_state->gs_base = gs_base;
2919 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002920
Avi Kivity26bb0982009-09-07 11:14:12 +03002921 for (i = 0; i < vmx->save_nmsrs; ++i)
2922 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002923 vmx->guest_msrs[i].data,
2924 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002925}
2926
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002927static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002928{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002929 struct vmcs_host_state *host_state;
2930
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002931 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002932 return;
2933
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002934 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002935 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002936
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002937 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002938 vmx->loaded_cpu_state = NULL;
2939
Avi Kivityc8770e72010-11-11 12:37:26 +02002940#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002941 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002942#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002943 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2944 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002945#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002946 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002947#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002948 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002949#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002950 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002951 if (host_state->fs_sel & 7)
2952 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002953#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002954 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2955 loadsegment(ds, host_state->ds_sel);
2956 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002957 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002958#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002959 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002960#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002961 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002962#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002963 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002964}
2965
Sean Christopherson678e3152018-07-23 12:32:43 -07002966#ifdef CONFIG_X86_64
2967static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002968{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002969 preempt_disable();
2970 if (vmx->loaded_cpu_state)
2971 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2972 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002973 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002974}
2975
Sean Christopherson678e3152018-07-23 12:32:43 -07002976static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2977{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002978 preempt_disable();
2979 if (vmx->loaded_cpu_state)
2980 wrmsrl(MSR_KERNEL_GS_BASE, data);
2981 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002982 vmx->msr_guest_kernel_gs_base = data;
2983}
2984#endif
2985
Feng Wu28b835d2015-09-18 22:29:54 +08002986static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2987{
2988 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2989 struct pi_desc old, new;
2990 unsigned int dest;
2991
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002992 /*
2993 * In case of hot-plug or hot-unplug, we may have to undo
2994 * vmx_vcpu_pi_put even if there is no assigned device. And we
2995 * always keep PI.NDST up to date for simplicity: it makes the
2996 * code easier, and CPU migration is not a fast path.
2997 */
2998 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002999 return;
3000
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003001 /*
3002 * First handle the simple case where no cmpxchg is necessary; just
3003 * allow posting non-urgent interrupts.
3004 *
3005 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3006 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3007 * expects the VCPU to be on the blocked_vcpu_list that matches
3008 * PI.NDST.
3009 */
3010 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3011 vcpu->cpu == cpu) {
3012 pi_clear_sn(pi_desc);
3013 return;
3014 }
3015
3016 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003017 do {
3018 old.control = new.control = pi_desc->control;
3019
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003020 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003021
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003022 if (x2apic_enabled())
3023 new.ndst = dest;
3024 else
3025 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003026
Feng Wu28b835d2015-09-18 22:29:54 +08003027 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003028 } while (cmpxchg64(&pi_desc->control, old.control,
3029 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003030}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003031
Peter Feinerc95ba922016-08-17 09:36:47 -07003032static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3033{
3034 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3035 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3036}
3037
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038/*
3039 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3040 * vcpu mutex is already taken.
3041 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003042static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003044 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003045 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003047 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003048 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003049 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003050 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003051
3052 /*
3053 * Read loaded_vmcs->cpu should be before fetching
3054 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3055 * See the comments in __loaded_vmcs_clear().
3056 */
3057 smp_rmb();
3058
Nadav Har'Eld462b812011-05-24 15:26:10 +03003059 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3060 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003061 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003062 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003063 }
3064
3065 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3066 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3067 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003068 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003069 }
3070
3071 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003072 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003073 unsigned long sysenter_esp;
3074
3075 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003076
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 /*
3078 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003079 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003081 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003082 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003083 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003085 /*
3086 * VM exits change the host TR limit to 0x67 after a VM
3087 * exit. This is okay, since 0x67 covers everything except
3088 * the IO bitmap and have have code to handle the IO bitmap
3089 * being lost after a VM exit.
3090 */
3091 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3092
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3094 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003095
Nadav Har'Eld462b812011-05-24 15:26:10 +03003096 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 }
Feng Wu28b835d2015-09-18 22:29:54 +08003098
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003099 /* Setup TSC multiplier */
3100 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003101 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3102 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003103
Feng Wu28b835d2015-09-18 22:29:54 +08003104 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003105 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003106 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003107}
3108
3109static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3110{
3111 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3112
3113 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003114 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3115 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003116 return;
3117
3118 /* Set SN when the vCPU is preempted */
3119 if (vcpu->preempted)
3120 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121}
3122
3123static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3124{
Feng Wu28b835d2015-09-18 22:29:54 +08003125 vmx_vcpu_pi_put(vcpu);
3126
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003127 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128}
3129
Wanpeng Lif244dee2017-07-20 01:11:54 -07003130static bool emulation_required(struct kvm_vcpu *vcpu)
3131{
3132 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3133}
3134
Avi Kivityedcafe32009-12-30 18:07:40 +02003135static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3136
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003137/*
3138 * Return the cr0 value that a nested guest would read. This is a combination
3139 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3140 * its hypervisor (cr0_read_shadow).
3141 */
3142static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3143{
3144 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3145 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3146}
3147static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3148{
3149 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3150 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3151}
3152
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3154{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003155 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003156
Avi Kivity6de12732011-03-07 12:51:22 +02003157 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3158 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3159 rflags = vmcs_readl(GUEST_RFLAGS);
3160 if (to_vmx(vcpu)->rmode.vm86_active) {
3161 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3162 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3163 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3164 }
3165 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003166 }
Avi Kivity6de12732011-03-07 12:51:22 +02003167 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168}
3169
3170static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3171{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003172 unsigned long old_rflags = vmx_get_rflags(vcpu);
3173
Avi Kivity6de12732011-03-07 12:51:22 +02003174 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3175 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003176 if (to_vmx(vcpu)->rmode.vm86_active) {
3177 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003178 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003179 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003181
3182 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3183 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184}
3185
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003186static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003187{
3188 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3189 int ret = 0;
3190
3191 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003192 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003193 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003194 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003195
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003196 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003197}
3198
3199static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3200{
3201 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3202 u32 interruptibility = interruptibility_old;
3203
3204 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3205
Jan Kiszka48005f62010-02-19 19:38:07 +01003206 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003207 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003208 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209 interruptibility |= GUEST_INTR_STATE_STI;
3210
3211 if ((interruptibility != interruptibility_old))
3212 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3213}
3214
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3216{
3217 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003219 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003221 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222
Glauber Costa2809f5d2009-05-12 16:21:05 -04003223 /* skipping an emulated instruction also counts */
3224 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003227static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3228 unsigned long exit_qual)
3229{
3230 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3231 unsigned int nr = vcpu->arch.exception.nr;
3232 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3233
3234 if (vcpu->arch.exception.has_error_code) {
3235 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3236 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3237 }
3238
3239 if (kvm_exception_is_soft(nr))
3240 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3241 else
3242 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3243
3244 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3245 vmx_get_nmi_mask(vcpu))
3246 intr_info |= INTR_INFO_UNBLOCK_NMI;
3247
3248 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3249}
3250
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003251/*
3252 * KVM wants to inject page-faults which it got to the guest. This function
3253 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003254 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003255static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003256{
3257 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003258 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003259
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003260 if (nr == PF_VECTOR) {
3261 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003262 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003263 return 1;
3264 }
3265 /*
3266 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3267 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3268 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3269 * can be written only when inject_pending_event runs. This should be
3270 * conditional on a new capability---if the capability is disabled,
3271 * kvm_multiple_exception would write the ancillary information to
3272 * CR2 or DR6, for backwards ABI-compatibility.
3273 */
3274 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3275 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003276 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003277 return 1;
3278 }
3279 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003280 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003281 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003282 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003283 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3284 *exit_qual ^= DR6_RTM;
3285 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003286 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003287 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003288 return 1;
3289 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003290 }
3291
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003292 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003293}
3294
Wanpeng Licaa057a2018-03-12 04:53:03 -07003295static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3296{
3297 /*
3298 * Ensure that we clear the HLT state in the VMCS. We don't need to
3299 * explicitly skip the instruction because if the HLT state is set,
3300 * then the instruction is already executing and RIP has already been
3301 * advanced.
3302 */
3303 if (kvm_hlt_in_guest(vcpu->kvm) &&
3304 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3305 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3306}
3307
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003308static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003309{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003311 unsigned nr = vcpu->arch.exception.nr;
3312 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003313 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003314 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003315
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003316 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003317 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003318 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3319 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003320
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003321 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003322 int inc_eip = 0;
3323 if (kvm_exception_is_soft(nr))
3324 inc_eip = vcpu->arch.event_exit_inst_len;
3325 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003326 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003327 return;
3328 }
3329
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003330 WARN_ON_ONCE(vmx->emulation_required);
3331
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003332 if (kvm_exception_is_soft(nr)) {
3333 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3334 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003335 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3336 } else
3337 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3338
3339 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003340
3341 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003342}
3343
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003344static bool vmx_rdtscp_supported(void)
3345{
3346 return cpu_has_vmx_rdtscp();
3347}
3348
Mao, Junjiead756a12012-07-02 01:18:48 +00003349static bool vmx_invpcid_supported(void)
3350{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003351 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003352}
3353
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354/*
Eddie Donga75beee2007-05-17 18:55:15 +03003355 * Swap MSR entry in host/guest MSR entry array.
3356 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003357static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003358{
Avi Kivity26bb0982009-09-07 11:14:12 +03003359 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003360
3361 tmp = vmx->guest_msrs[to];
3362 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3363 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003364}
3365
3366/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003367 * Set up the vmcs to automatically save and restore system
3368 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3369 * mode, as fiddling with msrs is very expensive.
3370 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003371static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003372{
Avi Kivity26bb0982009-09-07 11:14:12 +03003373 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003374
Eddie Donga75beee2007-05-17 18:55:15 +03003375 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003376#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003377 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003378 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003379 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003380 move_msr_up(vmx, index, save_nmsrs++);
3381 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003382 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003383 move_msr_up(vmx, index, save_nmsrs++);
3384 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003385 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003386 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003387 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003388 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003389 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003390 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003391 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003392 * if efer.sce is enabled.
3393 */
Brian Gerst8c065852010-07-17 09:03:26 -04003394 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003395 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003396 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003397 }
Eddie Donga75beee2007-05-17 18:55:15 +03003398#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003399 index = __find_msr_index(vmx, MSR_EFER);
3400 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003401 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003402
Avi Kivity26bb0982009-09-07 11:14:12 +03003403 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003404
Yang Zhang8d146952013-01-25 10:18:50 +08003405 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003406 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003407}
3408
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003409static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003411 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003413 if (is_guest_mode(vcpu) &&
3414 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3415 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3416
3417 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418}
3419
3420/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003421 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003423static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003425 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003426 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003427 * We're here if L1 chose not to trap WRMSR to TSC. According
3428 * to the spec, this should set L1's TSC; The offset that L1
3429 * set for L2 remains unchanged, and still needs to be added
3430 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003431 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003432 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003433 /* recalculate vmcs02.TSC_OFFSET: */
3434 vmcs12 = get_vmcs12(vcpu);
3435 vmcs_write64(TSC_OFFSET, offset +
3436 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3437 vmcs12->tsc_offset : 0));
3438 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003439 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3440 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003441 vmcs_write64(TSC_OFFSET, offset);
3442 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443}
3444
Nadav Har'El801d3422011-05-25 23:02:23 +03003445/*
3446 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3447 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3448 * all guests if the "nested" module option is off, and can also be disabled
3449 * for a single guest by disabling its VMX cpuid bit.
3450 */
3451static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3452{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003453 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003454}
3455
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003457 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3458 * returned for the various VMX controls MSRs when nested VMX is enabled.
3459 * The same values should also be used to verify that vmcs12 control fields are
3460 * valid during nested entry from L1 to L2.
3461 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3462 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3463 * bit in the high half is on if the corresponding bit in the control field
3464 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003465 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003466static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003467{
Paolo Bonzini13893092018-02-26 13:40:09 +01003468 if (!nested) {
3469 memset(msrs, 0, sizeof(*msrs));
3470 return;
3471 }
3472
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003473 /*
3474 * Note that as a general rule, the high half of the MSRs (bits in
3475 * the control fields which may be 1) should be initialized by the
3476 * intersection of the underlying hardware's MSR (i.e., features which
3477 * can be supported) and the list of features we want to expose -
3478 * because they are known to be properly supported in our code.
3479 * Also, usually, the low half of the MSRs (bits which must be 1) can
3480 * be set to 0, meaning that L1 may turn off any of these bits. The
3481 * reason is that if one of these bits is necessary, it will appear
3482 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3483 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003484 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003485 * These rules have exceptions below.
3486 */
3487
3488 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003489 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003490 msrs->pinbased_ctls_low,
3491 msrs->pinbased_ctls_high);
3492 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003493 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003494 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003495 PIN_BASED_EXT_INTR_MASK |
3496 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003497 PIN_BASED_VIRTUAL_NMIS |
3498 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003499 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003500 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003501 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003502
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003503 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003504 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 msrs->exit_ctls_low,
3506 msrs->exit_ctls_high);
3507 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003508 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003509
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003511#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003512 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003513#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003514 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003516 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003517 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003518 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3519
Jan Kiszka2996fca2014-06-16 13:59:43 +02003520 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003521 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003522
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003523 /* entry controls */
3524 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003525 msrs->entry_ctls_low,
3526 msrs->entry_ctls_high);
3527 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003528 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003530#ifdef CONFIG_X86_64
3531 VM_ENTRY_IA32E_MODE |
3532#endif
3533 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003534 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003535 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003536
Jan Kiszka2996fca2014-06-16 13:59:43 +02003537 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003538 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003539
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003540 /* cpu-based controls */
3541 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003542 msrs->procbased_ctls_low,
3543 msrs->procbased_ctls_high);
3544 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003545 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003547 CPU_BASED_VIRTUAL_INTR_PENDING |
3548 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003549 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3550 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3551 CPU_BASED_CR3_STORE_EXITING |
3552#ifdef CONFIG_X86_64
3553 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3554#endif
3555 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003556 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3557 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3558 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3559 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003560 /*
3561 * We can allow some features even when not supported by the
3562 * hardware. For example, L1 can specify an MSR bitmap - and we
3563 * can use it to avoid exits to L1 - even when L0 runs L2
3564 * without MSR bitmaps.
3565 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003566 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003567 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003568 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003569
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003570 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003571 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003572 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3573
Paolo Bonzini80154d72017-08-24 13:55:35 +02003574 /*
3575 * secondary cpu-based controls. Do not include those that
3576 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3577 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003578 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003579 msrs->secondary_ctls_low,
3580 msrs->secondary_ctls_high);
3581 msrs->secondary_ctls_low = 0;
3582 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003583 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003584 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003585 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003586 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003587 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003588
Liran Alon32c7acf2018-06-23 02:35:11 +03003589 /*
3590 * We can emulate "VMCS shadowing," even if the hardware
3591 * doesn't support it.
3592 */
3593 msrs->secondary_ctls_high |=
3594 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003595
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003596 if (enable_ept) {
3597 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003598 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003599 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003600 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003601 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003602 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003603 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003604 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003605 msrs->ept_caps &= vmx_capability.ept;
3606 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003607 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3608 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003609 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003611 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003613 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003614 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003615
Bandan Das27c42a12017-08-03 15:54:42 -04003616 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003617 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003618 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003619 /*
3620 * Advertise EPTP switching unconditionally
3621 * since we emulate it
3622 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003623 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003625 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003626 }
3627
Paolo Bonzinief697a72016-03-18 16:58:38 +01003628 /*
3629 * Old versions of KVM use the single-context version without
3630 * checking for support, so declare that it is supported even
3631 * though it is treated as global context. The alternative is
3632 * not failing the single-context invvpid, and it is worse.
3633 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003634 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003636 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003637 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003638 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003639 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003640
Radim Krčmář0790ec12015-03-17 14:02:32 +01003641 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003643 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3644
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003645 if (flexpriority_enabled)
3646 msrs->secondary_ctls_high |=
3647 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3648
Jan Kiszkac18911a2013-03-13 16:06:41 +01003649 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003650 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->misc_low,
3652 msrs->misc_high);
3653 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3654 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003655 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003656 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003657 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003659
3660 /*
3661 * This MSR reports some information about VMX support. We
3662 * should return information about the VMX we emulate for the
3663 * guest, and the VMCS structure we give it - not about the
3664 * VMX support of the underlying hardware.
3665 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003667 VMCS12_REVISION |
3668 VMX_BASIC_TRUE_CTLS |
3669 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3670 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3671
3672 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003674
3675 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003676 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003677 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3678 * We picked the standard core2 setting.
3679 */
3680#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3681#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3683 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003684
3685 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3687 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003688
3689 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003690 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003691}
3692
David Matlack38991522016-11-29 18:14:08 -08003693/*
3694 * if fixed0[i] == 1: val[i] must be 1
3695 * if fixed1[i] == 0: val[i] must be 0
3696 */
3697static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3698{
3699 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700}
3701
3702static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3703{
David Matlack38991522016-11-29 18:14:08 -08003704 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003705}
3706
3707static inline u64 vmx_control_msr(u32 low, u32 high)
3708{
3709 return low | ((u64)high << 32);
3710}
3711
David Matlack62cc6b9d2016-11-29 18:14:07 -08003712static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3713{
3714 superset &= mask;
3715 subset &= mask;
3716
3717 return (superset | subset) == superset;
3718}
3719
3720static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3721{
3722 const u64 feature_and_reserved =
3723 /* feature (except bit 48; see below) */
3724 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3725 /* reserved */
3726 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003727 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003728
3729 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3730 return -EINVAL;
3731
3732 /*
3733 * KVM does not emulate a version of VMX that constrains physical
3734 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3735 */
3736 if (data & BIT_ULL(48))
3737 return -EINVAL;
3738
3739 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3740 vmx_basic_vmcs_revision_id(data))
3741 return -EINVAL;
3742
3743 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3744 return -EINVAL;
3745
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003746 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003747 return 0;
3748}
3749
3750static int
3751vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3752{
3753 u64 supported;
3754 u32 *lowp, *highp;
3755
3756 switch (msr_index) {
3757 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3759 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003760 break;
3761 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003762 lowp = &vmx->nested.msrs.procbased_ctls_low;
3763 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003764 break;
3765 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003766 lowp = &vmx->nested.msrs.exit_ctls_low;
3767 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003768 break;
3769 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.entry_ctls_low;
3771 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.secondary_ctls_low;
3775 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 default:
3778 BUG();
3779 }
3780
3781 supported = vmx_control_msr(*lowp, *highp);
3782
3783 /* Check must-be-1 bits are still 1. */
3784 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3785 return -EINVAL;
3786
3787 /* Check must-be-0 bits are still 0. */
3788 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3789 return -EINVAL;
3790
3791 *lowp = data;
3792 *highp = data >> 32;
3793 return 0;
3794}
3795
3796static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3797{
3798 const u64 feature_and_reserved_bits =
3799 /* feature */
3800 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3801 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3802 /* reserved */
3803 GENMASK_ULL(13, 9) | BIT_ULL(31);
3804 u64 vmx_misc;
3805
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3807 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003808
3809 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3810 return -EINVAL;
3811
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003812 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003813 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3814 vmx_misc_preemption_timer_rate(data) !=
3815 vmx_misc_preemption_timer_rate(vmx_misc))
3816 return -EINVAL;
3817
3818 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3819 return -EINVAL;
3820
3821 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3822 return -EINVAL;
3823
3824 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3825 return -EINVAL;
3826
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003827 vmx->nested.msrs.misc_low = data;
3828 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003829
3830 /*
3831 * If L1 has read-only VM-exit information fields, use the
3832 * less permissive vmx_vmwrite_bitmap to specify write
3833 * permissions for the shadow VMCS.
3834 */
3835 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3836 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3837
David Matlack62cc6b9d2016-11-29 18:14:07 -08003838 return 0;
3839}
3840
3841static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3842{
3843 u64 vmx_ept_vpid_cap;
3844
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003845 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3846 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003847
3848 /* Every bit is either reserved or a feature bit. */
3849 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3850 return -EINVAL;
3851
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003852 vmx->nested.msrs.ept_caps = data;
3853 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003854 return 0;
3855}
3856
3857static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3858{
3859 u64 *msr;
3860
3861 switch (msr_index) {
3862 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003863 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003864 break;
3865 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003866 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003867 break;
3868 default:
3869 BUG();
3870 }
3871
3872 /*
3873 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3874 * must be 1 in the restored value.
3875 */
3876 if (!is_bitwise_subset(data, *msr, -1ULL))
3877 return -EINVAL;
3878
3879 *msr = data;
3880 return 0;
3881}
3882
3883/*
3884 * Called when userspace is restoring VMX MSRs.
3885 *
3886 * Returns 0 on success, non-0 otherwise.
3887 */
3888static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3889{
3890 struct vcpu_vmx *vmx = to_vmx(vcpu);
3891
Jim Mattsona943ac52018-05-29 09:11:32 -07003892 /*
3893 * Don't allow changes to the VMX capability MSRs while the vCPU
3894 * is in VMX operation.
3895 */
3896 if (vmx->nested.vmxon)
3897 return -EBUSY;
3898
David Matlack62cc6b9d2016-11-29 18:14:07 -08003899 switch (msr_index) {
3900 case MSR_IA32_VMX_BASIC:
3901 return vmx_restore_vmx_basic(vmx, data);
3902 case MSR_IA32_VMX_PINBASED_CTLS:
3903 case MSR_IA32_VMX_PROCBASED_CTLS:
3904 case MSR_IA32_VMX_EXIT_CTLS:
3905 case MSR_IA32_VMX_ENTRY_CTLS:
3906 /*
3907 * The "non-true" VMX capability MSRs are generated from the
3908 * "true" MSRs, so we do not support restoring them directly.
3909 *
3910 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3911 * should restore the "true" MSRs with the must-be-1 bits
3912 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3913 * DEFAULT SETTINGS".
3914 */
3915 return -EINVAL;
3916 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3917 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3918 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3919 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3920 case MSR_IA32_VMX_PROCBASED_CTLS2:
3921 return vmx_restore_control_msr(vmx, msr_index, data);
3922 case MSR_IA32_VMX_MISC:
3923 return vmx_restore_vmx_misc(vmx, data);
3924 case MSR_IA32_VMX_CR0_FIXED0:
3925 case MSR_IA32_VMX_CR4_FIXED0:
3926 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3927 case MSR_IA32_VMX_CR0_FIXED1:
3928 case MSR_IA32_VMX_CR4_FIXED1:
3929 /*
3930 * These MSRs are generated based on the vCPU's CPUID, so we
3931 * do not support restoring them directly.
3932 */
3933 return -EINVAL;
3934 case MSR_IA32_VMX_EPT_VPID_CAP:
3935 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3936 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003937 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003938 return 0;
3939 default:
3940 /*
3941 * The rest of the VMX capability MSRs do not support restore.
3942 */
3943 return -EINVAL;
3944 }
3945}
3946
Jan Kiszkacae50132014-01-04 18:47:22 +01003947/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003948static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003949{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003950 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003951 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003952 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003953 break;
3954 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3955 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003956 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003957 msrs->pinbased_ctls_low,
3958 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003959 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3960 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003961 break;
3962 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3963 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003964 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003965 msrs->procbased_ctls_low,
3966 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003967 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3968 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003969 break;
3970 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3971 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003972 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003973 msrs->exit_ctls_low,
3974 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003975 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3976 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003977 break;
3978 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3979 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003980 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003981 msrs->entry_ctls_low,
3982 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003983 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3984 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003985 break;
3986 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003987 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003988 msrs->misc_low,
3989 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003990 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003992 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003993 break;
3994 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003995 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003996 break;
3997 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003998 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
4000 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004007 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004008 msrs->secondary_ctls_low,
4009 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004010 break;
4011 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004012 *pdata = msrs->ept_caps |
4013 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004015 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004017 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004018 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004020 }
4021
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 return 0;
4023}
4024
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004025static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4026 uint64_t val)
4027{
4028 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4029
4030 return !(val & ~valid_bits);
4031}
4032
Tom Lendacky801e4592018-02-21 13:39:51 -06004033static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4034{
Paolo Bonzini13893092018-02-26 13:40:09 +01004035 switch (msr->index) {
4036 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4037 if (!nested)
4038 return 1;
4039 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4040 default:
4041 return 1;
4042 }
4043
4044 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004045}
4046
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004047/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048 * Reads an msr value (of 'msr_index') into 'pdata'.
4049 * Returns 0 on success, non-0 otherwise.
4050 * Assumes vcpu_load() was already called.
4051 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004052static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004054 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004055 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004057 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004058#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004060 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 break;
4062 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004063 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004065 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004066 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004067 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004068#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004070 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004071 case MSR_IA32_SPEC_CTRL:
4072 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004073 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4074 return 1;
4075
4076 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4077 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004078 case MSR_IA32_ARCH_CAPABILITIES:
4079 if (!msr_info->host_initiated &&
4080 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4081 return 1;
4082 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4083 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004085 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 break;
4087 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004088 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 break;
4090 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004091 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004093 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004094 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004095 (!msr_info->host_initiated &&
4096 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004097 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004098 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004099 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004100 case MSR_IA32_MCG_EXT_CTL:
4101 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004102 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004103 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004104 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004105 msr_info->data = vcpu->arch.mcg_ext_ctl;
4106 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004107 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004108 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004109 break;
4110 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4111 if (!nested_vmx_allowed(vcpu))
4112 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004113 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4114 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004115 case MSR_IA32_XSS:
4116 if (!vmx_xsaves_supported())
4117 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004118 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004119 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004120 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004121 if (!msr_info->host_initiated &&
4122 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004123 return 1;
4124 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004126 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004127 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004128 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004129 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004131 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 }
4133
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 return 0;
4135}
4136
Jan Kiszkacae50132014-01-04 18:47:22 +01004137static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4138
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139/*
4140 * Writes msr value into into the appropriate "register".
4141 * Returns 0 on success, non-0 otherwise.
4142 * Assumes vcpu_load() was already called.
4143 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004144static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004146 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004147 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004148 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004149 u32 msr_index = msr_info->index;
4150 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004151
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004153 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004154 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004155 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004156#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004158 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 vmcs_writel(GUEST_FS_BASE, data);
4160 break;
4161 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004162 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 vmcs_writel(GUEST_GS_BASE, data);
4164 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004165 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004166 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004167 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168#endif
4169 case MSR_IA32_SYSENTER_CS:
4170 vmcs_write32(GUEST_SYSENTER_CS, data);
4171 break;
4172 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004173 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 break;
4175 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004176 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004178 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004179 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004180 (!msr_info->host_initiated &&
4181 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004182 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004183 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004184 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004186 vmcs_write64(GUEST_BNDCFGS, data);
4187 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004188 case MSR_IA32_SPEC_CTRL:
4189 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004190 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4191 return 1;
4192
4193 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004194 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004195 return 1;
4196
4197 vmx->spec_ctrl = data;
4198
4199 if (!data)
4200 break;
4201
4202 /*
4203 * For non-nested:
4204 * When it's written (to non-zero) for the first time, pass
4205 * it through.
4206 *
4207 * For nested:
4208 * The handling of the MSR bitmap for L2 guests is done in
4209 * nested_vmx_merge_msr_bitmap. We should not touch the
4210 * vmcs02.msr_bitmap here since it gets completely overwritten
4211 * in the merging. We update the vmcs01 here for L1 as well
4212 * since it will end up touching the MSR anyway now.
4213 */
4214 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4215 MSR_IA32_SPEC_CTRL,
4216 MSR_TYPE_RW);
4217 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004218 case MSR_IA32_PRED_CMD:
4219 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004220 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4221 return 1;
4222
4223 if (data & ~PRED_CMD_IBPB)
4224 return 1;
4225
4226 if (!data)
4227 break;
4228
4229 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4230
4231 /*
4232 * For non-nested:
4233 * When it's written (to non-zero) for the first time, pass
4234 * it through.
4235 *
4236 * For nested:
4237 * The handling of the MSR bitmap for L2 guests is done in
4238 * nested_vmx_merge_msr_bitmap. We should not touch the
4239 * vmcs02.msr_bitmap here since it gets completely overwritten
4240 * in the merging.
4241 */
4242 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4243 MSR_TYPE_W);
4244 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004245 case MSR_IA32_ARCH_CAPABILITIES:
4246 if (!msr_info->host_initiated)
4247 return 1;
4248 vmx->arch_capabilities = data;
4249 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004250 case MSR_IA32_CR_PAT:
4251 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004252 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4253 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004254 vmcs_write64(GUEST_IA32_PAT, data);
4255 vcpu->arch.pat = data;
4256 break;
4257 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004258 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004259 break;
Will Auldba904632012-11-29 12:42:50 -08004260 case MSR_IA32_TSC_ADJUST:
4261 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004262 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004263 case MSR_IA32_MCG_EXT_CTL:
4264 if ((!msr_info->host_initiated &&
4265 !(to_vmx(vcpu)->msr_ia32_feature_control &
4266 FEATURE_CONTROL_LMCE)) ||
4267 (data & ~MCG_EXT_CTL_LMCE_EN))
4268 return 1;
4269 vcpu->arch.mcg_ext_ctl = data;
4270 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004271 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004272 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004273 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004274 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4275 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004276 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004277 if (msr_info->host_initiated && data == 0)
4278 vmx_leave_nested(vcpu);
4279 break;
4280 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004281 if (!msr_info->host_initiated)
4282 return 1; /* they are read-only */
4283 if (!nested_vmx_allowed(vcpu))
4284 return 1;
4285 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004286 case MSR_IA32_XSS:
4287 if (!vmx_xsaves_supported())
4288 return 1;
4289 /*
4290 * The only supported bit as of Skylake is bit 8, but
4291 * it is not supported on KVM.
4292 */
4293 if (data != 0)
4294 return 1;
4295 vcpu->arch.ia32_xss = data;
4296 if (vcpu->arch.ia32_xss != host_xss)
4297 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004298 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004299 else
4300 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4301 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004302 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004303 if (!msr_info->host_initiated &&
4304 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004305 return 1;
4306 /* Check reserved bit, higher 32 bits should be zero */
4307 if ((data >> 32) != 0)
4308 return 1;
4309 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004311 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004312 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004313 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004314 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004315 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4316 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004317 ret = kvm_set_shared_msr(msr->index, msr->data,
4318 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004319 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004320 if (ret)
4321 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004322 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004323 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004325 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326 }
4327
Eddie Dong2cc51562007-05-21 07:28:09 +03004328 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329}
4330
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004331static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004333 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4334 switch (reg) {
4335 case VCPU_REGS_RSP:
4336 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4337 break;
4338 case VCPU_REGS_RIP:
4339 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4340 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004341 case VCPU_EXREG_PDPTR:
4342 if (enable_ept)
4343 ept_save_pdptrs(vcpu);
4344 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 default:
4346 break;
4347 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348}
4349
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350static __init int cpu_has_kvm_support(void)
4351{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004352 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004353}
4354
4355static __init int vmx_disabled_by_bios(void)
4356{
4357 u64 msr;
4358
4359 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004360 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004361 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004362 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4363 && tboot_enabled())
4364 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004365 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004366 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004367 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004368 && !tboot_enabled()) {
4369 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004370 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004371 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004372 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 /* launched w/o TXT and VMX disabled */
4374 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4375 && !tboot_enabled())
4376 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004377 }
4378
4379 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380}
4381
Dongxiao Xu7725b892010-05-11 18:29:38 +08004382static void kvm_cpu_vmxon(u64 addr)
4383{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004384 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004385 intel_pt_handle_vmx(1);
4386
Dongxiao Xu7725b892010-05-11 18:29:38 +08004387 asm volatile (ASM_VMX_VMXON_RAX
4388 : : "a"(&addr), "m"(addr)
4389 : "memory", "cc");
4390}
4391
Radim Krčmář13a34e02014-08-28 15:13:03 +02004392static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393{
4394 int cpu = raw_smp_processor_id();
4395 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004396 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004398 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004399 return -EBUSY;
4400
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004401 /*
4402 * This can happen if we hot-added a CPU but failed to allocate
4403 * VP assist page for it.
4404 */
4405 if (static_branch_unlikely(&enable_evmcs) &&
4406 !hv_get_vp_assist_page(cpu))
4407 return -EFAULT;
4408
Nadav Har'Eld462b812011-05-24 15:26:10 +03004409 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004410 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4411 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004412
4413 /*
4414 * Now we can enable the vmclear operation in kdump
4415 * since the loaded_vmcss_on_cpu list on this cpu
4416 * has been initialized.
4417 *
4418 * Though the cpu is not in VMX operation now, there
4419 * is no problem to enable the vmclear operation
4420 * for the loaded_vmcss_on_cpu list is empty!
4421 */
4422 crash_enable_local_vmclear(cpu);
4423
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004425
4426 test_bits = FEATURE_CONTROL_LOCKED;
4427 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4428 if (tboot_enabled())
4429 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4430
4431 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004432 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004433 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4434 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004435 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004436 if (enable_ept)
4437 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004438
4439 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440}
4441
Nadav Har'Eld462b812011-05-24 15:26:10 +03004442static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004443{
4444 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004445 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004446
Nadav Har'Eld462b812011-05-24 15:26:10 +03004447 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4448 loaded_vmcss_on_cpu_link)
4449 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004450}
4451
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004452
4453/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4454 * tricks.
4455 */
4456static void kvm_cpu_vmxoff(void)
4457{
4458 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004459
4460 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004461 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004462}
4463
Radim Krčmář13a34e02014-08-28 15:13:03 +02004464static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004466 vmclear_local_loaded_vmcss();
4467 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468}
4469
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004470static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004471 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472{
4473 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004474 u32 ctl = ctl_min | ctl_opt;
4475
4476 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4477
4478 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4479 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4480
4481 /* Ensure minimum (required) set of control bits are supported. */
4482 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004483 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004484
4485 *result = ctl;
4486 return 0;
4487}
4488
Avi Kivity110312c2010-12-21 12:54:20 +02004489static __init bool allow_1_setting(u32 msr, u32 ctl)
4490{
4491 u32 vmx_msr_low, vmx_msr_high;
4492
4493 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4494 return vmx_msr_high & ctl;
4495}
4496
Yang, Sheng002c7f72007-07-31 14:23:01 +03004497static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004498{
4499 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004500 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004501 u32 _pin_based_exec_control = 0;
4502 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004503 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504 u32 _vmexit_control = 0;
4505 u32 _vmentry_control = 0;
4506
Paolo Bonzini13893092018-02-26 13:40:09 +01004507 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304508 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004509#ifdef CONFIG_X86_64
4510 CPU_BASED_CR8_LOAD_EXITING |
4511 CPU_BASED_CR8_STORE_EXITING |
4512#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004513 CPU_BASED_CR3_LOAD_EXITING |
4514 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004515 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004517 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004518 CPU_BASED_MWAIT_EXITING |
4519 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004520 CPU_BASED_INVLPG_EXITING |
4521 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004522
Sheng Yangf78e0e22007-10-29 09:40:42 +08004523 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004524 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004525 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4527 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004528 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004529#ifdef CONFIG_X86_64
4530 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4531 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4532 ~CPU_BASED_CR8_STORE_EXITING;
4533#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004534 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004535 min2 = 0;
4536 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004537 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004538 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004539 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004540 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004541 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004542 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004543 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004544 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004545 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004546 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004547 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004548 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004549 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004550 SECONDARY_EXEC_RDSEED_EXITING |
4551 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004552 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004553 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004554 SECONDARY_EXEC_ENABLE_VMFUNC |
4555 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004556 if (adjust_vmx_controls(min2, opt2,
4557 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004558 &_cpu_based_2nd_exec_control) < 0)
4559 return -EIO;
4560 }
4561#ifndef CONFIG_X86_64
4562 if (!(_cpu_based_2nd_exec_control &
4563 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4564 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4565#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004566
4567 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4568 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004569 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004570 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4571 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004572
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004573 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4574 &vmx_capability.ept, &vmx_capability.vpid);
4575
Sheng Yangd56f5462008-04-25 10:13:16 +08004576 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004577 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4578 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004579 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4580 CPU_BASED_CR3_STORE_EXITING |
4581 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004582 } else if (vmx_capability.ept) {
4583 vmx_capability.ept = 0;
4584 pr_warn_once("EPT CAP should not exist if not support "
4585 "1-setting enable EPT VM-execution control\n");
4586 }
4587 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4588 vmx_capability.vpid) {
4589 vmx_capability.vpid = 0;
4590 pr_warn_once("VPID CAP should not exist if not support "
4591 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004592 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004593
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004594 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004595#ifdef CONFIG_X86_64
4596 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4597#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004598 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004599 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004600 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4601 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004602 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004603
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004604 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4605 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4606 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004607 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4608 &_pin_based_exec_control) < 0)
4609 return -EIO;
4610
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004611 if (cpu_has_broken_vmx_preemption_timer())
4612 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004613 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004614 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004615 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4616
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004617 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004618 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004619 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4620 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004621 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004622
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004623 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004624
4625 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4626 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004627 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004628
4629#ifdef CONFIG_X86_64
4630 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4631 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004632 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004633#endif
4634
4635 /* Require Write-Back (WB) memory type for VMCS accesses. */
4636 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004637 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004638
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004640 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004641 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004642
Liran Alon2307af12018-06-29 22:59:04 +03004643 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004644
Yang, Sheng002c7f72007-07-31 14:23:01 +03004645 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4646 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004647 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004648 vmcs_conf->vmexit_ctrl = _vmexit_control;
4649 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004651 if (static_branch_unlikely(&enable_evmcs))
4652 evmcs_sanitize_exec_ctrls(vmcs_conf);
4653
Avi Kivity110312c2010-12-21 12:54:20 +02004654 cpu_has_load_ia32_efer =
4655 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4656 VM_ENTRY_LOAD_IA32_EFER)
4657 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4658 VM_EXIT_LOAD_IA32_EFER);
4659
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004660 cpu_has_load_perf_global_ctrl =
4661 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4662 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4663 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4664 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4665
4666 /*
4667 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004668 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004669 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4670 *
4671 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4672 *
4673 * AAK155 (model 26)
4674 * AAP115 (model 30)
4675 * AAT100 (model 37)
4676 * BC86,AAY89,BD102 (model 44)
4677 * BA97 (model 46)
4678 *
4679 */
4680 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4681 switch (boot_cpu_data.x86_model) {
4682 case 26:
4683 case 30:
4684 case 37:
4685 case 44:
4686 case 46:
4687 cpu_has_load_perf_global_ctrl = false;
4688 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4689 "does not work properly. Using workaround\n");
4690 break;
4691 default:
4692 break;
4693 }
4694 }
4695
Borislav Petkov782511b2016-04-04 22:25:03 +02004696 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004697 rdmsrl(MSR_IA32_XSS, host_xss);
4698
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004699 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004700}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701
Liran Alon491a6032018-06-23 02:35:12 +03004702static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004703{
4704 int node = cpu_to_node(cpu);
4705 struct page *pages;
4706 struct vmcs *vmcs;
4707
Vlastimil Babka96db8002015-09-08 15:03:50 -07004708 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709 if (!pages)
4710 return NULL;
4711 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004712 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004713
4714 /* KVM supports Enlightened VMCS v1 only */
4715 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004716 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004717 else
Liran Alon392b2f22018-06-23 02:35:01 +03004718 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004719
Liran Alon491a6032018-06-23 02:35:12 +03004720 if (shadow)
4721 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722 return vmcs;
4723}
4724
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725static void free_vmcs(struct vmcs *vmcs)
4726{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004727 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728}
4729
Nadav Har'Eld462b812011-05-24 15:26:10 +03004730/*
4731 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4732 */
4733static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4734{
4735 if (!loaded_vmcs->vmcs)
4736 return;
4737 loaded_vmcs_clear(loaded_vmcs);
4738 free_vmcs(loaded_vmcs->vmcs);
4739 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004740 if (loaded_vmcs->msr_bitmap)
4741 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004742 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004743}
4744
Liran Alon491a6032018-06-23 02:35:12 +03004745static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004746{
Liran Alon491a6032018-06-23 02:35:12 +03004747 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004748}
4749
4750static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4751{
Liran Alon491a6032018-06-23 02:35:12 +03004752 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004753 if (!loaded_vmcs->vmcs)
4754 return -ENOMEM;
4755
4756 loaded_vmcs->shadow_vmcs = NULL;
4757 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004758
4759 if (cpu_has_vmx_msr_bitmap()) {
4760 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4761 if (!loaded_vmcs->msr_bitmap)
4762 goto out_vmcs;
4763 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004764
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004765 if (IS_ENABLED(CONFIG_HYPERV) &&
4766 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004767 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4768 struct hv_enlightened_vmcs *evmcs =
4769 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4770
4771 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4772 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004773 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004774
4775 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4776
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004777 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004778
4779out_vmcs:
4780 free_loaded_vmcs(loaded_vmcs);
4781 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004782}
4783
Sam Ravnborg39959582007-06-01 00:47:13 -07004784static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785{
4786 int cpu;
4787
Zachary Amsden3230bb42009-09-29 11:38:37 -10004788 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004790 per_cpu(vmxarea, cpu) = NULL;
4791 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792}
4793
Jim Mattsond37f4262017-12-22 12:12:16 -08004794enum vmcs_field_width {
4795 VMCS_FIELD_WIDTH_U16 = 0,
4796 VMCS_FIELD_WIDTH_U64 = 1,
4797 VMCS_FIELD_WIDTH_U32 = 2,
4798 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004799};
4800
Jim Mattsond37f4262017-12-22 12:12:16 -08004801static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004802{
4803 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004804 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004805 return (field >> 13) & 0x3 ;
4806}
4807
4808static inline int vmcs_field_readonly(unsigned long field)
4809{
4810 return (((field >> 10) & 0x3) == 1);
4811}
4812
Bandan Dasfe2b2012014-04-21 15:20:14 -04004813static void init_vmcs_shadow_fields(void)
4814{
4815 int i, j;
4816
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004817 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4818 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004819 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004820 (i + 1 == max_shadow_read_only_fields ||
4821 shadow_read_only_fields[i + 1] != field + 1))
4822 pr_err("Missing field from shadow_read_only_field %x\n",
4823 field + 1);
4824
4825 clear_bit(field, vmx_vmread_bitmap);
4826#ifdef CONFIG_X86_64
4827 if (field & 1)
4828 continue;
4829#endif
4830 if (j < i)
4831 shadow_read_only_fields[j] = field;
4832 j++;
4833 }
4834 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004835
4836 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004837 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004838 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004839 (i + 1 == max_shadow_read_write_fields ||
4840 shadow_read_write_fields[i + 1] != field + 1))
4841 pr_err("Missing field from shadow_read_write_field %x\n",
4842 field + 1);
4843
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004844 /*
4845 * PML and the preemption timer can be emulated, but the
4846 * processor cannot vmwrite to fields that don't exist
4847 * on bare metal.
4848 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004849 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004850 case GUEST_PML_INDEX:
4851 if (!cpu_has_vmx_pml())
4852 continue;
4853 break;
4854 case VMX_PREEMPTION_TIMER_VALUE:
4855 if (!cpu_has_vmx_preemption_timer())
4856 continue;
4857 break;
4858 case GUEST_INTR_STATUS:
4859 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004860 continue;
4861 break;
4862 default:
4863 break;
4864 }
4865
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004866 clear_bit(field, vmx_vmwrite_bitmap);
4867 clear_bit(field, vmx_vmread_bitmap);
4868#ifdef CONFIG_X86_64
4869 if (field & 1)
4870 continue;
4871#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004872 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004873 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004874 j++;
4875 }
4876 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004877}
4878
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879static __init int alloc_kvm_area(void)
4880{
4881 int cpu;
4882
Zachary Amsden3230bb42009-09-29 11:38:37 -10004883 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 struct vmcs *vmcs;
4885
Liran Alon491a6032018-06-23 02:35:12 +03004886 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887 if (!vmcs) {
4888 free_kvm_area();
4889 return -ENOMEM;
4890 }
4891
Liran Alon2307af12018-06-29 22:59:04 +03004892 /*
4893 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4894 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4895 * revision_id reported by MSR_IA32_VMX_BASIC.
4896 *
4897 * However, even though not explictly documented by
4898 * TLFS, VMXArea passed as VMXON argument should
4899 * still be marked with revision_id reported by
4900 * physical CPU.
4901 */
4902 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004903 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004904
Avi Kivity6aa8b732006-12-10 02:21:36 -08004905 per_cpu(vmxarea, cpu) = vmcs;
4906 }
4907 return 0;
4908}
4909
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004910static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004911 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004913 if (!emulate_invalid_guest_state) {
4914 /*
4915 * CS and SS RPL should be equal during guest entry according
4916 * to VMX spec, but in reality it is not always so. Since vcpu
4917 * is in the middle of the transition from real mode to
4918 * protected mode it is safe to assume that RPL 0 is a good
4919 * default value.
4920 */
4921 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004922 save->selector &= ~SEGMENT_RPL_MASK;
4923 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004924 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004925 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004926 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004927}
4928
4929static void enter_pmode(struct kvm_vcpu *vcpu)
4930{
4931 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004932 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933
Gleb Natapovd99e4152012-12-20 16:57:45 +02004934 /*
4935 * Update real mode segment cache. It may be not up-to-date if sement
4936 * register was written while vcpu was in a guest mode.
4937 */
4938 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4939 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4940 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4944
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004945 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004946
Avi Kivity2fb92db2011-04-27 19:42:18 +03004947 vmx_segment_cache_clear(vmx);
4948
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004949 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950
4951 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004952 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4953 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954 vmcs_writel(GUEST_RFLAGS, flags);
4955
Rusty Russell66aee912007-07-17 23:34:16 +10004956 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4957 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958
4959 update_exception_bitmap(vcpu);
4960
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004961 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4962 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4963 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4964 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4965 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4966 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967}
4968
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004969static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970{
Mathias Krause772e0312012-08-30 01:30:19 +02004971 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004972 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973
Gleb Natapovd99e4152012-12-20 16:57:45 +02004974 var.dpl = 0x3;
4975 if (seg == VCPU_SREG_CS)
4976 var.type = 0x3;
4977
4978 if (!emulate_invalid_guest_state) {
4979 var.selector = var.base >> 4;
4980 var.base = var.base & 0xffff0;
4981 var.limit = 0xffff;
4982 var.g = 0;
4983 var.db = 0;
4984 var.present = 1;
4985 var.s = 1;
4986 var.l = 0;
4987 var.unusable = 0;
4988 var.type = 0x3;
4989 var.avl = 0;
4990 if (save->base & 0xf)
4991 printk_once(KERN_WARNING "kvm: segment base is not "
4992 "paragraph aligned when entering "
4993 "protected mode (seg=%d)", seg);
4994 }
4995
4996 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004997 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004998 vmcs_write32(sf->limit, var.limit);
4999 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000}
5001
5002static void enter_rmode(struct kvm_vcpu *vcpu)
5003{
5004 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005006 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005008 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5009 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5010 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5011 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005015
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005016 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005017
Gleb Natapov776e58e2011-03-13 12:34:27 +02005018 /*
5019 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005020 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005021 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005022 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005023 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5024 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005025
Avi Kivity2fb92db2011-04-27 19:42:18 +03005026 vmx_segment_cache_clear(vmx);
5027
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005028 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5031
5032 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005033 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005035 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036
5037 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005038 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039 update_exception_bitmap(vcpu);
5040
Gleb Natapovd99e4152012-12-20 16:57:45 +02005041 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5042 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5043 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5044 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5045 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5046 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005047
Eddie Dong8668a3c2007-10-10 14:26:45 +08005048 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049}
5050
Amit Shah401d10d2009-02-20 22:53:37 +05305051static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5052{
5053 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005054 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5055
5056 if (!msr)
5057 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305058
Avi Kivityf6801df2010-01-21 15:31:50 +02005059 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305060 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005061 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305062 msr->data = efer;
5063 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005064 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305065
5066 msr->data = efer & ~EFER_LME;
5067 }
5068 setup_msrs(vmx);
5069}
5070
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005071#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072
5073static void enter_lmode(struct kvm_vcpu *vcpu)
5074{
5075 u32 guest_tr_ar;
5076
Avi Kivity2fb92db2011-04-27 19:42:18 +03005077 vmx_segment_cache_clear(to_vmx(vcpu));
5078
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005080 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005081 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5082 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005084 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5085 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086 }
Avi Kivityda38f432010-07-06 11:30:49 +03005087 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005088}
5089
5090static void exit_lmode(struct kvm_vcpu *vcpu)
5091{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005092 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005093 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094}
5095
5096#endif
5097
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005098static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5099 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005100{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005101 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005102 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5103 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005104 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005105 } else {
5106 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005107 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005108}
5109
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005110static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005111{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005112 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005113}
5114
Junaid Shahidfaff8752018-06-29 13:10:05 -07005115static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5116{
5117 int vpid = to_vmx(vcpu)->vpid;
5118
5119 if (!vpid_sync_vcpu_addr(vpid, addr))
5120 vpid_sync_context(vpid);
5121
5122 /*
5123 * If VPIDs are not supported or enabled, then the above is a no-op.
5124 * But we don't really need a TLB flush in that case anyway, because
5125 * each VM entry/exit includes an implicit flush when VPID is 0.
5126 */
5127}
5128
Avi Kivitye8467fd2009-12-29 18:43:06 +02005129static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5130{
5131 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5132
5133 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5134 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5135}
5136
Avi Kivityaff48ba2010-12-05 18:56:11 +02005137static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5138{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005139 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005140 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5141 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5142}
5143
Anthony Liguori25c4c272007-04-27 09:29:21 +03005144static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005145{
Avi Kivityfc78f512009-12-07 12:16:48 +02005146 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5147
5148 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5149 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005150}
5151
Sheng Yang14394422008-04-28 12:24:45 +08005152static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5153{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005154 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5155
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005156 if (!test_bit(VCPU_EXREG_PDPTR,
5157 (unsigned long *)&vcpu->arch.regs_dirty))
5158 return;
5159
Sheng Yang14394422008-04-28 12:24:45 +08005160 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005161 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5162 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5163 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5164 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005165 }
5166}
5167
Avi Kivity8f5d5492009-05-31 18:41:29 +03005168static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5169{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005170 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5171
Avi Kivity8f5d5492009-05-31 18:41:29 +03005172 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005173 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5174 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5175 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5176 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005177 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005178
5179 __set_bit(VCPU_EXREG_PDPTR,
5180 (unsigned long *)&vcpu->arch.regs_avail);
5181 __set_bit(VCPU_EXREG_PDPTR,
5182 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005183}
5184
David Matlack38991522016-11-29 18:14:08 -08005185static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5186{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005187 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5188 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005189 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5190
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005191 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005192 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5193 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5194 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5195
5196 return fixed_bits_valid(val, fixed0, fixed1);
5197}
5198
5199static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5200{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005201 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5202 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005203
5204 return fixed_bits_valid(val, fixed0, fixed1);
5205}
5206
5207static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5208{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005209 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5210 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005211
5212 return fixed_bits_valid(val, fixed0, fixed1);
5213}
5214
5215/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5216#define nested_guest_cr4_valid nested_cr4_valid
5217#define nested_host_cr4_valid nested_cr4_valid
5218
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005219static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005220
5221static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5222 unsigned long cr0,
5223 struct kvm_vcpu *vcpu)
5224{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005225 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5226 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005227 if (!(cr0 & X86_CR0_PG)) {
5228 /* From paging/starting to nonpaging */
5229 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005230 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005231 (CPU_BASED_CR3_LOAD_EXITING |
5232 CPU_BASED_CR3_STORE_EXITING));
5233 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005234 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005235 } else if (!is_paging(vcpu)) {
5236 /* From nonpaging to paging */
5237 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005238 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005239 ~(CPU_BASED_CR3_LOAD_EXITING |
5240 CPU_BASED_CR3_STORE_EXITING));
5241 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005242 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005243 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005244
5245 if (!(cr0 & X86_CR0_WP))
5246 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005247}
5248
Avi Kivity6aa8b732006-12-10 02:21:36 -08005249static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5250{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005252 unsigned long hw_cr0;
5253
Sean Christopherson3de63472018-07-13 08:42:30 -07005254 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005255 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005256 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005257 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005258 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005259
Gleb Natapov218e7632013-01-21 15:36:45 +02005260 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5261 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005262
Gleb Natapov218e7632013-01-21 15:36:45 +02005263 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5264 enter_rmode(vcpu);
5265 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005267#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005268 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005269 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005271 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005272 exit_lmode(vcpu);
5273 }
5274#endif
5275
Sean Christophersonb4d18512018-03-05 12:04:40 -08005276 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005277 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5278
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005280 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005281 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005282
5283 /* depends on vcpu->arch.cr0 to be set to a new value */
5284 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005285}
5286
Yu Zhang855feb62017-08-24 20:27:55 +08005287static int get_ept_level(struct kvm_vcpu *vcpu)
5288{
5289 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5290 return 5;
5291 return 4;
5292}
5293
Peter Feiner995f00a2017-06-30 17:26:32 -07005294static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005295{
Yu Zhang855feb62017-08-24 20:27:55 +08005296 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005297
Yu Zhang855feb62017-08-24 20:27:55 +08005298 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005299
Peter Feiner995f00a2017-06-30 17:26:32 -07005300 if (enable_ept_ad_bits &&
5301 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005302 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005303 eptp |= (root_hpa & PAGE_MASK);
5304
5305 return eptp;
5306}
5307
Avi Kivity6aa8b732006-12-10 02:21:36 -08005308static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5309{
Tianyu Lan877ad952018-07-19 08:40:23 +00005310 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005311 unsigned long guest_cr3;
5312 u64 eptp;
5313
5314 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005315 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005316 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005317 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005318
5319 if (kvm_x86_ops->tlb_remote_flush) {
5320 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5321 to_vmx(vcpu)->ept_pointer = eptp;
5322 to_kvm_vmx(kvm)->ept_pointers_match
5323 = EPT_POINTERS_CHECK;
5324 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5325 }
5326
Sean Christophersone90008d2018-03-05 12:04:37 -08005327 if (enable_unrestricted_guest || is_paging(vcpu) ||
5328 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005329 guest_cr3 = kvm_read_cr3(vcpu);
5330 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005331 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005332 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005333 }
5334
Sheng Yang14394422008-04-28 12:24:45 +08005335 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005336}
5337
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005338static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005339{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005340 /*
5341 * Pass through host's Machine Check Enable value to hw_cr4, which
5342 * is in force while we are in guest mode. Do not let guests control
5343 * this bit, even if host CR4.MCE == 0.
5344 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005345 unsigned long hw_cr4;
5346
5347 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5348 if (enable_unrestricted_guest)
5349 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5350 else if (to_vmx(vcpu)->rmode.vm86_active)
5351 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5352 else
5353 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005354
Sean Christopherson64f7a112018-04-30 10:01:06 -07005355 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5356 if (cr4 & X86_CR4_UMIP) {
5357 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005358 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005359 hw_cr4 &= ~X86_CR4_UMIP;
5360 } else if (!is_guest_mode(vcpu) ||
5361 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5362 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5363 SECONDARY_EXEC_DESC);
5364 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005365
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005366 if (cr4 & X86_CR4_VMXE) {
5367 /*
5368 * To use VMXON (and later other VMX instructions), a guest
5369 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5370 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005371 * is here. We operate under the default treatment of SMM,
5372 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005373 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005374 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005375 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005376 }
David Matlack38991522016-11-29 18:14:08 -08005377
5378 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005379 return 1;
5380
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005381 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005382
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005383 if (!enable_unrestricted_guest) {
5384 if (enable_ept) {
5385 if (!is_paging(vcpu)) {
5386 hw_cr4 &= ~X86_CR4_PAE;
5387 hw_cr4 |= X86_CR4_PSE;
5388 } else if (!(cr4 & X86_CR4_PAE)) {
5389 hw_cr4 &= ~X86_CR4_PAE;
5390 }
5391 }
5392
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005393 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005394 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5395 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5396 * to be manually disabled when guest switches to non-paging
5397 * mode.
5398 *
5399 * If !enable_unrestricted_guest, the CPU is always running
5400 * with CR0.PG=1 and CR4 needs to be modified.
5401 * If enable_unrestricted_guest, the CPU automatically
5402 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005403 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005404 if (!is_paging(vcpu))
5405 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5406 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005407
Sheng Yang14394422008-04-28 12:24:45 +08005408 vmcs_writel(CR4_READ_SHADOW, cr4);
5409 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005410 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005411}
5412
Avi Kivity6aa8b732006-12-10 02:21:36 -08005413static void vmx_get_segment(struct kvm_vcpu *vcpu,
5414 struct kvm_segment *var, int seg)
5415{
Avi Kivitya9179492011-01-03 14:28:52 +02005416 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005417 u32 ar;
5418
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005419 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005420 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005421 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005422 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005423 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005424 var->base = vmx_read_guest_seg_base(vmx, seg);
5425 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5426 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005427 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005428 var->base = vmx_read_guest_seg_base(vmx, seg);
5429 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5430 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5431 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005432 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005433 var->type = ar & 15;
5434 var->s = (ar >> 4) & 1;
5435 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005436 /*
5437 * Some userspaces do not preserve unusable property. Since usable
5438 * segment has to be present according to VMX spec we can use present
5439 * property to amend userspace bug by making unusable segment always
5440 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5441 * segment as unusable.
5442 */
5443 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005444 var->avl = (ar >> 12) & 1;
5445 var->l = (ar >> 13) & 1;
5446 var->db = (ar >> 14) & 1;
5447 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448}
5449
Avi Kivitya9179492011-01-03 14:28:52 +02005450static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5451{
Avi Kivitya9179492011-01-03 14:28:52 +02005452 struct kvm_segment s;
5453
5454 if (to_vmx(vcpu)->rmode.vm86_active) {
5455 vmx_get_segment(vcpu, &s, seg);
5456 return s.base;
5457 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005458 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005459}
5460
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005461static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005462{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005463 struct vcpu_vmx *vmx = to_vmx(vcpu);
5464
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005465 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005466 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005467 else {
5468 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005469 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005470 }
Avi Kivity69c73022011-03-07 15:26:44 +02005471}
5472
Avi Kivity653e3102007-05-07 10:55:37 +03005473static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475 u32 ar;
5476
Avi Kivityf0495f92012-06-07 17:06:10 +03005477 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005478 ar = 1 << 16;
5479 else {
5480 ar = var->type & 15;
5481 ar |= (var->s & 1) << 4;
5482 ar |= (var->dpl & 3) << 5;
5483 ar |= (var->present & 1) << 7;
5484 ar |= (var->avl & 1) << 12;
5485 ar |= (var->l & 1) << 13;
5486 ar |= (var->db & 1) << 14;
5487 ar |= (var->g & 1) << 15;
5488 }
Avi Kivity653e3102007-05-07 10:55:37 +03005489
5490 return ar;
5491}
5492
5493static void vmx_set_segment(struct kvm_vcpu *vcpu,
5494 struct kvm_segment *var, int seg)
5495{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005496 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005497 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005498
Avi Kivity2fb92db2011-04-27 19:42:18 +03005499 vmx_segment_cache_clear(vmx);
5500
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005501 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5502 vmx->rmode.segs[seg] = *var;
5503 if (seg == VCPU_SREG_TR)
5504 vmcs_write16(sf->selector, var->selector);
5505 else if (var->s)
5506 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005507 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005508 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005509
Avi Kivity653e3102007-05-07 10:55:37 +03005510 vmcs_writel(sf->base, var->base);
5511 vmcs_write32(sf->limit, var->limit);
5512 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005513
5514 /*
5515 * Fix the "Accessed" bit in AR field of segment registers for older
5516 * qemu binaries.
5517 * IA32 arch specifies that at the time of processor reset the
5518 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005519 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005520 * state vmexit when "unrestricted guest" mode is turned on.
5521 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5522 * tree. Newer qemu binaries with that qemu fix would not need this
5523 * kvm hack.
5524 */
5525 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005526 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005527
Gleb Natapovf924d662012-12-12 19:10:55 +02005528 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005529
5530out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005531 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532}
5533
Avi Kivity6aa8b732006-12-10 02:21:36 -08005534static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5535{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005536 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005537
5538 *db = (ar >> 14) & 1;
5539 *l = (ar >> 13) & 1;
5540}
5541
Gleb Natapov89a27f42010-02-16 10:51:48 +02005542static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005544 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5545 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546}
5547
Gleb Natapov89a27f42010-02-16 10:51:48 +02005548static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005550 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5551 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552}
5553
Gleb Natapov89a27f42010-02-16 10:51:48 +02005554static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005556 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5557 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558}
5559
Gleb Natapov89a27f42010-02-16 10:51:48 +02005560static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005562 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5563 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564}
5565
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005566static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5567{
5568 struct kvm_segment var;
5569 u32 ar;
5570
5571 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005572 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005573 if (seg == VCPU_SREG_CS)
5574 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005575 ar = vmx_segment_access_rights(&var);
5576
5577 if (var.base != (var.selector << 4))
5578 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005579 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005580 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005581 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005582 return false;
5583
5584 return true;
5585}
5586
5587static bool code_segment_valid(struct kvm_vcpu *vcpu)
5588{
5589 struct kvm_segment cs;
5590 unsigned int cs_rpl;
5591
5592 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005593 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005594
Avi Kivity1872a3f2009-01-04 23:26:52 +02005595 if (cs.unusable)
5596 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005597 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005598 return false;
5599 if (!cs.s)
5600 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005601 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005602 if (cs.dpl > cs_rpl)
5603 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005604 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005605 if (cs.dpl != cs_rpl)
5606 return false;
5607 }
5608 if (!cs.present)
5609 return false;
5610
5611 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5612 return true;
5613}
5614
5615static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5616{
5617 struct kvm_segment ss;
5618 unsigned int ss_rpl;
5619
5620 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005621 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005622
Avi Kivity1872a3f2009-01-04 23:26:52 +02005623 if (ss.unusable)
5624 return true;
5625 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005626 return false;
5627 if (!ss.s)
5628 return false;
5629 if (ss.dpl != ss_rpl) /* DPL != RPL */
5630 return false;
5631 if (!ss.present)
5632 return false;
5633
5634 return true;
5635}
5636
5637static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5638{
5639 struct kvm_segment var;
5640 unsigned int rpl;
5641
5642 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005643 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005644
Avi Kivity1872a3f2009-01-04 23:26:52 +02005645 if (var.unusable)
5646 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005647 if (!var.s)
5648 return false;
5649 if (!var.present)
5650 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005651 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005652 if (var.dpl < rpl) /* DPL < RPL */
5653 return false;
5654 }
5655
5656 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5657 * rights flags
5658 */
5659 return true;
5660}
5661
5662static bool tr_valid(struct kvm_vcpu *vcpu)
5663{
5664 struct kvm_segment tr;
5665
5666 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5667
Avi Kivity1872a3f2009-01-04 23:26:52 +02005668 if (tr.unusable)
5669 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005670 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005671 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005672 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005673 return false;
5674 if (!tr.present)
5675 return false;
5676
5677 return true;
5678}
5679
5680static bool ldtr_valid(struct kvm_vcpu *vcpu)
5681{
5682 struct kvm_segment ldtr;
5683
5684 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5685
Avi Kivity1872a3f2009-01-04 23:26:52 +02005686 if (ldtr.unusable)
5687 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005688 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005689 return false;
5690 if (ldtr.type != 2)
5691 return false;
5692 if (!ldtr.present)
5693 return false;
5694
5695 return true;
5696}
5697
5698static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5699{
5700 struct kvm_segment cs, ss;
5701
5702 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5703 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5704
Nadav Amitb32a9912015-03-29 16:33:04 +03005705 return ((cs.selector & SEGMENT_RPL_MASK) ==
5706 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005707}
5708
5709/*
5710 * Check if guest state is valid. Returns true if valid, false if
5711 * not.
5712 * We assume that registers are always usable
5713 */
5714static bool guest_state_valid(struct kvm_vcpu *vcpu)
5715{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005716 if (enable_unrestricted_guest)
5717 return true;
5718
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005719 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005720 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005721 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5722 return false;
5723 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5724 return false;
5725 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5726 return false;
5727 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5728 return false;
5729 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5730 return false;
5731 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5732 return false;
5733 } else {
5734 /* protected mode guest state checks */
5735 if (!cs_ss_rpl_check(vcpu))
5736 return false;
5737 if (!code_segment_valid(vcpu))
5738 return false;
5739 if (!stack_segment_valid(vcpu))
5740 return false;
5741 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5742 return false;
5743 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5744 return false;
5745 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5746 return false;
5747 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5748 return false;
5749 if (!tr_valid(vcpu))
5750 return false;
5751 if (!ldtr_valid(vcpu))
5752 return false;
5753 }
5754 /* TODO:
5755 * - Add checks on RIP
5756 * - Add checks on RFLAGS
5757 */
5758
5759 return true;
5760}
5761
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005762static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5763{
5764 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5765}
5766
Mike Dayd77c26f2007-10-08 09:02:08 -04005767static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005768{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005769 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005770 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005771 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005773 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005774 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005775 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5776 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005777 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005778 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005779 r = kvm_write_guest_page(kvm, fn++, &data,
5780 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005781 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005782 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005783 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5784 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005785 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005786 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5787 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005788 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005789 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005790 r = kvm_write_guest_page(kvm, fn, &data,
5791 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5792 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005793out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005794 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005795 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796}
5797
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005798static int init_rmode_identity_map(struct kvm *kvm)
5799{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005800 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005801 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005802 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005803 u32 tmp;
5804
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005805 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005806 mutex_lock(&kvm->slots_lock);
5807
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005808 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005809 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005810
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005811 if (!kvm_vmx->ept_identity_map_addr)
5812 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5813 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005814
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005815 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005816 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005817 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005818 goto out2;
5819
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005820 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005821 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5822 if (r < 0)
5823 goto out;
5824 /* Set up identity-mapping pagetable for EPT in real mode */
5825 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5826 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5827 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5828 r = kvm_write_guest_page(kvm, identity_map_pfn,
5829 &tmp, i * sizeof(tmp), sizeof(tmp));
5830 if (r < 0)
5831 goto out;
5832 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005833 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005834
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005835out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005836 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005837
5838out2:
5839 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005840 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005841}
5842
Avi Kivity6aa8b732006-12-10 02:21:36 -08005843static void seg_setup(int seg)
5844{
Mathias Krause772e0312012-08-30 01:30:19 +02005845 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005846 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005847
5848 vmcs_write16(sf->selector, 0);
5849 vmcs_writel(sf->base, 0);
5850 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005851 ar = 0x93;
5852 if (seg == VCPU_SREG_CS)
5853 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005854
5855 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856}
5857
Sheng Yangf78e0e22007-10-29 09:40:42 +08005858static int alloc_apic_access_page(struct kvm *kvm)
5859{
Xiao Guangrong44841412012-09-07 14:14:20 +08005860 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005861 int r = 0;
5862
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005863 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005864 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005865 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005866 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5867 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005868 if (r)
5869 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005870
Tang Chen73a6d942014-09-11 13:38:00 +08005871 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005872 if (is_error_page(page)) {
5873 r = -EFAULT;
5874 goto out;
5875 }
5876
Tang Chenc24ae0d2014-09-24 15:57:58 +08005877 /*
5878 * Do not pin the page in memory, so that memory hot-unplug
5879 * is able to migrate it.
5880 */
5881 put_page(page);
5882 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005883out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005884 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005885 return r;
5886}
5887
Wanpeng Li991e7a02015-09-16 17:30:05 +08005888static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005889{
5890 int vpid;
5891
Avi Kivity919818a2009-03-23 18:01:29 +02005892 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005893 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005894 spin_lock(&vmx_vpid_lock);
5895 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005896 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005897 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005898 else
5899 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005900 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005901 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005902}
5903
Wanpeng Li991e7a02015-09-16 17:30:05 +08005904static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005905{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005906 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005907 return;
5908 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005909 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005910 spin_unlock(&vmx_vpid_lock);
5911}
5912
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005913static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5914 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005915{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005916 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005917
5918 if (!cpu_has_vmx_msr_bitmap())
5919 return;
5920
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005921 if (static_branch_unlikely(&enable_evmcs))
5922 evmcs_touch_msr_bitmap();
5923
Sheng Yang25c5f222008-03-28 13:18:56 +08005924 /*
5925 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5926 * have the write-low and read-high bitmap offsets the wrong way round.
5927 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5928 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005929 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005930 if (type & MSR_TYPE_R)
5931 /* read-low */
5932 __clear_bit(msr, msr_bitmap + 0x000 / f);
5933
5934 if (type & MSR_TYPE_W)
5935 /* write-low */
5936 __clear_bit(msr, msr_bitmap + 0x800 / f);
5937
Sheng Yang25c5f222008-03-28 13:18:56 +08005938 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5939 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005940 if (type & MSR_TYPE_R)
5941 /* read-high */
5942 __clear_bit(msr, msr_bitmap + 0x400 / f);
5943
5944 if (type & MSR_TYPE_W)
5945 /* write-high */
5946 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5947
5948 }
5949}
5950
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005951static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5952 u32 msr, int type)
5953{
5954 int f = sizeof(unsigned long);
5955
5956 if (!cpu_has_vmx_msr_bitmap())
5957 return;
5958
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005959 if (static_branch_unlikely(&enable_evmcs))
5960 evmcs_touch_msr_bitmap();
5961
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005962 /*
5963 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5964 * have the write-low and read-high bitmap offsets the wrong way round.
5965 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5966 */
5967 if (msr <= 0x1fff) {
5968 if (type & MSR_TYPE_R)
5969 /* read-low */
5970 __set_bit(msr, msr_bitmap + 0x000 / f);
5971
5972 if (type & MSR_TYPE_W)
5973 /* write-low */
5974 __set_bit(msr, msr_bitmap + 0x800 / f);
5975
5976 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5977 msr &= 0x1fff;
5978 if (type & MSR_TYPE_R)
5979 /* read-high */
5980 __set_bit(msr, msr_bitmap + 0x400 / f);
5981
5982 if (type & MSR_TYPE_W)
5983 /* write-high */
5984 __set_bit(msr, msr_bitmap + 0xc00 / f);
5985
5986 }
5987}
5988
5989static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5990 u32 msr, int type, bool value)
5991{
5992 if (value)
5993 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5994 else
5995 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5996}
5997
Wincy Vanf2b93282015-02-03 23:56:03 +08005998/*
5999 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6000 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6001 */
6002static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6003 unsigned long *msr_bitmap_nested,
6004 u32 msr, int type)
6005{
6006 int f = sizeof(unsigned long);
6007
Wincy Vanf2b93282015-02-03 23:56:03 +08006008 /*
6009 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6010 * have the write-low and read-high bitmap offsets the wrong way round.
6011 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6012 */
6013 if (msr <= 0x1fff) {
6014 if (type & MSR_TYPE_R &&
6015 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6016 /* read-low */
6017 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6018
6019 if (type & MSR_TYPE_W &&
6020 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6021 /* write-low */
6022 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6023
6024 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6025 msr &= 0x1fff;
6026 if (type & MSR_TYPE_R &&
6027 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6028 /* read-high */
6029 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6030
6031 if (type & MSR_TYPE_W &&
6032 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6033 /* write-high */
6034 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6035
6036 }
6037}
6038
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006039static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006040{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006041 u8 mode = 0;
6042
6043 if (cpu_has_secondary_exec_ctrls() &&
6044 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6045 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6046 mode |= MSR_BITMAP_MODE_X2APIC;
6047 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6048 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6049 }
6050
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006051 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006052}
6053
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006054#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6055
6056static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6057 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006058{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006059 int msr;
6060
6061 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6062 unsigned word = msr / BITS_PER_LONG;
6063 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6064 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006065 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006066
6067 if (mode & MSR_BITMAP_MODE_X2APIC) {
6068 /*
6069 * TPR reads and writes can be virtualized even if virtual interrupt
6070 * delivery is not in use.
6071 */
6072 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6073 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6074 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6075 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6076 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6077 }
6078 }
6079}
6080
6081static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6082{
6083 struct vcpu_vmx *vmx = to_vmx(vcpu);
6084 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6085 u8 mode = vmx_msr_bitmap_mode(vcpu);
6086 u8 changed = mode ^ vmx->msr_bitmap_mode;
6087
6088 if (!changed)
6089 return;
6090
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006091 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6092 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6093
6094 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006095}
6096
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006097static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006098{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006099 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006100}
6101
David Matlackc9f04402017-08-01 14:00:40 -07006102static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6103{
6104 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6105 gfn_t gfn;
6106
6107 /*
6108 * Don't need to mark the APIC access page dirty; it is never
6109 * written to by the CPU during APIC virtualization.
6110 */
6111
6112 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6113 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6114 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6115 }
6116
6117 if (nested_cpu_has_posted_intr(vmcs12)) {
6118 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6119 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6120 }
6121}
6122
6123
David Hildenbrand6342c502017-01-25 11:58:58 +01006124static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006125{
6126 struct vcpu_vmx *vmx = to_vmx(vcpu);
6127 int max_irr;
6128 void *vapic_page;
6129 u16 status;
6130
David Matlackc9f04402017-08-01 14:00:40 -07006131 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6132 return;
Wincy Van705699a2015-02-03 23:58:17 +08006133
David Matlackc9f04402017-08-01 14:00:40 -07006134 vmx->nested.pi_pending = false;
6135 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6136 return;
Wincy Van705699a2015-02-03 23:58:17 +08006137
David Matlackc9f04402017-08-01 14:00:40 -07006138 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6139 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006140 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006141 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6142 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006143 kunmap(vmx->nested.virtual_apic_page);
6144
6145 status = vmcs_read16(GUEST_INTR_STATUS);
6146 if ((u8)max_irr > ((u8)status & 0xff)) {
6147 status &= ~0xff;
6148 status |= (u8)max_irr;
6149 vmcs_write16(GUEST_INTR_STATUS, status);
6150 }
6151 }
David Matlackc9f04402017-08-01 14:00:40 -07006152
6153 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006154}
6155
Paolo Bonzini7e712682018-10-03 13:44:26 +02006156static u8 vmx_get_rvi(void)
6157{
6158 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6159}
6160
Liran Alone6c67d82018-09-04 10:56:52 +03006161static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6162{
6163 struct vcpu_vmx *vmx = to_vmx(vcpu);
6164 void *vapic_page;
6165 u32 vppr;
6166 int rvi;
6167
6168 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6169 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6170 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6171 return false;
6172
Paolo Bonzini7e712682018-10-03 13:44:26 +02006173 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006174
6175 vapic_page = kmap(vmx->nested.virtual_apic_page);
6176 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6177 kunmap(vmx->nested.virtual_apic_page);
6178
6179 return ((rvi & 0xf0) > (vppr & 0xf0));
6180}
6181
Wincy Van06a55242017-04-28 13:13:59 +08006182static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6183 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006184{
6185#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006186 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6187
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006188 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006189 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006190 * The vector of interrupt to be delivered to vcpu had
6191 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006192 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006193 * Following cases will be reached in this block, and
6194 * we always send a notification event in all cases as
6195 * explained below.
6196 *
6197 * Case 1: vcpu keeps in non-root mode. Sending a
6198 * notification event posts the interrupt to vcpu.
6199 *
6200 * Case 2: vcpu exits to root mode and is still
6201 * runnable. PIR will be synced to vIRR before the
6202 * next vcpu entry. Sending a notification event in
6203 * this case has no effect, as vcpu is not in root
6204 * mode.
6205 *
6206 * Case 3: vcpu exits to root mode and is blocked.
6207 * vcpu_block() has already synced PIR to vIRR and
6208 * never blocks vcpu if vIRR is not cleared. Therefore,
6209 * a blocked vcpu here does not wait for any requested
6210 * interrupts in PIR, and sending a notification event
6211 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006212 */
Feng Wu28b835d2015-09-18 22:29:54 +08006213
Wincy Van06a55242017-04-28 13:13:59 +08006214 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006215 return true;
6216 }
6217#endif
6218 return false;
6219}
6220
Wincy Van705699a2015-02-03 23:58:17 +08006221static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6222 int vector)
6223{
6224 struct vcpu_vmx *vmx = to_vmx(vcpu);
6225
6226 if (is_guest_mode(vcpu) &&
6227 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006228 /*
6229 * If a posted intr is not recognized by hardware,
6230 * we will accomplish it in the next vmentry.
6231 */
6232 vmx->nested.pi_pending = true;
6233 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006234 /* the PIR and ON have been set by L1. */
6235 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6236 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006237 return 0;
6238 }
6239 return -1;
6240}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006241/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006242 * Send interrupt to vcpu via posted interrupt way.
6243 * 1. If target vcpu is running(non-root mode), send posted interrupt
6244 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6245 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6246 * interrupt from PIR in next vmentry.
6247 */
6248static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6249{
6250 struct vcpu_vmx *vmx = to_vmx(vcpu);
6251 int r;
6252
Wincy Van705699a2015-02-03 23:58:17 +08006253 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6254 if (!r)
6255 return;
6256
Yang Zhanga20ed542013-04-11 19:25:15 +08006257 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6258 return;
6259
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006260 /* If a previous notification has sent the IPI, nothing to do. */
6261 if (pi_test_and_set_on(&vmx->pi_desc))
6262 return;
6263
Wincy Van06a55242017-04-28 13:13:59 +08006264 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006265 kvm_vcpu_kick(vcpu);
6266}
6267
Avi Kivity6aa8b732006-12-10 02:21:36 -08006268/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006269 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6270 * will not change in the lifetime of the guest.
6271 * Note that host-state that does change is set elsewhere. E.g., host-state
6272 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6273 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006274static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006275{
6276 u32 low32, high32;
6277 unsigned long tmpl;
6278 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006279 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006280
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006281 cr0 = read_cr0();
6282 WARN_ON(cr0 & X86_CR0_TS);
6283 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006284
6285 /*
6286 * Save the most likely value for this task's CR3 in the VMCS.
6287 * We can't use __get_current_cr3_fast() because we're not atomic.
6288 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006289 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006290 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006291 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006292
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006293 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006294 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006295 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006296 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006297
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006298 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006299#ifdef CONFIG_X86_64
6300 /*
6301 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006302 * vmx_prepare_switch_to_host(), in case userspace uses
6303 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006304 */
6305 vmcs_write16(HOST_DS_SELECTOR, 0);
6306 vmcs_write16(HOST_ES_SELECTOR, 0);
6307#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006308 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6309 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006310#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006311 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6312 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6313
Juergen Gross87930012017-09-04 12:25:27 +02006314 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006315 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006316 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006317
Avi Kivity83287ea422012-09-16 15:10:57 +03006318 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006319
6320 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6321 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6322 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6323 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6324
6325 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6326 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6327 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6328 }
6329}
6330
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006331static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6332{
6333 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6334 if (enable_ept)
6335 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006336 if (is_guest_mode(&vmx->vcpu))
6337 vmx->vcpu.arch.cr4_guest_owned_bits &=
6338 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006339 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6340}
6341
Yang Zhang01e439b2013-04-11 19:25:12 +08006342static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6343{
6344 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6345
Andrey Smetanind62caab2015-11-10 15:36:33 +03006346 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006347 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006348
6349 if (!enable_vnmi)
6350 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6351
Yunhong Jiang64672c92016-06-13 14:19:59 -07006352 /* Enable the preemption timer dynamically */
6353 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006354 return pin_based_exec_ctrl;
6355}
6356
Andrey Smetanind62caab2015-11-10 15:36:33 +03006357static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6358{
6359 struct vcpu_vmx *vmx = to_vmx(vcpu);
6360
6361 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006362 if (cpu_has_secondary_exec_ctrls()) {
6363 if (kvm_vcpu_apicv_active(vcpu))
6364 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6365 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6366 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6367 else
6368 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6369 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6370 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6371 }
6372
6373 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006374 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006375}
6376
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006377static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6378{
6379 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006380
6381 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6382 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6383
Paolo Bonzini35754c92015-07-29 12:05:37 +02006384 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006385 exec_control &= ~CPU_BASED_TPR_SHADOW;
6386#ifdef CONFIG_X86_64
6387 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6388 CPU_BASED_CR8_LOAD_EXITING;
6389#endif
6390 }
6391 if (!enable_ept)
6392 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6393 CPU_BASED_CR3_LOAD_EXITING |
6394 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006395 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6396 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6397 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006398 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6399 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006400 return exec_control;
6401}
6402
Jim Mattson45ec3682017-08-23 16:32:04 -07006403static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006404{
Jim Mattson45ec3682017-08-23 16:32:04 -07006405 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006406 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006407}
6408
Jim Mattson75f4fc82017-08-23 16:32:03 -07006409static bool vmx_rdseed_supported(void)
6410{
6411 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006412 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006413}
6414
Paolo Bonzini80154d72017-08-24 13:55:35 +02006415static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006416{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006417 struct kvm_vcpu *vcpu = &vmx->vcpu;
6418
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006419 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006420
Paolo Bonzini80154d72017-08-24 13:55:35 +02006421 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006422 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6423 if (vmx->vpid == 0)
6424 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6425 if (!enable_ept) {
6426 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6427 enable_unrestricted_guest = 0;
6428 }
6429 if (!enable_unrestricted_guest)
6430 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006431 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006432 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006433 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006434 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6435 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006436 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006437
6438 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6439 * in vmx_set_cr4. */
6440 exec_control &= ~SECONDARY_EXEC_DESC;
6441
Abel Gordonabc4fc52013-04-18 14:35:25 +03006442 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6443 (handle_vmptrld).
6444 We can NOT enable shadow_vmcs here because we don't have yet
6445 a current VMCS12
6446 */
6447 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006448
6449 if (!enable_pml)
6450 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006451
Paolo Bonzini3db13482017-08-24 14:48:03 +02006452 if (vmx_xsaves_supported()) {
6453 /* Exposing XSAVES only when XSAVE is exposed */
6454 bool xsaves_enabled =
6455 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6456 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6457
6458 if (!xsaves_enabled)
6459 exec_control &= ~SECONDARY_EXEC_XSAVES;
6460
6461 if (nested) {
6462 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006463 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006464 SECONDARY_EXEC_XSAVES;
6465 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006466 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006467 ~SECONDARY_EXEC_XSAVES;
6468 }
6469 }
6470
Paolo Bonzini80154d72017-08-24 13:55:35 +02006471 if (vmx_rdtscp_supported()) {
6472 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6473 if (!rdtscp_enabled)
6474 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6475
6476 if (nested) {
6477 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006478 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006479 SECONDARY_EXEC_RDTSCP;
6480 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006481 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006482 ~SECONDARY_EXEC_RDTSCP;
6483 }
6484 }
6485
6486 if (vmx_invpcid_supported()) {
6487 /* Exposing INVPCID only when PCID is exposed */
6488 bool invpcid_enabled =
6489 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6490 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6491
6492 if (!invpcid_enabled) {
6493 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6494 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6495 }
6496
6497 if (nested) {
6498 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006499 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006500 SECONDARY_EXEC_ENABLE_INVPCID;
6501 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006502 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006503 ~SECONDARY_EXEC_ENABLE_INVPCID;
6504 }
6505 }
6506
Jim Mattson45ec3682017-08-23 16:32:04 -07006507 if (vmx_rdrand_supported()) {
6508 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6509 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006510 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006511
6512 if (nested) {
6513 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006514 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006515 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006516 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006517 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006518 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006519 }
6520 }
6521
Jim Mattson75f4fc82017-08-23 16:32:03 -07006522 if (vmx_rdseed_supported()) {
6523 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6524 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006525 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006526
6527 if (nested) {
6528 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006529 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006530 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006531 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006532 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006533 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006534 }
6535 }
6536
Paolo Bonzini80154d72017-08-24 13:55:35 +02006537 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006538}
6539
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006540static void ept_set_mmio_spte_mask(void)
6541{
6542 /*
6543 * EPT Misconfigurations can be generated if the value of bits 2:0
6544 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006545 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006546 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6547 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006548}
6549
Wanpeng Lif53cd632014-12-02 19:14:58 +08006550#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006551/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552 * Sets up the vmcs for emulated real mode.
6553 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006554static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006555{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006556 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006557
Abel Gordon4607c2d2013-04-18 14:35:55 +03006558 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006559 /*
6560 * At vCPU creation, "VMWRITE to any supported field
6561 * in the VMCS" is supported, so use the more
6562 * permissive vmx_vmread_bitmap to specify both read
6563 * and write permissions for the shadow VMCS.
6564 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006565 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006566 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006567 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006568 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006569 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006570
Avi Kivity6aa8b732006-12-10 02:21:36 -08006571 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6572
Avi Kivity6aa8b732006-12-10 02:21:36 -08006573 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006574 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006575 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006576
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006577 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006578
Dan Williamsdfa169b2016-06-02 11:17:24 -07006579 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006580 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006581 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006582 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006583 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006584
Andrey Smetanind62caab2015-11-10 15:36:33 +03006585 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006586 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6587 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6588 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6589 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6590
6591 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006592
Li RongQing0bcf2612015-12-03 13:29:34 +08006593 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006594 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006595 }
6596
Wanpeng Lib31c1142018-03-12 04:53:04 -07006597 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006598 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006599 vmx->ple_window = ple_window;
6600 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006601 }
6602
Xiao Guangrongc3707952011-07-12 03:28:04 +08006603 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6604 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006605 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6606
Avi Kivity9581d442010-10-19 16:46:55 +02006607 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6608 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006609 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006610 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6611 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612
Bandan Das2a499e42017-08-03 15:54:41 -04006613 if (cpu_has_vmx_vmfunc())
6614 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6615
Eddie Dong2cc51562007-05-21 07:28:09 +03006616 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6617 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006618 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006619 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006620 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621
Radim Krčmář74545702015-04-27 15:11:25 +02006622 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6623 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006624
Paolo Bonzini03916db2014-07-24 14:21:57 +02006625 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006626 u32 index = vmx_msr_index[i];
6627 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006628 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006629
6630 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6631 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006632 if (wrmsr_safe(index, data_low, data_high) < 0)
6633 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006634 vmx->guest_msrs[j].index = i;
6635 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006636 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006637 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006638 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006640 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006641
6642 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643
6644 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006645 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006646
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006647 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6648 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6649
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006650 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006651
Wanpeng Lif53cd632014-12-02 19:14:58 +08006652 if (vmx_xsaves_supported())
6653 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6654
Peter Feiner4e595162016-07-07 14:49:58 -07006655 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006656 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6657 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6658 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006659
6660 if (cpu_has_vmx_encls_vmexit())
6661 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006662}
6663
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006664static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006665{
6666 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006667 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006668 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006669
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006670 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006671 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006672
Wanpeng Li518e7b92018-02-28 14:03:31 +08006673 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006674 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006675 kvm_set_cr8(vcpu, 0);
6676
6677 if (!init_event) {
6678 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6679 MSR_IA32_APICBASE_ENABLE;
6680 if (kvm_vcpu_is_reset_bsp(vcpu))
6681 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6682 apic_base_msr.host_initiated = true;
6683 kvm_set_apic_base(vcpu, &apic_base_msr);
6684 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006685
Avi Kivity2fb92db2011-04-27 19:42:18 +03006686 vmx_segment_cache_clear(vmx);
6687
Avi Kivity5706be02008-08-20 15:07:31 +03006688 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006689 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006690 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006691
6692 seg_setup(VCPU_SREG_DS);
6693 seg_setup(VCPU_SREG_ES);
6694 seg_setup(VCPU_SREG_FS);
6695 seg_setup(VCPU_SREG_GS);
6696 seg_setup(VCPU_SREG_SS);
6697
6698 vmcs_write16(GUEST_TR_SELECTOR, 0);
6699 vmcs_writel(GUEST_TR_BASE, 0);
6700 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6701 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6702
6703 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6704 vmcs_writel(GUEST_LDTR_BASE, 0);
6705 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6706 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6707
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006708 if (!init_event) {
6709 vmcs_write32(GUEST_SYSENTER_CS, 0);
6710 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6711 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6712 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6713 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006714
Wanpeng Lic37c2872017-11-20 14:52:21 -08006715 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006716 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006717
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006718 vmcs_writel(GUEST_GDTR_BASE, 0);
6719 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6720
6721 vmcs_writel(GUEST_IDTR_BASE, 0);
6722 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6723
Anthony Liguori443381a2010-12-06 10:53:38 -06006724 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006725 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006726 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006727 if (kvm_mpx_supported())
6728 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006730 setup_msrs(vmx);
6731
Avi Kivity6aa8b732006-12-10 02:21:36 -08006732 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6733
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006734 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006735 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006736 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006737 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006738 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006739 vmcs_write32(TPR_THRESHOLD, 0);
6740 }
6741
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006742 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743
Sheng Yang2384d2b2008-01-17 15:14:33 +08006744 if (vmx->vpid != 0)
6745 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6746
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006747 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006748 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006749 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006750 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006751 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006752
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006753 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006754
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006755 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006756 if (init_event)
6757 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006758}
6759
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006760/*
6761 * In nested virtualization, check if L1 asked to exit on external interrupts.
6762 * For most existing hypervisors, this will always return true.
6763 */
6764static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6765{
6766 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6767 PIN_BASED_EXT_INTR_MASK;
6768}
6769
Bandan Das77b0f5d2014-04-19 18:17:45 -04006770/*
6771 * In nested virtualization, check if L1 has set
6772 * VM_EXIT_ACK_INTR_ON_EXIT
6773 */
6774static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6775{
6776 return get_vmcs12(vcpu)->vm_exit_controls &
6777 VM_EXIT_ACK_INTR_ON_EXIT;
6778}
6779
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006780static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6781{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006782 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006783}
6784
Jan Kiszkac9a79532014-03-07 20:03:15 +01006785static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006786{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006787 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6788 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006789}
6790
Jan Kiszkac9a79532014-03-07 20:03:15 +01006791static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006792{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006793 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006794 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006795 enable_irq_window(vcpu);
6796 return;
6797 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006798
Paolo Bonzini47c01522016-12-19 11:44:07 +01006799 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6800 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006801}
6802
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006803static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006804{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006805 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006806 uint32_t intr;
6807 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006808
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006809 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006810
Avi Kivityfa89a812008-09-01 15:57:51 +03006811 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006812 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006813 int inc_eip = 0;
6814 if (vcpu->arch.interrupt.soft)
6815 inc_eip = vcpu->arch.event_exit_inst_len;
6816 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006817 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006818 return;
6819 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006820 intr = irq | INTR_INFO_VALID_MASK;
6821 if (vcpu->arch.interrupt.soft) {
6822 intr |= INTR_TYPE_SOFT_INTR;
6823 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6824 vmx->vcpu.arch.event_exit_inst_len);
6825 } else
6826 intr |= INTR_TYPE_EXT_INTR;
6827 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006828
6829 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006830}
6831
Sheng Yangf08864b2008-05-15 18:23:25 +08006832static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6833{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006834 struct vcpu_vmx *vmx = to_vmx(vcpu);
6835
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006836 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006837 /*
6838 * Tracking the NMI-blocked state in software is built upon
6839 * finding the next open IRQ window. This, in turn, depends on
6840 * well-behaving guests: They have to keep IRQs disabled at
6841 * least as long as the NMI handler runs. Otherwise we may
6842 * cause NMI nesting, maybe breaking the guest. But as this is
6843 * highly unlikely, we can live with the residual risk.
6844 */
6845 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6846 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6847 }
6848
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006849 ++vcpu->stat.nmi_injections;
6850 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006851
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006852 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006853 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006854 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006855 return;
6856 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006857
Sheng Yangf08864b2008-05-15 18:23:25 +08006858 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6859 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006860
6861 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006862}
6863
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006864static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6865{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006866 struct vcpu_vmx *vmx = to_vmx(vcpu);
6867 bool masked;
6868
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006869 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006870 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006871 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006872 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006873 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6874 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6875 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006876}
6877
6878static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6879{
6880 struct vcpu_vmx *vmx = to_vmx(vcpu);
6881
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006882 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006883 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6884 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6885 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6886 }
6887 } else {
6888 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6889 if (masked)
6890 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6891 GUEST_INTR_STATE_NMI);
6892 else
6893 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6894 GUEST_INTR_STATE_NMI);
6895 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006896}
6897
Jan Kiszka2505dc92013-04-14 12:12:47 +02006898static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6899{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006900 if (to_vmx(vcpu)->nested.nested_run_pending)
6901 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006902
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006903 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006904 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6905 return 0;
6906
Jan Kiszka2505dc92013-04-14 12:12:47 +02006907 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6908 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6909 | GUEST_INTR_STATE_NMI));
6910}
6911
Gleb Natapov78646122009-03-23 12:12:11 +02006912static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6913{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006914 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6915 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006916 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6917 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006918}
6919
Izik Eiduscbc94022007-10-25 00:29:55 +02006920static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6921{
6922 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006923
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006924 if (enable_unrestricted_guest)
6925 return 0;
6926
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006927 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6928 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006929 if (ret)
6930 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006931 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006932 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006933}
6934
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006935static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6936{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006937 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006938 return 0;
6939}
6940
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006941static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006942{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006943 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006944 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006945 /*
6946 * Update instruction length as we may reinject the exception
6947 * from user space while in guest debugging mode.
6948 */
6949 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6950 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006951 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006952 return false;
6953 /* fall through */
6954 case DB_VECTOR:
6955 if (vcpu->guest_debug &
6956 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6957 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006958 /* fall through */
6959 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006960 case OF_VECTOR:
6961 case BR_VECTOR:
6962 case UD_VECTOR:
6963 case DF_VECTOR:
6964 case SS_VECTOR:
6965 case GP_VECTOR:
6966 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006967 return true;
6968 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006969 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006970 return false;
6971}
6972
6973static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6974 int vec, u32 err_code)
6975{
6976 /*
6977 * Instruction with address size override prefix opcode 0x67
6978 * Cause the #SS fault with 0 error code in VM86 mode.
6979 */
6980 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006981 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006982 if (vcpu->arch.halt_request) {
6983 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006984 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006985 }
6986 return 1;
6987 }
6988 return 0;
6989 }
6990
6991 /*
6992 * Forward all other exceptions that are valid in real mode.
6993 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6994 * the required debugging infrastructure rework.
6995 */
6996 kvm_queue_exception(vcpu, vec);
6997 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998}
6999
Andi Kleena0861c02009-06-08 17:37:09 +08007000/*
7001 * Trigger machine check on the host. We assume all the MSRs are already set up
7002 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7003 * We pass a fake environment to the machine check handler because we want
7004 * the guest to be always treated like user space, no matter what context
7005 * it used internally.
7006 */
7007static void kvm_machine_check(void)
7008{
7009#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7010 struct pt_regs regs = {
7011 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7012 .flags = X86_EFLAGS_IF,
7013 };
7014
7015 do_machine_check(&regs, 0);
7016#endif
7017}
7018
Avi Kivity851ba692009-08-24 11:10:17 +03007019static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007020{
7021 /* already handled by vcpu_run */
7022 return 1;
7023}
7024
Avi Kivity851ba692009-08-24 11:10:17 +03007025static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026{
Avi Kivity1155f762007-11-22 11:30:47 +02007027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007028 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007029 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007030 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031 u32 vect_info;
7032 enum emulation_result er;
7033
Avi Kivity1155f762007-11-22 11:30:47 +02007034 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007035 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036
Andi Kleena0861c02009-06-08 17:37:09 +08007037 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007038 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007039
Jim Mattsonef85b672016-12-12 11:01:37 -08007040 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007041 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007042
Wanpeng Li082d06e2018-04-03 16:28:48 -07007043 if (is_invalid_opcode(intr_info))
7044 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007045
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007047 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007049
Liran Alon9e869482018-03-12 13:12:51 +02007050 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7051 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007052 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007053 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7054 if (er == EMULATE_USER_EXIT)
7055 return 0;
7056 else if (er != EMULATE_DONE)
7057 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7058 return 1;
7059 }
7060
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007061 /*
7062 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7063 * MMIO, it is better to report an internal error.
7064 * See the comments in vmx_handle_exit.
7065 */
7066 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7067 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7068 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7069 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007070 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007071 vcpu->run->internal.data[0] = vect_info;
7072 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007073 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007074 return 0;
7075 }
7076
Avi Kivity6aa8b732006-12-10 02:21:36 -08007077 if (is_page_fault(intr_info)) {
7078 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007079 /* EPT won't cause page fault directly */
7080 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007081 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 }
7083
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007084 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007085
7086 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7087 return handle_rmode_exception(vcpu, ex_no, error_code);
7088
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007089 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007090 case AC_VECTOR:
7091 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7092 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007093 case DB_VECTOR:
7094 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7095 if (!(vcpu->guest_debug &
7096 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007097 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007098 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007099 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007100 skip_emulated_instruction(vcpu);
7101
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007102 kvm_queue_exception(vcpu, DB_VECTOR);
7103 return 1;
7104 }
7105 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7106 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7107 /* fall through */
7108 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007109 /*
7110 * Update instruction length as we may reinject #BP from
7111 * user space while in guest debugging mode. Reading it for
7112 * #DB as well causes no harm, it is not used in that case.
7113 */
7114 vmx->vcpu.arch.event_exit_inst_len =
7115 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007117 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007118 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7119 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007120 break;
7121 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007122 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7123 kvm_run->ex.exception = ex_no;
7124 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127 return 0;
7128}
7129
Avi Kivity851ba692009-08-24 11:10:17 +03007130static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007132 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133 return 1;
7134}
7135
Avi Kivity851ba692009-08-24 11:10:17 +03007136static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007137{
Avi Kivity851ba692009-08-24 11:10:17 +03007138 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007139 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007140 return 0;
7141}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142
Avi Kivity851ba692009-08-24 11:10:17 +03007143static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144{
He, Qingbfdaab02007-09-12 14:18:28 +08007145 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007146 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007147 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148
He, Qingbfdaab02007-09-12 14:18:28 +08007149 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007150 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007151
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007152 ++vcpu->stat.io_exits;
7153
Sean Christopherson432baf62018-03-08 08:57:26 -08007154 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007155 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007156
7157 port = exit_qualification >> 16;
7158 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007159 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007160
Sean Christophersondca7f122018-03-08 08:57:27 -08007161 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007162}
7163
Ingo Molnar102d8322007-02-19 14:37:47 +02007164static void
7165vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7166{
7167 /*
7168 * Patch in the VMCALL instruction:
7169 */
7170 hypercall[0] = 0x0f;
7171 hypercall[1] = 0x01;
7172 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007173}
7174
Guo Chao0fa06072012-06-28 15:16:19 +08007175/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007176static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7177{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007178 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007179 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7180 unsigned long orig_val = val;
7181
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007182 /*
7183 * We get here when L2 changed cr0 in a way that did not change
7184 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007185 * but did change L0 shadowed bits. So we first calculate the
7186 * effective cr0 value that L1 would like to write into the
7187 * hardware. It consists of the L2-owned bits from the new
7188 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007189 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007190 val = (val & ~vmcs12->cr0_guest_host_mask) |
7191 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7192
David Matlack38991522016-11-29 18:14:08 -08007193 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007194 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195
7196 if (kvm_set_cr0(vcpu, val))
7197 return 1;
7198 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007199 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007200 } else {
7201 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007202 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007203 return 1;
David Matlack38991522016-11-29 18:14:08 -08007204
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007205 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007206 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007207}
7208
7209static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7210{
7211 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007212 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7213 unsigned long orig_val = val;
7214
7215 /* analogously to handle_set_cr0 */
7216 val = (val & ~vmcs12->cr4_guest_host_mask) |
7217 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7218 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007219 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007220 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 return 0;
7222 } else
7223 return kvm_set_cr4(vcpu, val);
7224}
7225
Paolo Bonzini0367f202016-07-12 10:44:55 +02007226static int handle_desc(struct kvm_vcpu *vcpu)
7227{
7228 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007229 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007230}
7231
Avi Kivity851ba692009-08-24 11:10:17 +03007232static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007233{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007234 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007235 int cr;
7236 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007237 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007238 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007239
He, Qingbfdaab02007-09-12 14:18:28 +08007240 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241 cr = exit_qualification & 15;
7242 reg = (exit_qualification >> 8) & 15;
7243 switch ((exit_qualification >> 4) & 3) {
7244 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007245 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007246 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007247 switch (cr) {
7248 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007249 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007250 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007251 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007252 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007253 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007255 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007256 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007258 case 8: {
7259 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007260 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007261 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007262 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007263 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007264 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007265 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007266 return ret;
7267 /*
7268 * TODO: we might be squashing a
7269 * KVM_GUESTDBG_SINGLESTEP-triggered
7270 * KVM_EXIT_DEBUG here.
7271 */
Avi Kivity851ba692009-08-24 11:10:17 +03007272 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007273 return 0;
7274 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007276 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007277 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007278 WARN_ONCE(1, "Guest should always own CR0.TS");
7279 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007280 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007281 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007282 case 1: /*mov from cr*/
7283 switch (cr) {
7284 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007285 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007286 val = kvm_read_cr3(vcpu);
7287 kvm_register_write(vcpu, reg, val);
7288 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007289 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007290 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007291 val = kvm_get_cr8(vcpu);
7292 kvm_register_write(vcpu, reg, val);
7293 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007294 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007295 }
7296 break;
7297 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007298 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007299 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007300 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301
Kyle Huey6affcbe2016-11-29 12:40:40 -08007302 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303 default:
7304 break;
7305 }
Avi Kivity851ba692009-08-24 11:10:17 +03007306 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007307 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 (int)(exit_qualification >> 4) & 3, cr);
7309 return 0;
7310}
7311
Avi Kivity851ba692009-08-24 11:10:17 +03007312static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007313{
He, Qingbfdaab02007-09-12 14:18:28 +08007314 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007315 int dr, dr7, reg;
7316
7317 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7318 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7319
7320 /* First, if DR does not exist, trigger UD */
7321 if (!kvm_require_dr(vcpu, dr))
7322 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007323
Jan Kiszkaf2483412010-01-20 18:20:20 +01007324 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007325 if (!kvm_require_cpl(vcpu, 0))
7326 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007327 dr7 = vmcs_readl(GUEST_DR7);
7328 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007329 /*
7330 * As the vm-exit takes precedence over the debug trap, we
7331 * need to emulate the latter, either for the host or the
7332 * guest debugging itself.
7333 */
7334 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007335 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007336 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007337 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007338 vcpu->run->debug.arch.exception = DB_VECTOR;
7339 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007340 return 0;
7341 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007342 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007343 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007344 kvm_queue_exception(vcpu, DB_VECTOR);
7345 return 1;
7346 }
7347 }
7348
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007349 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007350 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7351 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007352
7353 /*
7354 * No more DR vmexits; force a reload of the debug registers
7355 * and reenter on this instruction. The next vmexit will
7356 * retrieve the full state of the debug registers.
7357 */
7358 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7359 return 1;
7360 }
7361
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007362 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7363 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007364 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007365
7366 if (kvm_get_dr(vcpu, dr, &val))
7367 return 1;
7368 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007369 } else
Nadav Amit57773922014-06-18 17:19:23 +03007370 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007371 return 1;
7372
Kyle Huey6affcbe2016-11-29 12:40:40 -08007373 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007374}
7375
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007376static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7377{
7378 return vcpu->arch.dr6;
7379}
7380
7381static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7382{
7383}
7384
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007385static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7386{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007387 get_debugreg(vcpu->arch.db[0], 0);
7388 get_debugreg(vcpu->arch.db[1], 1);
7389 get_debugreg(vcpu->arch.db[2], 2);
7390 get_debugreg(vcpu->arch.db[3], 3);
7391 get_debugreg(vcpu->arch.dr6, 6);
7392 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7393
7394 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007395 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007396}
7397
Gleb Natapov020df072010-04-13 10:05:23 +03007398static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7399{
7400 vmcs_writel(GUEST_DR7, val);
7401}
7402
Avi Kivity851ba692009-08-24 11:10:17 +03007403static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007404{
Kyle Huey6a908b62016-11-29 12:40:37 -08007405 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406}
7407
Avi Kivity851ba692009-08-24 11:10:17 +03007408static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007409{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007410 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007411 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007412
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007413 msr_info.index = ecx;
7414 msr_info.host_initiated = false;
7415 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007416 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007417 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007418 return 1;
7419 }
7420
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007421 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007422
Avi Kivity6aa8b732006-12-10 02:21:36 -08007423 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007424 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7425 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007426 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007427}
7428
Avi Kivity851ba692009-08-24 11:10:17 +03007429static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007430{
Will Auld8fe8ab42012-11-29 12:42:12 -08007431 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007432 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7433 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7434 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007435
Will Auld8fe8ab42012-11-29 12:42:12 -08007436 msr.data = data;
7437 msr.index = ecx;
7438 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007439 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007440 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007441 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007442 return 1;
7443 }
7444
Avi Kivity59200272010-01-25 19:47:02 +02007445 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007446 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447}
7448
Avi Kivity851ba692009-08-24 11:10:17 +03007449static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007450{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007451 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007452 return 1;
7453}
7454
Avi Kivity851ba692009-08-24 11:10:17 +03007455static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007457 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7458 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007459
Avi Kivity3842d132010-07-27 12:30:24 +03007460 kvm_make_request(KVM_REQ_EVENT, vcpu);
7461
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007462 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007463 return 1;
7464}
7465
Avi Kivity851ba692009-08-24 11:10:17 +03007466static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467{
Avi Kivityd3bef152007-06-05 15:53:05 +03007468 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469}
7470
Avi Kivity851ba692009-08-24 11:10:17 +03007471static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007472{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007473 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007474}
7475
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007476static int handle_invd(struct kvm_vcpu *vcpu)
7477{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007478 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007482{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007483 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007484
7485 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007486 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007487}
7488
Avi Kivityfee84b02011-11-10 14:57:25 +02007489static int handle_rdpmc(struct kvm_vcpu *vcpu)
7490{
7491 int err;
7492
7493 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007494 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007495}
7496
Avi Kivity851ba692009-08-24 11:10:17 +03007497static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007498{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007499 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007500}
7501
Dexuan Cui2acf9232010-06-10 11:27:12 +08007502static int handle_xsetbv(struct kvm_vcpu *vcpu)
7503{
7504 u64 new_bv = kvm_read_edx_eax(vcpu);
7505 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7506
7507 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007508 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007509 return 1;
7510}
7511
Wanpeng Lif53cd632014-12-02 19:14:58 +08007512static int handle_xsaves(struct kvm_vcpu *vcpu)
7513{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007514 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007515 WARN(1, "this should never happen\n");
7516 return 1;
7517}
7518
7519static int handle_xrstors(struct kvm_vcpu *vcpu)
7520{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007521 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007522 WARN(1, "this should never happen\n");
7523 return 1;
7524}
7525
Avi Kivity851ba692009-08-24 11:10:17 +03007526static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007527{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007528 if (likely(fasteoi)) {
7529 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7530 int access_type, offset;
7531
7532 access_type = exit_qualification & APIC_ACCESS_TYPE;
7533 offset = exit_qualification & APIC_ACCESS_OFFSET;
7534 /*
7535 * Sane guest uses MOV to write EOI, with written value
7536 * not cared. So make a short-circuit here by avoiding
7537 * heavy instruction emulation.
7538 */
7539 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7540 (offset == APIC_EOI)) {
7541 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007542 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007543 }
7544 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007545 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007546}
7547
Yang Zhangc7c9c562013-01-25 10:18:51 +08007548static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7549{
7550 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7551 int vector = exit_qualification & 0xff;
7552
7553 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7554 kvm_apic_set_eoi_accelerated(vcpu, vector);
7555 return 1;
7556}
7557
Yang Zhang83d4c282013-01-25 10:18:49 +08007558static int handle_apic_write(struct kvm_vcpu *vcpu)
7559{
7560 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7561 u32 offset = exit_qualification & 0xfff;
7562
7563 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7564 kvm_apic_write_nodecode(vcpu, offset);
7565 return 1;
7566}
7567
Avi Kivity851ba692009-08-24 11:10:17 +03007568static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007569{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007571 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007572 bool has_error_code = false;
7573 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007574 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007575 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007576
7577 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007578 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007579 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007580
7581 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7582
7583 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007584 if (reason == TASK_SWITCH_GATE && idt_v) {
7585 switch (type) {
7586 case INTR_TYPE_NMI_INTR:
7587 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007588 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589 break;
7590 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007591 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007592 kvm_clear_interrupt_queue(vcpu);
7593 break;
7594 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007595 if (vmx->idt_vectoring_info &
7596 VECTORING_INFO_DELIVER_CODE_MASK) {
7597 has_error_code = true;
7598 error_code =
7599 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7600 }
7601 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007602 case INTR_TYPE_SOFT_EXCEPTION:
7603 kvm_clear_exception_queue(vcpu);
7604 break;
7605 default:
7606 break;
7607 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007608 }
Izik Eidus37817f22008-03-24 23:14:53 +02007609 tss_selector = exit_qualification;
7610
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007611 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7612 type != INTR_TYPE_EXT_INTR &&
7613 type != INTR_TYPE_NMI_INTR))
7614 skip_emulated_instruction(vcpu);
7615
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007616 if (kvm_task_switch(vcpu, tss_selector,
7617 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7618 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007619 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7620 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7621 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007622 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007623 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007624
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007625 /*
7626 * TODO: What about debug traps on tss switch?
7627 * Are we supposed to inject them and update dr6?
7628 */
7629
7630 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007631}
7632
Avi Kivity851ba692009-08-24 11:10:17 +03007633static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007634{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007635 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007636 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007637 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007638
Sheng Yangf9c617f2009-03-25 10:08:52 +08007639 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007640
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007641 /*
7642 * EPT violation happened while executing iret from NMI,
7643 * "blocked by NMI" bit has to be set before next VM entry.
7644 * There are errata that may cause this bit to not be set:
7645 * AAK134, BY25.
7646 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007647 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007648 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007649 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007650 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7651
Sheng Yang14394422008-04-28 12:24:45 +08007652 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007653 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007654
Junaid Shahid27959a42016-12-06 16:46:10 -08007655 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007656 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007657 ? PFERR_USER_MASK : 0;
7658 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007659 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007660 ? PFERR_WRITE_MASK : 0;
7661 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007662 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007663 ? PFERR_FETCH_MASK : 0;
7664 /* ept page table entry is present? */
7665 error_code |= (exit_qualification &
7666 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7667 EPT_VIOLATION_EXECUTABLE))
7668 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007669
Paolo Bonzinieebed242016-11-28 14:39:58 +01007670 error_code |= (exit_qualification & 0x100) != 0 ?
7671 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007672
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007673 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007674 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007675}
7676
Avi Kivity851ba692009-08-24 11:10:17 +03007677static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007678{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007679 gpa_t gpa;
7680
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007681 /*
7682 * A nested guest cannot optimize MMIO vmexits, because we have an
7683 * nGPA here instead of the required GPA.
7684 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007685 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007686 if (!is_guest_mode(vcpu) &&
7687 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007688 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007689 /*
7690 * Doing kvm_skip_emulated_instruction() depends on undefined
7691 * behavior: Intel's manual doesn't mandate
7692 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7693 * occurs and while on real hardware it was observed to be set,
7694 * other hypervisors (namely Hyper-V) don't set it, we end up
7695 * advancing IP with some random value. Disable fast mmio when
7696 * running nested and keep it for real hardware in hope that
7697 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7698 */
7699 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7700 return kvm_skip_emulated_instruction(vcpu);
7701 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007702 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007703 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007704 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007705
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007706 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007707}
7708
Avi Kivity851ba692009-08-24 11:10:17 +03007709static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007710{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007711 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007712 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7713 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007714 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007715 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007716
7717 return 1;
7718}
7719
Mohammed Gamal80ced182009-09-01 12:48:18 +02007720static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007721{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007722 struct vcpu_vmx *vmx = to_vmx(vcpu);
7723 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007724 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007725 u32 cpu_exec_ctrl;
7726 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007727 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007728
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007729 /*
7730 * We should never reach the point where we are emulating L2
7731 * due to invalid guest state as that means we incorrectly
7732 * allowed a nested VMEntry with an invalid vmcs12.
7733 */
7734 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7735
Avi Kivity49e9d552010-09-19 14:34:08 +02007736 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7737 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007738
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007739 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007740 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007741 return handle_interrupt_window(&vmx->vcpu);
7742
Radim Krčmář72875d82017-04-26 22:32:19 +02007743 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007744 return 1;
7745
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007746 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007747
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007748 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007749 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007750 ret = 0;
7751 goto out;
7752 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007753
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007754 if (err != EMULATE_DONE)
7755 goto emulation_error;
7756
7757 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7758 vcpu->arch.exception.pending)
7759 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007760
Gleb Natapov8d76c492013-05-08 18:38:44 +03007761 if (vcpu->arch.halt_request) {
7762 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007763 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007764 goto out;
7765 }
7766
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007767 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007768 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007769 if (need_resched())
7770 schedule();
7771 }
7772
Mohammed Gamal80ced182009-09-01 12:48:18 +02007773out:
7774 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007775
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007776emulation_error:
7777 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7778 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7779 vcpu->run->internal.ndata = 0;
7780 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007781}
7782
7783static void grow_ple_window(struct kvm_vcpu *vcpu)
7784{
7785 struct vcpu_vmx *vmx = to_vmx(vcpu);
7786 int old = vmx->ple_window;
7787
Babu Mogerc8e88712018-03-16 16:37:24 -04007788 vmx->ple_window = __grow_ple_window(old, ple_window,
7789 ple_window_grow,
7790 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007791
7792 if (vmx->ple_window != old)
7793 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007794
7795 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007796}
7797
7798static void shrink_ple_window(struct kvm_vcpu *vcpu)
7799{
7800 struct vcpu_vmx *vmx = to_vmx(vcpu);
7801 int old = vmx->ple_window;
7802
Babu Mogerc8e88712018-03-16 16:37:24 -04007803 vmx->ple_window = __shrink_ple_window(old, ple_window,
7804 ple_window_shrink,
7805 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007806
7807 if (vmx->ple_window != old)
7808 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007809
7810 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007811}
7812
7813/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007814 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7815 */
7816static void wakeup_handler(void)
7817{
7818 struct kvm_vcpu *vcpu;
7819 int cpu = smp_processor_id();
7820
7821 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7822 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7823 blocked_vcpu_list) {
7824 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7825
7826 if (pi_test_on(pi_desc) == 1)
7827 kvm_vcpu_kick(vcpu);
7828 }
7829 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7830}
7831
Peng Haoe01bca22018-04-07 05:47:32 +08007832static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007833{
7834 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7835 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7836 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7837 0ull, VMX_EPT_EXECUTABLE_MASK,
7838 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007839 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007840
7841 ept_set_mmio_spte_mask();
7842 kvm_enable_tdp();
7843}
7844
Tiejun Chenf2c76482014-10-28 10:14:47 +08007845static __init int hardware_setup(void)
7846{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007847 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007848 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007849
7850 rdmsrl_safe(MSR_EFER, &host_efer);
7851
7852 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7853 kvm_define_shared_msr(i, vmx_msr_index[i]);
7854
Radim Krčmář23611332016-09-29 22:41:33 +02007855 for (i = 0; i < VMX_BITMAP_NR; i++) {
7856 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7857 if (!vmx_bitmap[i])
7858 goto out;
7859 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007860
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007861 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7862 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7863
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007864 if (setup_vmcs_config(&vmcs_config) < 0) {
7865 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007866 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007867 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007868
7869 if (boot_cpu_has(X86_FEATURE_NX))
7870 kvm_enable_efer_bits(EFER_NX);
7871
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007872 if (boot_cpu_has(X86_FEATURE_MPX)) {
7873 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7874 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7875 }
7876
Wanpeng Li08d839c2017-03-23 05:30:08 -07007877 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7878 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007879 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007880
Tiejun Chenf2c76482014-10-28 10:14:47 +08007881 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007882 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007883 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007884 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007885 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007886
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007887 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007888 enable_ept_ad_bits = 0;
7889
Wanpeng Li8ad81822017-10-09 15:51:53 -07007890 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891 enable_unrestricted_guest = 0;
7892
Paolo Bonziniad15a292015-01-30 16:18:49 +01007893 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007894 flexpriority_enabled = 0;
7895
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007896 if (!cpu_has_virtual_nmis())
7897 enable_vnmi = 0;
7898
Paolo Bonziniad15a292015-01-30 16:18:49 +01007899 /*
7900 * set_apic_access_page_addr() is used to reload apic access
7901 * page upon invalidation. No need to do anything if not
7902 * using the APIC_ACCESS_ADDR VMCS field.
7903 */
7904 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007905 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007906
7907 if (!cpu_has_vmx_tpr_shadow())
7908 kvm_x86_ops->update_cr8_intercept = NULL;
7909
7910 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7911 kvm_disable_largepages();
7912
Tianyu Lan877ad952018-07-19 08:40:23 +00007913#if IS_ENABLED(CONFIG_HYPERV)
7914 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7915 && enable_ept)
7916 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7917#endif
7918
Wanpeng Li0f107682017-09-28 18:06:24 -07007919 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007920 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007921 ple_window = 0;
7922 ple_window_grow = 0;
7923 ple_window_max = 0;
7924 ple_window_shrink = 0;
7925 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007926
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007927 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007928 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007929 kvm_x86_ops->sync_pir_to_irr = NULL;
7930 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931
Haozhong Zhang64903d62015-10-20 15:39:09 +08007932 if (cpu_has_vmx_tsc_scaling()) {
7933 kvm_has_tsc_control = true;
7934 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7935 kvm_tsc_scaling_ratio_frac_bits = 48;
7936 }
7937
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007938 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7939
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007940 if (enable_ept)
7941 vmx_enable_tdp();
7942 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007943 kvm_disable_tdp();
7944
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007945 if (!nested) {
7946 kvm_x86_ops->get_nested_state = NULL;
7947 kvm_x86_ops->set_nested_state = NULL;
7948 }
7949
Kai Huang843e4332015-01-28 10:54:28 +08007950 /*
7951 * Only enable PML when hardware supports PML feature, and both EPT
7952 * and EPT A/D bit features are enabled -- PML depends on them to work.
7953 */
7954 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7955 enable_pml = 0;
7956
7957 if (!enable_pml) {
7958 kvm_x86_ops->slot_enable_log_dirty = NULL;
7959 kvm_x86_ops->slot_disable_log_dirty = NULL;
7960 kvm_x86_ops->flush_log_dirty = NULL;
7961 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7962 }
7963
Sean Christophersond264ee02018-08-27 15:21:12 -07007964 if (!cpu_has_vmx_preemption_timer())
7965 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7966
Yunhong Jiang64672c92016-06-13 14:19:59 -07007967 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7968 u64 vmx_msr;
7969
7970 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7971 cpu_preemption_timer_multi =
7972 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7973 } else {
7974 kvm_x86_ops->set_hv_timer = NULL;
7975 kvm_x86_ops->cancel_hv_timer = NULL;
7976 }
7977
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007978 if (!cpu_has_vmx_shadow_vmcs())
7979 enable_shadow_vmcs = 0;
7980 if (enable_shadow_vmcs)
7981 init_vmcs_shadow_fields();
7982
Feng Wubf9f6ac2015-09-18 22:29:55 +08007983 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007984 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007985
Ashok Rajc45dcc72016-06-22 14:59:56 +08007986 kvm_mce_cap_supported |= MCG_LMCE_P;
7987
Tiejun Chenf2c76482014-10-28 10:14:47 +08007988 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007989
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007990out:
Radim Krčmář23611332016-09-29 22:41:33 +02007991 for (i = 0; i < VMX_BITMAP_NR; i++)
7992 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007993
7994 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007995}
7996
7997static __exit void hardware_unsetup(void)
7998{
Radim Krčmář23611332016-09-29 22:41:33 +02007999 int i;
8000
8001 for (i = 0; i < VMX_BITMAP_NR; i++)
8002 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008003
Tiejun Chenf2c76482014-10-28 10:14:47 +08008004 free_kvm_area();
8005}
8006
Avi Kivity6aa8b732006-12-10 02:21:36 -08008007/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008008 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8009 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8010 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008011static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008012{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008013 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008014 grow_ple_window(vcpu);
8015
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008016 /*
8017 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8018 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8019 * never set PAUSE_EXITING and just set PLE if supported,
8020 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8021 */
8022 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008023 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008024}
8025
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008026static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008027{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008028 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008029}
8030
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008031static int handle_mwait(struct kvm_vcpu *vcpu)
8032{
8033 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8034 return handle_nop(vcpu);
8035}
8036
Jim Mattson45ec3682017-08-23 16:32:04 -07008037static int handle_invalid_op(struct kvm_vcpu *vcpu)
8038{
8039 kvm_queue_exception(vcpu, UD_VECTOR);
8040 return 1;
8041}
8042
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008043static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8044{
8045 return 1;
8046}
8047
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008048static int handle_monitor(struct kvm_vcpu *vcpu)
8049{
8050 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8051 return handle_nop(vcpu);
8052}
8053
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008054/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008055 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8056 * set the success or error code of an emulated VMX instruction, as specified
8057 * by Vol 2B, VMX Instruction Reference, "Conventions".
8058 */
8059static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8060{
8061 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8062 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8063 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8064}
8065
8066static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8067{
8068 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8069 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8070 X86_EFLAGS_SF | X86_EFLAGS_OF))
8071 | X86_EFLAGS_CF);
8072}
8073
Abel Gordon145c28d2013-04-18 14:36:55 +03008074static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008075 u32 vm_instruction_error)
8076{
8077 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8078 /*
8079 * failValid writes the error number to the current VMCS, which
8080 * can't be done there isn't a current VMCS.
8081 */
8082 nested_vmx_failInvalid(vcpu);
8083 return;
8084 }
8085 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8086 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8087 X86_EFLAGS_SF | X86_EFLAGS_OF))
8088 | X86_EFLAGS_ZF);
8089 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8090 /*
8091 * We don't need to force a shadow sync because
8092 * VM_INSTRUCTION_ERROR is not shadowed
8093 */
8094}
Abel Gordon145c28d2013-04-18 14:36:55 +03008095
Wincy Vanff651cb2014-12-11 08:52:58 +03008096static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8097{
8098 /* TODO: not to reset guest simply here. */
8099 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008100 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008101}
8102
Jan Kiszkaf4124502014-03-07 20:03:13 +01008103static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8104{
8105 struct vcpu_vmx *vmx =
8106 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8107
8108 vmx->nested.preemption_timer_expired = true;
8109 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8110 kvm_vcpu_kick(&vmx->vcpu);
8111
8112 return HRTIMER_NORESTART;
8113}
8114
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008115/*
Bandan Das19677e32014-05-06 02:19:15 -04008116 * Decode the memory-address operand of a vmx instruction, as recorded on an
8117 * exit caused by such an instruction (run by a guest hypervisor).
8118 * On success, returns 0. When the operand is invalid, returns 1 and throws
8119 * #UD or #GP.
8120 */
8121static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8122 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008123 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008124{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008125 gva_t off;
8126 bool exn;
8127 struct kvm_segment s;
8128
Bandan Das19677e32014-05-06 02:19:15 -04008129 /*
8130 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8131 * Execution", on an exit, vmx_instruction_info holds most of the
8132 * addressing components of the operand. Only the displacement part
8133 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8134 * For how an actual address is calculated from all these components,
8135 * refer to Vol. 1, "Operand Addressing".
8136 */
8137 int scaling = vmx_instruction_info & 3;
8138 int addr_size = (vmx_instruction_info >> 7) & 7;
8139 bool is_reg = vmx_instruction_info & (1u << 10);
8140 int seg_reg = (vmx_instruction_info >> 15) & 7;
8141 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8142 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8143 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8144 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8145
8146 if (is_reg) {
8147 kvm_queue_exception(vcpu, UD_VECTOR);
8148 return 1;
8149 }
8150
8151 /* Addr = segment_base + offset */
8152 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008153 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008154 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008156 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off += kvm_register_read(vcpu, index_reg)<<scaling;
8158 vmx_get_segment(vcpu, &s, seg_reg);
8159 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008160
8161 if (addr_size == 1) /* 32 bit */
8162 *ret &= 0xffffffff;
8163
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008164 /* Checks for #GP/#SS exceptions. */
8165 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008166 if (is_long_mode(vcpu)) {
8167 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8168 * non-canonical form. This is the only check on the memory
8169 * destination for long mode!
8170 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008171 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008172 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008173 /* Protected mode: apply checks for segment validity in the
8174 * following order:
8175 * - segment type check (#GP(0) may be thrown)
8176 * - usability check (#GP(0)/#SS(0))
8177 * - limit check (#GP(0)/#SS(0))
8178 */
8179 if (wr)
8180 /* #GP(0) if the destination operand is located in a
8181 * read-only data segment or any code segment.
8182 */
8183 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8184 else
8185 /* #GP(0) if the source operand is located in an
8186 * execute-only code segment
8187 */
8188 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008189 if (exn) {
8190 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8191 return 1;
8192 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008193 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8194 */
8195 exn = (s.unusable != 0);
8196 /* Protected mode: #GP(0)/#SS(0) if the memory
8197 * operand is outside the segment limit.
8198 */
8199 exn = exn || (off + sizeof(u64) > s.limit);
8200 }
8201 if (exn) {
8202 kvm_queue_exception_e(vcpu,
8203 seg_reg == VCPU_SREG_SS ?
8204 SS_VECTOR : GP_VECTOR,
8205 0);
8206 return 1;
8207 }
8208
Bandan Das19677e32014-05-06 02:19:15 -04008209 return 0;
8210}
8211
Radim Krčmářcbf71272017-05-19 15:48:51 +02008212static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008213{
8214 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008215 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008216
8217 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008218 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008219 return 1;
8220
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008221 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008222 kvm_inject_page_fault(vcpu, &e);
8223 return 1;
8224 }
8225
Bandan Das3573e222014-05-06 02:19:16 -04008226 return 0;
8227}
8228
Liran Alonabfc52c2018-06-23 02:35:13 +03008229/*
8230 * Allocate a shadow VMCS and associate it with the currently loaded
8231 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8232 * VMCS is also VMCLEARed, so that it is ready for use.
8233 */
8234static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8235{
8236 struct vcpu_vmx *vmx = to_vmx(vcpu);
8237 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8238
8239 /*
8240 * We should allocate a shadow vmcs for vmcs01 only when L1
8241 * executes VMXON and free it when L1 executes VMXOFF.
8242 * As it is invalid to execute VMXON twice, we shouldn't reach
8243 * here when vmcs01 already have an allocated shadow vmcs.
8244 */
8245 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8246
8247 if (!loaded_vmcs->shadow_vmcs) {
8248 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8249 if (loaded_vmcs->shadow_vmcs)
8250 vmcs_clear(loaded_vmcs->shadow_vmcs);
8251 }
8252 return loaded_vmcs->shadow_vmcs;
8253}
8254
Jim Mattsone29acc52016-11-30 12:03:43 -08008255static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8256{
8257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008258 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008259
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008260 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8261 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008262 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008263
8264 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8265 if (!vmx->nested.cached_vmcs12)
8266 goto out_cached_vmcs12;
8267
Liran Alon61ada742018-06-23 02:35:08 +03008268 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8269 if (!vmx->nested.cached_shadow_vmcs12)
8270 goto out_cached_shadow_vmcs12;
8271
Liran Alonabfc52c2018-06-23 02:35:13 +03008272 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8273 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008274
Jim Mattsone29acc52016-11-30 12:03:43 -08008275 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8276 HRTIMER_MODE_REL_PINNED);
8277 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8278
Roman Kagan63aff652018-07-19 21:59:07 +03008279 vmx->nested.vpid02 = allocate_vpid();
8280
Jim Mattsone29acc52016-11-30 12:03:43 -08008281 vmx->nested.vmxon = true;
8282 return 0;
8283
8284out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008285 kfree(vmx->nested.cached_shadow_vmcs12);
8286
8287out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008288 kfree(vmx->nested.cached_vmcs12);
8289
8290out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008291 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008292
Jim Mattsonde3a0022017-11-27 17:22:25 -06008293out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008294 return -ENOMEM;
8295}
8296
Bandan Das3573e222014-05-06 02:19:16 -04008297/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008298 * Emulate the VMXON instruction.
8299 * Currently, we just remember that VMX is active, and do not save or even
8300 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8301 * do not currently need to store anything in that guest-allocated memory
8302 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8303 * argument is different from the VMXON pointer (which the spec says they do).
8304 */
8305static int handle_vmon(struct kvm_vcpu *vcpu)
8306{
Jim Mattsone29acc52016-11-30 12:03:43 -08008307 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008308 gpa_t vmptr;
8309 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008311 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8312 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008313
Jim Mattson70f3aac2017-04-26 08:53:46 -07008314 /*
8315 * The Intel VMX Instruction Reference lists a bunch of bits that are
8316 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8317 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8318 * Otherwise, we should fail with #UD. But most faulting conditions
8319 * have already been checked by hardware, prior to the VM-exit for
8320 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8321 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008322 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008323 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008324 kvm_queue_exception(vcpu, UD_VECTOR);
8325 return 1;
8326 }
8327
Felix Wilhelm727ba742018-06-11 09:43:44 +02008328 /* CPL=0 must be checked manually. */
8329 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008330 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008331 return 1;
8332 }
8333
Abel Gordon145c28d2013-04-18 14:36:55 +03008334 if (vmx->nested.vmxon) {
8335 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008336 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008337 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008338
Haozhong Zhang3b840802016-06-22 14:59:54 +08008339 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008340 != VMXON_NEEDED_FEATURES) {
8341 kvm_inject_gp(vcpu, 0);
8342 return 1;
8343 }
8344
Radim Krčmářcbf71272017-05-19 15:48:51 +02008345 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008346 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008347
8348 /*
8349 * SDM 3: 24.11.5
8350 * The first 4 bytes of VMXON region contain the supported
8351 * VMCS revision identifier
8352 *
8353 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8354 * which replaces physical address width with 32
8355 */
8356 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8357 nested_vmx_failInvalid(vcpu);
8358 return kvm_skip_emulated_instruction(vcpu);
8359 }
8360
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008361 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8362 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008363 nested_vmx_failInvalid(vcpu);
8364 return kvm_skip_emulated_instruction(vcpu);
8365 }
8366 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8367 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008368 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008369 nested_vmx_failInvalid(vcpu);
8370 return kvm_skip_emulated_instruction(vcpu);
8371 }
8372 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008373 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008374
8375 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008376 ret = enter_vmx_operation(vcpu);
8377 if (ret)
8378 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008379
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008380 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008381 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008382}
8383
8384/*
8385 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8386 * for running VMX instructions (except VMXON, whose prerequisites are
8387 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008388 * Note that many of these exceptions have priority over VM exits, so they
8389 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008390 */
8391static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8392{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008393 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008394 kvm_queue_exception(vcpu, UD_VECTOR);
8395 return 0;
8396 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008397
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008398 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008399 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008400 return 0;
8401 }
8402
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008403 return 1;
8404}
8405
David Matlack8ca44e82017-08-01 14:00:39 -07008406static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8407{
8408 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8409 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8410}
8411
Abel Gordone7953d72013-04-18 14:37:55 +03008412static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8413{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008414 if (vmx->nested.current_vmptr == -1ull)
8415 return;
8416
Abel Gordon012f83c2013-04-18 14:39:25 +03008417 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008418 /* copy to memory all shadowed fields in case
8419 they were modified */
8420 copy_shadow_to_vmcs12(vmx);
8421 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008422 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008423 }
Wincy Van705699a2015-02-03 23:58:17 +08008424 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008425
8426 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008427 kvm_vcpu_write_guest_page(&vmx->vcpu,
8428 vmx->nested.current_vmptr >> PAGE_SHIFT,
8429 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008430
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008431 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008432}
8433
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008434/*
8435 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8436 * just stops using VMX.
8437 */
8438static void free_nested(struct vcpu_vmx *vmx)
8439{
Wanpeng Lib7455822017-11-22 14:04:00 -08008440 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008441 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008442
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008443 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008444 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008445 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008446 vmx->nested.posted_intr_nv = -1;
8447 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008448 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008449 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008450 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8451 free_vmcs(vmx->vmcs01.shadow_vmcs);
8452 vmx->vmcs01.shadow_vmcs = NULL;
8453 }
David Matlack4f2777b2016-07-13 17:16:37 -07008454 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008455 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008456 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008457 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008458 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008459 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008460 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008461 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008462 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008463 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008464 }
Wincy Van705699a2015-02-03 23:58:17 +08008465 if (vmx->nested.pi_desc_page) {
8466 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008467 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008468 vmx->nested.pi_desc_page = NULL;
8469 vmx->nested.pi_desc = NULL;
8470 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008471
Jim Mattsonde3a0022017-11-27 17:22:25 -06008472 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008473}
8474
8475/* Emulate the VMXOFF instruction */
8476static int handle_vmoff(struct kvm_vcpu *vcpu)
8477{
8478 if (!nested_vmx_check_permission(vcpu))
8479 return 1;
8480 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008481 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008482 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008483}
8484
Nadav Har'El27d6c862011-05-25 23:06:59 +03008485/* Emulate the VMCLEAR instruction */
8486static int handle_vmclear(struct kvm_vcpu *vcpu)
8487{
8488 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008489 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008490 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008491
8492 if (!nested_vmx_check_permission(vcpu))
8493 return 1;
8494
Radim Krčmářcbf71272017-05-19 15:48:51 +02008495 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008496 return 1;
8497
Radim Krčmářcbf71272017-05-19 15:48:51 +02008498 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8499 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8500 return kvm_skip_emulated_instruction(vcpu);
8501 }
8502
8503 if (vmptr == vmx->nested.vmxon_ptr) {
8504 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8505 return kvm_skip_emulated_instruction(vcpu);
8506 }
8507
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008508 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008509 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008510
Jim Mattson587d7e722017-03-02 12:41:48 -08008511 kvm_vcpu_write_guest(vcpu,
8512 vmptr + offsetof(struct vmcs12, launch_state),
8513 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008514
Nadav Har'El27d6c862011-05-25 23:06:59 +03008515 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008516 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008517}
8518
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008519static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8520
8521/* Emulate the VMLAUNCH instruction */
8522static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8523{
8524 return nested_vmx_run(vcpu, true);
8525}
8526
8527/* Emulate the VMRESUME instruction */
8528static int handle_vmresume(struct kvm_vcpu *vcpu)
8529{
8530
8531 return nested_vmx_run(vcpu, false);
8532}
8533
Nadav Har'El49f705c2011-05-25 23:08:30 +03008534/*
8535 * Read a vmcs12 field. Since these can have varying lengths and we return
8536 * one type, we chose the biggest type (u64) and zero-extend the return value
8537 * to that size. Note that the caller, handle_vmread, might need to use only
8538 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8539 * 64-bit fields are to be returned).
8540 */
Liran Alone2536742018-06-23 02:35:02 +03008541static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008542 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543{
8544 short offset = vmcs_field_to_offset(field);
8545 char *p;
8546
8547 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008548 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008549
Liran Alone2536742018-06-23 02:35:02 +03008550 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551
Jim Mattsond37f4262017-12-22 12:12:16 -08008552 switch (vmcs_field_width(field)) {
8553 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008554 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008555 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008556 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008557 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008558 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008559 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008560 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008561 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008562 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008563 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008564 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008566 WARN_ON(1);
8567 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008568 }
8569}
8570
Abel Gordon20b97fe2013-04-18 14:36:25 +03008571
Liran Alone2536742018-06-23 02:35:02 +03008572static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008573 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008574 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008575 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008576 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578
Jim Mattsond37f4262017-12-22 12:12:16 -08008579 switch (vmcs_field_width(field)) {
8580 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008581 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008582 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008583 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008585 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008586 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008587 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008588 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008589 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008590 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008591 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008592 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 WARN_ON(1);
8594 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008595 }
8596
8597}
8598
Jim Mattsonf4160e42018-05-29 09:11:33 -07008599/*
8600 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8601 * they have been modified by the L1 guest. Note that the "read-only"
8602 * VM-exit information fields are actually writable if the vCPU is
8603 * configured to support "VMWRITE to any supported field in the VMCS."
8604 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008605static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8606{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008607 const u16 *fields[] = {
8608 shadow_read_write_fields,
8609 shadow_read_only_fields
8610 };
8611 const int max_fields[] = {
8612 max_shadow_read_write_fields,
8613 max_shadow_read_only_fields
8614 };
8615 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008616 unsigned long field;
8617 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008618 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008619
Jan Kiszka282da872014-10-08 18:05:39 +02008620 preempt_disable();
8621
Abel Gordon16f5b902013-04-18 14:38:25 +03008622 vmcs_load(shadow_vmcs);
8623
Jim Mattsonf4160e42018-05-29 09:11:33 -07008624 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8625 for (i = 0; i < max_fields[q]; i++) {
8626 field = fields[q][i];
8627 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008628 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008629 }
8630 /*
8631 * Skip the VM-exit information fields if they are read-only.
8632 */
8633 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8634 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008635 }
8636
8637 vmcs_clear(shadow_vmcs);
8638 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008639
8640 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008641}
8642
Abel Gordonc3114422013-04-18 14:38:55 +03008643static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8644{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008645 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008646 shadow_read_write_fields,
8647 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008648 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008649 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008650 max_shadow_read_write_fields,
8651 max_shadow_read_only_fields
8652 };
8653 int i, q;
8654 unsigned long field;
8655 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008656 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008657
8658 vmcs_load(shadow_vmcs);
8659
Mathias Krausec2bae892013-06-26 20:36:21 +02008660 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008661 for (i = 0; i < max_fields[q]; i++) {
8662 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008663 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008664 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008665 }
8666 }
8667
8668 vmcs_clear(shadow_vmcs);
8669 vmcs_load(vmx->loaded_vmcs->vmcs);
8670}
8671
Nadav Har'El49f705c2011-05-25 23:08:30 +03008672/*
8673 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8674 * used before) all generate the same failure when it is missing.
8675 */
8676static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8677{
8678 struct vcpu_vmx *vmx = to_vmx(vcpu);
8679 if (vmx->nested.current_vmptr == -1ull) {
8680 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008681 return 0;
8682 }
8683 return 1;
8684}
8685
8686static int handle_vmread(struct kvm_vcpu *vcpu)
8687{
8688 unsigned long field;
8689 u64 field_value;
8690 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8691 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8692 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008693 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008694
Kyle Hueyeb277562016-11-29 12:40:39 -08008695 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008696 return 1;
8697
Kyle Huey6affcbe2016-11-29 12:40:40 -08008698 if (!nested_vmx_check_vmcs12(vcpu))
8699 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008700
Liran Alon6d894f42018-06-23 02:35:09 +03008701 if (!is_guest_mode(vcpu))
8702 vmcs12 = get_vmcs12(vcpu);
8703 else {
8704 /*
8705 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8706 * to shadowed-field sets the ALU flags for VMfailInvalid.
8707 */
8708 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8709 nested_vmx_failInvalid(vcpu);
8710 return kvm_skip_emulated_instruction(vcpu);
8711 }
8712 vmcs12 = get_shadow_vmcs12(vcpu);
8713 }
8714
Nadav Har'El49f705c2011-05-25 23:08:30 +03008715 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008716 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008717 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008718 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008720 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008721 }
8722 /*
8723 * Now copy part of this value to register or memory, as requested.
8724 * Note that the number of bits actually copied is 32 or 64 depending
8725 * on the guest's mode (32 or 64 bit), not on the given field's length.
8726 */
8727 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008728 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008729 field_value);
8730 } else {
8731 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008732 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008733 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008734 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008735 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8736 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737 }
8738
8739 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008740 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008741}
8742
8743
8744static int handle_vmwrite(struct kvm_vcpu *vcpu)
8745{
8746 unsigned long field;
8747 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8750 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008751
Nadav Har'El49f705c2011-05-25 23:08:30 +03008752 /* The value to write might be 32 or 64 bits, depending on L1's long
8753 * mode, and eventually we need to write that into a field of several
8754 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008755 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 * bits into the vmcs12 field.
8757 */
8758 u64 field_value = 0;
8759 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008760 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008761
Kyle Hueyeb277562016-11-29 12:40:39 -08008762 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763 return 1;
8764
Kyle Huey6affcbe2016-11-29 12:40:40 -08008765 if (!nested_vmx_check_vmcs12(vcpu))
8766 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008767
Nadav Har'El49f705c2011-05-25 23:08:30 +03008768 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008769 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 (((vmx_instruction_info) >> 3) & 0xf));
8771 else {
8772 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008773 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008774 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008775 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8776 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008777 kvm_inject_page_fault(vcpu, &e);
8778 return 1;
8779 }
8780 }
8781
8782
Nadav Amit27e6fb52014-06-18 17:19:26 +03008783 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008784 /*
8785 * If the vCPU supports "VMWRITE to any supported field in the
8786 * VMCS," then the "read-only" fields are actually read/write.
8787 */
8788 if (vmcs_field_readonly(field) &&
8789 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008790 nested_vmx_failValid(vcpu,
8791 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008792 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008793 }
8794
Liran Alon6d894f42018-06-23 02:35:09 +03008795 if (!is_guest_mode(vcpu))
8796 vmcs12 = get_vmcs12(vcpu);
8797 else {
8798 /*
8799 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8800 * to shadowed-field sets the ALU flags for VMfailInvalid.
8801 */
8802 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8803 nested_vmx_failInvalid(vcpu);
8804 return kvm_skip_emulated_instruction(vcpu);
8805 }
8806 vmcs12 = get_shadow_vmcs12(vcpu);
8807
8808 }
8809
8810 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008811 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008812 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008813 }
8814
Liran Alon6d894f42018-06-23 02:35:09 +03008815 /*
8816 * Do not track vmcs12 dirty-state if in guest-mode
8817 * as we actually dirty shadow vmcs12 instead of vmcs12.
8818 */
8819 if (!is_guest_mode(vcpu)) {
8820 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008821#define SHADOW_FIELD_RW(x) case x:
8822#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008823 /*
8824 * The fields that can be updated by L1 without a vmexit are
8825 * always updated in the vmcs02, the others go down the slow
8826 * path of prepare_vmcs02.
8827 */
8828 break;
8829 default:
8830 vmx->nested.dirty_vmcs12 = true;
8831 break;
8832 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008833 }
8834
Nadav Har'El49f705c2011-05-25 23:08:30 +03008835 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008836 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008837}
8838
Jim Mattsona8bc2842016-11-30 12:03:44 -08008839static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8840{
8841 vmx->nested.current_vmptr = vmptr;
8842 if (enable_shadow_vmcs) {
8843 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8844 SECONDARY_EXEC_SHADOW_VMCS);
8845 vmcs_write64(VMCS_LINK_POINTER,
8846 __pa(vmx->vmcs01.shadow_vmcs));
8847 vmx->nested.sync_shadow_vmcs = true;
8848 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008849 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008850}
8851
Nadav Har'El63846662011-05-25 23:07:29 +03008852/* Emulate the VMPTRLD instruction */
8853static int handle_vmptrld(struct kvm_vcpu *vcpu)
8854{
8855 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008856 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008857
8858 if (!nested_vmx_check_permission(vcpu))
8859 return 1;
8860
Radim Krčmářcbf71272017-05-19 15:48:51 +02008861 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008862 return 1;
8863
Radim Krčmářcbf71272017-05-19 15:48:51 +02008864 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8865 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8866 return kvm_skip_emulated_instruction(vcpu);
8867 }
8868
8869 if (vmptr == vmx->nested.vmxon_ptr) {
8870 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8871 return kvm_skip_emulated_instruction(vcpu);
8872 }
8873
Nadav Har'El63846662011-05-25 23:07:29 +03008874 if (vmx->nested.current_vmptr != vmptr) {
8875 struct vmcs12 *new_vmcs12;
8876 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008877 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8878 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008879 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008880 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008881 }
8882 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008883 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008884 (new_vmcs12->hdr.shadow_vmcs &&
8885 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008886 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008887 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008888 nested_vmx_failValid(vcpu,
8889 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008890 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008891 }
Nadav Har'El63846662011-05-25 23:07:29 +03008892
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008893 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008894 /*
8895 * Load VMCS12 from guest memory since it is not already
8896 * cached.
8897 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008898 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8899 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008900 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008901
Jim Mattsona8bc2842016-11-30 12:03:44 -08008902 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008903 }
8904
8905 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008906 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008907}
8908
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008909/* Emulate the VMPTRST instruction */
8910static int handle_vmptrst(struct kvm_vcpu *vcpu)
8911{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008912 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8913 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8914 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008915 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008916 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917
8918 if (!nested_vmx_check_permission(vcpu))
8919 return 1;
8920
Sean Christopherson0a06d422018-07-19 10:31:00 -07008921 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008922 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008923 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008924 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8925 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008926 kvm_inject_page_fault(vcpu, &e);
8927 return 1;
8928 }
8929 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008930 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008931}
8932
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008933/* Emulate the INVEPT instruction */
8934static int handle_invept(struct kvm_vcpu *vcpu)
8935{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008937 u32 vmx_instruction_info, types;
8938 unsigned long type;
8939 gva_t gva;
8940 struct x86_exception e;
8941 struct {
8942 u64 eptp, gpa;
8943 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008944
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008945 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008946 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008947 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008948 kvm_queue_exception(vcpu, UD_VECTOR);
8949 return 1;
8950 }
8951
8952 if (!nested_vmx_check_permission(vcpu))
8953 return 1;
8954
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008955 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008956 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008958 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959
Jim Mattson85c856b2016-10-26 08:38:38 -07008960 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961 nested_vmx_failValid(vcpu,
8962 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008963 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964 }
8965
8966 /* According to the Intel VMX instruction reference, the memory
8967 * operand is read even if it isn't needed (e.g., for type==global)
8968 */
8969 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008970 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008971 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008972 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008973 kvm_inject_page_fault(vcpu, &e);
8974 return 1;
8975 }
8976
8977 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008978 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008979 /*
8980 * TODO: track mappings and invalidate
8981 * single context requests appropriately
8982 */
8983 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008984 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008985 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 nested_vmx_succeed(vcpu);
8987 break;
8988 default:
8989 BUG_ON(1);
8990 break;
8991 }
8992
Kyle Huey6affcbe2016-11-29 12:40:40 -08008993 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008994}
8995
Liran Alon3d5bdae2018-10-08 23:42:18 +03008996static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8997{
8998 struct vcpu_vmx *vmx = to_vmx(vcpu);
8999
9000 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9001}
9002
Petr Matouseka642fc32014-09-23 20:22:30 +02009003static int handle_invvpid(struct kvm_vcpu *vcpu)
9004{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009005 struct vcpu_vmx *vmx = to_vmx(vcpu);
9006 u32 vmx_instruction_info;
9007 unsigned long type, types;
9008 gva_t gva;
9009 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009010 struct {
9011 u64 vpid;
9012 u64 gla;
9013 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009014 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009015
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009016 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009017 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009018 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009019 kvm_queue_exception(vcpu, UD_VECTOR);
9020 return 1;
9021 }
9022
9023 if (!nested_vmx_check_permission(vcpu))
9024 return 1;
9025
9026 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9027 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9028
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009029 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009030 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009031
Jim Mattson85c856b2016-10-26 08:38:38 -07009032 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009033 nested_vmx_failValid(vcpu,
9034 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009035 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009036 }
9037
9038 /* according to the intel vmx instruction reference, the memory
9039 * operand is read even if it isn't needed (e.g., for type==global)
9040 */
9041 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9042 vmx_instruction_info, false, &gva))
9043 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009044 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009045 kvm_inject_page_fault(vcpu, &e);
9046 return 1;
9047 }
Jim Mattson40352602017-06-28 09:37:37 -07009048 if (operand.vpid >> 16) {
9049 nested_vmx_failValid(vcpu,
9050 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9051 return kvm_skip_emulated_instruction(vcpu);
9052 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009053
Liran Alon3d5bdae2018-10-08 23:42:18 +03009054 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009055 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009056 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009057 if (!operand.vpid ||
9058 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009059 nested_vmx_failValid(vcpu,
9060 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9061 return kvm_skip_emulated_instruction(vcpu);
9062 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009063 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009064 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009065 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009066 } else
Liran Alon327c0722018-10-08 23:42:19 +03009067 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009068 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009069 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009070 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009071 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009072 nested_vmx_failValid(vcpu,
9073 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009074 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009075 }
Liran Alon327c0722018-10-08 23:42:19 +03009076 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009077 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009078 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009079 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009080 break;
9081 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009082 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009083 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009084 }
9085
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009086 nested_vmx_succeed(vcpu);
9087
Kyle Huey6affcbe2016-11-29 12:40:40 -08009088 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009089}
9090
Junaid Shahideb4b2482018-06-27 14:59:14 -07009091static int handle_invpcid(struct kvm_vcpu *vcpu)
9092{
9093 u32 vmx_instruction_info;
9094 unsigned long type;
9095 bool pcid_enabled;
9096 gva_t gva;
9097 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009098 unsigned i;
9099 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009100 struct {
9101 u64 pcid;
9102 u64 gla;
9103 } operand;
9104
9105 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9106 kvm_queue_exception(vcpu, UD_VECTOR);
9107 return 1;
9108 }
9109
9110 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9111 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9112
9113 if (type > 3) {
9114 kvm_inject_gp(vcpu, 0);
9115 return 1;
9116 }
9117
9118 /* According to the Intel instruction reference, the memory operand
9119 * is read even if it isn't needed (e.g., for type==all)
9120 */
9121 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9122 vmx_instruction_info, false, &gva))
9123 return 1;
9124
9125 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9126 kvm_inject_page_fault(vcpu, &e);
9127 return 1;
9128 }
9129
9130 if (operand.pcid >> 12 != 0) {
9131 kvm_inject_gp(vcpu, 0);
9132 return 1;
9133 }
9134
9135 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9136
9137 switch (type) {
9138 case INVPCID_TYPE_INDIV_ADDR:
9139 if ((!pcid_enabled && (operand.pcid != 0)) ||
9140 is_noncanonical_address(operand.gla, vcpu)) {
9141 kvm_inject_gp(vcpu, 0);
9142 return 1;
9143 }
9144 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9145 return kvm_skip_emulated_instruction(vcpu);
9146
9147 case INVPCID_TYPE_SINGLE_CTXT:
9148 if (!pcid_enabled && (operand.pcid != 0)) {
9149 kvm_inject_gp(vcpu, 0);
9150 return 1;
9151 }
9152
9153 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9154 kvm_mmu_sync_roots(vcpu);
9155 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9156 }
9157
Junaid Shahidb94742c2018-06-27 14:59:20 -07009158 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9159 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9160 == operand.pcid)
9161 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009162
Junaid Shahidb94742c2018-06-27 14:59:20 -07009163 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009164 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009165 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009166 * given PCID, then nothing needs to be done here because a
9167 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009168 */
9169
9170 return kvm_skip_emulated_instruction(vcpu);
9171
9172 case INVPCID_TYPE_ALL_NON_GLOBAL:
9173 /*
9174 * Currently, KVM doesn't mark global entries in the shadow
9175 * page tables, so a non-global flush just degenerates to a
9176 * global flush. If needed, we could optimize this later by
9177 * keeping track of global entries in shadow page tables.
9178 */
9179
9180 /* fall-through */
9181 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9182 kvm_mmu_unload(vcpu);
9183 return kvm_skip_emulated_instruction(vcpu);
9184
9185 default:
9186 BUG(); /* We have already checked above that type <= 3 */
9187 }
9188}
9189
Kai Huang843e4332015-01-28 10:54:28 +08009190static int handle_pml_full(struct kvm_vcpu *vcpu)
9191{
9192 unsigned long exit_qualification;
9193
9194 trace_kvm_pml_full(vcpu->vcpu_id);
9195
9196 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9197
9198 /*
9199 * PML buffer FULL happened while executing iret from NMI,
9200 * "blocked by NMI" bit has to be set before next VM entry.
9201 */
9202 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009203 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009204 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9205 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9206 GUEST_INTR_STATE_NMI);
9207
9208 /*
9209 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9210 * here.., and there's no userspace involvement needed for PML.
9211 */
9212 return 1;
9213}
9214
Yunhong Jiang64672c92016-06-13 14:19:59 -07009215static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9216{
Sean Christophersond264ee02018-08-27 15:21:12 -07009217 if (!to_vmx(vcpu)->req_immediate_exit)
9218 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009219 return 1;
9220}
9221
Bandan Das41ab9372017-08-03 15:54:43 -04009222static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9223{
9224 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009225 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9226
9227 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009228 switch (address & VMX_EPTP_MT_MASK) {
9229 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009230 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009231 return false;
9232 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009233 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009234 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009235 return false;
9236 break;
9237 default:
9238 return false;
9239 }
9240
David Hildenbrandbb97a012017-08-10 23:15:28 +02009241 /* only 4 levels page-walk length are valid */
9242 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009243 return false;
9244
9245 /* Reserved bits should not be set */
9246 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9247 return false;
9248
9249 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009250 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009251 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009252 return false;
9253 }
9254
9255 return true;
9256}
9257
9258static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9259 struct vmcs12 *vmcs12)
9260{
9261 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9262 u64 address;
9263 bool accessed_dirty;
9264 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9265
9266 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9267 !nested_cpu_has_ept(vmcs12))
9268 return 1;
9269
9270 if (index >= VMFUNC_EPTP_ENTRIES)
9271 return 1;
9272
9273
9274 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9275 &address, index * 8, 8))
9276 return 1;
9277
David Hildenbrandbb97a012017-08-10 23:15:28 +02009278 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009279
9280 /*
9281 * If the (L2) guest does a vmfunc to the currently
9282 * active ept pointer, we don't have to do anything else
9283 */
9284 if (vmcs12->ept_pointer != address) {
9285 if (!valid_ept_address(vcpu, address))
9286 return 1;
9287
9288 kvm_mmu_unload(vcpu);
9289 mmu->ept_ad = accessed_dirty;
9290 mmu->base_role.ad_disabled = !accessed_dirty;
9291 vmcs12->ept_pointer = address;
9292 /*
9293 * TODO: Check what's the correct approach in case
9294 * mmu reload fails. Currently, we just let the next
9295 * reload potentially fail
9296 */
9297 kvm_mmu_reload(vcpu);
9298 }
9299
9300 return 0;
9301}
9302
Bandan Das2a499e42017-08-03 15:54:41 -04009303static int handle_vmfunc(struct kvm_vcpu *vcpu)
9304{
Bandan Das27c42a12017-08-03 15:54:42 -04009305 struct vcpu_vmx *vmx = to_vmx(vcpu);
9306 struct vmcs12 *vmcs12;
9307 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9308
9309 /*
9310 * VMFUNC is only supported for nested guests, but we always enable the
9311 * secondary control for simplicity; for non-nested mode, fake that we
9312 * didn't by injecting #UD.
9313 */
9314 if (!is_guest_mode(vcpu)) {
9315 kvm_queue_exception(vcpu, UD_VECTOR);
9316 return 1;
9317 }
9318
9319 vmcs12 = get_vmcs12(vcpu);
9320 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9321 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009322
9323 switch (function) {
9324 case 0:
9325 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9326 goto fail;
9327 break;
9328 default:
9329 goto fail;
9330 }
9331 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009332
9333fail:
9334 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9335 vmcs_read32(VM_EXIT_INTR_INFO),
9336 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009337 return 1;
9338}
9339
Sean Christopherson0b665d32018-08-14 09:33:34 -07009340static int handle_encls(struct kvm_vcpu *vcpu)
9341{
9342 /*
9343 * SGX virtualization is not yet supported. There is no software
9344 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9345 * to prevent the guest from executing ENCLS.
9346 */
9347 kvm_queue_exception(vcpu, UD_VECTOR);
9348 return 1;
9349}
9350
Nadav Har'El0140cae2011-05-25 23:06:28 +03009351/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009352 * The exit handlers return 1 if the exit was handled fully and guest execution
9353 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9354 * to be done to userspace and return 0.
9355 */
Mathias Krause772e0312012-08-30 01:30:19 +02009356static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009357 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9358 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009359 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009360 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009361 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009362 [EXIT_REASON_CR_ACCESS] = handle_cr,
9363 [EXIT_REASON_DR_ACCESS] = handle_dr,
9364 [EXIT_REASON_CPUID] = handle_cpuid,
9365 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9366 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9367 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9368 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009369 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009370 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009371 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009372 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009373 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009374 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009375 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009376 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009377 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009378 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009379 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009380 [EXIT_REASON_VMOFF] = handle_vmoff,
9381 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009382 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9383 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009384 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009385 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009386 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009387 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009388 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009389 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009390 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9391 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009392 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9393 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009394 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009395 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009396 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009397 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009398 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009399 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009400 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009401 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009402 [EXIT_REASON_XSAVES] = handle_xsaves,
9403 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009404 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009405 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009406 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009407 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009408 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009409};
9410
9411static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009412 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009413
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009414static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9415 struct vmcs12 *vmcs12)
9416{
9417 unsigned long exit_qualification;
9418 gpa_t bitmap, last_bitmap;
9419 unsigned int port;
9420 int size;
9421 u8 b;
9422
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009423 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009424 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009425
9426 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9427
9428 port = exit_qualification >> 16;
9429 size = (exit_qualification & 7) + 1;
9430
9431 last_bitmap = (gpa_t)-1;
9432 b = -1;
9433
9434 while (size > 0) {
9435 if (port < 0x8000)
9436 bitmap = vmcs12->io_bitmap_a;
9437 else if (port < 0x10000)
9438 bitmap = vmcs12->io_bitmap_b;
9439 else
Joe Perches1d804d02015-03-30 16:46:09 -07009440 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009441 bitmap += (port & 0x7fff) / 8;
9442
9443 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009444 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009445 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009446 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009447 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009448
9449 port++;
9450 size--;
9451 last_bitmap = bitmap;
9452 }
9453
Joe Perches1d804d02015-03-30 16:46:09 -07009454 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009455}
9456
Nadav Har'El644d7112011-05-25 23:12:35 +03009457/*
9458 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9459 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9460 * disinterest in the current event (read or write a specific MSR) by using an
9461 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9462 */
9463static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9464 struct vmcs12 *vmcs12, u32 exit_reason)
9465{
9466 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9467 gpa_t bitmap;
9468
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009469 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009470 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009471
9472 /*
9473 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9474 * for the four combinations of read/write and low/high MSR numbers.
9475 * First we need to figure out which of the four to use:
9476 */
9477 bitmap = vmcs12->msr_bitmap;
9478 if (exit_reason == EXIT_REASON_MSR_WRITE)
9479 bitmap += 2048;
9480 if (msr_index >= 0xc0000000) {
9481 msr_index -= 0xc0000000;
9482 bitmap += 1024;
9483 }
9484
9485 /* Then read the msr_index'th bit from this bitmap: */
9486 if (msr_index < 1024*8) {
9487 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009488 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009489 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009490 return 1 & (b >> (msr_index & 7));
9491 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009492 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009493}
9494
9495/*
9496 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9497 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9498 * intercept (via guest_host_mask etc.) the current event.
9499 */
9500static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9501 struct vmcs12 *vmcs12)
9502{
9503 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9504 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009505 int reg;
9506 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009507
9508 switch ((exit_qualification >> 4) & 3) {
9509 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009510 reg = (exit_qualification >> 8) & 15;
9511 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009512 switch (cr) {
9513 case 0:
9514 if (vmcs12->cr0_guest_host_mask &
9515 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009516 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009517 break;
9518 case 3:
9519 if ((vmcs12->cr3_target_count >= 1 &&
9520 vmcs12->cr3_target_value0 == val) ||
9521 (vmcs12->cr3_target_count >= 2 &&
9522 vmcs12->cr3_target_value1 == val) ||
9523 (vmcs12->cr3_target_count >= 3 &&
9524 vmcs12->cr3_target_value2 == val) ||
9525 (vmcs12->cr3_target_count >= 4 &&
9526 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009527 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009528 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009529 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009530 break;
9531 case 4:
9532 if (vmcs12->cr4_guest_host_mask &
9533 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009534 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009535 break;
9536 case 8:
9537 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009538 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009539 break;
9540 }
9541 break;
9542 case 2: /* clts */
9543 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9544 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009545 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009546 break;
9547 case 1: /* mov from cr */
9548 switch (cr) {
9549 case 3:
9550 if (vmcs12->cpu_based_vm_exec_control &
9551 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009552 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009553 break;
9554 case 8:
9555 if (vmcs12->cpu_based_vm_exec_control &
9556 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009557 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009558 break;
9559 }
9560 break;
9561 case 3: /* lmsw */
9562 /*
9563 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9564 * cr0. Other attempted changes are ignored, with no exit.
9565 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009566 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009567 if (vmcs12->cr0_guest_host_mask & 0xe &
9568 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009569 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009570 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9571 !(vmcs12->cr0_read_shadow & 0x1) &&
9572 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009573 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009574 break;
9575 }
Joe Perches1d804d02015-03-30 16:46:09 -07009576 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009577}
9578
Liran Alona7cde482018-06-23 02:35:10 +03009579static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9580 struct vmcs12 *vmcs12, gpa_t bitmap)
9581{
9582 u32 vmx_instruction_info;
9583 unsigned long field;
9584 u8 b;
9585
9586 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9587 return true;
9588
9589 /* Decode instruction info and find the field to access */
9590 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9591 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9592
9593 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9594 if (field >> 15)
9595 return true;
9596
9597 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9598 return true;
9599
9600 return 1 & (b >> (field & 7));
9601}
9602
Nadav Har'El644d7112011-05-25 23:12:35 +03009603/*
9604 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9605 * should handle it ourselves in L0 (and then continue L2). Only call this
9606 * when in is_guest_mode (L2).
9607 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009608static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009609{
Nadav Har'El644d7112011-05-25 23:12:35 +03009610 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9611 struct vcpu_vmx *vmx = to_vmx(vcpu);
9612 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9613
Jim Mattson4f350c62017-09-14 16:31:44 -07009614 if (vmx->nested.nested_run_pending)
9615 return false;
9616
9617 if (unlikely(vmx->fail)) {
9618 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9619 vmcs_read32(VM_INSTRUCTION_ERROR));
9620 return true;
9621 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009622
David Matlackc9f04402017-08-01 14:00:40 -07009623 /*
9624 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009625 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9626 * Page). The CPU may write to these pages via their host
9627 * physical address while L2 is running, bypassing any
9628 * address-translation-based dirty tracking (e.g. EPT write
9629 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009630 *
9631 * Mark them dirty on every exit from L2 to prevent them from
9632 * getting out of sync with dirty tracking.
9633 */
9634 nested_mark_vmcs12_pages_dirty(vcpu);
9635
Jim Mattson4f350c62017-09-14 16:31:44 -07009636 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9637 vmcs_readl(EXIT_QUALIFICATION),
9638 vmx->idt_vectoring_info,
9639 intr_info,
9640 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9641 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009642
9643 switch (exit_reason) {
9644 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009645 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009646 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009647 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009648 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009649 else if (is_debug(intr_info) &&
9650 vcpu->guest_debug &
9651 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9652 return false;
9653 else if (is_breakpoint(intr_info) &&
9654 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9655 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009656 return vmcs12->exception_bitmap &
9657 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9658 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009659 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009661 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009662 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009663 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009665 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009667 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009668 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_HLT:
9671 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9672 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009673 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009674 case EXIT_REASON_INVLPG:
9675 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9676 case EXIT_REASON_RDPMC:
9677 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009678 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009679 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009680 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009681 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009682 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009683 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009684 case EXIT_REASON_VMREAD:
9685 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9686 vmcs12->vmread_bitmap);
9687 case EXIT_REASON_VMWRITE:
9688 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9689 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009690 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9691 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009692 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009693 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009694 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009695 /*
9696 * VMX instructions trap unconditionally. This allows L1 to
9697 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9698 */
Joe Perches1d804d02015-03-30 16:46:09 -07009699 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009700 case EXIT_REASON_CR_ACCESS:
9701 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9702 case EXIT_REASON_DR_ACCESS:
9703 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9704 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009705 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009706 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9707 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009708 case EXIT_REASON_MSR_READ:
9709 case EXIT_REASON_MSR_WRITE:
9710 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9711 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009712 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009713 case EXIT_REASON_MWAIT_INSTRUCTION:
9714 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009715 case EXIT_REASON_MONITOR_TRAP_FLAG:
9716 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009717 case EXIT_REASON_MONITOR_INSTRUCTION:
9718 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9719 case EXIT_REASON_PAUSE_INSTRUCTION:
9720 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9721 nested_cpu_has2(vmcs12,
9722 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9723 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009724 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009725 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009726 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009727 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009728 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009729 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009730 /*
9731 * The controls for "virtualize APIC accesses," "APIC-
9732 * register virtualization," and "virtual-interrupt
9733 * delivery" only come from vmcs12.
9734 */
Joe Perches1d804d02015-03-30 16:46:09 -07009735 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009736 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009737 /*
9738 * L0 always deals with the EPT violation. If nested EPT is
9739 * used, and the nested mmu code discovers that the address is
9740 * missing in the guest EPT table (EPT12), the EPT violation
9741 * will be injected with nested_ept_inject_page_fault()
9742 */
Joe Perches1d804d02015-03-30 16:46:09 -07009743 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009744 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009745 /*
9746 * L2 never uses directly L1's EPT, but rather L0's own EPT
9747 * table (shadow on EPT) or a merged EPT table that L0 built
9748 * (EPT on EPT). So any problems with the structure of the
9749 * table is L0's fault.
9750 */
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009752 case EXIT_REASON_INVPCID:
9753 return
9754 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9755 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009756 case EXIT_REASON_WBINVD:
9757 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9758 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009759 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009760 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9761 /*
9762 * This should never happen, since it is not possible to
9763 * set XSS to a non-zero value---neither in L1 nor in L2.
9764 * If if it were, XSS would have to be checked against
9765 * the XSS exit bitmap in vmcs12.
9766 */
9767 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009768 case EXIT_REASON_PREEMPTION_TIMER:
9769 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009770 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009771 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009772 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009773 case EXIT_REASON_VMFUNC:
9774 /* VM functions are emulated through L2->L0 vmexits. */
9775 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009776 case EXIT_REASON_ENCLS:
9777 /* SGX is never exposed to L1 */
9778 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009779 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009780 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009781 }
9782}
9783
Paolo Bonzini7313c692017-07-27 10:31:25 +02009784static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9785{
9786 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9787
9788 /*
9789 * At this point, the exit interruption info in exit_intr_info
9790 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9791 * we need to query the in-kernel LAPIC.
9792 */
9793 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9794 if ((exit_intr_info &
9795 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9796 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9797 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9798 vmcs12->vm_exit_intr_error_code =
9799 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9800 }
9801
9802 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9803 vmcs_readl(EXIT_QUALIFICATION));
9804 return 1;
9805}
9806
Avi Kivity586f9602010-11-18 13:09:54 +02009807static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9808{
9809 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9810 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9811}
9812
Kai Huanga3eaa862015-11-04 13:46:05 +08009813static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009814{
Kai Huanga3eaa862015-11-04 13:46:05 +08009815 if (vmx->pml_pg) {
9816 __free_page(vmx->pml_pg);
9817 vmx->pml_pg = NULL;
9818 }
Kai Huang843e4332015-01-28 10:54:28 +08009819}
9820
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009821static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009822{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009823 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009824 u64 *pml_buf;
9825 u16 pml_idx;
9826
9827 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9828
9829 /* Do nothing if PML buffer is empty */
9830 if (pml_idx == (PML_ENTITY_NUM - 1))
9831 return;
9832
9833 /* PML index always points to next available PML buffer entity */
9834 if (pml_idx >= PML_ENTITY_NUM)
9835 pml_idx = 0;
9836 else
9837 pml_idx++;
9838
9839 pml_buf = page_address(vmx->pml_pg);
9840 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9841 u64 gpa;
9842
9843 gpa = pml_buf[pml_idx];
9844 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009845 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009846 }
9847
9848 /* reset PML index */
9849 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9850}
9851
9852/*
9853 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9854 * Called before reporting dirty_bitmap to userspace.
9855 */
9856static void kvm_flush_pml_buffers(struct kvm *kvm)
9857{
9858 int i;
9859 struct kvm_vcpu *vcpu;
9860 /*
9861 * We only need to kick vcpu out of guest mode here, as PML buffer
9862 * is flushed at beginning of all VMEXITs, and it's obvious that only
9863 * vcpus running in guest are possible to have unflushed GPAs in PML
9864 * buffer.
9865 */
9866 kvm_for_each_vcpu(i, vcpu, kvm)
9867 kvm_vcpu_kick(vcpu);
9868}
9869
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009870static void vmx_dump_sel(char *name, uint32_t sel)
9871{
9872 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009873 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009874 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9875 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9876 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9877}
9878
9879static void vmx_dump_dtsel(char *name, uint32_t limit)
9880{
9881 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9882 name, vmcs_read32(limit),
9883 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9884}
9885
9886static void dump_vmcs(void)
9887{
9888 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9889 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9890 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9891 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9892 u32 secondary_exec_control = 0;
9893 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009894 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009895 int i, n;
9896
9897 if (cpu_has_secondary_exec_ctrls())
9898 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9899
9900 pr_err("*** Guest State ***\n");
9901 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9902 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9903 vmcs_readl(CR0_GUEST_HOST_MASK));
9904 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9905 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9906 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9907 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9908 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9909 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009910 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9911 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9912 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9913 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009914 }
9915 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9916 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9917 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9918 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9919 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9920 vmcs_readl(GUEST_SYSENTER_ESP),
9921 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9922 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9923 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9924 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9925 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9926 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9927 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9928 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9929 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9930 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9931 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9932 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9933 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009934 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9935 efer, vmcs_read64(GUEST_IA32_PAT));
9936 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9937 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009938 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009939 if (cpu_has_load_perf_global_ctrl &&
9940 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009941 pr_err("PerfGlobCtl = 0x%016llx\n",
9942 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009943 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009944 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009945 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9946 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9947 vmcs_read32(GUEST_ACTIVITY_STATE));
9948 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9949 pr_err("InterruptStatus = %04x\n",
9950 vmcs_read16(GUEST_INTR_STATUS));
9951
9952 pr_err("*** Host State ***\n");
9953 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9954 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9955 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9956 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9957 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9958 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9959 vmcs_read16(HOST_TR_SELECTOR));
9960 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9961 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9962 vmcs_readl(HOST_TR_BASE));
9963 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9964 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9965 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9966 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9967 vmcs_readl(HOST_CR4));
9968 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9969 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9970 vmcs_read32(HOST_IA32_SYSENTER_CS),
9971 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9972 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009973 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9974 vmcs_read64(HOST_IA32_EFER),
9975 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009976 if (cpu_has_load_perf_global_ctrl &&
9977 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009978 pr_err("PerfGlobCtl = 0x%016llx\n",
9979 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009980
9981 pr_err("*** Control State ***\n");
9982 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9983 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9984 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9985 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9986 vmcs_read32(EXCEPTION_BITMAP),
9987 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9988 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9989 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9990 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9991 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9992 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9993 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9994 vmcs_read32(VM_EXIT_INTR_INFO),
9995 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9996 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9997 pr_err(" reason=%08x qualification=%016lx\n",
9998 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9999 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10000 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10001 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010002 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010003 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010004 pr_err("TSC Multiplier = 0x%016llx\n",
10005 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010006 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10007 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10008 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10009 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10010 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010011 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010012 n = vmcs_read32(CR3_TARGET_COUNT);
10013 for (i = 0; i + 1 < n; i += 4)
10014 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10015 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10016 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10017 if (i < n)
10018 pr_err("CR3 target%u=%016lx\n",
10019 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10020 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10021 pr_err("PLE Gap=%08x Window=%08x\n",
10022 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10023 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10024 pr_err("Virtual processor ID = 0x%04x\n",
10025 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10026}
10027
Avi Kivity6aa8b732006-12-10 02:21:36 -080010028/*
10029 * The guest has exited. See if we can fix it or if we need userspace
10030 * assistance.
10031 */
Avi Kivity851ba692009-08-24 11:10:17 +030010032static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010033{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010034 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010035 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010036 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010037
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010038 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10039
Kai Huang843e4332015-01-28 10:54:28 +080010040 /*
10041 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10042 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10043 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10044 * mode as if vcpus is in root mode, the PML buffer must has been
10045 * flushed already.
10046 */
10047 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010048 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010049
Mohammed Gamal80ced182009-09-01 12:48:18 +020010050 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010051 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010052 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010053
Paolo Bonzini7313c692017-07-27 10:31:25 +020010054 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10055 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010056
Mohammed Gamal51207022010-05-31 22:40:54 +030010057 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010058 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010059 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10060 vcpu->run->fail_entry.hardware_entry_failure_reason
10061 = exit_reason;
10062 return 0;
10063 }
10064
Avi Kivity29bd8a72007-09-10 17:27:03 +030010065 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010066 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10067 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010068 = vmcs_read32(VM_INSTRUCTION_ERROR);
10069 return 0;
10070 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010071
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010072 /*
10073 * Note:
10074 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10075 * delivery event since it indicates guest is accessing MMIO.
10076 * The vm-exit can be triggered again after return to guest that
10077 * will cause infinite loop.
10078 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010079 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010080 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010081 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010082 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010083 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10084 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10085 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010086 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010087 vcpu->run->internal.data[0] = vectoring_info;
10088 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010089 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10090 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10091 vcpu->run->internal.ndata++;
10092 vcpu->run->internal.data[3] =
10093 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10094 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010095 return 0;
10096 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010097
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010098 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010099 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10100 if (vmx_interrupt_allowed(vcpu)) {
10101 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10102 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10103 vcpu->arch.nmi_pending) {
10104 /*
10105 * This CPU don't support us in finding the end of an
10106 * NMI-blocked window if the guest runs with IRQs
10107 * disabled. So we pull the trigger after 1 s of
10108 * futile waiting, but inform the user about this.
10109 */
10110 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10111 "state on VCPU %d after 1 s timeout\n",
10112 __func__, vcpu->vcpu_id);
10113 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10114 }
10115 }
10116
Avi Kivity6aa8b732006-12-10 02:21:36 -080010117 if (exit_reason < kvm_vmx_max_exit_handlers
10118 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010119 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010120 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010121 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10122 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010123 kvm_queue_exception(vcpu, UD_VECTOR);
10124 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010125 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010126}
10127
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010128/*
10129 * Software based L1D cache flush which is used when microcode providing
10130 * the cache control MSR is not loaded.
10131 *
10132 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10133 * flush it is required to read in 64 KiB because the replacement algorithm
10134 * is not exactly LRU. This could be sized at runtime via topology
10135 * information but as all relevant affected CPUs have 32KiB L1D cache size
10136 * there is no point in doing so.
10137 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010138static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010139{
10140 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010141
10142 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010143 * This code is only executed when the the flush mode is 'cond' or
10144 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010145 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010146 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010147 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010148
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010149 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010150 * Clear the per-vcpu flush bit, it gets set again
10151 * either from vcpu_run() or from one of the unsafe
10152 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010153 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010154 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010155 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010156
10157 /*
10158 * Clear the per-cpu flush bit, it gets set again from
10159 * the interrupt handlers.
10160 */
10161 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10162 kvm_clear_cpu_l1tf_flush_l1d();
10163
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010164 if (!flush_l1d)
10165 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010166 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010167
10168 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010169
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010170 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10171 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10172 return;
10173 }
10174
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010175 asm volatile(
10176 /* First ensure the pages are in the TLB */
10177 "xorl %%eax, %%eax\n"
10178 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010179 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010180 "addl $4096, %%eax\n\t"
10181 "cmpl %%eax, %[size]\n\t"
10182 "jne .Lpopulate_tlb\n\t"
10183 "xorl %%eax, %%eax\n\t"
10184 "cpuid\n\t"
10185 /* Now fill the cache */
10186 "xorl %%eax, %%eax\n"
10187 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010188 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010189 "addl $64, %%eax\n\t"
10190 "cmpl %%eax, %[size]\n\t"
10191 "jne .Lfill_cache\n\t"
10192 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010193 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010194 [size] "r" (size)
10195 : "eax", "ebx", "ecx", "edx");
10196}
10197
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010198static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010199{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010200 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10201
10202 if (is_guest_mode(vcpu) &&
10203 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10204 return;
10205
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010206 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010207 vmcs_write32(TPR_THRESHOLD, 0);
10208 return;
10209 }
10210
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010211 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010212}
10213
Jim Mattson8d860bb2018-05-09 16:56:05 -040010214static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010215{
10216 u32 sec_exec_control;
10217
Jim Mattson8d860bb2018-05-09 16:56:05 -040010218 if (!lapic_in_kernel(vcpu))
10219 return;
10220
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010221 if (!flexpriority_enabled &&
10222 !cpu_has_vmx_virtualize_x2apic_mode())
10223 return;
10224
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010225 /* Postpone execution until vmcs01 is the current VMCS. */
10226 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010227 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010228 return;
10229 }
10230
Yang Zhang8d146952013-01-25 10:18:50 +080010231 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010232 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10233 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010234
Jim Mattson8d860bb2018-05-09 16:56:05 -040010235 switch (kvm_get_apic_mode(vcpu)) {
10236 case LAPIC_MODE_INVALID:
10237 WARN_ONCE(true, "Invalid local APIC state");
10238 case LAPIC_MODE_DISABLED:
10239 break;
10240 case LAPIC_MODE_XAPIC:
10241 if (flexpriority_enabled) {
10242 sec_exec_control |=
10243 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10244 vmx_flush_tlb(vcpu, true);
10245 }
10246 break;
10247 case LAPIC_MODE_X2APIC:
10248 if (cpu_has_vmx_virtualize_x2apic_mode())
10249 sec_exec_control |=
10250 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10251 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010252 }
10253 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10254
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010255 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010256}
10257
Tang Chen38b99172014-09-24 15:57:54 +080010258static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10259{
Jim Mattsonab5df312018-05-09 17:02:03 -040010260 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010261 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010262 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010263 }
Tang Chen38b99172014-09-24 15:57:54 +080010264}
10265
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010266static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010267{
10268 u16 status;
10269 u8 old;
10270
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010271 if (max_isr == -1)
10272 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010273
10274 status = vmcs_read16(GUEST_INTR_STATUS);
10275 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010276 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010277 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010278 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010279 vmcs_write16(GUEST_INTR_STATUS, status);
10280 }
10281}
10282
10283static void vmx_set_rvi(int vector)
10284{
10285 u16 status;
10286 u8 old;
10287
Wei Wang4114c272014-11-05 10:53:43 +080010288 if (vector == -1)
10289 vector = 0;
10290
Yang Zhangc7c9c562013-01-25 10:18:51 +080010291 status = vmcs_read16(GUEST_INTR_STATUS);
10292 old = (u8)status & 0xff;
10293 if ((u8)vector != old) {
10294 status &= ~0xff;
10295 status |= (u8)vector;
10296 vmcs_write16(GUEST_INTR_STATUS, status);
10297 }
10298}
10299
10300static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10301{
Liran Alon851c1a182017-12-24 18:12:56 +020010302 /*
10303 * When running L2, updating RVI is only relevant when
10304 * vmcs12 virtual-interrupt-delivery enabled.
10305 * However, it can be enabled only when L1 also
10306 * intercepts external-interrupts and in that case
10307 * we should not update vmcs02 RVI but instead intercept
10308 * interrupt. Therefore, do nothing when running L2.
10309 */
10310 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010311 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010312}
10313
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010314static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010315{
10316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010317 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010318 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010319
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010320 WARN_ON(!vcpu->arch.apicv_active);
10321 if (pi_test_on(&vmx->pi_desc)) {
10322 pi_clear_on(&vmx->pi_desc);
10323 /*
10324 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10325 * But on x86 this is just a compiler barrier anyway.
10326 */
10327 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010328 max_irr_updated =
10329 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10330
10331 /*
10332 * If we are running L2 and L1 has a new pending interrupt
10333 * which can be injected, we should re-evaluate
10334 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010335 * If L1 intercepts external-interrupts, we should
10336 * exit from L2 to L1. Otherwise, interrupt should be
10337 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010338 */
Liran Alon851c1a182017-12-24 18:12:56 +020010339 if (is_guest_mode(vcpu) && max_irr_updated) {
10340 if (nested_exit_on_intr(vcpu))
10341 kvm_vcpu_exiting_guest_mode(vcpu);
10342 else
10343 kvm_make_request(KVM_REQ_EVENT, vcpu);
10344 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010345 } else {
10346 max_irr = kvm_lapic_find_highest_irr(vcpu);
10347 }
10348 vmx_hwapic_irr_update(vcpu, max_irr);
10349 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010350}
10351
Paolo Bonzini7e712682018-10-03 13:44:26 +020010352static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10353{
10354 u8 rvi = vmx_get_rvi();
10355 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10356
10357 return ((rvi & 0xf0) > (vppr & 0xf0));
10358}
10359
Andrey Smetanin63086302015-11-10 15:36:32 +030010360static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010361{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010362 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010363 return;
10364
Yang Zhangc7c9c562013-01-25 10:18:51 +080010365 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10366 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10367 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10368 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10369}
10370
Paolo Bonzini967235d2016-12-19 14:03:45 +010010371static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10372{
10373 struct vcpu_vmx *vmx = to_vmx(vcpu);
10374
10375 pi_clear_on(&vmx->pi_desc);
10376 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10377}
10378
Avi Kivity51aa01d2010-07-20 14:31:20 +030010379static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010380{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010381 u32 exit_intr_info = 0;
10382 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010383
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010384 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10385 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010386 return;
10387
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010388 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10389 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10390 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010391
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010392 /* if exit due to PF check for async PF */
10393 if (is_page_fault(exit_intr_info))
10394 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10395
Andi Kleena0861c02009-06-08 17:37:09 +080010396 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010397 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10398 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010399 kvm_machine_check();
10400
Gleb Natapov20f65982009-05-11 13:35:55 +030010401 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010402 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010403 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010404 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010405 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010406 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010407}
Gleb Natapov20f65982009-05-11 13:35:55 +030010408
Yang Zhanga547c6d2013-04-11 19:25:10 +080010409static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10410{
10411 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10412
Yang Zhanga547c6d2013-04-11 19:25:10 +080010413 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10414 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10415 unsigned int vector;
10416 unsigned long entry;
10417 gate_desc *desc;
10418 struct vcpu_vmx *vmx = to_vmx(vcpu);
10419#ifdef CONFIG_X86_64
10420 unsigned long tmp;
10421#endif
10422
10423 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10424 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010425 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010426 asm volatile(
10427#ifdef CONFIG_X86_64
10428 "mov %%" _ASM_SP ", %[sp]\n\t"
10429 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10430 "push $%c[ss]\n\t"
10431 "push %[sp]\n\t"
10432#endif
10433 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010434 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010435 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010436 :
10437#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010438 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010439#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010440 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010441 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010442 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010443 [ss]"i"(__KERNEL_DS),
10444 [cs]"i"(__KERNEL_CS)
10445 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010446 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010447}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010448STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010449
Tom Lendackybc226f02018-05-10 22:06:39 +020010450static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010451{
Tom Lendackybc226f02018-05-10 22:06:39 +020010452 switch (index) {
10453 case MSR_IA32_SMBASE:
10454 /*
10455 * We cannot do SMM unless we can run the guest in big
10456 * real mode.
10457 */
10458 return enable_unrestricted_guest || emulate_invalid_guest_state;
10459 case MSR_AMD64_VIRT_SPEC_CTRL:
10460 /* This is AMD only. */
10461 return false;
10462 default:
10463 return true;
10464 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010465}
10466
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010467static bool vmx_mpx_supported(void)
10468{
10469 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10470 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10471}
10472
Wanpeng Li55412b22014-12-02 19:21:30 +080010473static bool vmx_xsaves_supported(void)
10474{
10475 return vmcs_config.cpu_based_2nd_exec_ctrl &
10476 SECONDARY_EXEC_XSAVES;
10477}
10478
Avi Kivity51aa01d2010-07-20 14:31:20 +030010479static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10480{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010481 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010482 bool unblock_nmi;
10483 u8 vector;
10484 bool idtv_info_valid;
10485
10486 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010487
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010488 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010489 if (vmx->loaded_vmcs->nmi_known_unmasked)
10490 return;
10491 /*
10492 * Can't use vmx->exit_intr_info since we're not sure what
10493 * the exit reason is.
10494 */
10495 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10496 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10497 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10498 /*
10499 * SDM 3: 27.7.1.2 (September 2008)
10500 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10501 * a guest IRET fault.
10502 * SDM 3: 23.2.2 (September 2008)
10503 * Bit 12 is undefined in any of the following cases:
10504 * If the VM exit sets the valid bit in the IDT-vectoring
10505 * information field.
10506 * If the VM exit is due to a double fault.
10507 */
10508 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10509 vector != DF_VECTOR && !idtv_info_valid)
10510 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10511 GUEST_INTR_STATE_NMI);
10512 else
10513 vmx->loaded_vmcs->nmi_known_unmasked =
10514 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10515 & GUEST_INTR_STATE_NMI);
10516 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10517 vmx->loaded_vmcs->vnmi_blocked_time +=
10518 ktime_to_ns(ktime_sub(ktime_get(),
10519 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010520}
10521
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010522static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010523 u32 idt_vectoring_info,
10524 int instr_len_field,
10525 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010526{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010527 u8 vector;
10528 int type;
10529 bool idtv_info_valid;
10530
10531 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010532
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010533 vcpu->arch.nmi_injected = false;
10534 kvm_clear_exception_queue(vcpu);
10535 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010536
10537 if (!idtv_info_valid)
10538 return;
10539
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010540 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010541
Avi Kivity668f6122008-07-02 09:28:55 +030010542 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10543 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010544
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010545 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010546 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010547 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010548 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010549 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010550 * Clear bit "block by NMI" before VM entry if a NMI
10551 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010552 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010553 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010555 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010556 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010557 /* fall through */
10558 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010559 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010560 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010561 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010562 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010563 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010564 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010565 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010566 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010567 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010568 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010569 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010570 break;
10571 default:
10572 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010573 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010574}
10575
Avi Kivity83422e12010-07-20 14:43:23 +030010576static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10577{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010578 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010579 VM_EXIT_INSTRUCTION_LEN,
10580 IDT_VECTORING_ERROR_CODE);
10581}
10582
Avi Kivityb463a6f2010-07-20 15:06:17 +030010583static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10584{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010585 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010586 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10587 VM_ENTRY_INSTRUCTION_LEN,
10588 VM_ENTRY_EXCEPTION_ERROR_CODE);
10589
10590 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10591}
10592
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010593static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10594{
10595 int i, nr_msrs;
10596 struct perf_guest_switch_msr *msrs;
10597
10598 msrs = perf_guest_get_msrs(&nr_msrs);
10599
10600 if (!msrs)
10601 return;
10602
10603 for (i = 0; i < nr_msrs; i++)
10604 if (msrs[i].host == msrs[i].guest)
10605 clear_atomic_switch_msr(vmx, msrs[i].msr);
10606 else
10607 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010608 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010609}
10610
Sean Christophersonf459a702018-08-27 15:21:11 -070010611static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10612{
10613 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10614 if (!vmx->loaded_vmcs->hv_timer_armed)
10615 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10616 PIN_BASED_VMX_PREEMPTION_TIMER);
10617 vmx->loaded_vmcs->hv_timer_armed = true;
10618}
10619
10620static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010621{
10622 struct vcpu_vmx *vmx = to_vmx(vcpu);
10623 u64 tscl;
10624 u32 delta_tsc;
10625
Sean Christophersond264ee02018-08-27 15:21:12 -070010626 if (vmx->req_immediate_exit) {
10627 vmx_arm_hv_timer(vmx, 0);
10628 return;
10629 }
10630
Sean Christophersonf459a702018-08-27 15:21:11 -070010631 if (vmx->hv_deadline_tsc != -1) {
10632 tscl = rdtsc();
10633 if (vmx->hv_deadline_tsc > tscl)
10634 /* set_hv_timer ensures the delta fits in 32-bits */
10635 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10636 cpu_preemption_timer_multi);
10637 else
10638 delta_tsc = 0;
10639
10640 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010641 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010642 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010643
Sean Christophersonf459a702018-08-27 15:21:11 -070010644 if (vmx->loaded_vmcs->hv_timer_armed)
10645 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10646 PIN_BASED_VMX_PREEMPTION_TIMER);
10647 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010648}
10649
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010650static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010651{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010652 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010653 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010654
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010655 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010656 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010657 vmx->loaded_vmcs->soft_vnmi_blocked))
10658 vmx->loaded_vmcs->entry_time = ktime_get();
10659
Avi Kivity104f2262010-11-18 13:12:52 +020010660 /* Don't enter VMX if guest state is invalid, let the exit handler
10661 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010662 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010663 return;
10664
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010665 if (vmx->ple_window_dirty) {
10666 vmx->ple_window_dirty = false;
10667 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10668 }
10669
Abel Gordon012f83c2013-04-18 14:39:25 +030010670 if (vmx->nested.sync_shadow_vmcs) {
10671 copy_vmcs12_to_shadow(vmx);
10672 vmx->nested.sync_shadow_vmcs = false;
10673 }
10674
Avi Kivity104f2262010-11-18 13:12:52 +020010675 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10676 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10677 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10678 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10679
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010680 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010681 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010682 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010683 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010684 }
10685
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010686 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010687 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010688 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010689 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010690 }
10691
Avi Kivity104f2262010-11-18 13:12:52 +020010692 /* When single-stepping over STI and MOV SS, we must clear the
10693 * corresponding interruptibility bits in the guest state. Otherwise
10694 * vmentry fails as it then expects bit 14 (BS) in pending debug
10695 * exceptions being set, but that's not correct for the guest debugging
10696 * case. */
10697 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10698 vmx_set_interrupt_shadow(vcpu, 0);
10699
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010700 if (static_cpu_has(X86_FEATURE_PKU) &&
10701 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10702 vcpu->arch.pkru != vmx->host_pkru)
10703 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010704
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010705 atomic_switch_perf_msrs(vmx);
10706
Sean Christophersonf459a702018-08-27 15:21:11 -070010707 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010708
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010709 /*
10710 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10711 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10712 * is no need to worry about the conditional branch over the wrmsr
10713 * being speculatively taken.
10714 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010715 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010716
Nadav Har'Eld462b812011-05-24 15:26:10 +030010717 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010718
10719 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10720 (unsigned long)&current_evmcs->host_rsp : 0;
10721
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010722 if (static_branch_unlikely(&vmx_l1d_should_flush))
10723 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010724
Avi Kivity104f2262010-11-18 13:12:52 +020010725 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010726 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010727 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10728 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10729 "push %%" _ASM_CX " \n\t"
10730 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010731 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010732 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010733 /* Avoid VMWRITE when Enlightened VMCS is in use */
10734 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10735 "jz 2f \n\t"
10736 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10737 "jmp 1f \n\t"
10738 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010739 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010740 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010741 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010742 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10743 "mov %%cr2, %%" _ASM_DX " \n\t"
10744 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010745 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010746 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010747 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010748 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010749 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010750 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010751 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10752 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10753 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10754 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10755 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10756 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010757#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010758 "mov %c[r8](%0), %%r8 \n\t"
10759 "mov %c[r9](%0), %%r9 \n\t"
10760 "mov %c[r10](%0), %%r10 \n\t"
10761 "mov %c[r11](%0), %%r11 \n\t"
10762 "mov %c[r12](%0), %%r12 \n\t"
10763 "mov %c[r13](%0), %%r13 \n\t"
10764 "mov %c[r14](%0), %%r14 \n\t"
10765 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010766#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010767 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010768
Avi Kivity6aa8b732006-12-10 02:21:36 -080010769 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010770 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010771 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010772 "jmp 2f \n\t"
10773 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10774 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010775 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010776 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010777 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010778 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010779 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10780 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10781 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10782 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10783 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10784 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10785 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010786#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010787 "mov %%r8, %c[r8](%0) \n\t"
10788 "mov %%r9, %c[r9](%0) \n\t"
10789 "mov %%r10, %c[r10](%0) \n\t"
10790 "mov %%r11, %c[r11](%0) \n\t"
10791 "mov %%r12, %c[r12](%0) \n\t"
10792 "mov %%r13, %c[r13](%0) \n\t"
10793 "mov %%r14, %c[r14](%0) \n\t"
10794 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010795 "xor %%r8d, %%r8d \n\t"
10796 "xor %%r9d, %%r9d \n\t"
10797 "xor %%r10d, %%r10d \n\t"
10798 "xor %%r11d, %%r11d \n\t"
10799 "xor %%r12d, %%r12d \n\t"
10800 "xor %%r13d, %%r13d \n\t"
10801 "xor %%r14d, %%r14d \n\t"
10802 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010803#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010804 "mov %%cr2, %%" _ASM_AX " \n\t"
10805 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010806
Jim Mattson0cb5b302018-01-03 14:31:38 -080010807 "xor %%eax, %%eax \n\t"
10808 "xor %%ebx, %%ebx \n\t"
10809 "xor %%esi, %%esi \n\t"
10810 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010811 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010812 ".pushsection .rodata \n\t"
10813 ".global vmx_return \n\t"
10814 "vmx_return: " _ASM_PTR " 2b \n\t"
10815 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010816 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010817 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010818 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010819 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010820 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10821 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10822 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10823 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10824 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10825 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10826 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010827#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010828 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10829 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10830 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10831 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10832 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10833 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10834 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10835 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010836#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010837 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10838 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010839 : "cc", "memory"
10840#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010841 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010842 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010843#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010844 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010845#endif
10846 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010847
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010848 /*
10849 * We do not use IBRS in the kernel. If this vCPU has used the
10850 * SPEC_CTRL MSR it may have left it on; save the value and
10851 * turn it off. This is much more efficient than blindly adding
10852 * it to the atomic save/restore list. Especially as the former
10853 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10854 *
10855 * For non-nested case:
10856 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10857 * save it.
10858 *
10859 * For nested case:
10860 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10861 * save it.
10862 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010863 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010864 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010865
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010866 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010867
David Woodhouse117cc7a2018-01-12 11:11:27 +000010868 /* Eliminate branch target predictions from guest mode */
10869 vmexit_fill_RSB();
10870
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010871 /* All fields are clean at this point */
10872 if (static_branch_unlikely(&enable_evmcs))
10873 current_evmcs->hv_clean_fields |=
10874 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10875
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010876 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010877 if (vmx->host_debugctlmsr)
10878 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010879
Avi Kivityaa67f602012-08-01 16:48:03 +030010880#ifndef CONFIG_X86_64
10881 /*
10882 * The sysexit path does not restore ds/es, so we must set them to
10883 * a reasonable value ourselves.
10884 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010885 * We can't defer this to vmx_prepare_switch_to_host() since that
10886 * function may be executed in interrupt context, which saves and
10887 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010888 */
10889 loadsegment(ds, __USER_DS);
10890 loadsegment(es, __USER_DS);
10891#endif
10892
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010893 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010894 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010895 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010896 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010897 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010898 vcpu->arch.regs_dirty = 0;
10899
Gleb Natapove0b890d2013-09-25 12:51:33 +030010900 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010901 * eager fpu is enabled if PKEY is supported and CR4 is switched
10902 * back on host, so it is safe to read guest PKRU from current
10903 * XSAVE.
10904 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010905 if (static_cpu_has(X86_FEATURE_PKU) &&
10906 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10907 vcpu->arch.pkru = __read_pkru();
10908 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010909 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010910 }
10911
Gleb Natapove0b890d2013-09-25 12:51:33 +030010912 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010913 vmx->idt_vectoring_info = 0;
10914
10915 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10916 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10917 return;
10918
10919 vmx->loaded_vmcs->launched = 1;
10920 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010921
Avi Kivity51aa01d2010-07-20 14:31:20 +030010922 vmx_complete_atomic_exit(vmx);
10923 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010924 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010925}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010926STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010927
Sean Christopherson434a1e92018-03-20 12:17:18 -070010928static struct kvm *vmx_vm_alloc(void)
10929{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010930 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010931 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010932}
10933
10934static void vmx_vm_free(struct kvm *kvm)
10935{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010936 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010937}
10938
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010939static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010940{
10941 struct vcpu_vmx *vmx = to_vmx(vcpu);
10942 int cpu;
10943
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010944 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010945 return;
10946
10947 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010948 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010949 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010950 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010951 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010952
10953 vm_entry_controls_reset_shadow(vmx);
10954 vm_exit_controls_reset_shadow(vmx);
10955 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010956}
10957
Jim Mattson2f1fe812016-07-08 15:36:06 -070010958/*
10959 * Ensure that the current vmcs of the logical processor is the
10960 * vmcs01 of the vcpu before calling free_nested().
10961 */
10962static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10963{
10964 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010965
Christoffer Dallec7660c2017-12-04 21:35:23 +010010966 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010967 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010968 free_nested(vmx);
10969 vcpu_put(vcpu);
10970}
10971
Avi Kivity6aa8b732006-12-10 02:21:36 -080010972static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10973{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010974 struct vcpu_vmx *vmx = to_vmx(vcpu);
10975
Kai Huang843e4332015-01-28 10:54:28 +080010976 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010977 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010978 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010979 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010980 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010981 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010982 kfree(vmx->guest_msrs);
10983 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010984 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010985}
10986
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010987static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010988{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010989 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010990 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010991 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010992 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010993
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010994 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010995 return ERR_PTR(-ENOMEM);
10996
Wanpeng Li991e7a02015-09-16 17:30:05 +080010997 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010998
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010999 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11000 if (err)
11001 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011002
Peter Feiner4e595162016-07-07 14:49:58 -070011003 err = -ENOMEM;
11004
11005 /*
11006 * If PML is turned on, failure on enabling PML just results in failure
11007 * of creating the vcpu, therefore we can simplify PML logic (by
11008 * avoiding dealing with cases, such as enabling PML partially on vcpus
11009 * for the guest, etc.
11010 */
11011 if (enable_pml) {
11012 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11013 if (!vmx->pml_pg)
11014 goto uninit_vcpu;
11015 }
11016
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011017 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011018 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11019 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011020
Peter Feiner4e595162016-07-07 14:49:58 -070011021 if (!vmx->guest_msrs)
11022 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011023
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011024 err = alloc_loaded_vmcs(&vmx->vmcs01);
11025 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011026 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011027
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011028 msr_bitmap = vmx->vmcs01.msr_bitmap;
11029 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11030 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11031 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11032 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11033 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11034 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11035 vmx->msr_bitmap_mode = 0;
11036
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011037 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011038 cpu = get_cpu();
11039 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011040 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011041 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011042 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011043 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011044 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011045 err = alloc_apic_access_page(kvm);
11046 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011047 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011048 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011049
Sean Christophersone90008d2018-03-05 12:04:37 -080011050 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011051 err = init_rmode_identity_map(kvm);
11052 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011053 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011054 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011055
Roman Kagan63aff652018-07-19 21:59:07 +030011056 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011057 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11058 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011059
Wincy Van705699a2015-02-03 23:58:17 +080011060 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011061 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011062
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011063 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11064
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011065 /*
11066 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11067 * or POSTED_INTR_WAKEUP_VECTOR.
11068 */
11069 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11070 vmx->pi_desc.sn = 1;
11071
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011072 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011073
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011074free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011075 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011076free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011077 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011078free_pml:
11079 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011080uninit_vcpu:
11081 kvm_vcpu_uninit(&vmx->vcpu);
11082free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011083 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011084 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011085 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011086}
11087
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011088#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"
11089#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 -040011090
Wanpeng Lib31c1142018-03-12 04:53:04 -070011091static int vmx_vm_init(struct kvm *kvm)
11092{
Tianyu Lan877ad952018-07-19 08:40:23 +000011093 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11094
Wanpeng Lib31c1142018-03-12 04:53:04 -070011095 if (!ple_gap)
11096 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011097
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011098 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11099 switch (l1tf_mitigation) {
11100 case L1TF_MITIGATION_OFF:
11101 case L1TF_MITIGATION_FLUSH_NOWARN:
11102 /* 'I explicitly don't care' is set */
11103 break;
11104 case L1TF_MITIGATION_FLUSH:
11105 case L1TF_MITIGATION_FLUSH_NOSMT:
11106 case L1TF_MITIGATION_FULL:
11107 /*
11108 * Warn upon starting the first VM in a potentially
11109 * insecure environment.
11110 */
11111 if (cpu_smt_control == CPU_SMT_ENABLED)
11112 pr_warn_once(L1TF_MSG_SMT);
11113 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11114 pr_warn_once(L1TF_MSG_L1D);
11115 break;
11116 case L1TF_MITIGATION_FULL_FORCE:
11117 /* Flush is enforced */
11118 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011119 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011120 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011121 return 0;
11122}
11123
Yang, Sheng002c7f72007-07-31 14:23:01 +030011124static void __init vmx_check_processor_compat(void *rtn)
11125{
11126 struct vmcs_config vmcs_conf;
11127
11128 *(int *)rtn = 0;
11129 if (setup_vmcs_config(&vmcs_conf) < 0)
11130 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011131 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011132 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11133 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11134 smp_processor_id());
11135 *(int *)rtn = -EIO;
11136 }
11137}
11138
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011139static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011140{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011141 u8 cache;
11142 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011143
Sheng Yang522c68c2009-04-27 20:35:43 +080011144 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011145 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011146 * 2. EPT with VT-d:
11147 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011148 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011149 * b. VT-d with snooping control feature: snooping control feature of
11150 * VT-d engine can guarantee the cache correctness. Just set it
11151 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011152 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011153 * consistent with host MTRR
11154 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011155 if (is_mmio) {
11156 cache = MTRR_TYPE_UNCACHABLE;
11157 goto exit;
11158 }
11159
11160 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011161 ipat = VMX_EPT_IPAT_BIT;
11162 cache = MTRR_TYPE_WRBACK;
11163 goto exit;
11164 }
11165
11166 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11167 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011168 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011169 cache = MTRR_TYPE_WRBACK;
11170 else
11171 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011172 goto exit;
11173 }
11174
Xiao Guangrongff536042015-06-15 16:55:22 +080011175 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011176
11177exit:
11178 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011179}
11180
Sheng Yang17cc3932010-01-05 19:02:27 +080011181static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011182{
Sheng Yang878403b2010-01-05 19:02:29 +080011183 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11184 return PT_DIRECTORY_LEVEL;
11185 else
11186 /* For shadow and EPT supported 1GB page */
11187 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011188}
11189
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011190static void vmcs_set_secondary_exec_control(u32 new_ctl)
11191{
11192 /*
11193 * These bits in the secondary execution controls field
11194 * are dynamic, the others are mostly based on the hypervisor
11195 * architecture and the guest's CPUID. Do not touch the
11196 * dynamic bits.
11197 */
11198 u32 mask =
11199 SECONDARY_EXEC_SHADOW_VMCS |
11200 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011201 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11202 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011203
11204 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11205
11206 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11207 (new_ctl & ~mask) | (cur_ctl & mask));
11208}
11209
David Matlack8322ebb2016-11-29 18:14:09 -080011210/*
11211 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11212 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11213 */
11214static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11215{
11216 struct vcpu_vmx *vmx = to_vmx(vcpu);
11217 struct kvm_cpuid_entry2 *entry;
11218
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011219 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11220 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011221
11222#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11223 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011224 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011225} while (0)
11226
11227 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11228 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11229 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11230 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11231 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11232 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11233 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11234 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11235 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11236 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11237 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11238 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11239 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11240 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11241 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11242
11243 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11244 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11245 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11246 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11247 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011248 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011249
11250#undef cr4_fixed1_update
11251}
11252
Liran Alon5f76f6f2018-09-14 03:25:52 +030011253static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11254{
11255 struct vcpu_vmx *vmx = to_vmx(vcpu);
11256
11257 if (kvm_mpx_supported()) {
11258 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11259
11260 if (mpx_enabled) {
11261 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11262 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11263 } else {
11264 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11265 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11266 }
11267 }
11268}
11269
Sheng Yang0e851882009-12-18 16:48:46 +080011270static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11271{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011272 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011273
Paolo Bonzini80154d72017-08-24 13:55:35 +020011274 if (cpu_has_secondary_exec_ctrls()) {
11275 vmx_compute_secondary_exec_control(vmx);
11276 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011277 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011278
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011279 if (nested_vmx_allowed(vcpu))
11280 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11281 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11282 else
11283 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11284 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011285
Liran Alon5f76f6f2018-09-14 03:25:52 +030011286 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011287 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011288 nested_vmx_entry_exit_ctls_update(vcpu);
11289 }
Sheng Yang0e851882009-12-18 16:48:46 +080011290}
11291
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011292static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11293{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011294 if (func == 1 && nested)
11295 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011296}
11297
Yang Zhang25d92082013-08-06 12:00:32 +030011298static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11299 struct x86_exception *fault)
11300{
Jan Kiszka533558b2014-01-04 18:47:20 +010011301 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011302 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011303 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011304 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011305
Bandan Dasc5f983f2017-05-05 15:25:14 -040011306 if (vmx->nested.pml_full) {
11307 exit_reason = EXIT_REASON_PML_FULL;
11308 vmx->nested.pml_full = false;
11309 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11310 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011311 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011312 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011313 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011314
11315 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011316 vmcs12->guest_physical_address = fault->address;
11317}
11318
Peter Feiner995f00a2017-06-30 17:26:32 -070011319static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11320{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011321 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011322}
11323
Nadav Har'El155a97a2013-08-05 11:07:16 +030011324/* Callbacks for nested_ept_init_mmu_context: */
11325
11326static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11327{
11328 /* return the page table to be shadowed - in our case, EPT12 */
11329 return get_vmcs12(vcpu)->ept_pointer;
11330}
11331
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011332static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011333{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011334 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011335
Paolo Bonziniad896af2013-10-02 16:56:14 +020011336 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011337 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011338 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011339 nested_ept_ad_enabled(vcpu),
11340 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011341 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11342 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11343 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11344
11345 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011346}
11347
11348static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11349{
11350 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11351}
11352
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011353static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11354 u16 error_code)
11355{
11356 bool inequality, bit;
11357
11358 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11359 inequality =
11360 (error_code & vmcs12->page_fault_error_code_mask) !=
11361 vmcs12->page_fault_error_code_match;
11362 return inequality ^ bit;
11363}
11364
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011365static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11366 struct x86_exception *fault)
11367{
11368 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11369
11370 WARN_ON(!is_guest_mode(vcpu));
11371
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011372 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11373 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011374 vmcs12->vm_exit_intr_error_code = fault->error_code;
11375 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11376 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11377 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11378 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011379 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011380 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011381 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011382}
11383
Paolo Bonzinic9923842017-12-13 14:16:30 +010011384static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11385 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011386
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011387static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011388{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011389 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011390 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011391 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011392 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011393
11394 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011395 /*
11396 * Translate L1 physical address to host physical
11397 * address for vmcs02. Keep the page pinned, so this
11398 * physical address remains valid. We keep a reference
11399 * to it so we can release it later.
11400 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011401 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011402 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011403 vmx->nested.apic_access_page = NULL;
11404 }
11405 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011406 /*
11407 * If translation failed, no matter: This feature asks
11408 * to exit when accessing the given address, and if it
11409 * can never be accessed, this feature won't do
11410 * anything anyway.
11411 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011412 if (!is_error_page(page)) {
11413 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011414 hpa = page_to_phys(vmx->nested.apic_access_page);
11415 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11416 } else {
11417 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11418 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11419 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011420 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011421
11422 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011423 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011424 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011425 vmx->nested.virtual_apic_page = NULL;
11426 }
11427 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011428
11429 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011430 * If translation failed, VM entry will fail because
11431 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11432 * Failing the vm entry is _not_ what the processor
11433 * does but it's basically the only possibility we
11434 * have. We could still enter the guest if CR8 load
11435 * exits are enabled, CR8 store exits are enabled, and
11436 * virtualize APIC access is disabled; in this case
11437 * the processor would never use the TPR shadow and we
11438 * could simply clear the bit from the execution
11439 * control. But such a configuration is useless, so
11440 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011441 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011442 if (!is_error_page(page)) {
11443 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011444 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11445 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11446 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011447 }
11448
Wincy Van705699a2015-02-03 23:58:17 +080011449 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011450 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11451 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011452 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011453 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011454 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011455 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11456 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011457 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011458 vmx->nested.pi_desc_page = page;
11459 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011460 vmx->nested.pi_desc =
11461 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11462 (unsigned long)(vmcs12->posted_intr_desc_addr &
11463 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011464 vmcs_write64(POSTED_INTR_DESC_ADDR,
11465 page_to_phys(vmx->nested.pi_desc_page) +
11466 (unsigned long)(vmcs12->posted_intr_desc_addr &
11467 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011468 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011469 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011470 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11471 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011472 else
11473 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11474 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011475}
11476
Jan Kiszkaf4124502014-03-07 20:03:13 +010011477static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11478{
11479 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11480 struct vcpu_vmx *vmx = to_vmx(vcpu);
11481
Sean Christopherson4c008122018-08-27 15:21:10 -070011482 /*
11483 * A timer value of zero is architecturally guaranteed to cause
11484 * a VMExit prior to executing any instructions in the guest.
11485 */
11486 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011487 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11488 return;
11489 }
11490
Sean Christopherson4c008122018-08-27 15:21:10 -070011491 if (vcpu->arch.virtual_tsc_khz == 0)
11492 return;
11493
Jan Kiszkaf4124502014-03-07 20:03:13 +010011494 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11495 preemption_timeout *= 1000000;
11496 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11497 hrtimer_start(&vmx->nested.preemption_timer,
11498 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11499}
11500
Jim Mattson56a20512017-07-06 16:33:06 -070011501static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11502 struct vmcs12 *vmcs12)
11503{
11504 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11505 return 0;
11506
11507 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11508 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11509 return -EINVAL;
11510
11511 return 0;
11512}
11513
Wincy Van3af18d92015-02-03 23:49:31 +080011514static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11515 struct vmcs12 *vmcs12)
11516{
Wincy Van3af18d92015-02-03 23:49:31 +080011517 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11518 return 0;
11519
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011520 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011521 return -EINVAL;
11522
11523 return 0;
11524}
11525
Jim Mattson712b12d2017-08-24 13:24:47 -070011526static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11527 struct vmcs12 *vmcs12)
11528{
11529 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11530 return 0;
11531
11532 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11533 return -EINVAL;
11534
11535 return 0;
11536}
11537
Wincy Van3af18d92015-02-03 23:49:31 +080011538/*
11539 * Merge L0's and L1's MSR bitmap, return false to indicate that
11540 * we do not use the hardware.
11541 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011542static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11543 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011544{
Wincy Van82f0dd42015-02-03 23:57:18 +080011545 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011546 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011547 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011548 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011549 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011550 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011551 *
11552 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11553 * ensures that we do not accidentally generate an L02 MSR bitmap
11554 * from the L12 MSR bitmap that is too permissive.
11555 * 2. That L1 or L2s have actually used the MSR. This avoids
11556 * unnecessarily merging of the bitmap if the MSR is unused. This
11557 * works properly because we only update the L01 MSR bitmap lazily.
11558 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11559 * updated to reflect this when L1 (or its L2s) actually write to
11560 * the MSR.
11561 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011562 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11563 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011564
Paolo Bonzinic9923842017-12-13 14:16:30 +010011565 /* Nothing to do if the MSR bitmap is not in use. */
11566 if (!cpu_has_vmx_msr_bitmap() ||
11567 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11568 return false;
11569
Ashok Raj15d45072018-02-01 22:59:43 +010011570 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011571 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011572 return false;
11573
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011574 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11575 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011576 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011577
Radim Krčmářd048c092016-08-08 20:16:22 +020011578 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011579 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11580 /*
11581 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11582 * just lets the processor take the value from the virtual-APIC page;
11583 * take those 256 bits directly from the L1 bitmap.
11584 */
11585 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11586 unsigned word = msr / BITS_PER_LONG;
11587 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11588 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011589 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011590 } else {
11591 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11592 unsigned word = msr / BITS_PER_LONG;
11593 msr_bitmap_l0[word] = ~0;
11594 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11595 }
11596 }
11597
11598 nested_vmx_disable_intercept_for_msr(
11599 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011600 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011601 MSR_TYPE_W);
11602
11603 if (nested_cpu_has_vid(vmcs12)) {
11604 nested_vmx_disable_intercept_for_msr(
11605 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011606 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011607 MSR_TYPE_W);
11608 nested_vmx_disable_intercept_for_msr(
11609 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011610 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011611 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011612 }
Ashok Raj15d45072018-02-01 22:59:43 +010011613
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011614 if (spec_ctrl)
11615 nested_vmx_disable_intercept_for_msr(
11616 msr_bitmap_l1, msr_bitmap_l0,
11617 MSR_IA32_SPEC_CTRL,
11618 MSR_TYPE_R | MSR_TYPE_W);
11619
Ashok Raj15d45072018-02-01 22:59:43 +010011620 if (pred_cmd)
11621 nested_vmx_disable_intercept_for_msr(
11622 msr_bitmap_l1, msr_bitmap_l0,
11623 MSR_IA32_PRED_CMD,
11624 MSR_TYPE_W);
11625
Wincy Vanf2b93282015-02-03 23:56:03 +080011626 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011627 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011628
11629 return true;
11630}
11631
Liran Alon61ada742018-06-23 02:35:08 +030011632static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11633 struct vmcs12 *vmcs12)
11634{
11635 struct vmcs12 *shadow;
11636 struct page *page;
11637
11638 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11639 vmcs12->vmcs_link_pointer == -1ull)
11640 return;
11641
11642 shadow = get_shadow_vmcs12(vcpu);
11643 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11644
11645 memcpy(shadow, kmap(page), VMCS12_SIZE);
11646
11647 kunmap(page);
11648 kvm_release_page_clean(page);
11649}
11650
11651static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11652 struct vmcs12 *vmcs12)
11653{
11654 struct vcpu_vmx *vmx = to_vmx(vcpu);
11655
11656 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11657 vmcs12->vmcs_link_pointer == -1ull)
11658 return;
11659
11660 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11661 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11662}
11663
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011664static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11665 struct vmcs12 *vmcs12)
11666{
11667 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11668 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11669 return -EINVAL;
11670 else
11671 return 0;
11672}
11673
Wincy Vanf2b93282015-02-03 23:56:03 +080011674static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11675 struct vmcs12 *vmcs12)
11676{
Wincy Van82f0dd42015-02-03 23:57:18 +080011677 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011678 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011679 !nested_cpu_has_vid(vmcs12) &&
11680 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011681 return 0;
11682
11683 /*
11684 * If virtualize x2apic mode is enabled,
11685 * virtualize apic access must be disabled.
11686 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011687 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11688 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011689 return -EINVAL;
11690
Wincy Van608406e2015-02-03 23:57:51 +080011691 /*
11692 * If virtual interrupt delivery is enabled,
11693 * we must exit on external interrupts.
11694 */
11695 if (nested_cpu_has_vid(vmcs12) &&
11696 !nested_exit_on_intr(vcpu))
11697 return -EINVAL;
11698
Wincy Van705699a2015-02-03 23:58:17 +080011699 /*
11700 * bits 15:8 should be zero in posted_intr_nv,
11701 * the descriptor address has been already checked
11702 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011703 *
11704 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011705 */
11706 if (nested_cpu_has_posted_intr(vmcs12) &&
11707 (!nested_cpu_has_vid(vmcs12) ||
11708 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011709 (vmcs12->posted_intr_nv & 0xff00) ||
11710 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11711 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011712 return -EINVAL;
11713
Wincy Vanf2b93282015-02-03 23:56:03 +080011714 /* tpr shadow is needed by all apicv features. */
11715 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11716 return -EINVAL;
11717
11718 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011719}
11720
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011721static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11722 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011723 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011724{
Liran Alone2536742018-06-23 02:35:02 +030011725 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011726 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011727 u64 count, addr;
11728
Liran Alone2536742018-06-23 02:35:02 +030011729 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11730 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011731 WARN_ON(1);
11732 return -EINVAL;
11733 }
11734 if (count == 0)
11735 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011736 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011737 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11738 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011739 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011740 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11741 addr_field, maxphyaddr, count, addr);
11742 return -EINVAL;
11743 }
11744 return 0;
11745}
11746
11747static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11748 struct vmcs12 *vmcs12)
11749{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011750 if (vmcs12->vm_exit_msr_load_count == 0 &&
11751 vmcs12->vm_exit_msr_store_count == 0 &&
11752 vmcs12->vm_entry_msr_load_count == 0)
11753 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011754 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011755 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011756 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011757 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011758 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011759 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011760 return -EINVAL;
11761 return 0;
11762}
11763
Bandan Dasc5f983f2017-05-05 15:25:14 -040011764static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11765 struct vmcs12 *vmcs12)
11766{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011767 if (!nested_cpu_has_pml(vmcs12))
11768 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011769
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011770 if (!nested_cpu_has_ept(vmcs12) ||
11771 !page_address_valid(vcpu, vmcs12->pml_address))
11772 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011773
11774 return 0;
11775}
11776
Liran Alona8a7c022018-06-23 02:35:06 +030011777static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11778 struct vmcs12 *vmcs12)
11779{
11780 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11781 return 0;
11782
11783 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11784 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11785 return -EINVAL;
11786
11787 return 0;
11788}
11789
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011790static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11791 struct vmx_msr_entry *e)
11792{
11793 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011794 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011795 return -EINVAL;
11796 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11797 e->index == MSR_IA32_UCODE_REV)
11798 return -EINVAL;
11799 if (e->reserved != 0)
11800 return -EINVAL;
11801 return 0;
11802}
11803
11804static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11805 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011806{
11807 if (e->index == MSR_FS_BASE ||
11808 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011809 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11810 nested_vmx_msr_check_common(vcpu, e))
11811 return -EINVAL;
11812 return 0;
11813}
11814
11815static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11816 struct vmx_msr_entry *e)
11817{
11818 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11819 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011820 return -EINVAL;
11821 return 0;
11822}
11823
11824/*
11825 * Load guest's/host's msr at nested entry/exit.
11826 * return 0 for success, entry index for failure.
11827 */
11828static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11829{
11830 u32 i;
11831 struct vmx_msr_entry e;
11832 struct msr_data msr;
11833
11834 msr.host_initiated = false;
11835 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011836 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11837 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011838 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011839 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11840 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011841 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011842 }
11843 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011844 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011845 "%s check failed (%u, 0x%x, 0x%x)\n",
11846 __func__, i, e.index, e.reserved);
11847 goto fail;
11848 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011849 msr.index = e.index;
11850 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011851 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011852 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011853 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11854 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011855 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011856 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011857 }
11858 return 0;
11859fail:
11860 return i + 1;
11861}
11862
11863static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11864{
11865 u32 i;
11866 struct vmx_msr_entry e;
11867
11868 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011869 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011870 if (kvm_vcpu_read_guest(vcpu,
11871 gpa + i * sizeof(e),
11872 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011873 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011874 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11875 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011876 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011877 }
11878 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011879 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011880 "%s check failed (%u, 0x%x, 0x%x)\n",
11881 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011882 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011883 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011884 msr_info.host_initiated = false;
11885 msr_info.index = e.index;
11886 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011887 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011888 "%s cannot read MSR (%u, 0x%x)\n",
11889 __func__, i, e.index);
11890 return -EINVAL;
11891 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011892 if (kvm_vcpu_write_guest(vcpu,
11893 gpa + i * sizeof(e) +
11894 offsetof(struct vmx_msr_entry, value),
11895 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011896 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011897 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011898 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011899 return -EINVAL;
11900 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011901 }
11902 return 0;
11903}
11904
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011905static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11906{
11907 unsigned long invalid_mask;
11908
11909 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11910 return (val & invalid_mask) == 0;
11911}
11912
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011913/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011914 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11915 * emulating VM entry into a guest with EPT enabled.
11916 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11917 * is assigned to entry_failure_code on failure.
11918 */
11919static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011920 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011921{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011922 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011923 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011924 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11925 return 1;
11926 }
11927
11928 /*
11929 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11930 * must not be dereferenced.
11931 */
11932 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11933 !nested_ept) {
11934 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11935 *entry_failure_code = ENTRY_FAIL_PDPTE;
11936 return 1;
11937 }
11938 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011939 }
11940
Junaid Shahid50c28f22018-06-27 14:59:11 -070011941 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011942 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011943
11944 vcpu->arch.cr3 = cr3;
11945 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11946
11947 kvm_init_mmu(vcpu, false);
11948
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011949 return 0;
11950}
11951
Liran Alonefebf0a2018-10-08 23:42:20 +030011952/*
11953 * Returns if KVM is able to config CPU to tag TLB entries
11954 * populated by L2 differently than TLB entries populated
11955 * by L1.
11956 *
11957 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11958 *
11959 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11960 * with different VPID (L1 entries are tagged with vmx->vpid
11961 * while L2 entries are tagged with vmx->nested.vpid02).
11962 */
11963static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11964{
11965 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11966
11967 return nested_cpu_has_ept(vmcs12) ||
11968 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11969}
11970
Sean Christopherson3df5c372018-09-26 09:23:44 -070011971static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11972{
11973 if (vmx->nested.nested_run_pending &&
11974 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11975 return vmcs12->guest_ia32_efer;
11976 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11977 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11978 else
11979 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11980}
11981
Sean Christopherson09abe322018-09-26 09:23:50 -070011982static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011983{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011984 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070011985 * If we have never launched vmcs02, set the constant vmcs02 state
11986 * according to L0's settings (vmcs12 is irrelevant here). Host
11987 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11988 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011989 */
Sean Christopherson09abe322018-09-26 09:23:50 -070011990 if (vmx->nested.vmcs02.launched)
11991 return;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011992
11993 /* All VMFUNCs are currently emulated through L0 vmexits. */
11994 if (cpu_has_vmx_vmfunc())
11995 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11996
Sean Christopherson09abe322018-09-26 09:23:50 -070011997 if (cpu_has_vmx_posted_intr())
11998 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11999
12000 if (cpu_has_vmx_msr_bitmap())
12001 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12002
12003 if (enable_pml)
12004 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012005
12006 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012007 * Set the MSR load/store lists to match L0's settings. Only the
12008 * addresses are constant (for vmcs02), the counts can change based
12009 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012010 */
12011 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012012 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012013 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012014
Sean Christopherson09abe322018-09-26 09:23:50 -070012015 vmx_set_constant_host_state(vmx);
12016}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012017
Sean Christopherson09abe322018-09-26 09:23:50 -070012018static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12019 struct vmcs12 *vmcs12)
12020{
12021 prepare_vmcs02_constant_state(vmx);
12022
12023 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012024
12025 if (enable_vpid) {
12026 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12027 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12028 else
12029 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12030 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012031}
12032
Sean Christopherson09abe322018-09-26 09:23:50 -070012033static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012034{
Bandan Das03efce62017-05-05 15:25:15 -040012035 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012036 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012037
Sean Christopherson09abe322018-09-26 09:23:50 -070012038 if (vmx->nested.dirty_vmcs12)
12039 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012040
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012041 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012042 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12043 * entry, but only if the current (host) sp changed from the value
12044 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12045 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12046 * here we just force the write to happen on entry.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012047 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012048 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012049
Sean Christopherson09abe322018-09-26 09:23:50 -070012050 /*
12051 * PIN CONTROLS
12052 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012053 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012054
Sean Christophersonf459a702018-08-27 15:21:11 -070012055 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012056 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012057 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12058 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012059
12060 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012061 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012062 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12063 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012064 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012065 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012066 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012067 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012068
Sean Christopherson09abe322018-09-26 09:23:50 -070012069 /*
12070 * EXEC CONTROLS
12071 */
12072 exec_control = vmx_exec_control(vmx); /* L0's desires */
12073 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12074 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12075 exec_control &= ~CPU_BASED_TPR_SHADOW;
12076 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012077
Sean Christopherson09abe322018-09-26 09:23:50 -070012078 /*
12079 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12080 * nested_get_vmcs12_pages can't fix it up, the illegal value
12081 * will result in a VM entry failure.
12082 */
12083 if (exec_control & CPU_BASED_TPR_SHADOW) {
12084 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12085 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12086 } else {
12087#ifdef CONFIG_X86_64
12088 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12089 CPU_BASED_CR8_STORE_EXITING;
12090#endif
12091 }
12092
12093 /*
12094 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12095 * for I/O port accesses.
12096 */
12097 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12098 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12099 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12100
12101 /*
12102 * SECONDARY EXEC CONTROLS
12103 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012104 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012105 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012106
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012107 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012108 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012109 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012110 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012111 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012112 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012113 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12114 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012115 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012116 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12117 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12118 ~SECONDARY_EXEC_ENABLE_PML;
12119 exec_control |= vmcs12_exec_ctrl;
12120 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012121
Liran Alon32c7acf2018-06-23 02:35:11 +030012122 /* VMCS shadowing for L2 is emulated for now */
12123 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12124
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012125 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012126 vmcs_write16(GUEST_INTR_STATUS,
12127 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012128
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012129 /*
12130 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12131 * nested_get_vmcs12_pages will either fix it up or
12132 * remove the VM execution control.
12133 */
12134 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12135 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12136
Sean Christopherson0b665d32018-08-14 09:33:34 -070012137 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12138 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12139
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012140 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12141 }
12142
Jim Mattson83bafef2016-10-04 10:48:38 -070012143 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012144 * ENTRY CONTROLS
12145 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012146 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012147 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12148 * on the related bits (if supported by the CPU) in the hope that
12149 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012150 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012151 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12152 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12153 if (cpu_has_load_ia32_efer) {
12154 if (guest_efer & EFER_LMA)
12155 exec_control |= VM_ENTRY_IA32E_MODE;
12156 if (guest_efer != host_efer)
12157 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12158 }
12159 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012160
Sean Christopherson09abe322018-09-26 09:23:50 -070012161 /*
12162 * EXIT CONTROLS
12163 *
12164 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12165 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12166 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12167 */
12168 exec_control = vmcs_config.vmexit_ctrl;
12169 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12170 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12171 vm_exit_controls_init(vmx, exec_control);
12172
12173 /*
12174 * Conceptually we want to copy the PML address and index from
12175 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12176 * since we always flush the log on each vmexit and never change
12177 * the PML address (once set), this happens to be equivalent to
12178 * simply resetting the index in vmcs02.
12179 */
12180 if (enable_pml)
12181 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12182
12183 /*
12184 * Interrupt/Exception Fields
12185 */
12186 if (vmx->nested.nested_run_pending) {
12187 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12188 vmcs12->vm_entry_intr_info_field);
12189 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12190 vmcs12->vm_entry_exception_error_code);
12191 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12192 vmcs12->vm_entry_instruction_len);
12193 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12194 vmcs12->guest_interruptibility_info);
12195 vmx->loaded_vmcs->nmi_known_unmasked =
12196 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12197 } else {
12198 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12199 }
12200}
12201
12202static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12203{
12204 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12205 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12206 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12207 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12208 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12209 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12210 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12211 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12212 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12213 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12214 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12215 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12216 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12217 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12218 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12219 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12220 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12221 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12222 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12223 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12224 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12225 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12226 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12227 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12228 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12229 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12230 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12231 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12232 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12233 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12234 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12235
12236 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12237 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12238 vmcs12->guest_pending_dbg_exceptions);
12239 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12240 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12241
12242 if (nested_cpu_has_xsaves(vmcs12))
12243 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12244
12245 /*
12246 * Whether page-faults are trapped is determined by a combination of
12247 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12248 * If enable_ept, L0 doesn't care about page faults and we should
12249 * set all of these to L1's desires. However, if !enable_ept, L0 does
12250 * care about (at least some) page faults, and because it is not easy
12251 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12252 * to exit on each and every L2 page fault. This is done by setting
12253 * MASK=MATCH=0 and (see below) EB.PF=1.
12254 * Note that below we don't need special code to set EB.PF beyond the
12255 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12256 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12257 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12258 */
12259 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12260 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12261 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12262 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12263
12264 if (cpu_has_vmx_apicv()) {
12265 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12266 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12267 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12268 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12269 }
12270
12271 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12272 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12273
12274 set_cr4_guest_host_mask(vmx);
12275
12276 if (kvm_mpx_supported()) {
12277 if (vmx->nested.nested_run_pending &&
12278 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12279 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12280 else
12281 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12282 }
12283
12284 /*
12285 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12286 */
12287 if (enable_ept) {
12288 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12289 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12290 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12291 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12292 }
12293}
12294
12295/*
12296 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12297 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12298 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12299 * guest in a way that will both be appropriate to L1's requests, and our
12300 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12301 * function also has additional necessary side-effects, like setting various
12302 * vcpu->arch fields.
12303 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12304 * is assigned to entry_failure_code on failure.
12305 */
12306static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12307 u32 *entry_failure_code)
12308{
12309 struct vcpu_vmx *vmx = to_vmx(vcpu);
12310
12311 if (vmx->nested.dirty_vmcs12) {
12312 prepare_vmcs02_full(vmx, vmcs12);
12313 vmx->nested.dirty_vmcs12 = false;
12314 }
12315
12316 /*
12317 * First, the fields that are shadowed. This must be kept in sync
12318 * with vmx_shadow_fields.h.
12319 */
12320
12321 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12322 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12323 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12324 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12325 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12326
12327 if (vmx->nested.nested_run_pending &&
12328 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12329 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12330 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12331 } else {
12332 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12333 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12334 }
12335 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12336
12337 vmx->nested.preemption_timer_expired = false;
12338 if (nested_cpu_has_preemption_timer(vmcs12))
12339 vmx_start_preemption_timer(vcpu);
12340
12341 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12342 * bitwise-or of what L1 wants to trap for L2, and what we want to
12343 * trap. Note that CR0.TS also needs updating - we do this later.
12344 */
12345 update_exception_bitmap(vcpu);
12346 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12347 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
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_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012351 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012352 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012353 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012354 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012355 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012356
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012357 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12358
Peter Feinerc95ba922016-08-17 09:36:47 -070012359 if (kvm_has_tsc_control)
12360 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012361
12362 if (enable_vpid) {
12363 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012364 * There is no direct mapping between vpid02 and vpid12, the
12365 * vpid02 is per-vCPU for L0 and reused while the value of
12366 * vpid12 is changed w/ one invvpid during nested vmentry.
12367 * The vpid12 is allocated by L1 for L2, so it will not
12368 * influence global bitmap(for vpid01 and vpid02 allocation)
12369 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012370 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012371 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012372 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12373 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012374 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012375 }
12376 } else {
Liran Alon14389212018-10-08 23:42:17 +030012377 /*
12378 * If L1 use EPT, then L0 needs to execute INVEPT on
12379 * EPTP02 instead of EPTP01. Therefore, delay TLB
12380 * flush until vmcs02->eptp is fully updated by
12381 * KVM_REQ_LOAD_CR3. Note that this assumes
12382 * KVM_REQ_TLB_FLUSH is evaluated after
12383 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12384 */
12385 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012386 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012387 }
12388
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012389 if (nested_cpu_has_ept(vmcs12))
12390 nested_ept_init_mmu_context(vcpu);
12391 else if (nested_cpu_has2(vmcs12,
12392 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012393 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012394
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012395 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012396 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12397 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012398 * The CR0_READ_SHADOW is what L2 should have expected to read given
12399 * the specifications by L1; It's not enough to take
12400 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12401 * have more bits than L1 expected.
12402 */
12403 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12404 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12405
12406 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12407 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12408
Sean Christopherson09abe322018-09-26 09:23:50 -070012409 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012410 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012411 vmx_set_efer(vcpu, vcpu->arch.efer);
12412
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012413 /*
12414 * Guest state is invalid and unrestricted guest is disabled,
12415 * which means L1 attempted VMEntry to L2 with invalid state.
12416 * Fail the VMEntry.
12417 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012418 if (vmx->emulation_required) {
12419 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012420 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012421 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012422
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012423 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012424 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012425 entry_failure_code))
12426 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012427
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012428 if (!enable_ept)
12429 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12430
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012431 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12432 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012433 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012434}
12435
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012436static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12437{
12438 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12439 nested_cpu_has_virtual_nmis(vmcs12))
12440 return -EINVAL;
12441
12442 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12443 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12444 return -EINVAL;
12445
12446 return 0;
12447}
12448
Jim Mattsonca0bde22016-11-30 12:03:46 -080012449static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12450{
12451 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012452 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012453
12454 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12455 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12456 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12457
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012458 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12459 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12460
Jim Mattson56a20512017-07-06 16:33:06 -070012461 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12462 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12463
Jim Mattsonca0bde22016-11-30 12:03:46 -080012464 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12465 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12466
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012467 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12468 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12469
Jim Mattson712b12d2017-08-24 13:24:47 -070012470 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12471 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12472
Jim Mattsonca0bde22016-11-30 12:03:46 -080012473 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12474 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12475
12476 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12477 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12478
Bandan Dasc5f983f2017-05-05 15:25:14 -040012479 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12480 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12481
Liran Alona8a7c022018-06-23 02:35:06 +030012482 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484
Jim Mattsonca0bde22016-11-30 12:03:46 -080012485 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012486 vmx->nested.msrs.procbased_ctls_low,
12487 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012488 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12489 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012490 vmx->nested.msrs.secondary_ctls_low,
12491 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012492 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012493 vmx->nested.msrs.pinbased_ctls_low,
12494 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012495 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012496 vmx->nested.msrs.exit_ctls_low,
12497 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012498 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012499 vmx->nested.msrs.entry_ctls_low,
12500 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012501 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12502
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012503 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012504 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12505
Bandan Das41ab9372017-08-03 15:54:43 -040012506 if (nested_cpu_has_vmfunc(vmcs12)) {
12507 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012508 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012509 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12510
12511 if (nested_cpu_has_eptp_switching(vmcs12)) {
12512 if (!nested_cpu_has_ept(vmcs12) ||
12513 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12514 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12515 }
12516 }
Bandan Das27c42a12017-08-03 15:54:42 -040012517
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012518 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12519 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12520
Jim Mattsonca0bde22016-11-30 12:03:46 -080012521 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12522 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12523 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12524 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12525
Marc Orr04473782018-06-20 17:21:29 -070012526 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012527 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12528 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12529 * the values of the LMA and LME bits in the field must each be that of
12530 * the host address-space size VM-exit control.
12531 */
12532 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12533 ia32e = (vmcs12->vm_exit_controls &
12534 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12535 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12536 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12537 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12538 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12539 }
12540
12541 /*
Marc Orr04473782018-06-20 17:21:29 -070012542 * From the Intel SDM, volume 3:
12543 * Fields relevant to VM-entry event injection must be set properly.
12544 * These fields are the VM-entry interruption-information field, the
12545 * VM-entry exception error code, and the VM-entry instruction length.
12546 */
12547 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12548 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12549 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12550 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12551 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12552 bool should_have_error_code;
12553 bool urg = nested_cpu_has2(vmcs12,
12554 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12555 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12556
12557 /* VM-entry interruption-info field: interruption type */
12558 if (intr_type == INTR_TYPE_RESERVED ||
12559 (intr_type == INTR_TYPE_OTHER_EVENT &&
12560 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12561 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12562
12563 /* VM-entry interruption-info field: vector */
12564 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12565 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12566 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12567 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12568
12569 /* VM-entry interruption-info field: deliver error code */
12570 should_have_error_code =
12571 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12572 x86_exception_has_error_code(vector);
12573 if (has_error_code != should_have_error_code)
12574 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12575
12576 /* VM-entry exception error code */
12577 if (has_error_code &&
12578 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12579 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12580
12581 /* VM-entry interruption-info field: reserved bits */
12582 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12583 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12584
12585 /* VM-entry instruction length */
12586 switch (intr_type) {
12587 case INTR_TYPE_SOFT_EXCEPTION:
12588 case INTR_TYPE_SOFT_INTR:
12589 case INTR_TYPE_PRIV_SW_EXCEPTION:
12590 if ((vmcs12->vm_entry_instruction_len > 15) ||
12591 (vmcs12->vm_entry_instruction_len == 0 &&
12592 !nested_cpu_has_zero_length_injection(vcpu)))
12593 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12594 }
12595 }
12596
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012597 if (nested_cpu_has_ept(vmcs12) &&
12598 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12599 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12600
Jim Mattsonca0bde22016-11-30 12:03:46 -080012601 return 0;
12602}
12603
Liran Alonf145d902018-06-23 02:35:07 +030012604static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12605 struct vmcs12 *vmcs12)
12606{
12607 int r;
12608 struct page *page;
12609 struct vmcs12 *shadow;
12610
12611 if (vmcs12->vmcs_link_pointer == -1ull)
12612 return 0;
12613
12614 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12615 return -EINVAL;
12616
12617 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12618 if (is_error_page(page))
12619 return -EINVAL;
12620
12621 r = 0;
12622 shadow = kmap(page);
12623 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12624 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12625 r = -EINVAL;
12626 kunmap(page);
12627 kvm_release_page_clean(page);
12628 return r;
12629}
12630
Jim Mattsonca0bde22016-11-30 12:03:46 -080012631static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12632 u32 *exit_qual)
12633{
12634 bool ia32e;
12635
12636 *exit_qual = ENTRY_FAIL_DEFAULT;
12637
12638 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12639 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12640 return 1;
12641
Liran Alonf145d902018-06-23 02:35:07 +030012642 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012643 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12644 return 1;
12645 }
12646
12647 /*
12648 * If the load IA32_EFER VM-entry control is 1, the following checks
12649 * are performed on the field for the IA32_EFER MSR:
12650 * - Bits reserved in the IA32_EFER MSR must be 0.
12651 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12652 * the IA-32e mode guest VM-exit control. It must also be identical
12653 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12654 * CR0.PG) is 1.
12655 */
12656 if (to_vmx(vcpu)->nested.nested_run_pending &&
12657 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12658 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12659 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12660 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12661 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12662 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12663 return 1;
12664 }
12665
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012666 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12667 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12668 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12669 return 1;
12670
Jim Mattsonca0bde22016-11-30 12:03:46 -080012671 return 0;
12672}
12673
Sean Christophersona633e412018-09-26 09:23:47 -070012674static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12675 struct vmcs12 *vmcs12);
12676
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012677/*
Sean Christophersona633e412018-09-26 09:23:47 -070012678 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012679 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012680 */
Sean Christophersona633e412018-09-26 09:23:47 -070012681static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12682 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012683{
12684 struct vcpu_vmx *vmx = to_vmx(vcpu);
12685 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012686 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012687 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12688 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012689
Paolo Bonzini7e712682018-10-03 13:44:26 +020012690 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12691 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12692 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12693 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012694
Sean Christophersona633e412018-09-26 09:23:47 -070012695 if (from_vmentry && check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12696 goto vmentry_fail_vmexit;
Sean Christopherson7671ce22018-09-26 09:23:46 -070012697
Jim Mattson858e25c2016-11-30 12:03:47 -080012698 enter_guest_mode(vcpu);
12699
12700 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12701 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012702 if (kvm_mpx_supported() &&
12703 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12704 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012705
Jim Mattsonde3a0022017-11-27 17:22:25 -060012706 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012707
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012708 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12709 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12710
Sean Christopherson09abe322018-09-26 09:23:50 -070012711 prepare_vmcs02_early(vmx, vmcs12);
12712
Sean Christophersona633e412018-09-26 09:23:47 -070012713 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012714 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012715
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012716 if (from_vmentry) {
12717 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012718
Sean Christophersona633e412018-09-26 09:23:47 -070012719 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12720 exit_qual = nested_vmx_load_msr(vcpu,
12721 vmcs12->vm_entry_msr_load_addr,
12722 vmcs12->vm_entry_msr_load_count);
12723 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012724 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012725 } else {
12726 /*
12727 * The MMU is not initialized to point at the right entities yet and
12728 * "get pages" would need to read data from the guest (i.e. we will
12729 * need to perform gpa to hpa translation). Request a call
12730 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12731 * have already been set at vmentry time and should not be reset.
12732 */
12733 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12734 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012735
Jim Mattson858e25c2016-11-30 12:03:47 -080012736 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012737 * If L1 had a pending IRQ/NMI until it executed
12738 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12739 * disallowed (e.g. interrupts disabled), L0 needs to
12740 * evaluate if this pending event should cause an exit from L2
12741 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12742 * intercept EXTERNAL_INTERRUPT).
12743 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012744 * Usually this would be handled by the processor noticing an
12745 * IRQ/NMI window request, or checking RVI during evaluation of
12746 * pending virtual interrupts. However, this setting was done
12747 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12748 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012749 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012750 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012751 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012752
12753 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012754 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12755 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12756 * returned as far as L1 is concerned. It will only return (and set
12757 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12758 */
12759 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012760
Sean Christophersona633e412018-09-26 09:23:47 -070012761 /*
12762 * A failed consistency check that leads to a VMExit during L1's
12763 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12764 * 26.7 "VM-entry failures during or after loading guest state".
12765 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012766vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012767 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12768 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12769 leave_guest_mode(vcpu);
12770 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012771
12772 if (!from_vmentry)
12773 return 1;
12774
12775vmentry_fail_vmexit:
12776 load_vmcs12_host_state(vcpu, vmcs12);
12777 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12778 vmcs12->exit_qualification = exit_qual;
12779 nested_vmx_succeed(vcpu);
12780 if (enable_shadow_vmcs)
12781 vmx->nested.sync_shadow_vmcs = true;
12782 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012783}
12784
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012785/*
12786 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12787 * for running an L2 nested guest.
12788 */
12789static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12790{
12791 struct vmcs12 *vmcs12;
12792 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012793 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012794 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012795
Kyle Hueyeb277562016-11-29 12:40:39 -080012796 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012797 return 1;
12798
Kyle Hueyeb277562016-11-29 12:40:39 -080012799 if (!nested_vmx_check_vmcs12(vcpu))
12800 goto out;
12801
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012802 vmcs12 = get_vmcs12(vcpu);
12803
Liran Alona6192d42018-06-23 02:35:04 +030012804 /*
12805 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12806 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12807 * rather than RFLAGS.ZF, and no error number is stored to the
12808 * VM-instruction error field.
12809 */
12810 if (vmcs12->hdr.shadow_vmcs) {
12811 nested_vmx_failInvalid(vcpu);
12812 goto out;
12813 }
12814
Abel Gordon012f83c2013-04-18 14:39:25 +030012815 if (enable_shadow_vmcs)
12816 copy_shadow_to_vmcs12(vmx);
12817
Nadav Har'El7c177932011-05-25 23:12:04 +030012818 /*
12819 * The nested entry process starts with enforcing various prerequisites
12820 * on vmcs12 as required by the Intel SDM, and act appropriately when
12821 * they fail: As the SDM explains, some conditions should cause the
12822 * instruction to fail, while others will cause the instruction to seem
12823 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12824 * To speed up the normal (success) code path, we should avoid checking
12825 * for misconfigurations which will anyway be caught by the processor
12826 * when using the merged vmcs02.
12827 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012828 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12829 nested_vmx_failValid(vcpu,
12830 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12831 goto out;
12832 }
12833
Nadav Har'El7c177932011-05-25 23:12:04 +030012834 if (vmcs12->launch_state == launch) {
12835 nested_vmx_failValid(vcpu,
12836 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12837 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012838 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012839 }
12840
Jim Mattsonca0bde22016-11-30 12:03:46 -080012841 ret = check_vmentry_prereqs(vcpu, vmcs12);
12842 if (ret) {
12843 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012844 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012845 }
12846
Nadav Har'El7c177932011-05-25 23:12:04 +030012847 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012848 * After this point, the trap flag no longer triggers a singlestep trap
12849 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12850 * This is not 100% correct; for performance reasons, we delegate most
12851 * of the checks on host state to the processor. If those fail,
12852 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012853 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012854 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012855
Jan Kiszka384bb782013-04-20 10:52:36 +020012856 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012857 * We're finally done with prerequisite checking, and can start with
12858 * the nested entry.
12859 */
12860
Jim Mattson6514dc32018-04-26 16:09:12 -070012861 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012862 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Jim Mattson6514dc32018-04-26 16:09:12 -070012863 if (ret) {
12864 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012865 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012866 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012867
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012868 /* Hide L1D cache contents from the nested guest. */
12869 vmx->vcpu.arch.l1tf_flush_l1d = true;
12870
Chao Gao135a06c2018-02-11 10:06:30 +080012871 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012872 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012873 * also be used as part of restoring nVMX state for
12874 * snapshot restore (migration).
12875 *
12876 * In this flow, it is assumed that vmcs12 cache was
12877 * trasferred as part of captured nVMX state and should
12878 * therefore not be read from guest memory (which may not
12879 * exist on destination host yet).
12880 */
12881 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12882
12883 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012884 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12885 * by event injection, halt vcpu.
12886 */
12887 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012888 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12889 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012890 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012891 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012892 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012893
12894out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012895 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012896}
12897
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012898/*
12899 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12900 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12901 * This function returns the new value we should put in vmcs12.guest_cr0.
12902 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12903 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12904 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12905 * didn't trap the bit, because if L1 did, so would L0).
12906 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12907 * been modified by L2, and L1 knows it. So just leave the old value of
12908 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12909 * isn't relevant, because if L0 traps this bit it can set it to anything.
12910 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12911 * changed these bits, and therefore they need to be updated, but L0
12912 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12913 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12914 */
12915static inline unsigned long
12916vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12917{
12918 return
12919 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12920 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12921 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12922 vcpu->arch.cr0_guest_owned_bits));
12923}
12924
12925static inline unsigned long
12926vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12927{
12928 return
12929 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12930 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12931 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12932 vcpu->arch.cr4_guest_owned_bits));
12933}
12934
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012935static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12936 struct vmcs12 *vmcs12)
12937{
12938 u32 idt_vectoring;
12939 unsigned int nr;
12940
Wanpeng Li664f8e22017-08-24 03:35:09 -070012941 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012942 nr = vcpu->arch.exception.nr;
12943 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12944
12945 if (kvm_exception_is_soft(nr)) {
12946 vmcs12->vm_exit_instruction_len =
12947 vcpu->arch.event_exit_inst_len;
12948 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12949 } else
12950 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12951
12952 if (vcpu->arch.exception.has_error_code) {
12953 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12954 vmcs12->idt_vectoring_error_code =
12955 vcpu->arch.exception.error_code;
12956 }
12957
12958 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012959 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012960 vmcs12->idt_vectoring_info_field =
12961 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012962 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012963 nr = vcpu->arch.interrupt.nr;
12964 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12965
12966 if (vcpu->arch.interrupt.soft) {
12967 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12968 vmcs12->vm_entry_instruction_len =
12969 vcpu->arch.event_exit_inst_len;
12970 } else
12971 idt_vectoring |= INTR_TYPE_EXT_INTR;
12972
12973 vmcs12->idt_vectoring_info_field = idt_vectoring;
12974 }
12975}
12976
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012977static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12978{
12979 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012980 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012981 bool block_nested_events =
12982 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012983
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012984 if (vcpu->arch.exception.pending &&
12985 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012986 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012987 return -EBUSY;
12988 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012989 return 0;
12990 }
12991
Jan Kiszkaf4124502014-03-07 20:03:13 +010012992 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12993 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012994 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012995 return -EBUSY;
12996 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12997 return 0;
12998 }
12999
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013000 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013001 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013002 return -EBUSY;
13003 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13004 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13005 INTR_INFO_VALID_MASK, 0);
13006 /*
13007 * The NMI-triggered VM exit counts as injection:
13008 * clear this one and block further NMIs.
13009 */
13010 vcpu->arch.nmi_pending = 0;
13011 vmx_set_nmi_mask(vcpu, true);
13012 return 0;
13013 }
13014
13015 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13016 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013017 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013018 return -EBUSY;
13019 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013020 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013021 }
13022
David Hildenbrand6342c502017-01-25 11:58:58 +010013023 vmx_complete_nested_posted_interrupt(vcpu);
13024 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013025}
13026
Sean Christophersond264ee02018-08-27 15:21:12 -070013027static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13028{
13029 to_vmx(vcpu)->req_immediate_exit = true;
13030}
13031
Jan Kiszkaf4124502014-03-07 20:03:13 +010013032static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13033{
13034 ktime_t remaining =
13035 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13036 u64 value;
13037
13038 if (ktime_to_ns(remaining) <= 0)
13039 return 0;
13040
13041 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13042 do_div(value, 1000000);
13043 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13044}
13045
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013046/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013047 * Update the guest state fields of vmcs12 to reflect changes that
13048 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13049 * VM-entry controls is also updated, since this is really a guest
13050 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013051 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013052static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013053{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013054 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13055 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13056
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013057 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13058 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13059 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13060
13061 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13062 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13063 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13064 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13065 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13066 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13067 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13068 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13069 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13070 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13071 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13072 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13073 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13074 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13075 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13076 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13077 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13078 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13079 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13080 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13081 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13082 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13083 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13084 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13085 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13086 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13087 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13088 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13089 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13090 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13091 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13092 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13093 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13094 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13095 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13096 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13097
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013098 vmcs12->guest_interruptibility_info =
13099 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13100 vmcs12->guest_pending_dbg_exceptions =
13101 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013102 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13103 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13104 else
13105 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013106
Jan Kiszkaf4124502014-03-07 20:03:13 +010013107 if (nested_cpu_has_preemption_timer(vmcs12)) {
13108 if (vmcs12->vm_exit_controls &
13109 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13110 vmcs12->vmx_preemption_timer_value =
13111 vmx_get_preemption_timer_value(vcpu);
13112 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13113 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013114
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013115 /*
13116 * In some cases (usually, nested EPT), L2 is allowed to change its
13117 * own CR3 without exiting. If it has changed it, we must keep it.
13118 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13119 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13120 *
13121 * Additionally, restore L2's PDPTR to vmcs12.
13122 */
13123 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013124 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013125 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13126 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13127 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13128 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13129 }
13130
Jim Mattsond281e132017-06-01 12:44:46 -070013131 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013132
Wincy Van608406e2015-02-03 23:57:51 +080013133 if (nested_cpu_has_vid(vmcs12))
13134 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13135
Jan Kiszkac18911a2013-03-13 16:06:41 +010013136 vmcs12->vm_entry_controls =
13137 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013138 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013139
Jan Kiszka2996fca2014-06-16 13:59:43 +020013140 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13141 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13142 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13143 }
13144
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013145 /* TODO: These cannot have changed unless we have MSR bitmaps and
13146 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013147 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013148 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013149 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13150 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013151 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13152 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13153 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013154 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013155 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013156}
13157
13158/*
13159 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13160 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13161 * and this function updates it to reflect the changes to the guest state while
13162 * L2 was running (and perhaps made some exits which were handled directly by L0
13163 * without going back to L1), and to reflect the exit reason.
13164 * Note that we do not have to copy here all VMCS fields, just those that
13165 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13166 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13167 * which already writes to vmcs12 directly.
13168 */
13169static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13170 u32 exit_reason, u32 exit_intr_info,
13171 unsigned long exit_qualification)
13172{
13173 /* update guest state fields: */
13174 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013175
13176 /* update exit information fields: */
13177
Jan Kiszka533558b2014-01-04 18:47:20 +010013178 vmcs12->vm_exit_reason = exit_reason;
13179 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013180 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013181
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013182 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013183 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13184 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13185
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013186 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013187 vmcs12->launch_state = 1;
13188
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013189 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13190 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013191 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013192
13193 /*
13194 * Transfer the event that L0 or L1 may wanted to inject into
13195 * L2 to IDT_VECTORING_INFO_FIELD.
13196 */
13197 vmcs12_save_pending_event(vcpu, vmcs12);
13198 }
13199
13200 /*
13201 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13202 * preserved above and would only end up incorrectly in L1.
13203 */
13204 vcpu->arch.nmi_injected = false;
13205 kvm_clear_exception_queue(vcpu);
13206 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013207}
13208
13209/*
13210 * A part of what we need to when the nested L2 guest exits and we want to
13211 * run its L1 parent, is to reset L1's guest state to the host state specified
13212 * in vmcs12.
13213 * This function is to be called not only on normal nested exit, but also on
13214 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13215 * Failures During or After Loading Guest State").
13216 * This function should be called when the active VMCS is L1's (vmcs01).
13217 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013218static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13219 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013220{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013221 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013222 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013223
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013224 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13225 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013226 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013227 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13228 else
13229 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13230 vmx_set_efer(vcpu, vcpu->arch.efer);
13231
13232 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13233 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013234 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013235 /*
13236 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013237 * actually changed, because vmx_set_cr0 refers to efer set above.
13238 *
13239 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13240 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013241 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013242 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013243 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013244
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013245 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013246 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013247 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013248
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013249 nested_ept_uninit_mmu_context(vcpu);
13250
13251 /*
13252 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13253 * couldn't have changed.
13254 */
13255 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13256 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13257
13258 if (!enable_ept)
13259 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013260
Liran Alon6f1e03b2018-05-22 17:16:14 +030013261 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013262 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013263 * VMEntry/VMExit. Thus, no need to flush TLB.
13264 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013265 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13266 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013267 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013268 * Otherwise, we can preserve TLB entries as long as we are
13269 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013270 *
13271 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13272 * and therefore we request the TLB flush to happen only after VMCS EPTP
13273 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013274 */
13275 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013276 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013277 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013278 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013279
13280 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13281 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13282 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13283 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13284 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013285 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13286 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013287
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013288 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13289 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13290 vmcs_write64(GUEST_BNDCFGS, 0);
13291
Jan Kiszka44811c02013-08-04 17:17:27 +020013292 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013293 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013294 vcpu->arch.pat = vmcs12->host_ia32_pat;
13295 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013296 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13297 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13298 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013299
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013300 /* Set L1 segment info according to Intel SDM
13301 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13302 seg = (struct kvm_segment) {
13303 .base = 0,
13304 .limit = 0xFFFFFFFF,
13305 .selector = vmcs12->host_cs_selector,
13306 .type = 11,
13307 .present = 1,
13308 .s = 1,
13309 .g = 1
13310 };
13311 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13312 seg.l = 1;
13313 else
13314 seg.db = 1;
13315 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13316 seg = (struct kvm_segment) {
13317 .base = 0,
13318 .limit = 0xFFFFFFFF,
13319 .type = 3,
13320 .present = 1,
13321 .s = 1,
13322 .db = 1,
13323 .g = 1
13324 };
13325 seg.selector = vmcs12->host_ds_selector;
13326 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13327 seg.selector = vmcs12->host_es_selector;
13328 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13329 seg.selector = vmcs12->host_ss_selector;
13330 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13331 seg.selector = vmcs12->host_fs_selector;
13332 seg.base = vmcs12->host_fs_base;
13333 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13334 seg.selector = vmcs12->host_gs_selector;
13335 seg.base = vmcs12->host_gs_base;
13336 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13337 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013338 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013339 .limit = 0x67,
13340 .selector = vmcs12->host_tr_selector,
13341 .type = 11,
13342 .present = 1
13343 };
13344 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13345
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013346 kvm_set_dr(vcpu, 7, 0x400);
13347 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013348
Wincy Van3af18d92015-02-03 23:49:31 +080013349 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013350 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013351
Wincy Vanff651cb2014-12-11 08:52:58 +030013352 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13353 vmcs12->vm_exit_msr_load_count))
13354 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013355}
13356
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013357static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13358{
13359 struct shared_msr_entry *efer_msr;
13360 unsigned int i;
13361
13362 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13363 return vmcs_read64(GUEST_IA32_EFER);
13364
13365 if (cpu_has_load_ia32_efer)
13366 return host_efer;
13367
13368 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13369 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13370 return vmx->msr_autoload.guest.val[i].value;
13371 }
13372
13373 efer_msr = find_msr_entry(vmx, MSR_EFER);
13374 if (efer_msr)
13375 return efer_msr->data;
13376
13377 return host_efer;
13378}
13379
13380static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13381{
13382 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13383 struct vcpu_vmx *vmx = to_vmx(vcpu);
13384 struct vmx_msr_entry g, h;
13385 struct msr_data msr;
13386 gpa_t gpa;
13387 u32 i, j;
13388
13389 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13390
13391 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13392 /*
13393 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13394 * as vmcs01.GUEST_DR7 contains a userspace defined value
13395 * and vcpu->arch.dr7 is not squirreled away before the
13396 * nested VMENTER (not worth adding a variable in nested_vmx).
13397 */
13398 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13399 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13400 else
13401 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13402 }
13403
13404 /*
13405 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13406 * handle a variety of side effects to KVM's software model.
13407 */
13408 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13409
13410 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13411 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13412
13413 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13414 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13415
13416 nested_ept_uninit_mmu_context(vcpu);
13417 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13418 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13419
13420 /*
13421 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13422 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13423 * VMFail, like everything else we just need to ensure our
13424 * software model is up-to-date.
13425 */
13426 ept_save_pdptrs(vcpu);
13427
13428 kvm_mmu_reset_context(vcpu);
13429
13430 if (cpu_has_vmx_msr_bitmap())
13431 vmx_update_msr_bitmap(vcpu);
13432
13433 /*
13434 * This nasty bit of open coding is a compromise between blindly
13435 * loading L1's MSRs using the exit load lists (incorrect emulation
13436 * of VMFail), leaving the nested VM's MSRs in the software model
13437 * (incorrect behavior) and snapshotting the modified MSRs (too
13438 * expensive since the lists are unbound by hardware). For each
13439 * MSR that was (prematurely) loaded from the nested VMEntry load
13440 * list, reload it from the exit load list if it exists and differs
13441 * from the guest value. The intent is to stuff host state as
13442 * silently as possible, not to fully process the exit load list.
13443 */
13444 msr.host_initiated = false;
13445 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13446 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13447 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13448 pr_debug_ratelimited(
13449 "%s read MSR index failed (%u, 0x%08llx)\n",
13450 __func__, i, gpa);
13451 goto vmabort;
13452 }
13453
13454 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13455 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13456 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13457 pr_debug_ratelimited(
13458 "%s read MSR failed (%u, 0x%08llx)\n",
13459 __func__, j, gpa);
13460 goto vmabort;
13461 }
13462 if (h.index != g.index)
13463 continue;
13464 if (h.value == g.value)
13465 break;
13466
13467 if (nested_vmx_load_msr_check(vcpu, &h)) {
13468 pr_debug_ratelimited(
13469 "%s check failed (%u, 0x%x, 0x%x)\n",
13470 __func__, j, h.index, h.reserved);
13471 goto vmabort;
13472 }
13473
13474 msr.index = h.index;
13475 msr.data = h.value;
13476 if (kvm_set_msr(vcpu, &msr)) {
13477 pr_debug_ratelimited(
13478 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13479 __func__, j, h.index, h.value);
13480 goto vmabort;
13481 }
13482 }
13483 }
13484
13485 return;
13486
13487vmabort:
13488 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13489}
13490
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013491/*
13492 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13493 * and modify vmcs12 to make it see what it would expect to see there if
13494 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13495 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013496static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13497 u32 exit_intr_info,
13498 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013499{
13500 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013501 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13502
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013503 /* trying to cancel vmlaunch/vmresume is a bug */
13504 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13505
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013506 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013507 * The only expected VM-instruction error is "VM entry with
13508 * invalid control field(s)." Anything else indicates a
13509 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013510 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013511 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13512 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13513
13514 leave_guest_mode(vcpu);
13515
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013516 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13517 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13518
Jim Mattson4f350c62017-09-14 16:31:44 -070013519 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013520 if (exit_reason == -1)
13521 sync_vmcs12(vcpu, vmcs12);
13522 else
13523 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13524 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013525
Liran Alon61ada742018-06-23 02:35:08 +030013526 /*
13527 * Must happen outside of sync_vmcs12() as it will
13528 * also be used to capture vmcs12 cache as part of
13529 * capturing nVMX state for snapshot (migration).
13530 *
13531 * Otherwise, this flush will dirty guest memory at a
13532 * point it is already assumed by user-space to be
13533 * immutable.
13534 */
13535 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13536
Jim Mattson4f350c62017-09-14 16:31:44 -070013537 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13538 vmcs12->vm_exit_msr_store_count))
13539 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013540 }
13541
Jim Mattson4f350c62017-09-14 16:31:44 -070013542 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013543
Paolo Bonzini93140062016-07-06 13:23:51 +020013544 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013545 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13546 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013547 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013548
Peter Feinerc95ba922016-08-17 09:36:47 -070013549 if (kvm_has_tsc_control)
13550 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013551
Jim Mattson8d860bb2018-05-09 16:56:05 -040013552 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13553 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13554 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013555 } else if (!nested_cpu_has_ept(vmcs12) &&
13556 nested_cpu_has2(vmcs12,
13557 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013558 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013559 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013560
13561 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13562 vmx->host_rsp = 0;
13563
13564 /* Unpin physical memory we referred to in vmcs02 */
13565 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013566 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013567 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013568 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013569 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013570 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013571 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013572 }
Wincy Van705699a2015-02-03 23:58:17 +080013573 if (vmx->nested.pi_desc_page) {
13574 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013575 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013576 vmx->nested.pi_desc_page = NULL;
13577 vmx->nested.pi_desc = NULL;
13578 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013579
13580 /*
Tang Chen38b99172014-09-24 15:57:54 +080013581 * We are now running in L2, mmu_notifier will force to reload the
13582 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13583 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013584 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013585
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013586 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013587 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013588
13589 /* in case we halted in L2 */
13590 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013591
13592 if (likely(!vmx->fail)) {
13593 /*
13594 * TODO: SDM says that with acknowledge interrupt on
13595 * exit, bit 31 of the VM-exit interrupt information
13596 * (valid interrupt) is always set to 1 on
13597 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13598 * need kvm_cpu_has_interrupt(). See the commit
13599 * message for details.
13600 */
13601 if (nested_exit_intr_ack_set(vcpu) &&
13602 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13603 kvm_cpu_has_interrupt(vcpu)) {
13604 int irq = kvm_cpu_get_interrupt(vcpu);
13605 WARN_ON(irq < 0);
13606 vmcs12->vm_exit_intr_info = irq |
13607 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13608 }
13609
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013610 if (exit_reason != -1)
13611 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13612 vmcs12->exit_qualification,
13613 vmcs12->idt_vectoring_info_field,
13614 vmcs12->vm_exit_intr_info,
13615 vmcs12->vm_exit_intr_error_code,
13616 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013617
13618 load_vmcs12_host_state(vcpu, vmcs12);
13619
13620 return;
13621 }
13622
13623 /*
13624 * After an early L2 VM-entry failure, we're now back
13625 * in L1 which thinks it just finished a VMLAUNCH or
13626 * VMRESUME instruction, so we need to set the failure
13627 * flag and the VM-instruction error field of the VMCS
13628 * accordingly.
13629 */
13630 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013631
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013632 /*
13633 * Restore L1's host state to KVM's software model. We're here
13634 * because a consistency check was caught by hardware, which
13635 * means some amount of guest state has been propagated to KVM's
13636 * model and needs to be unwound to the host's state.
13637 */
13638 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013639
Jim Mattson4f350c62017-09-14 16:31:44 -070013640 /*
13641 * The emulated instruction was already skipped in
13642 * nested_vmx_run, but the updated RIP was never
13643 * written back to the vmcs01.
13644 */
13645 skip_emulated_instruction(vcpu);
13646 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013647}
13648
Nadav Har'El7c177932011-05-25 23:12:04 +030013649/*
Jan Kiszka42124922014-01-04 18:47:19 +010013650 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13651 */
13652static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13653{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013654 if (is_guest_mode(vcpu)) {
13655 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013656 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013657 }
Jan Kiszka42124922014-01-04 18:47:19 +010013658 free_nested(to_vmx(vcpu));
13659}
13660
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013661static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13662 struct x86_instruction_info *info,
13663 enum x86_intercept_stage stage)
13664{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013665 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13666 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13667
13668 /*
13669 * RDPID causes #UD if disabled through secondary execution controls.
13670 * Because it is marked as EmulateOnUD, we need to intercept it here.
13671 */
13672 if (info->intercept == x86_intercept_rdtscp &&
13673 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13674 ctxt->exception.vector = UD_VECTOR;
13675 ctxt->exception.error_code_valid = false;
13676 return X86EMUL_PROPAGATE_FAULT;
13677 }
13678
13679 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013680 return X86EMUL_CONTINUE;
13681}
13682
Yunhong Jiang64672c92016-06-13 14:19:59 -070013683#ifdef CONFIG_X86_64
13684/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13685static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13686 u64 divisor, u64 *result)
13687{
13688 u64 low = a << shift, high = a >> (64 - shift);
13689
13690 /* To avoid the overflow on divq */
13691 if (high >= divisor)
13692 return 1;
13693
13694 /* Low hold the result, high hold rem which is discarded */
13695 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13696 "rm" (divisor), "0" (low), "1" (high));
13697 *result = low;
13698
13699 return 0;
13700}
13701
13702static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13703{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013704 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013705 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013706
13707 if (kvm_mwait_in_guest(vcpu->kvm))
13708 return -EOPNOTSUPP;
13709
13710 vmx = to_vmx(vcpu);
13711 tscl = rdtsc();
13712 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13713 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013714 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13715
13716 if (delta_tsc > lapic_timer_advance_cycles)
13717 delta_tsc -= lapic_timer_advance_cycles;
13718 else
13719 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013720
13721 /* Convert to host delta tsc if tsc scaling is enabled */
13722 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13723 u64_shl_div_u64(delta_tsc,
13724 kvm_tsc_scaling_ratio_frac_bits,
13725 vcpu->arch.tsc_scaling_ratio,
13726 &delta_tsc))
13727 return -ERANGE;
13728
13729 /*
13730 * If the delta tsc can't fit in the 32 bit after the multi shift,
13731 * we can't use the preemption timer.
13732 * It's possible that it fits on later vmentries, but checking
13733 * on every vmentry is costly so we just use an hrtimer.
13734 */
13735 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13736 return -ERANGE;
13737
13738 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013739 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013740}
13741
13742static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13743{
Sean Christophersonf459a702018-08-27 15:21:11 -070013744 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013745}
13746#endif
13747
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013748static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013749{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013750 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013751 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013752}
13753
Kai Huang843e4332015-01-28 10:54:28 +080013754static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13755 struct kvm_memory_slot *slot)
13756{
13757 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13758 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13759}
13760
13761static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13762 struct kvm_memory_slot *slot)
13763{
13764 kvm_mmu_slot_set_dirty(kvm, slot);
13765}
13766
13767static void vmx_flush_log_dirty(struct kvm *kvm)
13768{
13769 kvm_flush_pml_buffers(kvm);
13770}
13771
Bandan Dasc5f983f2017-05-05 15:25:14 -040013772static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13773{
13774 struct vmcs12 *vmcs12;
13775 struct vcpu_vmx *vmx = to_vmx(vcpu);
13776 gpa_t gpa;
13777 struct page *page = NULL;
13778 u64 *pml_address;
13779
13780 if (is_guest_mode(vcpu)) {
13781 WARN_ON_ONCE(vmx->nested.pml_full);
13782
13783 /*
13784 * Check if PML is enabled for the nested guest.
13785 * Whether eptp bit 6 is set is already checked
13786 * as part of A/D emulation.
13787 */
13788 vmcs12 = get_vmcs12(vcpu);
13789 if (!nested_cpu_has_pml(vmcs12))
13790 return 0;
13791
Dan Carpenter47698862017-05-10 22:43:17 +030013792 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013793 vmx->nested.pml_full = true;
13794 return 1;
13795 }
13796
13797 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13798
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013799 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13800 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013801 return 0;
13802
13803 pml_address = kmap(page);
13804 pml_address[vmcs12->guest_pml_index--] = gpa;
13805 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013806 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013807 }
13808
13809 return 0;
13810}
13811
Kai Huang843e4332015-01-28 10:54:28 +080013812static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13813 struct kvm_memory_slot *memslot,
13814 gfn_t offset, unsigned long mask)
13815{
13816 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13817}
13818
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013819static void __pi_post_block(struct kvm_vcpu *vcpu)
13820{
13821 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13822 struct pi_desc old, new;
13823 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013824
13825 do {
13826 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013827 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13828 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013829
13830 dest = cpu_physical_id(vcpu->cpu);
13831
13832 if (x2apic_enabled())
13833 new.ndst = dest;
13834 else
13835 new.ndst = (dest << 8) & 0xFF00;
13836
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013837 /* set 'NV' to 'notification vector' */
13838 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013839 } while (cmpxchg64(&pi_desc->control, old.control,
13840 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013841
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013842 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13843 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013844 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013845 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013846 vcpu->pre_pcpu = -1;
13847 }
13848}
13849
Feng Wuefc64402015-09-18 22:29:51 +080013850/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013851 * This routine does the following things for vCPU which is going
13852 * to be blocked if VT-d PI is enabled.
13853 * - Store the vCPU to the wakeup list, so when interrupts happen
13854 * we can find the right vCPU to wake up.
13855 * - Change the Posted-interrupt descriptor as below:
13856 * 'NDST' <-- vcpu->pre_pcpu
13857 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13858 * - If 'ON' is set during this process, which means at least one
13859 * interrupt is posted for this vCPU, we cannot block it, in
13860 * this case, return 1, otherwise, return 0.
13861 *
13862 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013863static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013864{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013865 unsigned int dest;
13866 struct pi_desc old, new;
13867 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13868
13869 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013870 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13871 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013872 return 0;
13873
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013874 WARN_ON(irqs_disabled());
13875 local_irq_disable();
13876 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13877 vcpu->pre_pcpu = vcpu->cpu;
13878 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13879 list_add_tail(&vcpu->blocked_vcpu_list,
13880 &per_cpu(blocked_vcpu_on_cpu,
13881 vcpu->pre_pcpu));
13882 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13883 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013884
13885 do {
13886 old.control = new.control = pi_desc->control;
13887
Feng Wubf9f6ac2015-09-18 22:29:55 +080013888 WARN((pi_desc->sn == 1),
13889 "Warning: SN field of posted-interrupts "
13890 "is set before blocking\n");
13891
13892 /*
13893 * Since vCPU can be preempted during this process,
13894 * vcpu->cpu could be different with pre_pcpu, we
13895 * need to set pre_pcpu as the destination of wakeup
13896 * notification event, then we can find the right vCPU
13897 * to wakeup in wakeup handler if interrupts happen
13898 * when the vCPU is in blocked state.
13899 */
13900 dest = cpu_physical_id(vcpu->pre_pcpu);
13901
13902 if (x2apic_enabled())
13903 new.ndst = dest;
13904 else
13905 new.ndst = (dest << 8) & 0xFF00;
13906
13907 /* set 'NV' to 'wakeup vector' */
13908 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013909 } while (cmpxchg64(&pi_desc->control, old.control,
13910 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013911
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013912 /* We should not block the vCPU if an interrupt is posted for it. */
13913 if (pi_test_on(pi_desc) == 1)
13914 __pi_post_block(vcpu);
13915
13916 local_irq_enable();
13917 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013918}
13919
Yunhong Jiangbc225122016-06-13 14:19:58 -070013920static int vmx_pre_block(struct kvm_vcpu *vcpu)
13921{
13922 if (pi_pre_block(vcpu))
13923 return 1;
13924
Yunhong Jiang64672c92016-06-13 14:19:59 -070013925 if (kvm_lapic_hv_timer_in_use(vcpu))
13926 kvm_lapic_switch_to_sw_timer(vcpu);
13927
Yunhong Jiangbc225122016-06-13 14:19:58 -070013928 return 0;
13929}
13930
13931static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013932{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013933 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013934 return;
13935
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013936 WARN_ON(irqs_disabled());
13937 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013938 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013939 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013940}
13941
Yunhong Jiangbc225122016-06-13 14:19:58 -070013942static void vmx_post_block(struct kvm_vcpu *vcpu)
13943{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013944 if (kvm_x86_ops->set_hv_timer)
13945 kvm_lapic_switch_to_hv_timer(vcpu);
13946
Yunhong Jiangbc225122016-06-13 14:19:58 -070013947 pi_post_block(vcpu);
13948}
13949
Feng Wubf9f6ac2015-09-18 22:29:55 +080013950/*
Feng Wuefc64402015-09-18 22:29:51 +080013951 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13952 *
13953 * @kvm: kvm
13954 * @host_irq: host irq of the interrupt
13955 * @guest_irq: gsi of the interrupt
13956 * @set: set or unset PI
13957 * returns 0 on success, < 0 on failure
13958 */
13959static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13960 uint32_t guest_irq, bool set)
13961{
13962 struct kvm_kernel_irq_routing_entry *e;
13963 struct kvm_irq_routing_table *irq_rt;
13964 struct kvm_lapic_irq irq;
13965 struct kvm_vcpu *vcpu;
13966 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013967 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013968
13969 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013970 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13971 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013972 return 0;
13973
13974 idx = srcu_read_lock(&kvm->irq_srcu);
13975 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013976 if (guest_irq >= irq_rt->nr_rt_entries ||
13977 hlist_empty(&irq_rt->map[guest_irq])) {
13978 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13979 guest_irq, irq_rt->nr_rt_entries);
13980 goto out;
13981 }
Feng Wuefc64402015-09-18 22:29:51 +080013982
13983 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13984 if (e->type != KVM_IRQ_ROUTING_MSI)
13985 continue;
13986 /*
13987 * VT-d PI cannot support posting multicast/broadcast
13988 * interrupts to a vCPU, we still use interrupt remapping
13989 * for these kind of interrupts.
13990 *
13991 * For lowest-priority interrupts, we only support
13992 * those with single CPU as the destination, e.g. user
13993 * configures the interrupts via /proc/irq or uses
13994 * irqbalance to make the interrupts single-CPU.
13995 *
13996 * We will support full lowest-priority interrupt later.
13997 */
13998
Radim Krčmář371313132016-07-12 22:09:27 +020013999 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014000 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14001 /*
14002 * Make sure the IRTE is in remapped mode if
14003 * we don't handle it in posted mode.
14004 */
14005 ret = irq_set_vcpu_affinity(host_irq, NULL);
14006 if (ret < 0) {
14007 printk(KERN_INFO
14008 "failed to back to remapped mode, irq: %u\n",
14009 host_irq);
14010 goto out;
14011 }
14012
Feng Wuefc64402015-09-18 22:29:51 +080014013 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014014 }
Feng Wuefc64402015-09-18 22:29:51 +080014015
14016 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14017 vcpu_info.vector = irq.vector;
14018
hu huajun2698d822018-04-11 15:16:40 +080014019 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014020 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14021
14022 if (set)
14023 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014024 else
Feng Wuefc64402015-09-18 22:29:51 +080014025 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014026
14027 if (ret < 0) {
14028 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14029 __func__);
14030 goto out;
14031 }
14032 }
14033
14034 ret = 0;
14035out:
14036 srcu_read_unlock(&kvm->irq_srcu, idx);
14037 return ret;
14038}
14039
Ashok Rajc45dcc72016-06-22 14:59:56 +080014040static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14041{
14042 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14043 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14044 FEATURE_CONTROL_LMCE;
14045 else
14046 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14047 ~FEATURE_CONTROL_LMCE;
14048}
14049
Ladi Prosek72d7b372017-10-11 16:54:41 +020014050static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14051{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014052 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14053 if (to_vmx(vcpu)->nested.nested_run_pending)
14054 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014055 return 1;
14056}
14057
Ladi Prosek0234bf82017-10-11 16:54:40 +020014058static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14059{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014060 struct vcpu_vmx *vmx = to_vmx(vcpu);
14061
14062 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14063 if (vmx->nested.smm.guest_mode)
14064 nested_vmx_vmexit(vcpu, -1, 0, 0);
14065
14066 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14067 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014068 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014069 return 0;
14070}
14071
14072static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14073{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014074 struct vcpu_vmx *vmx = to_vmx(vcpu);
14075 int ret;
14076
14077 if (vmx->nested.smm.vmxon) {
14078 vmx->nested.vmxon = true;
14079 vmx->nested.smm.vmxon = false;
14080 }
14081
14082 if (vmx->nested.smm.guest_mode) {
14083 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014084 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014085 vcpu->arch.hflags |= HF_SMM_MASK;
14086 if (ret)
14087 return ret;
14088
14089 vmx->nested.smm.guest_mode = false;
14090 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014091 return 0;
14092}
14093
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014094static int enable_smi_window(struct kvm_vcpu *vcpu)
14095{
14096 return 0;
14097}
14098
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014099static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14100 struct kvm_nested_state __user *user_kvm_nested_state,
14101 u32 user_data_size)
14102{
14103 struct vcpu_vmx *vmx;
14104 struct vmcs12 *vmcs12;
14105 struct kvm_nested_state kvm_state = {
14106 .flags = 0,
14107 .format = 0,
14108 .size = sizeof(kvm_state),
14109 .vmx.vmxon_pa = -1ull,
14110 .vmx.vmcs_pa = -1ull,
14111 };
14112
14113 if (!vcpu)
14114 return kvm_state.size + 2 * VMCS12_SIZE;
14115
14116 vmx = to_vmx(vcpu);
14117 vmcs12 = get_vmcs12(vcpu);
14118 if (nested_vmx_allowed(vcpu) &&
14119 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14120 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14121 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14122
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014123 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014124 kvm_state.size += VMCS12_SIZE;
14125
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014126 if (is_guest_mode(vcpu) &&
14127 nested_cpu_has_shadow_vmcs(vmcs12) &&
14128 vmcs12->vmcs_link_pointer != -1ull)
14129 kvm_state.size += VMCS12_SIZE;
14130 }
14131
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014132 if (vmx->nested.smm.vmxon)
14133 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14134
14135 if (vmx->nested.smm.guest_mode)
14136 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14137
14138 if (is_guest_mode(vcpu)) {
14139 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14140
14141 if (vmx->nested.nested_run_pending)
14142 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14143 }
14144 }
14145
14146 if (user_data_size < kvm_state.size)
14147 goto out;
14148
14149 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14150 return -EFAULT;
14151
14152 if (vmx->nested.current_vmptr == -1ull)
14153 goto out;
14154
14155 /*
14156 * When running L2, the authoritative vmcs12 state is in the
14157 * vmcs02. When running L1, the authoritative vmcs12 state is
14158 * in the shadow vmcs linked to vmcs01, unless
14159 * sync_shadow_vmcs is set, in which case, the authoritative
14160 * vmcs12 state is in the vmcs12 already.
14161 */
14162 if (is_guest_mode(vcpu))
14163 sync_vmcs12(vcpu, vmcs12);
14164 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14165 copy_shadow_to_vmcs12(vmx);
14166
14167 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14168 return -EFAULT;
14169
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014170 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14171 vmcs12->vmcs_link_pointer != -1ull) {
14172 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14173 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14174 return -EFAULT;
14175 }
14176
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014177out:
14178 return kvm_state.size;
14179}
14180
14181static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14182 struct kvm_nested_state __user *user_kvm_nested_state,
14183 struct kvm_nested_state *kvm_state)
14184{
14185 struct vcpu_vmx *vmx = to_vmx(vcpu);
14186 struct vmcs12 *vmcs12;
14187 u32 exit_qual;
14188 int ret;
14189
14190 if (kvm_state->format != 0)
14191 return -EINVAL;
14192
14193 if (!nested_vmx_allowed(vcpu))
14194 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14195
14196 if (kvm_state->vmx.vmxon_pa == -1ull) {
14197 if (kvm_state->vmx.smm.flags)
14198 return -EINVAL;
14199
14200 if (kvm_state->vmx.vmcs_pa != -1ull)
14201 return -EINVAL;
14202
14203 vmx_leave_nested(vcpu);
14204 return 0;
14205 }
14206
14207 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14208 return -EINVAL;
14209
14210 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14211 return -EINVAL;
14212
14213 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14214 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14215 return -EINVAL;
14216
14217 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14218 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14219 return -EINVAL;
14220
14221 if (kvm_state->vmx.smm.flags &
14222 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14223 return -EINVAL;
14224
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014225 /*
14226 * SMM temporarily disables VMX, so we cannot be in guest mode,
14227 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14228 * must be zero.
14229 */
14230 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14231 return -EINVAL;
14232
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014233 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14234 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14235 return -EINVAL;
14236
14237 vmx_leave_nested(vcpu);
14238 if (kvm_state->vmx.vmxon_pa == -1ull)
14239 return 0;
14240
14241 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14242 ret = enter_vmx_operation(vcpu);
14243 if (ret)
14244 return ret;
14245
14246 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14247
14248 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14249 vmx->nested.smm.vmxon = true;
14250 vmx->nested.vmxon = false;
14251
14252 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14253 vmx->nested.smm.guest_mode = true;
14254 }
14255
14256 vmcs12 = get_vmcs12(vcpu);
14257 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14258 return -EFAULT;
14259
Liran Alon392b2f22018-06-23 02:35:01 +030014260 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014261 return -EINVAL;
14262
14263 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14264 return 0;
14265
14266 vmx->nested.nested_run_pending =
14267 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14268
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014269 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14270 vmcs12->vmcs_link_pointer != -1ull) {
14271 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14272 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14273 return -EINVAL;
14274
14275 if (copy_from_user(shadow_vmcs12,
14276 user_kvm_nested_state->data + VMCS12_SIZE,
14277 sizeof(*vmcs12)))
14278 return -EFAULT;
14279
14280 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14281 !shadow_vmcs12->hdr.shadow_vmcs)
14282 return -EINVAL;
14283 }
14284
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014285 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14286 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14287 return -EINVAL;
14288
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014289 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014290 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014291 if (ret)
14292 return -EINVAL;
14293
14294 return 0;
14295}
14296
Kees Cook404f6aa2016-08-08 16:29:06 -070014297static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014298 .cpu_has_kvm_support = cpu_has_kvm_support,
14299 .disabled_by_bios = vmx_disabled_by_bios,
14300 .hardware_setup = hardware_setup,
14301 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014302 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014303 .hardware_enable = hardware_enable,
14304 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014305 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014306 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014307
Wanpeng Lib31c1142018-03-12 04:53:04 -070014308 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014309 .vm_alloc = vmx_vm_alloc,
14310 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014311
Avi Kivity6aa8b732006-12-10 02:21:36 -080014312 .vcpu_create = vmx_create_vcpu,
14313 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014314 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014315
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014316 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014317 .vcpu_load = vmx_vcpu_load,
14318 .vcpu_put = vmx_vcpu_put,
14319
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014320 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014321 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014322 .get_msr = vmx_get_msr,
14323 .set_msr = vmx_set_msr,
14324 .get_segment_base = vmx_get_segment_base,
14325 .get_segment = vmx_get_segment,
14326 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014327 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014328 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014329 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014330 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014331 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014332 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014333 .set_cr3 = vmx_set_cr3,
14334 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014335 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014336 .get_idt = vmx_get_idt,
14337 .set_idt = vmx_set_idt,
14338 .get_gdt = vmx_get_gdt,
14339 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014340 .get_dr6 = vmx_get_dr6,
14341 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014342 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014343 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014344 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014345 .get_rflags = vmx_get_rflags,
14346 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014347
Avi Kivity6aa8b732006-12-10 02:21:36 -080014348 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014349 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014350
Avi Kivity6aa8b732006-12-10 02:21:36 -080014351 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014352 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014353 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014354 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14355 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014356 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014357 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014358 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014359 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014360 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014361 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014362 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014363 .get_nmi_mask = vmx_get_nmi_mask,
14364 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014365 .enable_nmi_window = enable_nmi_window,
14366 .enable_irq_window = enable_irq_window,
14367 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014368 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014369 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014370 .get_enable_apicv = vmx_get_enable_apicv,
14371 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014372 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014373 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014374 .hwapic_irr_update = vmx_hwapic_irr_update,
14375 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014376 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014377 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14378 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014379
Izik Eiduscbc94022007-10-25 00:29:55 +020014380 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014381 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014382 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014383 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014384
Avi Kivity586f9602010-11-18 13:09:54 +020014385 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014386
Sheng Yang17cc3932010-01-05 19:02:27 +080014387 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014388
14389 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014390
14391 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014392 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014393
14394 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014395
14396 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014397
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014398 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014399 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014400
14401 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014402
14403 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014404 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014405 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014406 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014407 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014408
14409 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014410 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014411
14412 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014413
14414 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14415 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14416 .flush_log_dirty = vmx_flush_log_dirty,
14417 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014418 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014419
Feng Wubf9f6ac2015-09-18 22:29:55 +080014420 .pre_block = vmx_pre_block,
14421 .post_block = vmx_post_block,
14422
Wei Huang25462f72015-06-19 15:45:05 +020014423 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014424
14425 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014426
14427#ifdef CONFIG_X86_64
14428 .set_hv_timer = vmx_set_hv_timer,
14429 .cancel_hv_timer = vmx_cancel_hv_timer,
14430#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014431
14432 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014433
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014434 .get_nested_state = vmx_get_nested_state,
14435 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014436 .get_vmcs12_pages = nested_get_vmcs12_pages,
14437
Ladi Prosek72d7b372017-10-11 16:54:41 +020014438 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014439 .pre_enter_smm = vmx_pre_enter_smm,
14440 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014441 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014442};
14443
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014444static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014445{
14446 if (vmx_l1d_flush_pages) {
14447 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14448 vmx_l1d_flush_pages = NULL;
14449 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014450 /* Restore state so sysfs ignores VMX */
14451 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014452}
14453
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014454static void vmx_exit(void)
14455{
14456#ifdef CONFIG_KEXEC_CORE
14457 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14458 synchronize_rcu();
14459#endif
14460
14461 kvm_exit();
14462
14463#if IS_ENABLED(CONFIG_HYPERV)
14464 if (static_branch_unlikely(&enable_evmcs)) {
14465 int cpu;
14466 struct hv_vp_assist_page *vp_ap;
14467 /*
14468 * Reset everything to support using non-enlightened VMCS
14469 * access later (e.g. when we reload the module with
14470 * enlightened_vmcs=0)
14471 */
14472 for_each_online_cpu(cpu) {
14473 vp_ap = hv_get_vp_assist_page(cpu);
14474
14475 if (!vp_ap)
14476 continue;
14477
14478 vp_ap->current_nested_vmcs = 0;
14479 vp_ap->enlighten_vmentry = 0;
14480 }
14481
14482 static_branch_disable(&enable_evmcs);
14483 }
14484#endif
14485 vmx_cleanup_l1d_flush();
14486}
14487module_exit(vmx_exit);
14488
Avi Kivity6aa8b732006-12-10 02:21:36 -080014489static int __init vmx_init(void)
14490{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014491 int r;
14492
14493#if IS_ENABLED(CONFIG_HYPERV)
14494 /*
14495 * Enlightened VMCS usage should be recommended and the host needs
14496 * to support eVMCS v1 or above. We can also disable eVMCS support
14497 * with module parameter.
14498 */
14499 if (enlightened_vmcs &&
14500 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14501 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14502 KVM_EVMCS_VERSION) {
14503 int cpu;
14504
14505 /* Check that we have assist pages on all online CPUs */
14506 for_each_online_cpu(cpu) {
14507 if (!hv_get_vp_assist_page(cpu)) {
14508 enlightened_vmcs = false;
14509 break;
14510 }
14511 }
14512
14513 if (enlightened_vmcs) {
14514 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14515 static_branch_enable(&enable_evmcs);
14516 }
14517 } else {
14518 enlightened_vmcs = false;
14519 }
14520#endif
14521
14522 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014523 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014524 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014525 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014526
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014527 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014528 * Must be called after kvm_init() so enable_ept is properly set
14529 * up. Hand the parameter mitigation value in which was stored in
14530 * the pre module init parser. If no parameter was given, it will
14531 * contain 'auto' which will be turned into the default 'cond'
14532 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014533 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014534 if (boot_cpu_has(X86_BUG_L1TF)) {
14535 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14536 if (r) {
14537 vmx_exit();
14538 return r;
14539 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014540 }
14541
Dave Young2965faa2015-09-09 15:38:55 -070014542#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014543 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14544 crash_vmclear_local_loaded_vmcss);
14545#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014546 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014547
He, Qingfdef3ad2007-04-30 09:45:24 +030014548 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014549}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014550module_init(vmx_init);