blob: 12b1a5dae767680fb4a1f9393400f8f213b7f18d [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040074static bool __read_mostly nosmt;
75module_param(nosmt, bool, S_IRUGO);
76
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Xudong Hao83c3a332012-05-28 19:33:35 +080093static bool __read_mostly enable_ept_ad_bits = 1;
94module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Yang Zhang5a717852013-04-11 19:25:16 +0800102static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Abel Gordonabc4fc52013-04-18 14:35:25 +0300105static bool __read_mostly enable_shadow_vmcs = 1;
106module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300107/*
108 * If nested=1, nested virtualization is supported, i.e., guests may use
109 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
110 * use VMX instructions.
111 */
Rusty Russell476bc002012-01-13 09:32:18 +1030112static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300113module_param(nested, bool, S_IRUGO);
114
Wanpeng Li20300092014-12-02 19:14:59 +0800115static u64 __read_mostly host_xss;
116
Kai Huang843e4332015-01-28 10:54:28 +0800117static bool __read_mostly enable_pml = 1;
118module_param_named(pml, enable_pml, bool, S_IRUGO);
119
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120#define MSR_TYPE_R 1
121#define MSR_TYPE_W 2
122#define MSR_TYPE_RW 3
123
124#define MSR_BITMAP_MODE_X2APIC 1
125#define MSR_BITMAP_MODE_X2APIC_APICV 2
126#define MSR_BITMAP_MODE_LM 4
127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Gleb Natapov50378782013-02-04 16:00:28 +0200137#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf41245002014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
193
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200195static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_always);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200196static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200198/* Storage for pre module init parameter parsing */
199static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200200
201static const struct {
202 const char *option;
203 enum vmx_l1d_flush_state cmd;
204} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200205 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200206 {"never", VMENTER_L1D_FLUSH_NEVER},
207 {"cond", VMENTER_L1D_FLUSH_COND},
208 {"always", VMENTER_L1D_FLUSH_ALWAYS},
209};
210
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200211#define L1D_CACHE_ORDER 4
212static void *vmx_l1d_flush_pages;
213
214static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
215{
216 struct page *page;
217
218 /* If set to 'auto' select 'cond' */
219 if (l1tf == VMENTER_L1D_FLUSH_AUTO)
220 l1tf = VMENTER_L1D_FLUSH_COND;
221
222 if (!enable_ept) {
223 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
224 return 0;
225 }
226
227 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
228 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
229 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
230 if (!page)
231 return -ENOMEM;
232 vmx_l1d_flush_pages = page_address(page);
233 }
234
235 l1tf_vmx_mitigation = l1tf;
236
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200237 if (l1tf == VMENTER_L1D_FLUSH_NEVER)
238 return 0;
239
240 static_branch_enable(&vmx_l1d_should_flush);
241 if (l1tf == VMENTER_L1D_FLUSH_ALWAYS)
242 static_branch_enable(&vmx_l1d_flush_always);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200243 return 0;
244}
245
246static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200247{
248 unsigned int i;
249
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200250 if (s) {
251 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
252 if (!strcmp(s, vmentry_l1d_param[i].option))
253 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200254 }
255 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200256 return -EINVAL;
257}
258
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200259static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
260{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200261 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200262
263 if (!boot_cpu_has(X86_BUG_L1TF))
264 return 0;
265
266 l1tf = vmentry_l1d_flush_parse(s);
267 if (l1tf < 0)
268 return l1tf;
269
270 /*
271 * Has vmx_init() run already? If not then this is the pre init
272 * parameter parsing. In that case just store the value and let
273 * vmx_init() do the proper setup after enable_ept has been
274 * established.
275 */
276 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
277 vmentry_l1d_flush_param = l1tf;
278 return 0;
279 }
280
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200281 mutex_lock(&vmx_l1d_flush_mutex);
282 ret = vmx_setup_l1d_flush(l1tf);
283 mutex_unlock(&vmx_l1d_flush_mutex);
284 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200285}
286
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200287static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
288{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200289 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200290}
291
292static const struct kernel_param_ops vmentry_l1d_flush_ops = {
293 .set = vmentry_l1d_flush_set,
294 .get = vmentry_l1d_flush_get,
295};
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200296module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, S_IRUGO);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200297
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700298struct kvm_vmx {
299 struct kvm kvm;
300
301 unsigned int tss_addr;
302 bool ept_identity_pagetable_done;
303 gpa_t ept_identity_map_addr;
304};
305
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200306#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300307
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400308struct vmcs {
309 u32 revision_id;
310 u32 abort;
311 char data[0];
312};
313
Nadav Har'Eld462b812011-05-24 15:26:10 +0300314/*
315 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
316 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
317 * loaded on this CPU (so we can clear them if the CPU goes down).
318 */
319struct loaded_vmcs {
320 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700321 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300322 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200323 bool launched;
324 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200325 unsigned long vmcs_host_cr3; /* May not match real cr3 */
326 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100327 /* Support for vnmi-less CPUs */
328 int soft_vnmi_blocked;
329 ktime_t entry_time;
330 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100331 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300332 struct list_head loaded_vmcss_on_cpu_link;
333};
334
Avi Kivity26bb0982009-09-07 11:14:12 +0300335struct shared_msr_entry {
336 unsigned index;
337 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200338 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300339};
340
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300341/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300342 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
343 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
344 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
345 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
346 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
347 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600348 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300349 * underlying hardware which will be used to run L2.
350 * This structure is packed to ensure that its layout is identical across
351 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700352 *
353 * IMPORTANT: Changing the layout of existing fields in this structure
354 * will break save/restore compatibility with older kvm releases. When
355 * adding new fields, either use space in the reserved padding* arrays
356 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300357 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300358typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300359struct __packed vmcs12 {
360 /* According to the Intel spec, a VMCS region must start with the
361 * following two fields. Then follow implementation-specific data.
362 */
363 u32 revision_id;
364 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300365
Nadav Har'El27d6c862011-05-25 23:06:59 +0300366 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
367 u32 padding[7]; /* room for future expansion */
368
Nadav Har'El22bd0352011-05-25 23:05:57 +0300369 u64 io_bitmap_a;
370 u64 io_bitmap_b;
371 u64 msr_bitmap;
372 u64 vm_exit_msr_store_addr;
373 u64 vm_exit_msr_load_addr;
374 u64 vm_entry_msr_load_addr;
375 u64 tsc_offset;
376 u64 virtual_apic_page_addr;
377 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800378 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300379 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800380 u64 eoi_exit_bitmap0;
381 u64 eoi_exit_bitmap1;
382 u64 eoi_exit_bitmap2;
383 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800384 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300385 u64 guest_physical_address;
386 u64 vmcs_link_pointer;
387 u64 guest_ia32_debugctl;
388 u64 guest_ia32_pat;
389 u64 guest_ia32_efer;
390 u64 guest_ia32_perf_global_ctrl;
391 u64 guest_pdptr0;
392 u64 guest_pdptr1;
393 u64 guest_pdptr2;
394 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100395 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300396 u64 host_ia32_pat;
397 u64 host_ia32_efer;
398 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700399 u64 vmread_bitmap;
400 u64 vmwrite_bitmap;
401 u64 vm_function_control;
402 u64 eptp_list_address;
403 u64 pml_address;
404 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300405 /*
406 * To allow migration of L1 (complete with its L2 guests) between
407 * machines of different natural widths (32 or 64 bit), we cannot have
408 * unsigned long fields with no explict size. We use u64 (aliased
409 * natural_width) instead. Luckily, x86 is little-endian.
410 */
411 natural_width cr0_guest_host_mask;
412 natural_width cr4_guest_host_mask;
413 natural_width cr0_read_shadow;
414 natural_width cr4_read_shadow;
415 natural_width cr3_target_value0;
416 natural_width cr3_target_value1;
417 natural_width cr3_target_value2;
418 natural_width cr3_target_value3;
419 natural_width exit_qualification;
420 natural_width guest_linear_address;
421 natural_width guest_cr0;
422 natural_width guest_cr3;
423 natural_width guest_cr4;
424 natural_width guest_es_base;
425 natural_width guest_cs_base;
426 natural_width guest_ss_base;
427 natural_width guest_ds_base;
428 natural_width guest_fs_base;
429 natural_width guest_gs_base;
430 natural_width guest_ldtr_base;
431 natural_width guest_tr_base;
432 natural_width guest_gdtr_base;
433 natural_width guest_idtr_base;
434 natural_width guest_dr7;
435 natural_width guest_rsp;
436 natural_width guest_rip;
437 natural_width guest_rflags;
438 natural_width guest_pending_dbg_exceptions;
439 natural_width guest_sysenter_esp;
440 natural_width guest_sysenter_eip;
441 natural_width host_cr0;
442 natural_width host_cr3;
443 natural_width host_cr4;
444 natural_width host_fs_base;
445 natural_width host_gs_base;
446 natural_width host_tr_base;
447 natural_width host_gdtr_base;
448 natural_width host_idtr_base;
449 natural_width host_ia32_sysenter_esp;
450 natural_width host_ia32_sysenter_eip;
451 natural_width host_rsp;
452 natural_width host_rip;
453 natural_width paddingl[8]; /* room for future expansion */
454 u32 pin_based_vm_exec_control;
455 u32 cpu_based_vm_exec_control;
456 u32 exception_bitmap;
457 u32 page_fault_error_code_mask;
458 u32 page_fault_error_code_match;
459 u32 cr3_target_count;
460 u32 vm_exit_controls;
461 u32 vm_exit_msr_store_count;
462 u32 vm_exit_msr_load_count;
463 u32 vm_entry_controls;
464 u32 vm_entry_msr_load_count;
465 u32 vm_entry_intr_info_field;
466 u32 vm_entry_exception_error_code;
467 u32 vm_entry_instruction_len;
468 u32 tpr_threshold;
469 u32 secondary_vm_exec_control;
470 u32 vm_instruction_error;
471 u32 vm_exit_reason;
472 u32 vm_exit_intr_info;
473 u32 vm_exit_intr_error_code;
474 u32 idt_vectoring_info_field;
475 u32 idt_vectoring_error_code;
476 u32 vm_exit_instruction_len;
477 u32 vmx_instruction_info;
478 u32 guest_es_limit;
479 u32 guest_cs_limit;
480 u32 guest_ss_limit;
481 u32 guest_ds_limit;
482 u32 guest_fs_limit;
483 u32 guest_gs_limit;
484 u32 guest_ldtr_limit;
485 u32 guest_tr_limit;
486 u32 guest_gdtr_limit;
487 u32 guest_idtr_limit;
488 u32 guest_es_ar_bytes;
489 u32 guest_cs_ar_bytes;
490 u32 guest_ss_ar_bytes;
491 u32 guest_ds_ar_bytes;
492 u32 guest_fs_ar_bytes;
493 u32 guest_gs_ar_bytes;
494 u32 guest_ldtr_ar_bytes;
495 u32 guest_tr_ar_bytes;
496 u32 guest_interruptibility_info;
497 u32 guest_activity_state;
498 u32 guest_sysenter_cs;
499 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100500 u32 vmx_preemption_timer_value;
501 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300502 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800503 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300504 u16 guest_es_selector;
505 u16 guest_cs_selector;
506 u16 guest_ss_selector;
507 u16 guest_ds_selector;
508 u16 guest_fs_selector;
509 u16 guest_gs_selector;
510 u16 guest_ldtr_selector;
511 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800512 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300513 u16 host_es_selector;
514 u16 host_cs_selector;
515 u16 host_ss_selector;
516 u16 host_ds_selector;
517 u16 host_fs_selector;
518 u16 host_gs_selector;
519 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700520 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300521};
522
523/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700524 * For save/restore compatibility, the vmcs12 field offsets must not change.
525 */
526#define CHECK_OFFSET(field, loc) \
527 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
528 "Offset of " #field " in struct vmcs12 has changed.")
529
530static inline void vmx_check_vmcs12_offsets(void) {
531 CHECK_OFFSET(revision_id, 0);
532 CHECK_OFFSET(abort, 4);
533 CHECK_OFFSET(launch_state, 8);
534 CHECK_OFFSET(io_bitmap_a, 40);
535 CHECK_OFFSET(io_bitmap_b, 48);
536 CHECK_OFFSET(msr_bitmap, 56);
537 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
538 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
539 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
540 CHECK_OFFSET(tsc_offset, 88);
541 CHECK_OFFSET(virtual_apic_page_addr, 96);
542 CHECK_OFFSET(apic_access_addr, 104);
543 CHECK_OFFSET(posted_intr_desc_addr, 112);
544 CHECK_OFFSET(ept_pointer, 120);
545 CHECK_OFFSET(eoi_exit_bitmap0, 128);
546 CHECK_OFFSET(eoi_exit_bitmap1, 136);
547 CHECK_OFFSET(eoi_exit_bitmap2, 144);
548 CHECK_OFFSET(eoi_exit_bitmap3, 152);
549 CHECK_OFFSET(xss_exit_bitmap, 160);
550 CHECK_OFFSET(guest_physical_address, 168);
551 CHECK_OFFSET(vmcs_link_pointer, 176);
552 CHECK_OFFSET(guest_ia32_debugctl, 184);
553 CHECK_OFFSET(guest_ia32_pat, 192);
554 CHECK_OFFSET(guest_ia32_efer, 200);
555 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
556 CHECK_OFFSET(guest_pdptr0, 216);
557 CHECK_OFFSET(guest_pdptr1, 224);
558 CHECK_OFFSET(guest_pdptr2, 232);
559 CHECK_OFFSET(guest_pdptr3, 240);
560 CHECK_OFFSET(guest_bndcfgs, 248);
561 CHECK_OFFSET(host_ia32_pat, 256);
562 CHECK_OFFSET(host_ia32_efer, 264);
563 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
564 CHECK_OFFSET(vmread_bitmap, 280);
565 CHECK_OFFSET(vmwrite_bitmap, 288);
566 CHECK_OFFSET(vm_function_control, 296);
567 CHECK_OFFSET(eptp_list_address, 304);
568 CHECK_OFFSET(pml_address, 312);
569 CHECK_OFFSET(cr0_guest_host_mask, 344);
570 CHECK_OFFSET(cr4_guest_host_mask, 352);
571 CHECK_OFFSET(cr0_read_shadow, 360);
572 CHECK_OFFSET(cr4_read_shadow, 368);
573 CHECK_OFFSET(cr3_target_value0, 376);
574 CHECK_OFFSET(cr3_target_value1, 384);
575 CHECK_OFFSET(cr3_target_value2, 392);
576 CHECK_OFFSET(cr3_target_value3, 400);
577 CHECK_OFFSET(exit_qualification, 408);
578 CHECK_OFFSET(guest_linear_address, 416);
579 CHECK_OFFSET(guest_cr0, 424);
580 CHECK_OFFSET(guest_cr3, 432);
581 CHECK_OFFSET(guest_cr4, 440);
582 CHECK_OFFSET(guest_es_base, 448);
583 CHECK_OFFSET(guest_cs_base, 456);
584 CHECK_OFFSET(guest_ss_base, 464);
585 CHECK_OFFSET(guest_ds_base, 472);
586 CHECK_OFFSET(guest_fs_base, 480);
587 CHECK_OFFSET(guest_gs_base, 488);
588 CHECK_OFFSET(guest_ldtr_base, 496);
589 CHECK_OFFSET(guest_tr_base, 504);
590 CHECK_OFFSET(guest_gdtr_base, 512);
591 CHECK_OFFSET(guest_idtr_base, 520);
592 CHECK_OFFSET(guest_dr7, 528);
593 CHECK_OFFSET(guest_rsp, 536);
594 CHECK_OFFSET(guest_rip, 544);
595 CHECK_OFFSET(guest_rflags, 552);
596 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
597 CHECK_OFFSET(guest_sysenter_esp, 568);
598 CHECK_OFFSET(guest_sysenter_eip, 576);
599 CHECK_OFFSET(host_cr0, 584);
600 CHECK_OFFSET(host_cr3, 592);
601 CHECK_OFFSET(host_cr4, 600);
602 CHECK_OFFSET(host_fs_base, 608);
603 CHECK_OFFSET(host_gs_base, 616);
604 CHECK_OFFSET(host_tr_base, 624);
605 CHECK_OFFSET(host_gdtr_base, 632);
606 CHECK_OFFSET(host_idtr_base, 640);
607 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
608 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
609 CHECK_OFFSET(host_rsp, 664);
610 CHECK_OFFSET(host_rip, 672);
611 CHECK_OFFSET(pin_based_vm_exec_control, 744);
612 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
613 CHECK_OFFSET(exception_bitmap, 752);
614 CHECK_OFFSET(page_fault_error_code_mask, 756);
615 CHECK_OFFSET(page_fault_error_code_match, 760);
616 CHECK_OFFSET(cr3_target_count, 764);
617 CHECK_OFFSET(vm_exit_controls, 768);
618 CHECK_OFFSET(vm_exit_msr_store_count, 772);
619 CHECK_OFFSET(vm_exit_msr_load_count, 776);
620 CHECK_OFFSET(vm_entry_controls, 780);
621 CHECK_OFFSET(vm_entry_msr_load_count, 784);
622 CHECK_OFFSET(vm_entry_intr_info_field, 788);
623 CHECK_OFFSET(vm_entry_exception_error_code, 792);
624 CHECK_OFFSET(vm_entry_instruction_len, 796);
625 CHECK_OFFSET(tpr_threshold, 800);
626 CHECK_OFFSET(secondary_vm_exec_control, 804);
627 CHECK_OFFSET(vm_instruction_error, 808);
628 CHECK_OFFSET(vm_exit_reason, 812);
629 CHECK_OFFSET(vm_exit_intr_info, 816);
630 CHECK_OFFSET(vm_exit_intr_error_code, 820);
631 CHECK_OFFSET(idt_vectoring_info_field, 824);
632 CHECK_OFFSET(idt_vectoring_error_code, 828);
633 CHECK_OFFSET(vm_exit_instruction_len, 832);
634 CHECK_OFFSET(vmx_instruction_info, 836);
635 CHECK_OFFSET(guest_es_limit, 840);
636 CHECK_OFFSET(guest_cs_limit, 844);
637 CHECK_OFFSET(guest_ss_limit, 848);
638 CHECK_OFFSET(guest_ds_limit, 852);
639 CHECK_OFFSET(guest_fs_limit, 856);
640 CHECK_OFFSET(guest_gs_limit, 860);
641 CHECK_OFFSET(guest_ldtr_limit, 864);
642 CHECK_OFFSET(guest_tr_limit, 868);
643 CHECK_OFFSET(guest_gdtr_limit, 872);
644 CHECK_OFFSET(guest_idtr_limit, 876);
645 CHECK_OFFSET(guest_es_ar_bytes, 880);
646 CHECK_OFFSET(guest_cs_ar_bytes, 884);
647 CHECK_OFFSET(guest_ss_ar_bytes, 888);
648 CHECK_OFFSET(guest_ds_ar_bytes, 892);
649 CHECK_OFFSET(guest_fs_ar_bytes, 896);
650 CHECK_OFFSET(guest_gs_ar_bytes, 900);
651 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
652 CHECK_OFFSET(guest_tr_ar_bytes, 908);
653 CHECK_OFFSET(guest_interruptibility_info, 912);
654 CHECK_OFFSET(guest_activity_state, 916);
655 CHECK_OFFSET(guest_sysenter_cs, 920);
656 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
657 CHECK_OFFSET(vmx_preemption_timer_value, 928);
658 CHECK_OFFSET(virtual_processor_id, 960);
659 CHECK_OFFSET(posted_intr_nv, 962);
660 CHECK_OFFSET(guest_es_selector, 964);
661 CHECK_OFFSET(guest_cs_selector, 966);
662 CHECK_OFFSET(guest_ss_selector, 968);
663 CHECK_OFFSET(guest_ds_selector, 970);
664 CHECK_OFFSET(guest_fs_selector, 972);
665 CHECK_OFFSET(guest_gs_selector, 974);
666 CHECK_OFFSET(guest_ldtr_selector, 976);
667 CHECK_OFFSET(guest_tr_selector, 978);
668 CHECK_OFFSET(guest_intr_status, 980);
669 CHECK_OFFSET(host_es_selector, 982);
670 CHECK_OFFSET(host_cs_selector, 984);
671 CHECK_OFFSET(host_ss_selector, 986);
672 CHECK_OFFSET(host_ds_selector, 988);
673 CHECK_OFFSET(host_fs_selector, 990);
674 CHECK_OFFSET(host_gs_selector, 992);
675 CHECK_OFFSET(host_tr_selector, 994);
676 CHECK_OFFSET(guest_pml_index, 996);
677}
678
679/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300680 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
681 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
682 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700683 *
684 * IMPORTANT: Changing this value will break save/restore compatibility with
685 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300686 */
687#define VMCS12_REVISION 0x11e57ed0
688
689/*
690 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
691 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
692 * current implementation, 4K are reserved to avoid future complications.
693 */
694#define VMCS12_SIZE 0x1000
695
696/*
Jim Mattson5b157062017-12-22 12:11:12 -0800697 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
698 * supported VMCS12 field encoding.
699 */
700#define VMCS12_MAX_FIELD_INDEX 0x17
701
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100702struct nested_vmx_msrs {
703 /*
704 * We only store the "true" versions of the VMX capability MSRs. We
705 * generate the "non-true" versions by setting the must-be-1 bits
706 * according to the SDM.
707 */
708 u32 procbased_ctls_low;
709 u32 procbased_ctls_high;
710 u32 secondary_ctls_low;
711 u32 secondary_ctls_high;
712 u32 pinbased_ctls_low;
713 u32 pinbased_ctls_high;
714 u32 exit_ctls_low;
715 u32 exit_ctls_high;
716 u32 entry_ctls_low;
717 u32 entry_ctls_high;
718 u32 misc_low;
719 u32 misc_high;
720 u32 ept_caps;
721 u32 vpid_caps;
722 u64 basic;
723 u64 cr0_fixed0;
724 u64 cr0_fixed1;
725 u64 cr4_fixed0;
726 u64 cr4_fixed1;
727 u64 vmcs_enum;
728 u64 vmfunc_controls;
729};
730
Jim Mattson5b157062017-12-22 12:11:12 -0800731/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300732 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
733 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
734 */
735struct nested_vmx {
736 /* Has the level1 guest done vmxon? */
737 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400738 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400739 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300740
741 /* The guest-physical address of the current VMCS L1 keeps for L2 */
742 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700743 /*
744 * Cache of the guest's VMCS, existing outside of guest memory.
745 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700746 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700747 */
748 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300749 /*
750 * Indicates if the shadow vmcs must be updated with the
751 * data hold by vmcs12
752 */
753 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100754 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300755
Jim Mattson8d860bb2018-05-09 16:56:05 -0400756 bool change_vmcs01_virtual_apic_mode;
757
Nadav Har'El644d7112011-05-25 23:12:35 +0300758 /* L2 must run next, and mustn't decide to exit to L1. */
759 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600760
761 struct loaded_vmcs vmcs02;
762
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300763 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600764 * Guest pages referred to in the vmcs02 with host-physical
765 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300766 */
767 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800768 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800769 struct page *pi_desc_page;
770 struct pi_desc *pi_desc;
771 bool pi_pending;
772 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100773
774 struct hrtimer preemption_timer;
775 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200776
777 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
778 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800779
Wanpeng Li5c614b32015-10-13 09:18:36 -0700780 u16 vpid02;
781 u16 last_vpid;
782
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100783 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200784
785 /* SMM related state */
786 struct {
787 /* in VMX operation on SMM entry? */
788 bool vmxon;
789 /* in guest mode on SMM entry? */
790 bool guest_mode;
791 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300792};
793
Yang Zhang01e439b2013-04-11 19:25:12 +0800794#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800795#define POSTED_INTR_SN 1
796
Yang Zhang01e439b2013-04-11 19:25:12 +0800797/* Posted-Interrupt Descriptor */
798struct pi_desc {
799 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800800 union {
801 struct {
802 /* bit 256 - Outstanding Notification */
803 u16 on : 1,
804 /* bit 257 - Suppress Notification */
805 sn : 1,
806 /* bit 271:258 - Reserved */
807 rsvd_1 : 14;
808 /* bit 279:272 - Notification Vector */
809 u8 nv;
810 /* bit 287:280 - Reserved */
811 u8 rsvd_2;
812 /* bit 319:288 - Notification Destination */
813 u32 ndst;
814 };
815 u64 control;
816 };
817 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800818} __aligned(64);
819
Yang Zhanga20ed542013-04-11 19:25:15 +0800820static bool pi_test_and_set_on(struct pi_desc *pi_desc)
821{
822 return test_and_set_bit(POSTED_INTR_ON,
823 (unsigned long *)&pi_desc->control);
824}
825
826static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
827{
828 return test_and_clear_bit(POSTED_INTR_ON,
829 (unsigned long *)&pi_desc->control);
830}
831
832static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
833{
834 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
835}
836
Feng Wuebbfc762015-09-18 22:29:46 +0800837static inline void pi_clear_sn(struct pi_desc *pi_desc)
838{
839 return clear_bit(POSTED_INTR_SN,
840 (unsigned long *)&pi_desc->control);
841}
842
843static inline void pi_set_sn(struct pi_desc *pi_desc)
844{
845 return set_bit(POSTED_INTR_SN,
846 (unsigned long *)&pi_desc->control);
847}
848
Paolo Bonziniad361092016-09-20 16:15:05 +0200849static inline void pi_clear_on(struct pi_desc *pi_desc)
850{
851 clear_bit(POSTED_INTR_ON,
852 (unsigned long *)&pi_desc->control);
853}
854
Feng Wuebbfc762015-09-18 22:29:46 +0800855static inline int pi_test_on(struct pi_desc *pi_desc)
856{
857 return test_bit(POSTED_INTR_ON,
858 (unsigned long *)&pi_desc->control);
859}
860
861static inline int pi_test_sn(struct pi_desc *pi_desc)
862{
863 return test_bit(POSTED_INTR_SN,
864 (unsigned long *)&pi_desc->control);
865}
866
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400867struct vmx_msrs {
868 unsigned int nr;
869 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
870};
871
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400872struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000873 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300874 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300875 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100876 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300877 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200878 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200879 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300880 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400881 int nmsrs;
882 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800883 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400884#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300885 u64 msr_host_kernel_gs_base;
886 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400887#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100888
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100889 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100890 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100891
Gleb Natapov2961e8762013-11-25 15:37:13 +0200892 u32 vm_entry_controls_shadow;
893 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200894 u32 secondary_exec_control;
895
Nadav Har'Eld462b812011-05-24 15:26:10 +0300896 /*
897 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
898 * non-nested (L1) guest, it always points to vmcs01. For a nested
899 * guest (L2), it points to a different VMCS.
900 */
901 struct loaded_vmcs vmcs01;
902 struct loaded_vmcs *loaded_vmcs;
903 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300904 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400905 struct vmx_msrs guest;
906 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300907 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400908 struct {
909 int loaded;
910 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300911#ifdef CONFIG_X86_64
912 u16 ds_sel, es_sel;
913#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200914 int gs_ldt_reload_needed;
915 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000916 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400917 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200918 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300919 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300920 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300921 struct kvm_segment segs[8];
922 } rmode;
923 struct {
924 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300925 struct kvm_save_segment {
926 u16 selector;
927 unsigned long base;
928 u32 limit;
929 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300930 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300931 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800932 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300933 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200934
Andi Kleena0861c02009-06-08 17:37:09 +0800935 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800936
Yang Zhang01e439b2013-04-11 19:25:12 +0800937 /* Posted interrupt descriptor */
938 struct pi_desc pi_desc;
939
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300940 /* Support for a guest hypervisor (nested VMX) */
941 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200942
943 /* Dynamic PLE window. */
944 int ple_window;
945 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800946
947 /* Support for PML */
948#define PML_ENTITY_NUM 512
949 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800950
Yunhong Jiang64672c92016-06-13 14:19:59 -0700951 /* apic deadline value in host tsc */
952 u64 hv_deadline_tsc;
953
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800954 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800955
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800956 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800957
Wanpeng Li74c55932017-11-29 01:31:20 -0800958 unsigned long host_debugctlmsr;
959
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800960 /*
961 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
962 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
963 * in msr_ia32_feature_control_valid_bits.
964 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800965 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800966 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400967};
968
Avi Kivity2fb92db2011-04-27 19:42:18 +0300969enum segment_cache_field {
970 SEG_FIELD_SEL = 0,
971 SEG_FIELD_BASE = 1,
972 SEG_FIELD_LIMIT = 2,
973 SEG_FIELD_AR = 3,
974
975 SEG_FIELD_NR = 4
976};
977
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700978static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
979{
980 return container_of(kvm, struct kvm_vmx, kvm);
981}
982
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400983static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
984{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000985 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986}
987
Feng Wuefc64402015-09-18 22:29:51 +0800988static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
989{
990 return &(to_vmx(vcpu)->pi_desc);
991}
992
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800993#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300994#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800995#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
996#define FIELD64(number, name) \
997 FIELD(number, name), \
998 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300999
Abel Gordon4607c2d2013-04-18 14:35:55 +03001000
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001001static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001002#define SHADOW_FIELD_RO(x) x,
1003#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001004};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001005static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001006 ARRAY_SIZE(shadow_read_only_fields);
1007
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001008static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001009#define SHADOW_FIELD_RW(x) x,
1010#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001011};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001012static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001013 ARRAY_SIZE(shadow_read_write_fields);
1014
Mathias Krause772e0312012-08-30 01:30:19 +02001015static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001016 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001017 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001018 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1019 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1020 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1021 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1022 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1023 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1024 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1025 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001026 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001027 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001028 FIELD(HOST_ES_SELECTOR, host_es_selector),
1029 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1030 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1031 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1032 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1033 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1034 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1035 FIELD64(IO_BITMAP_A, io_bitmap_a),
1036 FIELD64(IO_BITMAP_B, io_bitmap_b),
1037 FIELD64(MSR_BITMAP, msr_bitmap),
1038 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1039 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1040 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001041 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001042 FIELD64(TSC_OFFSET, tsc_offset),
1043 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1044 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001045 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001046 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001047 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001048 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1049 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1050 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1051 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001052 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001053 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1054 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001055 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001056 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1057 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1058 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1059 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1060 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1061 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1062 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1063 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1064 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1065 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001066 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001067 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1068 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1069 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1070 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1071 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1072 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1073 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1074 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1075 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1076 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1077 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1078 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1079 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1080 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1081 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1082 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1083 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1084 FIELD(TPR_THRESHOLD, tpr_threshold),
1085 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1086 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1087 FIELD(VM_EXIT_REASON, vm_exit_reason),
1088 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1089 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1090 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1091 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1092 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1093 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1094 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1095 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1096 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1097 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1098 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1099 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1100 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1101 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1102 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1103 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1104 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1105 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1106 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1107 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1108 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1109 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1110 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1111 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1112 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1113 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1114 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1115 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001116 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001117 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1118 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1119 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1120 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1121 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1122 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1123 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1124 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1125 FIELD(EXIT_QUALIFICATION, exit_qualification),
1126 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1127 FIELD(GUEST_CR0, guest_cr0),
1128 FIELD(GUEST_CR3, guest_cr3),
1129 FIELD(GUEST_CR4, guest_cr4),
1130 FIELD(GUEST_ES_BASE, guest_es_base),
1131 FIELD(GUEST_CS_BASE, guest_cs_base),
1132 FIELD(GUEST_SS_BASE, guest_ss_base),
1133 FIELD(GUEST_DS_BASE, guest_ds_base),
1134 FIELD(GUEST_FS_BASE, guest_fs_base),
1135 FIELD(GUEST_GS_BASE, guest_gs_base),
1136 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1137 FIELD(GUEST_TR_BASE, guest_tr_base),
1138 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1139 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1140 FIELD(GUEST_DR7, guest_dr7),
1141 FIELD(GUEST_RSP, guest_rsp),
1142 FIELD(GUEST_RIP, guest_rip),
1143 FIELD(GUEST_RFLAGS, guest_rflags),
1144 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1145 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1146 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1147 FIELD(HOST_CR0, host_cr0),
1148 FIELD(HOST_CR3, host_cr3),
1149 FIELD(HOST_CR4, host_cr4),
1150 FIELD(HOST_FS_BASE, host_fs_base),
1151 FIELD(HOST_GS_BASE, host_gs_base),
1152 FIELD(HOST_TR_BASE, host_tr_base),
1153 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1154 FIELD(HOST_IDTR_BASE, host_idtr_base),
1155 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1156 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1157 FIELD(HOST_RSP, host_rsp),
1158 FIELD(HOST_RIP, host_rip),
1159};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001160
1161static inline short vmcs_field_to_offset(unsigned long field)
1162{
Dan Williams085331d2018-01-31 17:47:03 -08001163 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1164 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001165 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001166
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001167 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001168 return -ENOENT;
1169
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001170 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001171 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001172 return -ENOENT;
1173
Linus Torvalds15303ba2018-02-10 13:16:35 -08001174 index = array_index_nospec(index, size);
1175 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001176 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001177 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001178 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001179}
1180
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001181static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1182{
David Matlack4f2777b2016-07-13 17:16:37 -07001183 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001184}
1185
Peter Feiner995f00a2017-06-30 17:26:32 -07001186static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001187static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001188static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001189static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001190static void vmx_set_segment(struct kvm_vcpu *vcpu,
1191 struct kvm_segment *var, int seg);
1192static void vmx_get_segment(struct kvm_vcpu *vcpu,
1193 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001194static bool guest_state_valid(struct kvm_vcpu *vcpu);
1195static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001196static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001197static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1198static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1199static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1200 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001201static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001202static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1203 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001204
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1206static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001207/*
1208 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1209 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1210 */
1211static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212
Feng Wubf9f6ac2015-09-18 22:29:55 +08001213/*
1214 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1215 * can find which vCPU should be waken up.
1216 */
1217static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1218static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1219
Radim Krčmář23611332016-09-29 22:41:33 +02001220enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001221 VMX_VMREAD_BITMAP,
1222 VMX_VMWRITE_BITMAP,
1223 VMX_BITMAP_NR
1224};
1225
1226static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1227
Radim Krčmář23611332016-09-29 22:41:33 +02001228#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1229#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001230
Avi Kivity110312c2010-12-21 12:54:20 +02001231static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001232static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001233
Sheng Yang2384d2b2008-01-17 15:14:33 +08001234static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1235static DEFINE_SPINLOCK(vmx_vpid_lock);
1236
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001237static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 int size;
1239 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001240 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001242 u32 pin_based_exec_ctrl;
1243 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001244 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001245 u32 vmexit_ctrl;
1246 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001247 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001248} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249
Hannes Ederefff9e52008-11-28 17:02:06 +01001250static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001251 u32 ept;
1252 u32 vpid;
1253} vmx_capability;
1254
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255#define VMX_SEGMENT_FIELD(seg) \
1256 [VCPU_SREG_##seg] = { \
1257 .selector = GUEST_##seg##_SELECTOR, \
1258 .base = GUEST_##seg##_BASE, \
1259 .limit = GUEST_##seg##_LIMIT, \
1260 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1261 }
1262
Mathias Krause772e0312012-08-30 01:30:19 +02001263static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 unsigned selector;
1265 unsigned base;
1266 unsigned limit;
1267 unsigned ar_bytes;
1268} kvm_vmx_segment_fields[] = {
1269 VMX_SEGMENT_FIELD(CS),
1270 VMX_SEGMENT_FIELD(DS),
1271 VMX_SEGMENT_FIELD(ES),
1272 VMX_SEGMENT_FIELD(FS),
1273 VMX_SEGMENT_FIELD(GS),
1274 VMX_SEGMENT_FIELD(SS),
1275 VMX_SEGMENT_FIELD(TR),
1276 VMX_SEGMENT_FIELD(LDTR),
1277};
1278
Avi Kivity26bb0982009-09-07 11:14:12 +03001279static u64 host_efer;
1280
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001281static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1282
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001283/*
Brian Gerst8c065852010-07-17 09:03:26 -04001284 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001285 * away by decrementing the array size.
1286 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001288#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001289 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001291 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001294DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1295
1296#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1297
1298#define KVM_EVMCS_VERSION 1
1299
1300#if IS_ENABLED(CONFIG_HYPERV)
1301static bool __read_mostly enlightened_vmcs = true;
1302module_param(enlightened_vmcs, bool, 0444);
1303
1304static inline void evmcs_write64(unsigned long field, u64 value)
1305{
1306 u16 clean_field;
1307 int offset = get_evmcs_offset(field, &clean_field);
1308
1309 if (offset < 0)
1310 return;
1311
1312 *(u64 *)((char *)current_evmcs + offset) = value;
1313
1314 current_evmcs->hv_clean_fields &= ~clean_field;
1315}
1316
1317static inline void evmcs_write32(unsigned long field, u32 value)
1318{
1319 u16 clean_field;
1320 int offset = get_evmcs_offset(field, &clean_field);
1321
1322 if (offset < 0)
1323 return;
1324
1325 *(u32 *)((char *)current_evmcs + offset) = value;
1326 current_evmcs->hv_clean_fields &= ~clean_field;
1327}
1328
1329static inline void evmcs_write16(unsigned long field, u16 value)
1330{
1331 u16 clean_field;
1332 int offset = get_evmcs_offset(field, &clean_field);
1333
1334 if (offset < 0)
1335 return;
1336
1337 *(u16 *)((char *)current_evmcs + offset) = value;
1338 current_evmcs->hv_clean_fields &= ~clean_field;
1339}
1340
1341static inline u64 evmcs_read64(unsigned long field)
1342{
1343 int offset = get_evmcs_offset(field, NULL);
1344
1345 if (offset < 0)
1346 return 0;
1347
1348 return *(u64 *)((char *)current_evmcs + offset);
1349}
1350
1351static inline u32 evmcs_read32(unsigned long field)
1352{
1353 int offset = get_evmcs_offset(field, NULL);
1354
1355 if (offset < 0)
1356 return 0;
1357
1358 return *(u32 *)((char *)current_evmcs + offset);
1359}
1360
1361static inline u16 evmcs_read16(unsigned long field)
1362{
1363 int offset = get_evmcs_offset(field, NULL);
1364
1365 if (offset < 0)
1366 return 0;
1367
1368 return *(u16 *)((char *)current_evmcs + offset);
1369}
1370
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001371static inline void evmcs_touch_msr_bitmap(void)
1372{
1373 if (unlikely(!current_evmcs))
1374 return;
1375
1376 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1377 current_evmcs->hv_clean_fields &=
1378 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1379}
1380
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001381static void evmcs_load(u64 phys_addr)
1382{
1383 struct hv_vp_assist_page *vp_ap =
1384 hv_get_vp_assist_page(smp_processor_id());
1385
1386 vp_ap->current_nested_vmcs = phys_addr;
1387 vp_ap->enlighten_vmentry = 1;
1388}
1389
1390static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1391{
1392 /*
1393 * Enlightened VMCSv1 doesn't support these:
1394 *
1395 * POSTED_INTR_NV = 0x00000002,
1396 * GUEST_INTR_STATUS = 0x00000810,
1397 * APIC_ACCESS_ADDR = 0x00002014,
1398 * POSTED_INTR_DESC_ADDR = 0x00002016,
1399 * EOI_EXIT_BITMAP0 = 0x0000201c,
1400 * EOI_EXIT_BITMAP1 = 0x0000201e,
1401 * EOI_EXIT_BITMAP2 = 0x00002020,
1402 * EOI_EXIT_BITMAP3 = 0x00002022,
1403 */
1404 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1405 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1406 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1407 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1408 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1409 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1410 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1411
1412 /*
1413 * GUEST_PML_INDEX = 0x00000812,
1414 * PML_ADDRESS = 0x0000200e,
1415 */
1416 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1417
1418 /* VM_FUNCTION_CONTROL = 0x00002018, */
1419 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1420
1421 /*
1422 * EPTP_LIST_ADDRESS = 0x00002024,
1423 * VMREAD_BITMAP = 0x00002026,
1424 * VMWRITE_BITMAP = 0x00002028,
1425 */
1426 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1427
1428 /*
1429 * TSC_MULTIPLIER = 0x00002032,
1430 */
1431 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1432
1433 /*
1434 * PLE_GAP = 0x00004020,
1435 * PLE_WINDOW = 0x00004022,
1436 */
1437 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1438
1439 /*
1440 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1441 */
1442 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1443
1444 /*
1445 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1446 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1447 */
1448 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1449 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1450
1451 /*
1452 * Currently unsupported in KVM:
1453 * GUEST_IA32_RTIT_CTL = 0x00002814,
1454 */
1455}
1456#else /* !IS_ENABLED(CONFIG_HYPERV) */
1457static inline void evmcs_write64(unsigned long field, u64 value) {}
1458static inline void evmcs_write32(unsigned long field, u32 value) {}
1459static inline void evmcs_write16(unsigned long field, u16 value) {}
1460static inline u64 evmcs_read64(unsigned long field) { return 0; }
1461static inline u32 evmcs_read32(unsigned long field) { return 0; }
1462static inline u16 evmcs_read16(unsigned long field) { return 0; }
1463static inline void evmcs_load(u64 phys_addr) {}
1464static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001465static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001466#endif /* IS_ENABLED(CONFIG_HYPERV) */
1467
Jan Kiszka5bb16012016-02-09 20:14:21 +01001468static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469{
1470 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1471 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001472 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1473}
1474
Jan Kiszka6f054852016-02-09 20:15:18 +01001475static inline bool is_debug(u32 intr_info)
1476{
1477 return is_exception_n(intr_info, DB_VECTOR);
1478}
1479
1480static inline bool is_breakpoint(u32 intr_info)
1481{
1482 return is_exception_n(intr_info, BP_VECTOR);
1483}
1484
Jan Kiszka5bb16012016-02-09 20:14:21 +01001485static inline bool is_page_fault(u32 intr_info)
1486{
1487 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
Gui Jianfeng31299942010-03-15 17:29:09 +08001490static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001491{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001492 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001493}
1494
Gui Jianfeng31299942010-03-15 17:29:09 +08001495static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001496{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001497 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001498}
1499
Liran Alon9e869482018-03-12 13:12:51 +02001500static inline bool is_gp_fault(u32 intr_info)
1501{
1502 return is_exception_n(intr_info, GP_VECTOR);
1503}
1504
Gui Jianfeng31299942010-03-15 17:29:09 +08001505static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
1507 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1508 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1509}
1510
Gui Jianfeng31299942010-03-15 17:29:09 +08001511static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001512{
1513 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1514 INTR_INFO_VALID_MASK)) ==
1515 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1516}
1517
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001518/* Undocumented: icebp/int1 */
1519static inline bool is_icebp(u32 intr_info)
1520{
1521 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1522 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1523}
1524
Gui Jianfeng31299942010-03-15 17:29:09 +08001525static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001526{
Sheng Yang04547152009-04-01 15:52:31 +08001527 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001528}
1529
Gui Jianfeng31299942010-03-15 17:29:09 +08001530static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001531{
Sheng Yang04547152009-04-01 15:52:31 +08001532 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001533}
1534
Paolo Bonzini35754c92015-07-29 12:05:37 +02001535static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001536{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001537 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001538}
1539
Gui Jianfeng31299942010-03-15 17:29:09 +08001540static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001541{
Sheng Yang04547152009-04-01 15:52:31 +08001542 return vmcs_config.cpu_based_exec_ctrl &
1543 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001544}
1545
Avi Kivity774ead32007-12-26 13:57:04 +02001546static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001547{
Sheng Yang04547152009-04-01 15:52:31 +08001548 return vmcs_config.cpu_based_2nd_exec_ctrl &
1549 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1550}
1551
Yang Zhang8d146952013-01-25 10:18:50 +08001552static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1553{
1554 return vmcs_config.cpu_based_2nd_exec_ctrl &
1555 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1556}
1557
Yang Zhang83d4c282013-01-25 10:18:49 +08001558static inline bool cpu_has_vmx_apic_register_virt(void)
1559{
1560 return vmcs_config.cpu_based_2nd_exec_ctrl &
1561 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1562}
1563
Yang Zhangc7c9c562013-01-25 10:18:51 +08001564static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1565{
1566 return vmcs_config.cpu_based_2nd_exec_ctrl &
1567 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1568}
1569
Yunhong Jiang64672c92016-06-13 14:19:59 -07001570/*
1571 * Comment's format: document - errata name - stepping - processor name.
1572 * Refer from
1573 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1574 */
1575static u32 vmx_preemption_cpu_tfms[] = {
1576/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15770x000206E6,
1578/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1579/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1580/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15810x00020652,
1582/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15830x00020655,
1584/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1585/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1586/*
1587 * 320767.pdf - AAP86 - B1 -
1588 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1589 */
15900x000106E5,
1591/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15920x000106A0,
1593/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15940x000106A1,
1595/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15960x000106A4,
1597 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1598 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1599 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
16000x000106A5,
1601};
1602
1603static inline bool cpu_has_broken_vmx_preemption_timer(void)
1604{
1605 u32 eax = cpuid_eax(0x00000001), i;
1606
1607 /* Clear the reserved bits */
1608 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001609 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001610 if (eax == vmx_preemption_cpu_tfms[i])
1611 return true;
1612
1613 return false;
1614}
1615
1616static inline bool cpu_has_vmx_preemption_timer(void)
1617{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001618 return vmcs_config.pin_based_exec_ctrl &
1619 PIN_BASED_VMX_PREEMPTION_TIMER;
1620}
1621
Yang Zhang01e439b2013-04-11 19:25:12 +08001622static inline bool cpu_has_vmx_posted_intr(void)
1623{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001624 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1625 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001626}
1627
1628static inline bool cpu_has_vmx_apicv(void)
1629{
1630 return cpu_has_vmx_apic_register_virt() &&
1631 cpu_has_vmx_virtual_intr_delivery() &&
1632 cpu_has_vmx_posted_intr();
1633}
1634
Sheng Yang04547152009-04-01 15:52:31 +08001635static inline bool cpu_has_vmx_flexpriority(void)
1636{
1637 return cpu_has_vmx_tpr_shadow() &&
1638 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001639}
1640
Marcelo Tosattie7997942009-06-11 12:07:40 -03001641static inline bool cpu_has_vmx_ept_execute_only(void)
1642{
Gui Jianfeng31299942010-03-15 17:29:09 +08001643 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001644}
1645
Marcelo Tosattie7997942009-06-11 12:07:40 -03001646static inline bool cpu_has_vmx_ept_2m_page(void)
1647{
Gui Jianfeng31299942010-03-15 17:29:09 +08001648 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001649}
1650
Sheng Yang878403b2010-01-05 19:02:29 +08001651static inline bool cpu_has_vmx_ept_1g_page(void)
1652{
Gui Jianfeng31299942010-03-15 17:29:09 +08001653 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001654}
1655
Sheng Yang4bc9b982010-06-02 14:05:24 +08001656static inline bool cpu_has_vmx_ept_4levels(void)
1657{
1658 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1659}
1660
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001661static inline bool cpu_has_vmx_ept_mt_wb(void)
1662{
1663 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1664}
1665
Yu Zhang855feb62017-08-24 20:27:55 +08001666static inline bool cpu_has_vmx_ept_5levels(void)
1667{
1668 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1669}
1670
Xudong Hao83c3a332012-05-28 19:33:35 +08001671static inline bool cpu_has_vmx_ept_ad_bits(void)
1672{
1673 return vmx_capability.ept & VMX_EPT_AD_BIT;
1674}
1675
Gui Jianfeng31299942010-03-15 17:29:09 +08001676static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001677{
Gui Jianfeng31299942010-03-15 17:29:09 +08001678 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001679}
1680
Gui Jianfeng31299942010-03-15 17:29:09 +08001681static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001682{
Gui Jianfeng31299942010-03-15 17:29:09 +08001683 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001684}
1685
Liran Aloncd9a4912018-05-22 17:16:15 +03001686static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1687{
1688 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1689}
1690
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001691static inline bool cpu_has_vmx_invvpid_single(void)
1692{
1693 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1694}
1695
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001696static inline bool cpu_has_vmx_invvpid_global(void)
1697{
1698 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1699}
1700
Wanpeng Li08d839c2017-03-23 05:30:08 -07001701static inline bool cpu_has_vmx_invvpid(void)
1702{
1703 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1704}
1705
Gui Jianfeng31299942010-03-15 17:29:09 +08001706static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001707{
Sheng Yang04547152009-04-01 15:52:31 +08001708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001710}
1711
Gui Jianfeng31299942010-03-15 17:29:09 +08001712static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001713{
1714 return vmcs_config.cpu_based_2nd_exec_ctrl &
1715 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1716}
1717
Gui Jianfeng31299942010-03-15 17:29:09 +08001718static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001719{
1720 return vmcs_config.cpu_based_2nd_exec_ctrl &
1721 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1722}
1723
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001724static inline bool cpu_has_vmx_basic_inout(void)
1725{
1726 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1727}
1728
Paolo Bonzini35754c92015-07-29 12:05:37 +02001729static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001730{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001731 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001732}
1733
Gui Jianfeng31299942010-03-15 17:29:09 +08001734static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001735{
Sheng Yang04547152009-04-01 15:52:31 +08001736 return vmcs_config.cpu_based_2nd_exec_ctrl &
1737 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001738}
1739
Gui Jianfeng31299942010-03-15 17:29:09 +08001740static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001741{
1742 return vmcs_config.cpu_based_2nd_exec_ctrl &
1743 SECONDARY_EXEC_RDTSCP;
1744}
1745
Mao, Junjiead756a12012-07-02 01:18:48 +00001746static inline bool cpu_has_vmx_invpcid(void)
1747{
1748 return vmcs_config.cpu_based_2nd_exec_ctrl &
1749 SECONDARY_EXEC_ENABLE_INVPCID;
1750}
1751
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001752static inline bool cpu_has_virtual_nmis(void)
1753{
1754 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1755}
1756
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001757static inline bool cpu_has_vmx_wbinvd_exit(void)
1758{
1759 return vmcs_config.cpu_based_2nd_exec_ctrl &
1760 SECONDARY_EXEC_WBINVD_EXITING;
1761}
1762
Abel Gordonabc4fc52013-04-18 14:35:25 +03001763static inline bool cpu_has_vmx_shadow_vmcs(void)
1764{
1765 u64 vmx_msr;
1766 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1767 /* check if the cpu supports writing r/o exit information fields */
1768 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1769 return false;
1770
1771 return vmcs_config.cpu_based_2nd_exec_ctrl &
1772 SECONDARY_EXEC_SHADOW_VMCS;
1773}
1774
Kai Huang843e4332015-01-28 10:54:28 +08001775static inline bool cpu_has_vmx_pml(void)
1776{
1777 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1778}
1779
Haozhong Zhang64903d62015-10-20 15:39:09 +08001780static inline bool cpu_has_vmx_tsc_scaling(void)
1781{
1782 return vmcs_config.cpu_based_2nd_exec_ctrl &
1783 SECONDARY_EXEC_TSC_SCALING;
1784}
1785
Bandan Das2a499e42017-08-03 15:54:41 -04001786static inline bool cpu_has_vmx_vmfunc(void)
1787{
1788 return vmcs_config.cpu_based_2nd_exec_ctrl &
1789 SECONDARY_EXEC_ENABLE_VMFUNC;
1790}
1791
Sean Christopherson64f7a112018-04-30 10:01:06 -07001792static bool vmx_umip_emulated(void)
1793{
1794 return vmcs_config.cpu_based_2nd_exec_ctrl &
1795 SECONDARY_EXEC_DESC;
1796}
1797
Sheng Yang04547152009-04-01 15:52:31 +08001798static inline bool report_flexpriority(void)
1799{
1800 return flexpriority_enabled;
1801}
1802
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001803static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1804{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001805 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001806}
1807
Jim Mattsonf4160e42018-05-29 09:11:33 -07001808/*
1809 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1810 * to modify any valid field of the VMCS, or are the VM-exit
1811 * information fields read-only?
1812 */
1813static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1814{
1815 return to_vmx(vcpu)->nested.msrs.misc_low &
1816 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1817}
1818
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001819static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1820{
1821 return vmcs12->cpu_based_vm_exec_control & bit;
1822}
1823
1824static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1825{
1826 return (vmcs12->cpu_based_vm_exec_control &
1827 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1828 (vmcs12->secondary_vm_exec_control & bit);
1829}
1830
Jan Kiszkaf41245002014-03-07 20:03:13 +01001831static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1832{
1833 return vmcs12->pin_based_vm_exec_control &
1834 PIN_BASED_VMX_PREEMPTION_TIMER;
1835}
1836
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001837static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1838{
1839 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1840}
1841
1842static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1843{
1844 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1845}
1846
Nadav Har'El155a97a2013-08-05 11:07:16 +03001847static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1848{
1849 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1850}
1851
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001852static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1853{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001854 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001855}
1856
Bandan Dasc5f983f2017-05-05 15:25:14 -04001857static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1858{
1859 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1860}
1861
Wincy Vanf2b93282015-02-03 23:56:03 +08001862static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1863{
1864 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1865}
1866
Wanpeng Li5c614b32015-10-13 09:18:36 -07001867static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1868{
1869 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1870}
1871
Wincy Van82f0dd42015-02-03 23:57:18 +08001872static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1873{
1874 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1875}
1876
Wincy Van608406e2015-02-03 23:57:51 +08001877static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1878{
1879 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1880}
1881
Wincy Van705699a2015-02-03 23:58:17 +08001882static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1883{
1884 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1885}
1886
Bandan Das27c42a12017-08-03 15:54:42 -04001887static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1888{
1889 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1890}
1891
Bandan Das41ab9372017-08-03 15:54:43 -04001892static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1893{
1894 return nested_cpu_has_vmfunc(vmcs12) &&
1895 (vmcs12->vm_function_control &
1896 VMX_VMFUNC_EPTP_SWITCHING);
1897}
1898
Jim Mattsonef85b672016-12-12 11:01:37 -08001899static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001900{
1901 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001902 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001903}
1904
Jan Kiszka533558b2014-01-04 18:47:20 +01001905static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1906 u32 exit_intr_info,
1907 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001908static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1909 struct vmcs12 *vmcs12,
1910 u32 reason, unsigned long qualification);
1911
Rusty Russell8b9cf982007-07-30 16:31:43 +10001912static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001913{
1914 int i;
1915
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001916 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001917 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001918 return i;
1919 return -1;
1920}
1921
Sheng Yang2384d2b2008-01-17 15:14:33 +08001922static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1923{
1924 struct {
1925 u64 vpid : 16;
1926 u64 rsvd : 48;
1927 u64 gva;
1928 } operand = { vpid, 0, gva };
1929
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001930 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001931 /* CF==1 or ZF==1 --> rc = -1 */
1932 "; ja 1f ; ud2 ; 1:"
1933 : : "a"(&operand), "c"(ext) : "cc", "memory");
1934}
1935
Sheng Yang14394422008-04-28 12:24:45 +08001936static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1937{
1938 struct {
1939 u64 eptp, gpa;
1940 } operand = {eptp, gpa};
1941
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001942 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001943 /* CF==1 or ZF==1 --> rc = -1 */
1944 "; ja 1f ; ud2 ; 1:\n"
1945 : : "a" (&operand), "c" (ext) : "cc", "memory");
1946}
1947
Avi Kivity26bb0982009-09-07 11:14:12 +03001948static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001949{
1950 int i;
1951
Rusty Russell8b9cf982007-07-30 16:31:43 +10001952 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001953 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001954 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001955 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001956}
1957
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958static void vmcs_clear(struct vmcs *vmcs)
1959{
1960 u64 phys_addr = __pa(vmcs);
1961 u8 error;
1962
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001963 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001964 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 : "cc", "memory");
1966 if (error)
1967 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1968 vmcs, phys_addr);
1969}
1970
Nadav Har'Eld462b812011-05-24 15:26:10 +03001971static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1972{
1973 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001974 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1975 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001976 loaded_vmcs->cpu = -1;
1977 loaded_vmcs->launched = 0;
1978}
1979
Dongxiao Xu7725b892010-05-11 18:29:38 +08001980static void vmcs_load(struct vmcs *vmcs)
1981{
1982 u64 phys_addr = __pa(vmcs);
1983 u8 error;
1984
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001985 if (static_branch_unlikely(&enable_evmcs))
1986 return evmcs_load(phys_addr);
1987
Dongxiao Xu7725b892010-05-11 18:29:38 +08001988 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001989 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001990 : "cc", "memory");
1991 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001992 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001993 vmcs, phys_addr);
1994}
1995
Dave Young2965faa2015-09-09 15:38:55 -07001996#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001997/*
1998 * This bitmap is used to indicate whether the vmclear
1999 * operation is enabled on all cpus. All disabled by
2000 * default.
2001 */
2002static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2003
2004static inline void crash_enable_local_vmclear(int cpu)
2005{
2006 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2007}
2008
2009static inline void crash_disable_local_vmclear(int cpu)
2010{
2011 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2012}
2013
2014static inline int crash_local_vmclear_enabled(int cpu)
2015{
2016 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2017}
2018
2019static void crash_vmclear_local_loaded_vmcss(void)
2020{
2021 int cpu = raw_smp_processor_id();
2022 struct loaded_vmcs *v;
2023
2024 if (!crash_local_vmclear_enabled(cpu))
2025 return;
2026
2027 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2028 loaded_vmcss_on_cpu_link)
2029 vmcs_clear(v->vmcs);
2030}
2031#else
2032static inline void crash_enable_local_vmclear(int cpu) { }
2033static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002034#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002035
Nadav Har'Eld462b812011-05-24 15:26:10 +03002036static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002038 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002039 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040
Nadav Har'Eld462b812011-05-24 15:26:10 +03002041 if (loaded_vmcs->cpu != cpu)
2042 return; /* vcpu migration can race with cpu offline */
2043 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002045 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002046 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002047
2048 /*
2049 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2050 * is before setting loaded_vmcs->vcpu to -1 which is done in
2051 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2052 * then adds the vmcs into percpu list before it is deleted.
2053 */
2054 smp_wmb();
2055
Nadav Har'Eld462b812011-05-24 15:26:10 +03002056 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002057 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058}
2059
Nadav Har'Eld462b812011-05-24 15:26:10 +03002060static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002061{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002062 int cpu = loaded_vmcs->cpu;
2063
2064 if (cpu != -1)
2065 smp_call_function_single(cpu,
2066 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002067}
2068
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002069static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002070{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002071 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002072 return;
2073
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002074 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002075 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002076}
2077
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002078static inline void vpid_sync_vcpu_global(void)
2079{
2080 if (cpu_has_vmx_invvpid_global())
2081 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2082}
2083
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002084static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002085{
2086 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002087 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002088 else
2089 vpid_sync_vcpu_global();
2090}
2091
Sheng Yang14394422008-04-28 12:24:45 +08002092static inline void ept_sync_global(void)
2093{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002094 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002095}
2096
2097static inline void ept_sync_context(u64 eptp)
2098{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002099 if (cpu_has_vmx_invept_context())
2100 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2101 else
2102 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002103}
2104
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002105static __always_inline void vmcs_check16(unsigned long field)
2106{
2107 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2108 "16-bit accessor invalid for 64-bit field");
2109 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2110 "16-bit accessor invalid for 64-bit high field");
2111 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2112 "16-bit accessor invalid for 32-bit high field");
2113 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2114 "16-bit accessor invalid for natural width field");
2115}
2116
2117static __always_inline void vmcs_check32(unsigned long field)
2118{
2119 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2120 "32-bit accessor invalid for 16-bit field");
2121 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2122 "32-bit accessor invalid for natural width field");
2123}
2124
2125static __always_inline void vmcs_check64(unsigned long field)
2126{
2127 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2128 "64-bit accessor invalid for 16-bit field");
2129 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2130 "64-bit accessor invalid for 64-bit high field");
2131 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2132 "64-bit accessor invalid for 32-bit field");
2133 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2134 "64-bit accessor invalid for natural width field");
2135}
2136
2137static __always_inline void vmcs_checkl(unsigned long field)
2138{
2139 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2140 "Natural width accessor invalid for 16-bit field");
2141 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2142 "Natural width accessor invalid for 64-bit field");
2143 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2144 "Natural width accessor invalid for 64-bit high field");
2145 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2146 "Natural width accessor invalid for 32-bit field");
2147}
2148
2149static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150{
Avi Kivity5e520e62011-05-15 10:13:12 -04002151 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152
Avi Kivity5e520e62011-05-15 10:13:12 -04002153 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2154 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 return value;
2156}
2157
Avi Kivity96304212011-05-15 10:13:13 -04002158static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002160 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002161 if (static_branch_unlikely(&enable_evmcs))
2162 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002163 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164}
2165
Avi Kivity96304212011-05-15 10:13:13 -04002166static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002168 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002169 if (static_branch_unlikely(&enable_evmcs))
2170 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002171 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172}
2173
Avi Kivity96304212011-05-15 10:13:13 -04002174static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002176 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002177 if (static_branch_unlikely(&enable_evmcs))
2178 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002179#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002180 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183#endif
2184}
2185
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002186static __always_inline unsigned long vmcs_readl(unsigned long field)
2187{
2188 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002189 if (static_branch_unlikely(&enable_evmcs))
2190 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002191 return __vmcs_readl(field);
2192}
2193
Avi Kivitye52de1b2007-01-05 16:36:56 -08002194static noinline void vmwrite_error(unsigned long field, unsigned long value)
2195{
2196 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2197 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2198 dump_stack();
2199}
2200
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002201static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202{
2203 u8 error;
2204
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002205 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002206 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002207 if (unlikely(error))
2208 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002211static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002213 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002214 if (static_branch_unlikely(&enable_evmcs))
2215 return evmcs_write16(field, value);
2216
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002217 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218}
2219
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002220static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002222 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002223 if (static_branch_unlikely(&enable_evmcs))
2224 return evmcs_write32(field, value);
2225
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002226 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227}
2228
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002229static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002231 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002232 if (static_branch_unlikely(&enable_evmcs))
2233 return evmcs_write64(field, value);
2234
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002235 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002236#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002238 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239#endif
2240}
2241
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002242static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002243{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002244 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002245 if (static_branch_unlikely(&enable_evmcs))
2246 return evmcs_write64(field, value);
2247
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002248 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002249}
2250
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002251static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002252{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002253 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2254 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002255 if (static_branch_unlikely(&enable_evmcs))
2256 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2257
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002258 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2259}
2260
2261static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2262{
2263 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2264 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002265 if (static_branch_unlikely(&enable_evmcs))
2266 return evmcs_write32(field, evmcs_read32(field) | mask);
2267
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002268 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002269}
2270
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002271static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2272{
2273 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2274}
2275
Gleb Natapov2961e8762013-11-25 15:37:13 +02002276static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2277{
2278 vmcs_write32(VM_ENTRY_CONTROLS, val);
2279 vmx->vm_entry_controls_shadow = val;
2280}
2281
2282static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2283{
2284 if (vmx->vm_entry_controls_shadow != val)
2285 vm_entry_controls_init(vmx, val);
2286}
2287
2288static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2289{
2290 return vmx->vm_entry_controls_shadow;
2291}
2292
2293
2294static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2295{
2296 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2297}
2298
2299static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2300{
2301 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2302}
2303
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002304static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2305{
2306 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2307}
2308
Gleb Natapov2961e8762013-11-25 15:37:13 +02002309static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2310{
2311 vmcs_write32(VM_EXIT_CONTROLS, val);
2312 vmx->vm_exit_controls_shadow = val;
2313}
2314
2315static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2316{
2317 if (vmx->vm_exit_controls_shadow != val)
2318 vm_exit_controls_init(vmx, val);
2319}
2320
2321static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2322{
2323 return vmx->vm_exit_controls_shadow;
2324}
2325
2326
2327static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2328{
2329 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2330}
2331
2332static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2333{
2334 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2335}
2336
Avi Kivity2fb92db2011-04-27 19:42:18 +03002337static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2338{
2339 vmx->segment_cache.bitmask = 0;
2340}
2341
2342static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2343 unsigned field)
2344{
2345 bool ret;
2346 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2347
2348 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2349 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2350 vmx->segment_cache.bitmask = 0;
2351 }
2352 ret = vmx->segment_cache.bitmask & mask;
2353 vmx->segment_cache.bitmask |= mask;
2354 return ret;
2355}
2356
2357static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2358{
2359 u16 *p = &vmx->segment_cache.seg[seg].selector;
2360
2361 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2362 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2363 return *p;
2364}
2365
2366static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2367{
2368 ulong *p = &vmx->segment_cache.seg[seg].base;
2369
2370 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2371 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2372 return *p;
2373}
2374
2375static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2376{
2377 u32 *p = &vmx->segment_cache.seg[seg].limit;
2378
2379 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2380 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2381 return *p;
2382}
2383
2384static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2385{
2386 u32 *p = &vmx->segment_cache.seg[seg].ar;
2387
2388 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2389 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2390 return *p;
2391}
2392
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002393static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2394{
2395 u32 eb;
2396
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002397 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002398 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002399 /*
2400 * Guest access to VMware backdoor ports could legitimately
2401 * trigger #GP because of TSS I/O permission bitmap.
2402 * We intercept those #GP and allow access to them anyway
2403 * as VMware does.
2404 */
2405 if (enable_vmware_backdoor)
2406 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002407 if ((vcpu->guest_debug &
2408 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2409 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2410 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002411 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002412 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002413 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002414 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002415
2416 /* When we are running a nested L2 guest and L1 specified for it a
2417 * certain exception bitmap, we must trap the same exceptions and pass
2418 * them to L1. When running L2, we will only handle the exceptions
2419 * specified above if L1 did not want them.
2420 */
2421 if (is_guest_mode(vcpu))
2422 eb |= get_vmcs12(vcpu)->exception_bitmap;
2423
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002424 vmcs_write32(EXCEPTION_BITMAP, eb);
2425}
2426
Ashok Raj15d45072018-02-01 22:59:43 +01002427/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002428 * Check if MSR is intercepted for currently loaded MSR bitmap.
2429 */
2430static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2431{
2432 unsigned long *msr_bitmap;
2433 int f = sizeof(unsigned long);
2434
2435 if (!cpu_has_vmx_msr_bitmap())
2436 return true;
2437
2438 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2439
2440 if (msr <= 0x1fff) {
2441 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2442 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2443 msr &= 0x1fff;
2444 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2445 }
2446
2447 return true;
2448}
2449
2450/*
Ashok Raj15d45072018-02-01 22:59:43 +01002451 * Check if MSR is intercepted for L01 MSR bitmap.
2452 */
2453static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2454{
2455 unsigned long *msr_bitmap;
2456 int f = sizeof(unsigned long);
2457
2458 if (!cpu_has_vmx_msr_bitmap())
2459 return true;
2460
2461 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2462
2463 if (msr <= 0x1fff) {
2464 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2465 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2466 msr &= 0x1fff;
2467 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2468 }
2469
2470 return true;
2471}
2472
Gleb Natapov2961e8762013-11-25 15:37:13 +02002473static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2474 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002475{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002476 vm_entry_controls_clearbit(vmx, entry);
2477 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002478}
2479
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002480static int find_msr(struct vmx_msrs *m, unsigned int msr)
2481{
2482 unsigned int i;
2483
2484 for (i = 0; i < m->nr; ++i) {
2485 if (m->val[i].index == msr)
2486 return i;
2487 }
2488 return -ENOENT;
2489}
2490
Avi Kivity61d2ef22010-04-28 16:40:38 +03002491static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2492{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002493 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002494 struct msr_autoload *m = &vmx->msr_autoload;
2495
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002496 switch (msr) {
2497 case MSR_EFER:
2498 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002499 clear_atomic_switch_msr_special(vmx,
2500 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002501 VM_EXIT_LOAD_IA32_EFER);
2502 return;
2503 }
2504 break;
2505 case MSR_CORE_PERF_GLOBAL_CTRL:
2506 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002507 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002508 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2509 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2510 return;
2511 }
2512 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002513 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002514 i = find_msr(&m->guest, msr);
2515 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002516 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002517 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002518 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002519 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002520
2521skip_guest:
2522 i = find_msr(&m->host, msr);
2523 if (i < 0)
2524 return;
2525
2526 --m->host.nr;
2527 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002528 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002529}
2530
Gleb Natapov2961e8762013-11-25 15:37:13 +02002531static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2532 unsigned long entry, unsigned long exit,
2533 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2534 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002535{
2536 vmcs_write64(guest_val_vmcs, guest_val);
2537 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002538 vm_entry_controls_setbit(vmx, entry);
2539 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002540}
2541
Avi Kivity61d2ef22010-04-28 16:40:38 +03002542static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002543 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002544{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002545 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002546 struct msr_autoload *m = &vmx->msr_autoload;
2547
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002548 switch (msr) {
2549 case MSR_EFER:
2550 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002551 add_atomic_switch_msr_special(vmx,
2552 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002553 VM_EXIT_LOAD_IA32_EFER,
2554 GUEST_IA32_EFER,
2555 HOST_IA32_EFER,
2556 guest_val, host_val);
2557 return;
2558 }
2559 break;
2560 case MSR_CORE_PERF_GLOBAL_CTRL:
2561 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002562 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002563 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2564 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2565 GUEST_IA32_PERF_GLOBAL_CTRL,
2566 HOST_IA32_PERF_GLOBAL_CTRL,
2567 guest_val, host_val);
2568 return;
2569 }
2570 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002571 case MSR_IA32_PEBS_ENABLE:
2572 /* PEBS needs a quiescent period after being disabled (to write
2573 * a record). Disabling PEBS through VMX MSR swapping doesn't
2574 * provide that period, so a CPU could write host's record into
2575 * guest's memory.
2576 */
2577 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002578 }
2579
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002580 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002581 if (!entry_only)
2582 j = find_msr(&m->host, msr);
2583
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002584 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002585 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002586 "Can't add msr %x\n", msr);
2587 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002588 }
2589 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002590 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002591 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002592 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002593 m->guest.val[i].index = msr;
2594 m->guest.val[i].value = guest_val;
2595
2596 if (entry_only)
2597 return;
2598
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002599 if (j < 0) {
2600 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002601 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002602 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002603 m->host.val[j].index = msr;
2604 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002605}
2606
Avi Kivity92c0d902009-10-29 11:00:16 +02002607static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002608{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002609 u64 guest_efer = vmx->vcpu.arch.efer;
2610 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002611
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002612 if (!enable_ept) {
2613 /*
2614 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2615 * host CPUID is more efficient than testing guest CPUID
2616 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2617 */
2618 if (boot_cpu_has(X86_FEATURE_SMEP))
2619 guest_efer |= EFER_NX;
2620 else if (!(guest_efer & EFER_NX))
2621 ignore_bits |= EFER_NX;
2622 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002623
Avi Kivity51c6cf62007-08-29 03:48:05 +03002624 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002625 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002626 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002627 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002628#ifdef CONFIG_X86_64
2629 ignore_bits |= EFER_LMA | EFER_LME;
2630 /* SCE is meaningful only in long mode on Intel */
2631 if (guest_efer & EFER_LMA)
2632 ignore_bits &= ~(u64)EFER_SCE;
2633#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002634
2635 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002636
2637 /*
2638 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2639 * On CPUs that support "load IA32_EFER", always switch EFER
2640 * atomically, since it's faster than switching it manually.
2641 */
2642 if (cpu_has_load_ia32_efer ||
2643 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002644 if (!(guest_efer & EFER_LMA))
2645 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002646 if (guest_efer != host_efer)
2647 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002648 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002649 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002650 } else {
2651 guest_efer &= ~ignore_bits;
2652 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002653
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002654 vmx->guest_msrs[efer_offset].data = guest_efer;
2655 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2656
2657 return true;
2658 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002659}
2660
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002661#ifdef CONFIG_X86_32
2662/*
2663 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2664 * VMCS rather than the segment table. KVM uses this helper to figure
2665 * out the current bases to poke them into the VMCS before entry.
2666 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002667static unsigned long segment_base(u16 selector)
2668{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002669 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002670 unsigned long v;
2671
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002672 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002673 return 0;
2674
Thomas Garnier45fc8752017-03-14 10:05:08 -07002675 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002676
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002677 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002678 u16 ldt_selector = kvm_read_ldt();
2679
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002680 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002681 return 0;
2682
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002683 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002684 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002685 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002686 return v;
2687}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002688#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002689
Avi Kivity04d2cc72007-09-10 18:10:54 +03002690static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002691{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002692 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002693#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002694 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002695#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002696 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002697
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002698 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002699 return;
2700
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002701 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002702 /*
2703 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2704 * allow segment selectors with cpl > 0 or ti == 1.
2705 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002706 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002707 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002708
2709#ifdef CONFIG_X86_64
2710 save_fsgs_for_kvm();
2711 vmx->host_state.fs_sel = current->thread.fsindex;
2712 vmx->host_state.gs_sel = current->thread.gsindex;
2713#else
Avi Kivity9581d442010-10-19 16:46:55 +02002714 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002715 savesegment(gs, vmx->host_state.gs_sel);
2716#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002717 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002718 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002719 vmx->host_state.fs_reload_needed = 0;
2720 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002721 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002722 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002723 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002724 if (!(vmx->host_state.gs_sel & 7))
2725 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002726 else {
2727 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002728 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002729 }
2730
2731#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002732 savesegment(ds, vmx->host_state.ds_sel);
2733 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002734
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002735 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002736 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002737
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002738 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002739 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002740 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002741#else
2742 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2743 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2744#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002745 if (boot_cpu_has(X86_FEATURE_MPX))
2746 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002747 for (i = 0; i < vmx->save_nmsrs; ++i)
2748 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002749 vmx->guest_msrs[i].data,
2750 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002751}
2752
Avi Kivitya9b21b62008-06-24 11:48:49 +03002753static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002754{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002755 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002756 return;
2757
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002758 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002759 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002760#ifdef CONFIG_X86_64
2761 if (is_long_mode(&vmx->vcpu))
2762 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2763#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002764 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002765 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002766#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002767 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002768#else
2769 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002770#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002771 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002772 if (vmx->host_state.fs_reload_needed)
2773 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002774#ifdef CONFIG_X86_64
2775 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2776 loadsegment(ds, vmx->host_state.ds_sel);
2777 loadsegment(es, vmx->host_state.es_sel);
2778 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002779#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002780 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002781#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002782 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002783#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002784 if (vmx->host_state.msr_host_bndcfgs)
2785 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002786 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002787}
2788
Avi Kivitya9b21b62008-06-24 11:48:49 +03002789static void vmx_load_host_state(struct vcpu_vmx *vmx)
2790{
2791 preempt_disable();
2792 __vmx_load_host_state(vmx);
2793 preempt_enable();
2794}
2795
Feng Wu28b835d2015-09-18 22:29:54 +08002796static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2797{
2798 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2799 struct pi_desc old, new;
2800 unsigned int dest;
2801
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002802 /*
2803 * In case of hot-plug or hot-unplug, we may have to undo
2804 * vmx_vcpu_pi_put even if there is no assigned device. And we
2805 * always keep PI.NDST up to date for simplicity: it makes the
2806 * code easier, and CPU migration is not a fast path.
2807 */
2808 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002809 return;
2810
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002811 /*
2812 * First handle the simple case where no cmpxchg is necessary; just
2813 * allow posting non-urgent interrupts.
2814 *
2815 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2816 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2817 * expects the VCPU to be on the blocked_vcpu_list that matches
2818 * PI.NDST.
2819 */
2820 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2821 vcpu->cpu == cpu) {
2822 pi_clear_sn(pi_desc);
2823 return;
2824 }
2825
2826 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002827 do {
2828 old.control = new.control = pi_desc->control;
2829
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002830 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002831
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002832 if (x2apic_enabled())
2833 new.ndst = dest;
2834 else
2835 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002836
Feng Wu28b835d2015-09-18 22:29:54 +08002837 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002838 } while (cmpxchg64(&pi_desc->control, old.control,
2839 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002840}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002841
Peter Feinerc95ba922016-08-17 09:36:47 -07002842static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2843{
2844 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2845 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2846}
2847
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848/*
2849 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2850 * vcpu mutex is already taken.
2851 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002852static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002854 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002855 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002857 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002858 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002859 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002860 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002861
2862 /*
2863 * Read loaded_vmcs->cpu should be before fetching
2864 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2865 * See the comments in __loaded_vmcs_clear().
2866 */
2867 smp_rmb();
2868
Nadav Har'Eld462b812011-05-24 15:26:10 +03002869 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2870 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002871 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002872 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002873 }
2874
2875 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2876 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2877 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002878 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002879 }
2880
2881 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002882 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002883 unsigned long sysenter_esp;
2884
2885 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002886
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 /*
2888 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002889 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002891 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002892 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002893 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002895 /*
2896 * VM exits change the host TR limit to 0x67 after a VM
2897 * exit. This is okay, since 0x67 covers everything except
2898 * the IO bitmap and have have code to handle the IO bitmap
2899 * being lost after a VM exit.
2900 */
2901 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2902
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2904 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002905
Nadav Har'Eld462b812011-05-24 15:26:10 +03002906 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 }
Feng Wu28b835d2015-09-18 22:29:54 +08002908
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002909 /* Setup TSC multiplier */
2910 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002911 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2912 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002913
Feng Wu28b835d2015-09-18 22:29:54 +08002914 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002915 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002916 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002917}
2918
2919static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2920{
2921 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2922
2923 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002924 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2925 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002926 return;
2927
2928 /* Set SN when the vCPU is preempted */
2929 if (vcpu->preempted)
2930 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931}
2932
2933static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2934{
Feng Wu28b835d2015-09-18 22:29:54 +08002935 vmx_vcpu_pi_put(vcpu);
2936
Avi Kivitya9b21b62008-06-24 11:48:49 +03002937 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938}
2939
Wanpeng Lif244dee2017-07-20 01:11:54 -07002940static bool emulation_required(struct kvm_vcpu *vcpu)
2941{
2942 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2943}
2944
Avi Kivityedcafe32009-12-30 18:07:40 +02002945static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2946
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002947/*
2948 * Return the cr0 value that a nested guest would read. This is a combination
2949 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2950 * its hypervisor (cr0_read_shadow).
2951 */
2952static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2953{
2954 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2955 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2956}
2957static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2958{
2959 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2960 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2961}
2962
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2964{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002965 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002966
Avi Kivity6de12732011-03-07 12:51:22 +02002967 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2968 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2969 rflags = vmcs_readl(GUEST_RFLAGS);
2970 if (to_vmx(vcpu)->rmode.vm86_active) {
2971 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2972 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2973 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2974 }
2975 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002976 }
Avi Kivity6de12732011-03-07 12:51:22 +02002977 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978}
2979
2980static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2981{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002982 unsigned long old_rflags = vmx_get_rflags(vcpu);
2983
Avi Kivity6de12732011-03-07 12:51:22 +02002984 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2985 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002986 if (to_vmx(vcpu)->rmode.vm86_active) {
2987 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002988 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002989 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002991
2992 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2993 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994}
2995
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002996static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002997{
2998 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2999 int ret = 0;
3000
3001 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003002 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003003 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003004 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003005
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003006 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003007}
3008
3009static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3010{
3011 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3012 u32 interruptibility = interruptibility_old;
3013
3014 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3015
Jan Kiszka48005f62010-02-19 19:38:07 +01003016 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003017 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003018 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003019 interruptibility |= GUEST_INTR_STATE_STI;
3020
3021 if ((interruptibility != interruptibility_old))
3022 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3023}
3024
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3026{
3027 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003029 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003031 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032
Glauber Costa2809f5d2009-05-12 16:21:05 -04003033 /* skipping an emulated instruction also counts */
3034 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035}
3036
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003037static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3038 unsigned long exit_qual)
3039{
3040 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3041 unsigned int nr = vcpu->arch.exception.nr;
3042 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3043
3044 if (vcpu->arch.exception.has_error_code) {
3045 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3046 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3047 }
3048
3049 if (kvm_exception_is_soft(nr))
3050 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3051 else
3052 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3053
3054 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3055 vmx_get_nmi_mask(vcpu))
3056 intr_info |= INTR_INFO_UNBLOCK_NMI;
3057
3058 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3059}
3060
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003061/*
3062 * KVM wants to inject page-faults which it got to the guest. This function
3063 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003064 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003065static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003066{
3067 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003068 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003069
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003070 if (nr == PF_VECTOR) {
3071 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003072 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003073 return 1;
3074 }
3075 /*
3076 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3077 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3078 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3079 * can be written only when inject_pending_event runs. This should be
3080 * conditional on a new capability---if the capability is disabled,
3081 * kvm_multiple_exception would write the ancillary information to
3082 * CR2 or DR6, for backwards ABI-compatibility.
3083 */
3084 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3085 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003086 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003087 return 1;
3088 }
3089 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003090 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003091 if (nr == DB_VECTOR)
3092 *exit_qual = vcpu->arch.dr6;
3093 else
3094 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003095 return 1;
3096 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003097 }
3098
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003099 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003100}
3101
Wanpeng Licaa057a2018-03-12 04:53:03 -07003102static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3103{
3104 /*
3105 * Ensure that we clear the HLT state in the VMCS. We don't need to
3106 * explicitly skip the instruction because if the HLT state is set,
3107 * then the instruction is already executing and RIP has already been
3108 * advanced.
3109 */
3110 if (kvm_hlt_in_guest(vcpu->kvm) &&
3111 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3112 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3113}
3114
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003115static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003116{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003117 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003118 unsigned nr = vcpu->arch.exception.nr;
3119 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003120 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003121 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003122
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003123 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003124 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003125 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3126 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003127
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003128 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003129 int inc_eip = 0;
3130 if (kvm_exception_is_soft(nr))
3131 inc_eip = vcpu->arch.event_exit_inst_len;
3132 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003133 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003134 return;
3135 }
3136
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003137 WARN_ON_ONCE(vmx->emulation_required);
3138
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003139 if (kvm_exception_is_soft(nr)) {
3140 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3141 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003142 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3143 } else
3144 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3145
3146 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003147
3148 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003149}
3150
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003151static bool vmx_rdtscp_supported(void)
3152{
3153 return cpu_has_vmx_rdtscp();
3154}
3155
Mao, Junjiead756a12012-07-02 01:18:48 +00003156static bool vmx_invpcid_supported(void)
3157{
3158 return cpu_has_vmx_invpcid() && enable_ept;
3159}
3160
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161/*
Eddie Donga75beee2007-05-17 18:55:15 +03003162 * Swap MSR entry in host/guest MSR entry array.
3163 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003164static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003165{
Avi Kivity26bb0982009-09-07 11:14:12 +03003166 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003167
3168 tmp = vmx->guest_msrs[to];
3169 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3170 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003171}
3172
3173/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003174 * Set up the vmcs to automatically save and restore system
3175 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3176 * mode, as fiddling with msrs is very expensive.
3177 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003178static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003179{
Avi Kivity26bb0982009-09-07 11:14:12 +03003180 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003181
Eddie Donga75beee2007-05-17 18:55:15 +03003182 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003183#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003184 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003185 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003186 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003187 move_msr_up(vmx, index, save_nmsrs++);
3188 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003189 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003190 move_msr_up(vmx, index, save_nmsrs++);
3191 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003192 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003193 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003194 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003195 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003196 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003197 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003198 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003199 * if efer.sce is enabled.
3200 */
Brian Gerst8c065852010-07-17 09:03:26 -04003201 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003202 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003203 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003204 }
Eddie Donga75beee2007-05-17 18:55:15 +03003205#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003206 index = __find_msr_index(vmx, MSR_EFER);
3207 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003208 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003209
Avi Kivity26bb0982009-09-07 11:14:12 +03003210 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003211
Yang Zhang8d146952013-01-25 10:18:50 +08003212 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003213 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003214}
3215
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003216static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003218 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003220 if (is_guest_mode(vcpu) &&
3221 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3222 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3223
3224 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
3227/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003228 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003230static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003232 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003233 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003234 * We're here if L1 chose not to trap WRMSR to TSC. According
3235 * to the spec, this should set L1's TSC; The offset that L1
3236 * set for L2 remains unchanged, and still needs to be added
3237 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003238 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003239 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003240 /* recalculate vmcs02.TSC_OFFSET: */
3241 vmcs12 = get_vmcs12(vcpu);
3242 vmcs_write64(TSC_OFFSET, offset +
3243 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3244 vmcs12->tsc_offset : 0));
3245 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003246 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3247 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003248 vmcs_write64(TSC_OFFSET, offset);
3249 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250}
3251
Nadav Har'El801d3422011-05-25 23:02:23 +03003252/*
3253 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3254 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3255 * all guests if the "nested" module option is off, and can also be disabled
3256 * for a single guest by disabling its VMX cpuid bit.
3257 */
3258static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3259{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003260 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003261}
3262
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003264 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3265 * returned for the various VMX controls MSRs when nested VMX is enabled.
3266 * The same values should also be used to verify that vmcs12 control fields are
3267 * valid during nested entry from L1 to L2.
3268 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3269 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3270 * bit in the high half is on if the corresponding bit in the control field
3271 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003272 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003274{
Paolo Bonzini13893092018-02-26 13:40:09 +01003275 if (!nested) {
3276 memset(msrs, 0, sizeof(*msrs));
3277 return;
3278 }
3279
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003280 /*
3281 * Note that as a general rule, the high half of the MSRs (bits in
3282 * the control fields which may be 1) should be initialized by the
3283 * intersection of the underlying hardware's MSR (i.e., features which
3284 * can be supported) and the list of features we want to expose -
3285 * because they are known to be properly supported in our code.
3286 * Also, usually, the low half of the MSRs (bits which must be 1) can
3287 * be set to 0, meaning that L1 may turn off any of these bits. The
3288 * reason is that if one of these bits is necessary, it will appear
3289 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3290 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003291 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003292 * These rules have exceptions below.
3293 */
3294
3295 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003296 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->pinbased_ctls_low,
3298 msrs->pinbased_ctls_high);
3299 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003300 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003301 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003302 PIN_BASED_EXT_INTR_MASK |
3303 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003304 PIN_BASED_VIRTUAL_NMIS |
3305 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003307 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003308 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003309
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003310 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003311 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003312 msrs->exit_ctls_low,
3313 msrs->exit_ctls_high);
3314 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003315 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003316
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003317 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003318#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003319 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003320#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003321 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003323 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003324 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003325 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3326
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003327 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003329
Jan Kiszka2996fca2014-06-16 13:59:43 +02003330 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003331 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003332
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003333 /* entry controls */
3334 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->entry_ctls_low,
3336 msrs->entry_ctls_high);
3337 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003338 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003339 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003340#ifdef CONFIG_X86_64
3341 VM_ENTRY_IA32E_MODE |
3342#endif
3343 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003345 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003346 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003348
Jan Kiszka2996fca2014-06-16 13:59:43 +02003349 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003350 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003351
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003352 /* cpu-based controls */
3353 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003354 msrs->procbased_ctls_low,
3355 msrs->procbased_ctls_high);
3356 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003357 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003359 CPU_BASED_VIRTUAL_INTR_PENDING |
3360 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003361 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3362 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3363 CPU_BASED_CR3_STORE_EXITING |
3364#ifdef CONFIG_X86_64
3365 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3366#endif
3367 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003368 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3369 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3370 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3371 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003372 /*
3373 * We can allow some features even when not supported by the
3374 * hardware. For example, L1 can specify an MSR bitmap - and we
3375 * can use it to avoid exits to L1 - even when L0 runs L2
3376 * without MSR bitmaps.
3377 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003378 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003379 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003380 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003381
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003382 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003383 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003384 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3385
Paolo Bonzini80154d72017-08-24 13:55:35 +02003386 /*
3387 * secondary cpu-based controls. Do not include those that
3388 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3389 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003390 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->secondary_ctls_low,
3392 msrs->secondary_ctls_high);
3393 msrs->secondary_ctls_low = 0;
3394 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003395 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003396 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003397 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003398 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003399 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003400 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003401
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003402 if (enable_ept) {
3403 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003405 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003406 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003407 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003408 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003409 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003410 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 msrs->ept_caps &= vmx_capability.ept;
3412 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003413 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3414 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003415 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003416 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003417 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003418 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003419 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003420 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003421
Bandan Das27c42a12017-08-03 15:54:42 -04003422 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003423 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003424 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003425 /*
3426 * Advertise EPTP switching unconditionally
3427 * since we emulate it
3428 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003429 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003430 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003431 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003432 }
3433
Paolo Bonzinief697a72016-03-18 16:58:38 +01003434 /*
3435 * Old versions of KVM use the single-context version without
3436 * checking for support, so declare that it is supported even
3437 * though it is treated as global context. The alternative is
3438 * not failing the single-context invvpid, and it is worse.
3439 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003440 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003441 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003442 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003443 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003444 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003445 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003446
Radim Krčmář0790ec12015-03-17 14:02:32 +01003447 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003449 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3450
Jan Kiszkac18911a2013-03-13 16:06:41 +01003451 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003452 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003453 msrs->misc_low,
3454 msrs->misc_high);
3455 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3456 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003457 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003458 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003459 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003460 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003461
3462 /*
3463 * This MSR reports some information about VMX support. We
3464 * should return information about the VMX we emulate for the
3465 * guest, and the VMCS structure we give it - not about the
3466 * VMX support of the underlying hardware.
3467 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003469 VMCS12_REVISION |
3470 VMX_BASIC_TRUE_CTLS |
3471 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3472 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3473
3474 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003476
3477 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003478 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003479 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3480 * We picked the standard core2 setting.
3481 */
3482#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3483#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003484 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3485 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003486
3487 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003488 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3489 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003490
3491 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003492 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003493}
3494
David Matlack38991522016-11-29 18:14:08 -08003495/*
3496 * if fixed0[i] == 1: val[i] must be 1
3497 * if fixed1[i] == 0: val[i] must be 0
3498 */
3499static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3500{
3501 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003502}
3503
3504static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3505{
David Matlack38991522016-11-29 18:14:08 -08003506 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003507}
3508
3509static inline u64 vmx_control_msr(u32 low, u32 high)
3510{
3511 return low | ((u64)high << 32);
3512}
3513
David Matlack62cc6b9d2016-11-29 18:14:07 -08003514static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3515{
3516 superset &= mask;
3517 subset &= mask;
3518
3519 return (superset | subset) == superset;
3520}
3521
3522static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3523{
3524 const u64 feature_and_reserved =
3525 /* feature (except bit 48; see below) */
3526 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3527 /* reserved */
3528 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003530
3531 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3532 return -EINVAL;
3533
3534 /*
3535 * KVM does not emulate a version of VMX that constrains physical
3536 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3537 */
3538 if (data & BIT_ULL(48))
3539 return -EINVAL;
3540
3541 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3542 vmx_basic_vmcs_revision_id(data))
3543 return -EINVAL;
3544
3545 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3546 return -EINVAL;
3547
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003549 return 0;
3550}
3551
3552static int
3553vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3554{
3555 u64 supported;
3556 u32 *lowp, *highp;
3557
3558 switch (msr_index) {
3559 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003560 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3561 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003562 break;
3563 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 lowp = &vmx->nested.msrs.procbased_ctls_low;
3565 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003566 break;
3567 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003568 lowp = &vmx->nested.msrs.exit_ctls_low;
3569 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003570 break;
3571 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003572 lowp = &vmx->nested.msrs.entry_ctls_low;
3573 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003574 break;
3575 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 lowp = &vmx->nested.msrs.secondary_ctls_low;
3577 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003578 break;
3579 default:
3580 BUG();
3581 }
3582
3583 supported = vmx_control_msr(*lowp, *highp);
3584
3585 /* Check must-be-1 bits are still 1. */
3586 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3587 return -EINVAL;
3588
3589 /* Check must-be-0 bits are still 0. */
3590 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3591 return -EINVAL;
3592
3593 *lowp = data;
3594 *highp = data >> 32;
3595 return 0;
3596}
3597
3598static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3599{
3600 const u64 feature_and_reserved_bits =
3601 /* feature */
3602 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3603 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3604 /* reserved */
3605 GENMASK_ULL(13, 9) | BIT_ULL(31);
3606 u64 vmx_misc;
3607
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003608 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3609 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003610
3611 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3612 return -EINVAL;
3613
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003614 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003615 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3616 vmx_misc_preemption_timer_rate(data) !=
3617 vmx_misc_preemption_timer_rate(vmx_misc))
3618 return -EINVAL;
3619
3620 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3621 return -EINVAL;
3622
3623 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3624 return -EINVAL;
3625
3626 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3627 return -EINVAL;
3628
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003629 vmx->nested.msrs.misc_low = data;
3630 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003631
3632 /*
3633 * If L1 has read-only VM-exit information fields, use the
3634 * less permissive vmx_vmwrite_bitmap to specify write
3635 * permissions for the shadow VMCS.
3636 */
3637 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3638 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3639
David Matlack62cc6b9d2016-11-29 18:14:07 -08003640 return 0;
3641}
3642
3643static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3644{
3645 u64 vmx_ept_vpid_cap;
3646
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3648 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003649
3650 /* Every bit is either reserved or a feature bit. */
3651 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3652 return -EINVAL;
3653
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 vmx->nested.msrs.ept_caps = data;
3655 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003656 return 0;
3657}
3658
3659static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3660{
3661 u64 *msr;
3662
3663 switch (msr_index) {
3664 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003665 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003666 break;
3667 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003669 break;
3670 default:
3671 BUG();
3672 }
3673
3674 /*
3675 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3676 * must be 1 in the restored value.
3677 */
3678 if (!is_bitwise_subset(data, *msr, -1ULL))
3679 return -EINVAL;
3680
3681 *msr = data;
3682 return 0;
3683}
3684
3685/*
3686 * Called when userspace is restoring VMX MSRs.
3687 *
3688 * Returns 0 on success, non-0 otherwise.
3689 */
3690static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3691{
3692 struct vcpu_vmx *vmx = to_vmx(vcpu);
3693
Jim Mattsona943ac52018-05-29 09:11:32 -07003694 /*
3695 * Don't allow changes to the VMX capability MSRs while the vCPU
3696 * is in VMX operation.
3697 */
3698 if (vmx->nested.vmxon)
3699 return -EBUSY;
3700
David Matlack62cc6b9d2016-11-29 18:14:07 -08003701 switch (msr_index) {
3702 case MSR_IA32_VMX_BASIC:
3703 return vmx_restore_vmx_basic(vmx, data);
3704 case MSR_IA32_VMX_PINBASED_CTLS:
3705 case MSR_IA32_VMX_PROCBASED_CTLS:
3706 case MSR_IA32_VMX_EXIT_CTLS:
3707 case MSR_IA32_VMX_ENTRY_CTLS:
3708 /*
3709 * The "non-true" VMX capability MSRs are generated from the
3710 * "true" MSRs, so we do not support restoring them directly.
3711 *
3712 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3713 * should restore the "true" MSRs with the must-be-1 bits
3714 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3715 * DEFAULT SETTINGS".
3716 */
3717 return -EINVAL;
3718 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3719 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3720 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3721 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3722 case MSR_IA32_VMX_PROCBASED_CTLS2:
3723 return vmx_restore_control_msr(vmx, msr_index, data);
3724 case MSR_IA32_VMX_MISC:
3725 return vmx_restore_vmx_misc(vmx, data);
3726 case MSR_IA32_VMX_CR0_FIXED0:
3727 case MSR_IA32_VMX_CR4_FIXED0:
3728 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3729 case MSR_IA32_VMX_CR0_FIXED1:
3730 case MSR_IA32_VMX_CR4_FIXED1:
3731 /*
3732 * These MSRs are generated based on the vCPU's CPUID, so we
3733 * do not support restoring them directly.
3734 */
3735 return -EINVAL;
3736 case MSR_IA32_VMX_EPT_VPID_CAP:
3737 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3738 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003740 return 0;
3741 default:
3742 /*
3743 * The rest of the VMX capability MSRs do not support restore.
3744 */
3745 return -EINVAL;
3746 }
3747}
3748
Jan Kiszkacae50132014-01-04 18:47:22 +01003749/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003750static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003751{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003752 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003753 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003754 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003755 break;
3756 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3757 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003758 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003759 msrs->pinbased_ctls_low,
3760 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003761 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3762 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003763 break;
3764 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3765 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003766 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 msrs->procbased_ctls_low,
3768 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003769 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3770 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003771 break;
3772 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3773 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003774 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 msrs->exit_ctls_low,
3776 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003777 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3778 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3781 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003782 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003783 msrs->entry_ctls_low,
3784 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003785 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3786 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003787 break;
3788 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003789 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003790 msrs->misc_low,
3791 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003792 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003793 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003794 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003795 break;
3796 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003797 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003798 break;
3799 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003800 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003801 break;
3802 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003803 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003804 break;
3805 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003807 break;
3808 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003809 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 msrs->secondary_ctls_low,
3811 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 break;
3813 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003814 *pdata = msrs->ept_caps |
3815 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003816 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003817 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003819 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003820 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003821 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003822 }
3823
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003824 return 0;
3825}
3826
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003827static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3828 uint64_t val)
3829{
3830 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3831
3832 return !(val & ~valid_bits);
3833}
3834
Tom Lendacky801e4592018-02-21 13:39:51 -06003835static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3836{
Paolo Bonzini13893092018-02-26 13:40:09 +01003837 switch (msr->index) {
3838 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3839 if (!nested)
3840 return 1;
3841 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3842 default:
3843 return 1;
3844 }
3845
3846 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003847}
3848
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003849/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 * Reads an msr value (of 'msr_index') into 'pdata'.
3851 * Returns 0 on success, non-0 otherwise.
3852 * Assumes vcpu_load() was already called.
3853 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003854static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003856 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003857 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003859 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003860#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003862 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863 break;
3864 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003865 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003867 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003868 vmx_load_host_state(vmx);
3869 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003870 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003871#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003873 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003874 case MSR_IA32_SPEC_CTRL:
3875 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003876 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3877 return 1;
3878
3879 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3880 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003881 case MSR_IA32_ARCH_CAPABILITIES:
3882 if (!msr_info->host_initiated &&
3883 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3884 return 1;
3885 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3886 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003888 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 break;
3890 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003891 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892 break;
3893 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003894 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003896 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003897 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003898 (!msr_info->host_initiated &&
3899 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003900 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003901 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003902 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003903 case MSR_IA32_MCG_EXT_CTL:
3904 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003905 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003906 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003907 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003908 msr_info->data = vcpu->arch.mcg_ext_ctl;
3909 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003910 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003911 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003912 break;
3913 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3914 if (!nested_vmx_allowed(vcpu))
3915 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003916 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3917 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003918 case MSR_IA32_XSS:
3919 if (!vmx_xsaves_supported())
3920 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003921 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003922 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003923 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003924 if (!msr_info->host_initiated &&
3925 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003926 return 1;
3927 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003929 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003930 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003931 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003932 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003934 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935 }
3936
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937 return 0;
3938}
3939
Jan Kiszkacae50132014-01-04 18:47:22 +01003940static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3941
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942/*
3943 * Writes msr value into into the appropriate "register".
3944 * Returns 0 on success, non-0 otherwise.
3945 * Assumes vcpu_load() was already called.
3946 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003947static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003950 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003951 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003952 u32 msr_index = msr_info->index;
3953 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003954
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003956 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003957 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003958 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003959#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003961 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 vmcs_writel(GUEST_FS_BASE, data);
3963 break;
3964 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003965 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966 vmcs_writel(GUEST_GS_BASE, data);
3967 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003968 case MSR_KERNEL_GS_BASE:
3969 vmx_load_host_state(vmx);
3970 vmx->msr_guest_kernel_gs_base = data;
3971 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972#endif
3973 case MSR_IA32_SYSENTER_CS:
3974 vmcs_write32(GUEST_SYSENTER_CS, data);
3975 break;
3976 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003977 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 break;
3979 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003980 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003982 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003983 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003984 (!msr_info->host_initiated &&
3985 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003986 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003987 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003988 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003990 vmcs_write64(GUEST_BNDCFGS, data);
3991 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003992 case MSR_IA32_SPEC_CTRL:
3993 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003994 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3995 return 1;
3996
3997 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003998 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003999 return 1;
4000
4001 vmx->spec_ctrl = data;
4002
4003 if (!data)
4004 break;
4005
4006 /*
4007 * For non-nested:
4008 * When it's written (to non-zero) for the first time, pass
4009 * it through.
4010 *
4011 * For nested:
4012 * The handling of the MSR bitmap for L2 guests is done in
4013 * nested_vmx_merge_msr_bitmap. We should not touch the
4014 * vmcs02.msr_bitmap here since it gets completely overwritten
4015 * in the merging. We update the vmcs01 here for L1 as well
4016 * since it will end up touching the MSR anyway now.
4017 */
4018 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4019 MSR_IA32_SPEC_CTRL,
4020 MSR_TYPE_RW);
4021 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004022 case MSR_IA32_PRED_CMD:
4023 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004024 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4025 return 1;
4026
4027 if (data & ~PRED_CMD_IBPB)
4028 return 1;
4029
4030 if (!data)
4031 break;
4032
4033 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4034
4035 /*
4036 * For non-nested:
4037 * When it's written (to non-zero) for the first time, pass
4038 * it through.
4039 *
4040 * For nested:
4041 * The handling of the MSR bitmap for L2 guests is done in
4042 * nested_vmx_merge_msr_bitmap. We should not touch the
4043 * vmcs02.msr_bitmap here since it gets completely overwritten
4044 * in the merging.
4045 */
4046 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4047 MSR_TYPE_W);
4048 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004049 case MSR_IA32_ARCH_CAPABILITIES:
4050 if (!msr_info->host_initiated)
4051 return 1;
4052 vmx->arch_capabilities = data;
4053 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004054 case MSR_IA32_CR_PAT:
4055 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004056 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4057 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004058 vmcs_write64(GUEST_IA32_PAT, data);
4059 vcpu->arch.pat = data;
4060 break;
4061 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004062 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004063 break;
Will Auldba904632012-11-29 12:42:50 -08004064 case MSR_IA32_TSC_ADJUST:
4065 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004066 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004067 case MSR_IA32_MCG_EXT_CTL:
4068 if ((!msr_info->host_initiated &&
4069 !(to_vmx(vcpu)->msr_ia32_feature_control &
4070 FEATURE_CONTROL_LMCE)) ||
4071 (data & ~MCG_EXT_CTL_LMCE_EN))
4072 return 1;
4073 vcpu->arch.mcg_ext_ctl = data;
4074 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004075 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004076 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004077 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004078 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4079 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004080 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004081 if (msr_info->host_initiated && data == 0)
4082 vmx_leave_nested(vcpu);
4083 break;
4084 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004085 if (!msr_info->host_initiated)
4086 return 1; /* they are read-only */
4087 if (!nested_vmx_allowed(vcpu))
4088 return 1;
4089 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004090 case MSR_IA32_XSS:
4091 if (!vmx_xsaves_supported())
4092 return 1;
4093 /*
4094 * The only supported bit as of Skylake is bit 8, but
4095 * it is not supported on KVM.
4096 */
4097 if (data != 0)
4098 return 1;
4099 vcpu->arch.ia32_xss = data;
4100 if (vcpu->arch.ia32_xss != host_xss)
4101 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004102 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004103 else
4104 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4105 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004106 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004107 if (!msr_info->host_initiated &&
4108 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004109 return 1;
4110 /* Check reserved bit, higher 32 bits should be zero */
4111 if ((data >> 32) != 0)
4112 return 1;
4113 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004115 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004116 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004117 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004118 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004119 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4120 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004121 ret = kvm_set_shared_msr(msr->index, msr->data,
4122 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004123 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004124 if (ret)
4125 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004126 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004127 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004129 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130 }
4131
Eddie Dong2cc51562007-05-21 07:28:09 +03004132 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133}
4134
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004135static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004137 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4138 switch (reg) {
4139 case VCPU_REGS_RSP:
4140 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4141 break;
4142 case VCPU_REGS_RIP:
4143 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4144 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004145 case VCPU_EXREG_PDPTR:
4146 if (enable_ept)
4147 ept_save_pdptrs(vcpu);
4148 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004149 default:
4150 break;
4151 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152}
4153
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154static __init int cpu_has_kvm_support(void)
4155{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004156 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157}
4158
4159static __init int vmx_disabled_by_bios(void)
4160{
4161 u64 msr;
4162
4163 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004164 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004165 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004166 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4167 && tboot_enabled())
4168 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004169 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004170 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004171 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004172 && !tboot_enabled()) {
4173 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004174 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004175 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004176 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004177 /* launched w/o TXT and VMX disabled */
4178 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4179 && !tboot_enabled())
4180 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004181 }
4182
4183 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184}
4185
Dongxiao Xu7725b892010-05-11 18:29:38 +08004186static void kvm_cpu_vmxon(u64 addr)
4187{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004188 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004189 intel_pt_handle_vmx(1);
4190
Dongxiao Xu7725b892010-05-11 18:29:38 +08004191 asm volatile (ASM_VMX_VMXON_RAX
4192 : : "a"(&addr), "m"(addr)
4193 : "memory", "cc");
4194}
4195
Radim Krčmář13a34e02014-08-28 15:13:03 +02004196static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197{
4198 int cpu = raw_smp_processor_id();
4199 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004200 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004202 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004203 return -EBUSY;
4204
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004205 /*
4206 * This can happen if we hot-added a CPU but failed to allocate
4207 * VP assist page for it.
4208 */
4209 if (static_branch_unlikely(&enable_evmcs) &&
4210 !hv_get_vp_assist_page(cpu))
4211 return -EFAULT;
4212
Nadav Har'Eld462b812011-05-24 15:26:10 +03004213 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004214 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4215 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004216
4217 /*
4218 * Now we can enable the vmclear operation in kdump
4219 * since the loaded_vmcss_on_cpu list on this cpu
4220 * has been initialized.
4221 *
4222 * Though the cpu is not in VMX operation now, there
4223 * is no problem to enable the vmclear operation
4224 * for the loaded_vmcss_on_cpu list is empty!
4225 */
4226 crash_enable_local_vmclear(cpu);
4227
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004229
4230 test_bits = FEATURE_CONTROL_LOCKED;
4231 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4232 if (tboot_enabled())
4233 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4234
4235 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004236 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004237 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4238 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004239 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004240 if (enable_ept)
4241 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004242
4243 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244}
4245
Nadav Har'Eld462b812011-05-24 15:26:10 +03004246static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004247{
4248 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004249 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004250
Nadav Har'Eld462b812011-05-24 15:26:10 +03004251 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4252 loaded_vmcss_on_cpu_link)
4253 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004254}
4255
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004256
4257/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4258 * tricks.
4259 */
4260static void kvm_cpu_vmxoff(void)
4261{
4262 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004263
4264 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004265 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004266}
4267
Radim Krčmář13a34e02014-08-28 15:13:03 +02004268static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004270 vmclear_local_loaded_vmcss();
4271 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272}
4273
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004274static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004275 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276{
4277 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004278 u32 ctl = ctl_min | ctl_opt;
4279
4280 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4281
4282 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4283 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4284
4285 /* Ensure minimum (required) set of control bits are supported. */
4286 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004287 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004288
4289 *result = ctl;
4290 return 0;
4291}
4292
Avi Kivity110312c2010-12-21 12:54:20 +02004293static __init bool allow_1_setting(u32 msr, u32 ctl)
4294{
4295 u32 vmx_msr_low, vmx_msr_high;
4296
4297 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4298 return vmx_msr_high & ctl;
4299}
4300
Yang, Sheng002c7f72007-07-31 14:23:01 +03004301static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004302{
4303 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004304 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004305 u32 _pin_based_exec_control = 0;
4306 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004307 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004308 u32 _vmexit_control = 0;
4309 u32 _vmentry_control = 0;
4310
Paolo Bonzini13893092018-02-26 13:40:09 +01004311 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304312 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004313#ifdef CONFIG_X86_64
4314 CPU_BASED_CR8_LOAD_EXITING |
4315 CPU_BASED_CR8_STORE_EXITING |
4316#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004317 CPU_BASED_CR3_LOAD_EXITING |
4318 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004319 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004320 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004321 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004322 CPU_BASED_MWAIT_EXITING |
4323 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004324 CPU_BASED_INVLPG_EXITING |
4325 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004326
Sheng Yangf78e0e22007-10-29 09:40:42 +08004327 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004328 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004329 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004330 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4331 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004332 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004333#ifdef CONFIG_X86_64
4334 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4335 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4336 ~CPU_BASED_CR8_STORE_EXITING;
4337#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004338 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004339 min2 = 0;
4340 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004341 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004342 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004343 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004344 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004345 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004346 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004347 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004348 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004349 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004350 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004351 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004352 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004353 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004354 SECONDARY_EXEC_RDSEED_EXITING |
4355 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004356 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004357 SECONDARY_EXEC_TSC_SCALING |
4358 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004359 if (adjust_vmx_controls(min2, opt2,
4360 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004361 &_cpu_based_2nd_exec_control) < 0)
4362 return -EIO;
4363 }
4364#ifndef CONFIG_X86_64
4365 if (!(_cpu_based_2nd_exec_control &
4366 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4367 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4368#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004369
4370 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4371 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004372 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004373 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4374 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004375
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004376 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4377 &vmx_capability.ept, &vmx_capability.vpid);
4378
Sheng Yangd56f5462008-04-25 10:13:16 +08004379 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004380 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4381 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004382 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4383 CPU_BASED_CR3_STORE_EXITING |
4384 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004385 } else if (vmx_capability.ept) {
4386 vmx_capability.ept = 0;
4387 pr_warn_once("EPT CAP should not exist if not support "
4388 "1-setting enable EPT VM-execution control\n");
4389 }
4390 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4391 vmx_capability.vpid) {
4392 vmx_capability.vpid = 0;
4393 pr_warn_once("VPID CAP should not exist if not support "
4394 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004395 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004396
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004397 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004398#ifdef CONFIG_X86_64
4399 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4400#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004401 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004402 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004403 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4404 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004405 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004406
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004407 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4408 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4409 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004410 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4411 &_pin_based_exec_control) < 0)
4412 return -EIO;
4413
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004414 if (cpu_has_broken_vmx_preemption_timer())
4415 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004416 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004417 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004418 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4419
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004420 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004421 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004422 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4423 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004424 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004426 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004427
4428 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4429 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004430 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004431
4432#ifdef CONFIG_X86_64
4433 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4434 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004435 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004436#endif
4437
4438 /* Require Write-Back (WB) memory type for VMCS accesses. */
4439 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004440 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004441
Yang, Sheng002c7f72007-07-31 14:23:01 +03004442 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004443 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004444 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004445
4446 /* KVM supports Enlightened VMCS v1 only */
4447 if (static_branch_unlikely(&enable_evmcs))
4448 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4449 else
4450 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004451
Yang, Sheng002c7f72007-07-31 14:23:01 +03004452 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4453 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004454 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004455 vmcs_conf->vmexit_ctrl = _vmexit_control;
4456 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004457
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004458 if (static_branch_unlikely(&enable_evmcs))
4459 evmcs_sanitize_exec_ctrls(vmcs_conf);
4460
Avi Kivity110312c2010-12-21 12:54:20 +02004461 cpu_has_load_ia32_efer =
4462 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4463 VM_ENTRY_LOAD_IA32_EFER)
4464 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4465 VM_EXIT_LOAD_IA32_EFER);
4466
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004467 cpu_has_load_perf_global_ctrl =
4468 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4469 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4470 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4471 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4472
4473 /*
4474 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004475 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004476 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4477 *
4478 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4479 *
4480 * AAK155 (model 26)
4481 * AAP115 (model 30)
4482 * AAT100 (model 37)
4483 * BC86,AAY89,BD102 (model 44)
4484 * BA97 (model 46)
4485 *
4486 */
4487 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4488 switch (boot_cpu_data.x86_model) {
4489 case 26:
4490 case 30:
4491 case 37:
4492 case 44:
4493 case 46:
4494 cpu_has_load_perf_global_ctrl = false;
4495 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4496 "does not work properly. Using workaround\n");
4497 break;
4498 default:
4499 break;
4500 }
4501 }
4502
Borislav Petkov782511b2016-04-04 22:25:03 +02004503 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004504 rdmsrl(MSR_IA32_XSS, host_xss);
4505
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004506 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004507}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004508
4509static struct vmcs *alloc_vmcs_cpu(int cpu)
4510{
4511 int node = cpu_to_node(cpu);
4512 struct page *pages;
4513 struct vmcs *vmcs;
4514
Vlastimil Babka96db8002015-09-08 15:03:50 -07004515 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516 if (!pages)
4517 return NULL;
4518 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004519 memset(vmcs, 0, vmcs_config.size);
4520 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004521 return vmcs;
4522}
4523
Avi Kivity6aa8b732006-12-10 02:21:36 -08004524static void free_vmcs(struct vmcs *vmcs)
4525{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527}
4528
Nadav Har'Eld462b812011-05-24 15:26:10 +03004529/*
4530 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4531 */
4532static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4533{
4534 if (!loaded_vmcs->vmcs)
4535 return;
4536 loaded_vmcs_clear(loaded_vmcs);
4537 free_vmcs(loaded_vmcs->vmcs);
4538 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004539 if (loaded_vmcs->msr_bitmap)
4540 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004541 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004542}
4543
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004544static struct vmcs *alloc_vmcs(void)
4545{
4546 return alloc_vmcs_cpu(raw_smp_processor_id());
4547}
4548
4549static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4550{
4551 loaded_vmcs->vmcs = alloc_vmcs();
4552 if (!loaded_vmcs->vmcs)
4553 return -ENOMEM;
4554
4555 loaded_vmcs->shadow_vmcs = NULL;
4556 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004557
4558 if (cpu_has_vmx_msr_bitmap()) {
4559 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4560 if (!loaded_vmcs->msr_bitmap)
4561 goto out_vmcs;
4562 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004563
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004564 if (IS_ENABLED(CONFIG_HYPERV) &&
4565 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004566 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4567 struct hv_enlightened_vmcs *evmcs =
4568 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4569
4570 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4571 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004572 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004573 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004574
4575out_vmcs:
4576 free_loaded_vmcs(loaded_vmcs);
4577 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004578}
4579
Sam Ravnborg39959582007-06-01 00:47:13 -07004580static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581{
4582 int cpu;
4583
Zachary Amsden3230bb42009-09-29 11:38:37 -10004584 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004585 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004586 per_cpu(vmxarea, cpu) = NULL;
4587 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004588}
4589
Jim Mattsond37f4262017-12-22 12:12:16 -08004590enum vmcs_field_width {
4591 VMCS_FIELD_WIDTH_U16 = 0,
4592 VMCS_FIELD_WIDTH_U64 = 1,
4593 VMCS_FIELD_WIDTH_U32 = 2,
4594 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004595};
4596
Jim Mattsond37f4262017-12-22 12:12:16 -08004597static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004598{
4599 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004600 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004601 return (field >> 13) & 0x3 ;
4602}
4603
4604static inline int vmcs_field_readonly(unsigned long field)
4605{
4606 return (((field >> 10) & 0x3) == 1);
4607}
4608
Bandan Dasfe2b2012014-04-21 15:20:14 -04004609static void init_vmcs_shadow_fields(void)
4610{
4611 int i, j;
4612
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004613 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4614 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004615 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004616 (i + 1 == max_shadow_read_only_fields ||
4617 shadow_read_only_fields[i + 1] != field + 1))
4618 pr_err("Missing field from shadow_read_only_field %x\n",
4619 field + 1);
4620
4621 clear_bit(field, vmx_vmread_bitmap);
4622#ifdef CONFIG_X86_64
4623 if (field & 1)
4624 continue;
4625#endif
4626 if (j < i)
4627 shadow_read_only_fields[j] = field;
4628 j++;
4629 }
4630 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004631
4632 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004633 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004634 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004635 (i + 1 == max_shadow_read_write_fields ||
4636 shadow_read_write_fields[i + 1] != field + 1))
4637 pr_err("Missing field from shadow_read_write_field %x\n",
4638 field + 1);
4639
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004640 /*
4641 * PML and the preemption timer can be emulated, but the
4642 * processor cannot vmwrite to fields that don't exist
4643 * on bare metal.
4644 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004645 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004646 case GUEST_PML_INDEX:
4647 if (!cpu_has_vmx_pml())
4648 continue;
4649 break;
4650 case VMX_PREEMPTION_TIMER_VALUE:
4651 if (!cpu_has_vmx_preemption_timer())
4652 continue;
4653 break;
4654 case GUEST_INTR_STATUS:
4655 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004656 continue;
4657 break;
4658 default:
4659 break;
4660 }
4661
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004662 clear_bit(field, vmx_vmwrite_bitmap);
4663 clear_bit(field, vmx_vmread_bitmap);
4664#ifdef CONFIG_X86_64
4665 if (field & 1)
4666 continue;
4667#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004668 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004669 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004670 j++;
4671 }
4672 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004673}
4674
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675static __init int alloc_kvm_area(void)
4676{
4677 int cpu;
4678
Zachary Amsden3230bb42009-09-29 11:38:37 -10004679 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680 struct vmcs *vmcs;
4681
4682 vmcs = alloc_vmcs_cpu(cpu);
4683 if (!vmcs) {
4684 free_kvm_area();
4685 return -ENOMEM;
4686 }
4687
4688 per_cpu(vmxarea, cpu) = vmcs;
4689 }
4690 return 0;
4691}
4692
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004693static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004694 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004695{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004696 if (!emulate_invalid_guest_state) {
4697 /*
4698 * CS and SS RPL should be equal during guest entry according
4699 * to VMX spec, but in reality it is not always so. Since vcpu
4700 * is in the middle of the transition from real mode to
4701 * protected mode it is safe to assume that RPL 0 is a good
4702 * default value.
4703 */
4704 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004705 save->selector &= ~SEGMENT_RPL_MASK;
4706 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004707 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004709 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710}
4711
4712static void enter_pmode(struct kvm_vcpu *vcpu)
4713{
4714 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004715 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716
Gleb Natapovd99e4152012-12-20 16:57:45 +02004717 /*
4718 * Update real mode segment cache. It may be not up-to-date if sement
4719 * register was written while vcpu was in a guest mode.
4720 */
4721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4727
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004728 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729
Avi Kivity2fb92db2011-04-27 19:42:18 +03004730 vmx_segment_cache_clear(vmx);
4731
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004732 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733
4734 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004735 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4736 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737 vmcs_writel(GUEST_RFLAGS, flags);
4738
Rusty Russell66aee912007-07-17 23:34:16 +10004739 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4740 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741
4742 update_exception_bitmap(vcpu);
4743
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004744 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4745 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4746 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4747 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4748 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4749 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750}
4751
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004752static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753{
Mathias Krause772e0312012-08-30 01:30:19 +02004754 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004755 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756
Gleb Natapovd99e4152012-12-20 16:57:45 +02004757 var.dpl = 0x3;
4758 if (seg == VCPU_SREG_CS)
4759 var.type = 0x3;
4760
4761 if (!emulate_invalid_guest_state) {
4762 var.selector = var.base >> 4;
4763 var.base = var.base & 0xffff0;
4764 var.limit = 0xffff;
4765 var.g = 0;
4766 var.db = 0;
4767 var.present = 1;
4768 var.s = 1;
4769 var.l = 0;
4770 var.unusable = 0;
4771 var.type = 0x3;
4772 var.avl = 0;
4773 if (save->base & 0xf)
4774 printk_once(KERN_WARNING "kvm: segment base is not "
4775 "paragraph aligned when entering "
4776 "protected mode (seg=%d)", seg);
4777 }
4778
4779 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004780 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004781 vmcs_write32(sf->limit, var.limit);
4782 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004783}
4784
4785static void enter_rmode(struct kvm_vcpu *vcpu)
4786{
4787 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004788 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004789 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004791 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004796 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4797 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004798
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004799 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800
Gleb Natapov776e58e2011-03-13 12:34:27 +02004801 /*
4802 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004803 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004804 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004805 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004806 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4807 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004808
Avi Kivity2fb92db2011-04-27 19:42:18 +03004809 vmx_segment_cache_clear(vmx);
4810
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004811 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4814
4815 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004816 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004818 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819
4820 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004821 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822 update_exception_bitmap(vcpu);
4823
Gleb Natapovd99e4152012-12-20 16:57:45 +02004824 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4825 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4826 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4827 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4828 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4829 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004830
Eddie Dong8668a3c2007-10-10 14:26:45 +08004831 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832}
4833
Amit Shah401d10d2009-02-20 22:53:37 +05304834static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4835{
4836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004837 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4838
4839 if (!msr)
4840 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304841
Avi Kivity44ea2b12009-09-06 15:55:37 +03004842 /*
4843 * Force kernel_gs_base reloading before EFER changes, as control
4844 * of this msr depends on is_long_mode().
4845 */
4846 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004847 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304848 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004849 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304850 msr->data = efer;
4851 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004852 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304853
4854 msr->data = efer & ~EFER_LME;
4855 }
4856 setup_msrs(vmx);
4857}
4858
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004859#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860
4861static void enter_lmode(struct kvm_vcpu *vcpu)
4862{
4863 u32 guest_tr_ar;
4864
Avi Kivity2fb92db2011-04-27 19:42:18 +03004865 vmx_segment_cache_clear(to_vmx(vcpu));
4866
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004868 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004869 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4870 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004872 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4873 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 }
Avi Kivityda38f432010-07-06 11:30:49 +03004875 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876}
4877
4878static void exit_lmode(struct kvm_vcpu *vcpu)
4879{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004880 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004881 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882}
4883
4884#endif
4885
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004886static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4887 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004888{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004889 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004890 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4891 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004892 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004893 } else {
4894 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004895 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004896}
4897
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004898static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004899{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004900 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004901}
4902
Avi Kivitye8467fd2009-12-29 18:43:06 +02004903static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4904{
4905 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4906
4907 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4908 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4909}
4910
Avi Kivityaff48ba2010-12-05 18:56:11 +02004911static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4912{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004913 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004914 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4915 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4916}
4917
Anthony Liguori25c4c272007-04-27 09:29:21 +03004918static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004919{
Avi Kivityfc78f512009-12-07 12:16:48 +02004920 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4921
4922 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4923 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004924}
4925
Sheng Yang14394422008-04-28 12:24:45 +08004926static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4927{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004928 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4929
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004930 if (!test_bit(VCPU_EXREG_PDPTR,
4931 (unsigned long *)&vcpu->arch.regs_dirty))
4932 return;
4933
Sheng Yang14394422008-04-28 12:24:45 +08004934 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004935 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4936 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4937 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4938 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004939 }
4940}
4941
Avi Kivity8f5d5492009-05-31 18:41:29 +03004942static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4943{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004944 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4945
Avi Kivity8f5d5492009-05-31 18:41:29 +03004946 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004947 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4948 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4949 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4950 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004951 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004952
4953 __set_bit(VCPU_EXREG_PDPTR,
4954 (unsigned long *)&vcpu->arch.regs_avail);
4955 __set_bit(VCPU_EXREG_PDPTR,
4956 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004957}
4958
David Matlack38991522016-11-29 18:14:08 -08004959static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4960{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004961 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4962 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4964
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004965 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004966 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4967 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4968 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4969
4970 return fixed_bits_valid(val, fixed0, fixed1);
4971}
4972
4973static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4974{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004975 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4976 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004977
4978 return fixed_bits_valid(val, fixed0, fixed1);
4979}
4980
4981static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4982{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004983 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4984 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004985
4986 return fixed_bits_valid(val, fixed0, fixed1);
4987}
4988
4989/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4990#define nested_guest_cr4_valid nested_cr4_valid
4991#define nested_host_cr4_valid nested_cr4_valid
4992
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004993static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004994
4995static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4996 unsigned long cr0,
4997 struct kvm_vcpu *vcpu)
4998{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004999 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5000 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005001 if (!(cr0 & X86_CR0_PG)) {
5002 /* From paging/starting to nonpaging */
5003 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005004 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005005 (CPU_BASED_CR3_LOAD_EXITING |
5006 CPU_BASED_CR3_STORE_EXITING));
5007 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005008 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005009 } else if (!is_paging(vcpu)) {
5010 /* From nonpaging to paging */
5011 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005012 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005013 ~(CPU_BASED_CR3_LOAD_EXITING |
5014 CPU_BASED_CR3_STORE_EXITING));
5015 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005016 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005017 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005018
5019 if (!(cr0 & X86_CR0_WP))
5020 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005021}
5022
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5024{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005025 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005026 unsigned long hw_cr0;
5027
Gleb Natapov50378782013-02-04 16:00:28 +02005028 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005029 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005030 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005031 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005032 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005033
Gleb Natapov218e7632013-01-21 15:36:45 +02005034 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5035 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036
Gleb Natapov218e7632013-01-21 15:36:45 +02005037 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5038 enter_rmode(vcpu);
5039 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005041#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005042 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005043 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005045 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 exit_lmode(vcpu);
5047 }
5048#endif
5049
Sean Christophersonb4d18512018-03-05 12:04:40 -08005050 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005051 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5052
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005054 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005055 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005056
5057 /* depends on vcpu->arch.cr0 to be set to a new value */
5058 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059}
5060
Yu Zhang855feb62017-08-24 20:27:55 +08005061static int get_ept_level(struct kvm_vcpu *vcpu)
5062{
5063 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5064 return 5;
5065 return 4;
5066}
5067
Peter Feiner995f00a2017-06-30 17:26:32 -07005068static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005069{
Yu Zhang855feb62017-08-24 20:27:55 +08005070 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005071
Yu Zhang855feb62017-08-24 20:27:55 +08005072 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005073
Peter Feiner995f00a2017-06-30 17:26:32 -07005074 if (enable_ept_ad_bits &&
5075 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005076 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005077 eptp |= (root_hpa & PAGE_MASK);
5078
5079 return eptp;
5080}
5081
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5083{
Sheng Yang14394422008-04-28 12:24:45 +08005084 unsigned long guest_cr3;
5085 u64 eptp;
5086
5087 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005088 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005089 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005090 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005091 if (enable_unrestricted_guest || is_paging(vcpu) ||
5092 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005093 guest_cr3 = kvm_read_cr3(vcpu);
5094 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005095 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005096 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005097 }
5098
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005099 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005100 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005101}
5102
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005103static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005105 /*
5106 * Pass through host's Machine Check Enable value to hw_cr4, which
5107 * is in force while we are in guest mode. Do not let guests control
5108 * this bit, even if host CR4.MCE == 0.
5109 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005110 unsigned long hw_cr4;
5111
5112 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5113 if (enable_unrestricted_guest)
5114 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5115 else if (to_vmx(vcpu)->rmode.vm86_active)
5116 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5117 else
5118 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005119
Sean Christopherson64f7a112018-04-30 10:01:06 -07005120 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5121 if (cr4 & X86_CR4_UMIP) {
5122 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005123 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005124 hw_cr4 &= ~X86_CR4_UMIP;
5125 } else if (!is_guest_mode(vcpu) ||
5126 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5127 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5128 SECONDARY_EXEC_DESC);
5129 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005130
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005131 if (cr4 & X86_CR4_VMXE) {
5132 /*
5133 * To use VMXON (and later other VMX instructions), a guest
5134 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5135 * So basically the check on whether to allow nested VMX
5136 * is here.
5137 */
5138 if (!nested_vmx_allowed(vcpu))
5139 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005140 }
David Matlack38991522016-11-29 18:14:08 -08005141
5142 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005143 return 1;
5144
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005145 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005146
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005147 if (!enable_unrestricted_guest) {
5148 if (enable_ept) {
5149 if (!is_paging(vcpu)) {
5150 hw_cr4 &= ~X86_CR4_PAE;
5151 hw_cr4 |= X86_CR4_PSE;
5152 } else if (!(cr4 & X86_CR4_PAE)) {
5153 hw_cr4 &= ~X86_CR4_PAE;
5154 }
5155 }
5156
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005157 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005158 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5159 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5160 * to be manually disabled when guest switches to non-paging
5161 * mode.
5162 *
5163 * If !enable_unrestricted_guest, the CPU is always running
5164 * with CR0.PG=1 and CR4 needs to be modified.
5165 * If enable_unrestricted_guest, the CPU automatically
5166 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005167 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005168 if (!is_paging(vcpu))
5169 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5170 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005171
Sheng Yang14394422008-04-28 12:24:45 +08005172 vmcs_writel(CR4_READ_SHADOW, cr4);
5173 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005174 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175}
5176
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177static void vmx_get_segment(struct kvm_vcpu *vcpu,
5178 struct kvm_segment *var, int seg)
5179{
Avi Kivitya9179492011-01-03 14:28:52 +02005180 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005181 u32 ar;
5182
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005183 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005184 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005185 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005186 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005187 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005188 var->base = vmx_read_guest_seg_base(vmx, seg);
5189 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5190 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005191 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005192 var->base = vmx_read_guest_seg_base(vmx, seg);
5193 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5194 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5195 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005196 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005197 var->type = ar & 15;
5198 var->s = (ar >> 4) & 1;
5199 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005200 /*
5201 * Some userspaces do not preserve unusable property. Since usable
5202 * segment has to be present according to VMX spec we can use present
5203 * property to amend userspace bug by making unusable segment always
5204 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5205 * segment as unusable.
5206 */
5207 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208 var->avl = (ar >> 12) & 1;
5209 var->l = (ar >> 13) & 1;
5210 var->db = (ar >> 14) & 1;
5211 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005212}
5213
Avi Kivitya9179492011-01-03 14:28:52 +02005214static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5215{
Avi Kivitya9179492011-01-03 14:28:52 +02005216 struct kvm_segment s;
5217
5218 if (to_vmx(vcpu)->rmode.vm86_active) {
5219 vmx_get_segment(vcpu, &s, seg);
5220 return s.base;
5221 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005222 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005223}
5224
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005225static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005226{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005227 struct vcpu_vmx *vmx = to_vmx(vcpu);
5228
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005229 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005230 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005231 else {
5232 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005233 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005234 }
Avi Kivity69c73022011-03-07 15:26:44 +02005235}
5236
Avi Kivity653e3102007-05-07 10:55:37 +03005237static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239 u32 ar;
5240
Avi Kivityf0495f92012-06-07 17:06:10 +03005241 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242 ar = 1 << 16;
5243 else {
5244 ar = var->type & 15;
5245 ar |= (var->s & 1) << 4;
5246 ar |= (var->dpl & 3) << 5;
5247 ar |= (var->present & 1) << 7;
5248 ar |= (var->avl & 1) << 12;
5249 ar |= (var->l & 1) << 13;
5250 ar |= (var->db & 1) << 14;
5251 ar |= (var->g & 1) << 15;
5252 }
Avi Kivity653e3102007-05-07 10:55:37 +03005253
5254 return ar;
5255}
5256
5257static void vmx_set_segment(struct kvm_vcpu *vcpu,
5258 struct kvm_segment *var, int seg)
5259{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005261 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005262
Avi Kivity2fb92db2011-04-27 19:42:18 +03005263 vmx_segment_cache_clear(vmx);
5264
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005265 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5266 vmx->rmode.segs[seg] = *var;
5267 if (seg == VCPU_SREG_TR)
5268 vmcs_write16(sf->selector, var->selector);
5269 else if (var->s)
5270 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005271 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005272 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005273
Avi Kivity653e3102007-05-07 10:55:37 +03005274 vmcs_writel(sf->base, var->base);
5275 vmcs_write32(sf->limit, var->limit);
5276 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005277
5278 /*
5279 * Fix the "Accessed" bit in AR field of segment registers for older
5280 * qemu binaries.
5281 * IA32 arch specifies that at the time of processor reset the
5282 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005283 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005284 * state vmexit when "unrestricted guest" mode is turned on.
5285 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5286 * tree. Newer qemu binaries with that qemu fix would not need this
5287 * kvm hack.
5288 */
5289 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005290 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005291
Gleb Natapovf924d662012-12-12 19:10:55 +02005292 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005293
5294out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005295 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005296}
5297
Avi Kivity6aa8b732006-12-10 02:21:36 -08005298static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5299{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005300 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301
5302 *db = (ar >> 14) & 1;
5303 *l = (ar >> 13) & 1;
5304}
5305
Gleb Natapov89a27f42010-02-16 10:51:48 +02005306static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005308 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5309 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310}
5311
Gleb Natapov89a27f42010-02-16 10:51:48 +02005312static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005313{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005314 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5315 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005316}
5317
Gleb Natapov89a27f42010-02-16 10:51:48 +02005318static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005320 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5321 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322}
5323
Gleb Natapov89a27f42010-02-16 10:51:48 +02005324static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005325{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005326 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5327 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005328}
5329
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005330static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5331{
5332 struct kvm_segment var;
5333 u32 ar;
5334
5335 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005336 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005337 if (seg == VCPU_SREG_CS)
5338 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005339 ar = vmx_segment_access_rights(&var);
5340
5341 if (var.base != (var.selector << 4))
5342 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005343 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005344 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005345 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005346 return false;
5347
5348 return true;
5349}
5350
5351static bool code_segment_valid(struct kvm_vcpu *vcpu)
5352{
5353 struct kvm_segment cs;
5354 unsigned int cs_rpl;
5355
5356 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005357 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005358
Avi Kivity1872a3f2009-01-04 23:26:52 +02005359 if (cs.unusable)
5360 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005361 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362 return false;
5363 if (!cs.s)
5364 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005365 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005366 if (cs.dpl > cs_rpl)
5367 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005368 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005369 if (cs.dpl != cs_rpl)
5370 return false;
5371 }
5372 if (!cs.present)
5373 return false;
5374
5375 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5376 return true;
5377}
5378
5379static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5380{
5381 struct kvm_segment ss;
5382 unsigned int ss_rpl;
5383
5384 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005385 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005386
Avi Kivity1872a3f2009-01-04 23:26:52 +02005387 if (ss.unusable)
5388 return true;
5389 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390 return false;
5391 if (!ss.s)
5392 return false;
5393 if (ss.dpl != ss_rpl) /* DPL != RPL */
5394 return false;
5395 if (!ss.present)
5396 return false;
5397
5398 return true;
5399}
5400
5401static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5402{
5403 struct kvm_segment var;
5404 unsigned int rpl;
5405
5406 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005407 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005408
Avi Kivity1872a3f2009-01-04 23:26:52 +02005409 if (var.unusable)
5410 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005411 if (!var.s)
5412 return false;
5413 if (!var.present)
5414 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005415 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005416 if (var.dpl < rpl) /* DPL < RPL */
5417 return false;
5418 }
5419
5420 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5421 * rights flags
5422 */
5423 return true;
5424}
5425
5426static bool tr_valid(struct kvm_vcpu *vcpu)
5427{
5428 struct kvm_segment tr;
5429
5430 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5431
Avi Kivity1872a3f2009-01-04 23:26:52 +02005432 if (tr.unusable)
5433 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005434 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005435 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005436 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005437 return false;
5438 if (!tr.present)
5439 return false;
5440
5441 return true;
5442}
5443
5444static bool ldtr_valid(struct kvm_vcpu *vcpu)
5445{
5446 struct kvm_segment ldtr;
5447
5448 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5449
Avi Kivity1872a3f2009-01-04 23:26:52 +02005450 if (ldtr.unusable)
5451 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005452 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005453 return false;
5454 if (ldtr.type != 2)
5455 return false;
5456 if (!ldtr.present)
5457 return false;
5458
5459 return true;
5460}
5461
5462static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5463{
5464 struct kvm_segment cs, ss;
5465
5466 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5467 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5468
Nadav Amitb32a9912015-03-29 16:33:04 +03005469 return ((cs.selector & SEGMENT_RPL_MASK) ==
5470 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005471}
5472
5473/*
5474 * Check if guest state is valid. Returns true if valid, false if
5475 * not.
5476 * We assume that registers are always usable
5477 */
5478static bool guest_state_valid(struct kvm_vcpu *vcpu)
5479{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005480 if (enable_unrestricted_guest)
5481 return true;
5482
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005483 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005484 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005485 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5486 return false;
5487 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5488 return false;
5489 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5490 return false;
5491 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5492 return false;
5493 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5494 return false;
5495 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5496 return false;
5497 } else {
5498 /* protected mode guest state checks */
5499 if (!cs_ss_rpl_check(vcpu))
5500 return false;
5501 if (!code_segment_valid(vcpu))
5502 return false;
5503 if (!stack_segment_valid(vcpu))
5504 return false;
5505 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5506 return false;
5507 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5508 return false;
5509 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5510 return false;
5511 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5512 return false;
5513 if (!tr_valid(vcpu))
5514 return false;
5515 if (!ldtr_valid(vcpu))
5516 return false;
5517 }
5518 /* TODO:
5519 * - Add checks on RIP
5520 * - Add checks on RFLAGS
5521 */
5522
5523 return true;
5524}
5525
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005526static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5527{
5528 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5529}
5530
Mike Dayd77c26f2007-10-08 09:02:08 -04005531static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005533 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005534 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005535 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005537 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005538 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005539 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5540 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005541 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005542 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005543 r = kvm_write_guest_page(kvm, fn++, &data,
5544 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005545 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005546 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005547 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5548 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005549 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005550 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5551 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005552 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005553 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005554 r = kvm_write_guest_page(kvm, fn, &data,
5555 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5556 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005557out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005558 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005559 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005560}
5561
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005562static int init_rmode_identity_map(struct kvm *kvm)
5563{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005564 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005565 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005566 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005567 u32 tmp;
5568
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005569 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005570 mutex_lock(&kvm->slots_lock);
5571
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005572 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005573 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005574
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005575 if (!kvm_vmx->ept_identity_map_addr)
5576 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5577 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005578
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005579 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005580 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005581 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005582 goto out2;
5583
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005584 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005585 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5586 if (r < 0)
5587 goto out;
5588 /* Set up identity-mapping pagetable for EPT in real mode */
5589 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5590 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5591 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5592 r = kvm_write_guest_page(kvm, identity_map_pfn,
5593 &tmp, i * sizeof(tmp), sizeof(tmp));
5594 if (r < 0)
5595 goto out;
5596 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005597 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005598
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005599out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005600 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005601
5602out2:
5603 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005604 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005605}
5606
Avi Kivity6aa8b732006-12-10 02:21:36 -08005607static void seg_setup(int seg)
5608{
Mathias Krause772e0312012-08-30 01:30:19 +02005609 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005610 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005611
5612 vmcs_write16(sf->selector, 0);
5613 vmcs_writel(sf->base, 0);
5614 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005615 ar = 0x93;
5616 if (seg == VCPU_SREG_CS)
5617 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005618
5619 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005620}
5621
Sheng Yangf78e0e22007-10-29 09:40:42 +08005622static int alloc_apic_access_page(struct kvm *kvm)
5623{
Xiao Guangrong44841412012-09-07 14:14:20 +08005624 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005625 int r = 0;
5626
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005627 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005628 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005629 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005630 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5631 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005632 if (r)
5633 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005634
Tang Chen73a6d942014-09-11 13:38:00 +08005635 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005636 if (is_error_page(page)) {
5637 r = -EFAULT;
5638 goto out;
5639 }
5640
Tang Chenc24ae0d2014-09-24 15:57:58 +08005641 /*
5642 * Do not pin the page in memory, so that memory hot-unplug
5643 * is able to migrate it.
5644 */
5645 put_page(page);
5646 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005647out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005648 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005649 return r;
5650}
5651
Wanpeng Li991e7a02015-09-16 17:30:05 +08005652static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005653{
5654 int vpid;
5655
Avi Kivity919818a2009-03-23 18:01:29 +02005656 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005657 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005658 spin_lock(&vmx_vpid_lock);
5659 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005660 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005661 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005662 else
5663 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005664 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005665 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005666}
5667
Wanpeng Li991e7a02015-09-16 17:30:05 +08005668static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005669{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005670 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005671 return;
5672 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005673 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005674 spin_unlock(&vmx_vpid_lock);
5675}
5676
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005677static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5678 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005679{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005680 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005681
5682 if (!cpu_has_vmx_msr_bitmap())
5683 return;
5684
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005685 if (static_branch_unlikely(&enable_evmcs))
5686 evmcs_touch_msr_bitmap();
5687
Sheng Yang25c5f222008-03-28 13:18:56 +08005688 /*
5689 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5690 * have the write-low and read-high bitmap offsets the wrong way round.
5691 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5692 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005693 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005694 if (type & MSR_TYPE_R)
5695 /* read-low */
5696 __clear_bit(msr, msr_bitmap + 0x000 / f);
5697
5698 if (type & MSR_TYPE_W)
5699 /* write-low */
5700 __clear_bit(msr, msr_bitmap + 0x800 / f);
5701
Sheng Yang25c5f222008-03-28 13:18:56 +08005702 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5703 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005704 if (type & MSR_TYPE_R)
5705 /* read-high */
5706 __clear_bit(msr, msr_bitmap + 0x400 / f);
5707
5708 if (type & MSR_TYPE_W)
5709 /* write-high */
5710 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5711
5712 }
5713}
5714
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005715static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5716 u32 msr, int type)
5717{
5718 int f = sizeof(unsigned long);
5719
5720 if (!cpu_has_vmx_msr_bitmap())
5721 return;
5722
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005723 if (static_branch_unlikely(&enable_evmcs))
5724 evmcs_touch_msr_bitmap();
5725
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005726 /*
5727 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5728 * have the write-low and read-high bitmap offsets the wrong way round.
5729 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5730 */
5731 if (msr <= 0x1fff) {
5732 if (type & MSR_TYPE_R)
5733 /* read-low */
5734 __set_bit(msr, msr_bitmap + 0x000 / f);
5735
5736 if (type & MSR_TYPE_W)
5737 /* write-low */
5738 __set_bit(msr, msr_bitmap + 0x800 / f);
5739
5740 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5741 msr &= 0x1fff;
5742 if (type & MSR_TYPE_R)
5743 /* read-high */
5744 __set_bit(msr, msr_bitmap + 0x400 / f);
5745
5746 if (type & MSR_TYPE_W)
5747 /* write-high */
5748 __set_bit(msr, msr_bitmap + 0xc00 / f);
5749
5750 }
5751}
5752
5753static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5754 u32 msr, int type, bool value)
5755{
5756 if (value)
5757 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5758 else
5759 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5760}
5761
Wincy Vanf2b93282015-02-03 23:56:03 +08005762/*
5763 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5764 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5765 */
5766static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5767 unsigned long *msr_bitmap_nested,
5768 u32 msr, int type)
5769{
5770 int f = sizeof(unsigned long);
5771
Wincy Vanf2b93282015-02-03 23:56:03 +08005772 /*
5773 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5774 * have the write-low and read-high bitmap offsets the wrong way round.
5775 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5776 */
5777 if (msr <= 0x1fff) {
5778 if (type & MSR_TYPE_R &&
5779 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5780 /* read-low */
5781 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5782
5783 if (type & MSR_TYPE_W &&
5784 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5785 /* write-low */
5786 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5787
5788 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5789 msr &= 0x1fff;
5790 if (type & MSR_TYPE_R &&
5791 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5792 /* read-high */
5793 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5794
5795 if (type & MSR_TYPE_W &&
5796 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5797 /* write-high */
5798 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5799
5800 }
5801}
5802
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005803static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005804{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005805 u8 mode = 0;
5806
5807 if (cpu_has_secondary_exec_ctrls() &&
5808 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5809 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5810 mode |= MSR_BITMAP_MODE_X2APIC;
5811 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5812 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5813 }
5814
5815 if (is_long_mode(vcpu))
5816 mode |= MSR_BITMAP_MODE_LM;
5817
5818 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005819}
5820
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005821#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5822
5823static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5824 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005825{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005826 int msr;
5827
5828 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5829 unsigned word = msr / BITS_PER_LONG;
5830 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5831 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005832 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005833
5834 if (mode & MSR_BITMAP_MODE_X2APIC) {
5835 /*
5836 * TPR reads and writes can be virtualized even if virtual interrupt
5837 * delivery is not in use.
5838 */
5839 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5840 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5841 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5842 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5843 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5844 }
5845 }
5846}
5847
5848static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5849{
5850 struct vcpu_vmx *vmx = to_vmx(vcpu);
5851 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5852 u8 mode = vmx_msr_bitmap_mode(vcpu);
5853 u8 changed = mode ^ vmx->msr_bitmap_mode;
5854
5855 if (!changed)
5856 return;
5857
5858 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5859 !(mode & MSR_BITMAP_MODE_LM));
5860
5861 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5862 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5863
5864 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005865}
5866
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005867static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005868{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005869 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005870}
5871
David Matlackc9f04402017-08-01 14:00:40 -07005872static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5873{
5874 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5875 gfn_t gfn;
5876
5877 /*
5878 * Don't need to mark the APIC access page dirty; it is never
5879 * written to by the CPU during APIC virtualization.
5880 */
5881
5882 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5883 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5884 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5885 }
5886
5887 if (nested_cpu_has_posted_intr(vmcs12)) {
5888 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5889 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5890 }
5891}
5892
5893
David Hildenbrand6342c502017-01-25 11:58:58 +01005894static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005895{
5896 struct vcpu_vmx *vmx = to_vmx(vcpu);
5897 int max_irr;
5898 void *vapic_page;
5899 u16 status;
5900
David Matlackc9f04402017-08-01 14:00:40 -07005901 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5902 return;
Wincy Van705699a2015-02-03 23:58:17 +08005903
David Matlackc9f04402017-08-01 14:00:40 -07005904 vmx->nested.pi_pending = false;
5905 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5906 return;
Wincy Van705699a2015-02-03 23:58:17 +08005907
David Matlackc9f04402017-08-01 14:00:40 -07005908 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5909 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005910 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005911 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5912 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005913 kunmap(vmx->nested.virtual_apic_page);
5914
5915 status = vmcs_read16(GUEST_INTR_STATUS);
5916 if ((u8)max_irr > ((u8)status & 0xff)) {
5917 status &= ~0xff;
5918 status |= (u8)max_irr;
5919 vmcs_write16(GUEST_INTR_STATUS, status);
5920 }
5921 }
David Matlackc9f04402017-08-01 14:00:40 -07005922
5923 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005924}
5925
Wincy Van06a55242017-04-28 13:13:59 +08005926static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5927 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005928{
5929#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005930 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5931
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005932 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005933 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005934 * The vector of interrupt to be delivered to vcpu had
5935 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005936 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005937 * Following cases will be reached in this block, and
5938 * we always send a notification event in all cases as
5939 * explained below.
5940 *
5941 * Case 1: vcpu keeps in non-root mode. Sending a
5942 * notification event posts the interrupt to vcpu.
5943 *
5944 * Case 2: vcpu exits to root mode and is still
5945 * runnable. PIR will be synced to vIRR before the
5946 * next vcpu entry. Sending a notification event in
5947 * this case has no effect, as vcpu is not in root
5948 * mode.
5949 *
5950 * Case 3: vcpu exits to root mode and is blocked.
5951 * vcpu_block() has already synced PIR to vIRR and
5952 * never blocks vcpu if vIRR is not cleared. Therefore,
5953 * a blocked vcpu here does not wait for any requested
5954 * interrupts in PIR, and sending a notification event
5955 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005956 */
Feng Wu28b835d2015-09-18 22:29:54 +08005957
Wincy Van06a55242017-04-28 13:13:59 +08005958 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005959 return true;
5960 }
5961#endif
5962 return false;
5963}
5964
Wincy Van705699a2015-02-03 23:58:17 +08005965static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5966 int vector)
5967{
5968 struct vcpu_vmx *vmx = to_vmx(vcpu);
5969
5970 if (is_guest_mode(vcpu) &&
5971 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005972 /*
5973 * If a posted intr is not recognized by hardware,
5974 * we will accomplish it in the next vmentry.
5975 */
5976 vmx->nested.pi_pending = true;
5977 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005978 /* the PIR and ON have been set by L1. */
5979 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5980 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005981 return 0;
5982 }
5983 return -1;
5984}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005986 * Send interrupt to vcpu via posted interrupt way.
5987 * 1. If target vcpu is running(non-root mode), send posted interrupt
5988 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5989 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5990 * interrupt from PIR in next vmentry.
5991 */
5992static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5993{
5994 struct vcpu_vmx *vmx = to_vmx(vcpu);
5995 int r;
5996
Wincy Van705699a2015-02-03 23:58:17 +08005997 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5998 if (!r)
5999 return;
6000
Yang Zhanga20ed542013-04-11 19:25:15 +08006001 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6002 return;
6003
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006004 /* If a previous notification has sent the IPI, nothing to do. */
6005 if (pi_test_and_set_on(&vmx->pi_desc))
6006 return;
6007
Wincy Van06a55242017-04-28 13:13:59 +08006008 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006009 kvm_vcpu_kick(vcpu);
6010}
6011
Avi Kivity6aa8b732006-12-10 02:21:36 -08006012/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006013 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6014 * will not change in the lifetime of the guest.
6015 * Note that host-state that does change is set elsewhere. E.g., host-state
6016 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6017 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006018static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006019{
6020 u32 low32, high32;
6021 unsigned long tmpl;
6022 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006023 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006024
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006025 cr0 = read_cr0();
6026 WARN_ON(cr0 & X86_CR0_TS);
6027 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006028
6029 /*
6030 * Save the most likely value for this task's CR3 in the VMCS.
6031 * We can't use __get_current_cr3_fast() because we're not atomic.
6032 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006033 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006034 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006035 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006036
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006037 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006038 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006039 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006040 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006041
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006042 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006043#ifdef CONFIG_X86_64
6044 /*
6045 * Load null selectors, so we can avoid reloading them in
6046 * __vmx_load_host_state(), in case userspace uses the null selectors
6047 * too (the expected case).
6048 */
6049 vmcs_write16(HOST_DS_SELECTOR, 0);
6050 vmcs_write16(HOST_ES_SELECTOR, 0);
6051#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006052 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6053 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006054#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006055 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6056 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6057
Juergen Gross87930012017-09-04 12:25:27 +02006058 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006059 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006060 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006061
Avi Kivity83287ea422012-09-16 15:10:57 +03006062 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006063
6064 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6065 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6066 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6067 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6068
6069 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6070 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6071 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6072 }
6073}
6074
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006075static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6076{
6077 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6078 if (enable_ept)
6079 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006080 if (is_guest_mode(&vmx->vcpu))
6081 vmx->vcpu.arch.cr4_guest_owned_bits &=
6082 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006083 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6084}
6085
Yang Zhang01e439b2013-04-11 19:25:12 +08006086static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6087{
6088 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6089
Andrey Smetanind62caab2015-11-10 15:36:33 +03006090 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006091 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006092
6093 if (!enable_vnmi)
6094 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6095
Yunhong Jiang64672c92016-06-13 14:19:59 -07006096 /* Enable the preemption timer dynamically */
6097 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006098 return pin_based_exec_ctrl;
6099}
6100
Andrey Smetanind62caab2015-11-10 15:36:33 +03006101static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6102{
6103 struct vcpu_vmx *vmx = to_vmx(vcpu);
6104
6105 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006106 if (cpu_has_secondary_exec_ctrls()) {
6107 if (kvm_vcpu_apicv_active(vcpu))
6108 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6109 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6110 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6111 else
6112 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6113 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6114 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6115 }
6116
6117 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006118 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006119}
6120
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006121static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6122{
6123 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006124
6125 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6126 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6127
Paolo Bonzini35754c92015-07-29 12:05:37 +02006128 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006129 exec_control &= ~CPU_BASED_TPR_SHADOW;
6130#ifdef CONFIG_X86_64
6131 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6132 CPU_BASED_CR8_LOAD_EXITING;
6133#endif
6134 }
6135 if (!enable_ept)
6136 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6137 CPU_BASED_CR3_LOAD_EXITING |
6138 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006139 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6140 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6141 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006142 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6143 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006144 return exec_control;
6145}
6146
Jim Mattson45ec3682017-08-23 16:32:04 -07006147static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006148{
Jim Mattson45ec3682017-08-23 16:32:04 -07006149 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006150 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006151}
6152
Jim Mattson75f4fc82017-08-23 16:32:03 -07006153static bool vmx_rdseed_supported(void)
6154{
6155 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006156 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006157}
6158
Paolo Bonzini80154d72017-08-24 13:55:35 +02006159static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006160{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006161 struct kvm_vcpu *vcpu = &vmx->vcpu;
6162
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006163 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006164
Paolo Bonzini80154d72017-08-24 13:55:35 +02006165 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006166 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6167 if (vmx->vpid == 0)
6168 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6169 if (!enable_ept) {
6170 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6171 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006172 /* Enable INVPCID for non-ept guests may cause performance regression. */
6173 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006174 }
6175 if (!enable_unrestricted_guest)
6176 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006177 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006178 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006179 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006180 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6181 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006182 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006183
6184 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6185 * in vmx_set_cr4. */
6186 exec_control &= ~SECONDARY_EXEC_DESC;
6187
Abel Gordonabc4fc52013-04-18 14:35:25 +03006188 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6189 (handle_vmptrld).
6190 We can NOT enable shadow_vmcs here because we don't have yet
6191 a current VMCS12
6192 */
6193 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006194
6195 if (!enable_pml)
6196 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006197
Paolo Bonzini3db13482017-08-24 14:48:03 +02006198 if (vmx_xsaves_supported()) {
6199 /* Exposing XSAVES only when XSAVE is exposed */
6200 bool xsaves_enabled =
6201 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6202 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6203
6204 if (!xsaves_enabled)
6205 exec_control &= ~SECONDARY_EXEC_XSAVES;
6206
6207 if (nested) {
6208 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006209 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006210 SECONDARY_EXEC_XSAVES;
6211 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006212 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006213 ~SECONDARY_EXEC_XSAVES;
6214 }
6215 }
6216
Paolo Bonzini80154d72017-08-24 13:55:35 +02006217 if (vmx_rdtscp_supported()) {
6218 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6219 if (!rdtscp_enabled)
6220 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6221
6222 if (nested) {
6223 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006224 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006225 SECONDARY_EXEC_RDTSCP;
6226 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006227 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006228 ~SECONDARY_EXEC_RDTSCP;
6229 }
6230 }
6231
6232 if (vmx_invpcid_supported()) {
6233 /* Exposing INVPCID only when PCID is exposed */
6234 bool invpcid_enabled =
6235 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6236 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6237
6238 if (!invpcid_enabled) {
6239 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6240 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6241 }
6242
6243 if (nested) {
6244 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006245 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006246 SECONDARY_EXEC_ENABLE_INVPCID;
6247 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006248 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006249 ~SECONDARY_EXEC_ENABLE_INVPCID;
6250 }
6251 }
6252
Jim Mattson45ec3682017-08-23 16:32:04 -07006253 if (vmx_rdrand_supported()) {
6254 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6255 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006256 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006257
6258 if (nested) {
6259 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006260 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006261 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006262 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006263 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006264 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006265 }
6266 }
6267
Jim Mattson75f4fc82017-08-23 16:32:03 -07006268 if (vmx_rdseed_supported()) {
6269 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6270 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006271 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006272
6273 if (nested) {
6274 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006275 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006276 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006277 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006278 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006279 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006280 }
6281 }
6282
Paolo Bonzini80154d72017-08-24 13:55:35 +02006283 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006284}
6285
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006286static void ept_set_mmio_spte_mask(void)
6287{
6288 /*
6289 * EPT Misconfigurations can be generated if the value of bits 2:0
6290 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006291 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006292 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6293 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006294}
6295
Wanpeng Lif53cd632014-12-02 19:14:58 +08006296#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006297/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006298 * Sets up the vmcs for emulated real mode.
6299 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006300static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006301{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006302#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006304#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006305 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006306
Abel Gordon4607c2d2013-04-18 14:35:55 +03006307 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006308 /*
6309 * At vCPU creation, "VMWRITE to any supported field
6310 * in the VMCS" is supported, so use the more
6311 * permissive vmx_vmread_bitmap to specify both read
6312 * and write permissions for the shadow VMCS.
6313 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006314 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006315 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006316 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006317 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006318 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006319
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6321
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006323 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006324 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006325
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006326 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006327
Dan Williamsdfa169b2016-06-02 11:17:24 -07006328 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006329 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006330 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006331 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006332 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006333
Andrey Smetanind62caab2015-11-10 15:36:33 +03006334 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006335 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6336 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6337 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6338 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6339
6340 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006341
Li RongQing0bcf2612015-12-03 13:29:34 +08006342 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006343 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006344 }
6345
Wanpeng Lib31c1142018-03-12 04:53:04 -07006346 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006347 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006348 vmx->ple_window = ple_window;
6349 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006350 }
6351
Xiao Guangrongc3707952011-07-12 03:28:04 +08006352 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6353 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006354 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6355
Avi Kivity9581d442010-10-19 16:46:55 +02006356 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6357 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006358 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006359#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006360 rdmsrl(MSR_FS_BASE, a);
6361 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6362 rdmsrl(MSR_GS_BASE, a);
6363 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6364#else
6365 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6366 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6367#endif
6368
Bandan Das2a499e42017-08-03 15:54:41 -04006369 if (cpu_has_vmx_vmfunc())
6370 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6371
Eddie Dong2cc51562007-05-21 07:28:09 +03006372 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6373 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006374 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006375 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006376 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006377
Radim Krčmář74545702015-04-27 15:11:25 +02006378 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6379 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006380
Paolo Bonzini03916db2014-07-24 14:21:57 +02006381 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006382 u32 index = vmx_msr_index[i];
6383 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006384 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006385
6386 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6387 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006388 if (wrmsr_safe(index, data_low, data_high) < 0)
6389 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006390 vmx->guest_msrs[j].index = i;
6391 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006392 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006393 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006394 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006395
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006396 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6397 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006398
6399 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006400
6401 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006402 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006403
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006404 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6405 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6406
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006407 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006408
Wanpeng Lif53cd632014-12-02 19:14:58 +08006409 if (vmx_xsaves_supported())
6410 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6411
Peter Feiner4e595162016-07-07 14:49:58 -07006412 if (enable_pml) {
6413 ASSERT(vmx->pml_pg);
6414 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6415 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6416 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006417}
6418
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006419static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006420{
6421 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006422 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006423 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006424
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006425 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006426 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006427
Wanpeng Li518e7b92018-02-28 14:03:31 +08006428 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006429 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006430 kvm_set_cr8(vcpu, 0);
6431
6432 if (!init_event) {
6433 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6434 MSR_IA32_APICBASE_ENABLE;
6435 if (kvm_vcpu_is_reset_bsp(vcpu))
6436 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6437 apic_base_msr.host_initiated = true;
6438 kvm_set_apic_base(vcpu, &apic_base_msr);
6439 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006440
Avi Kivity2fb92db2011-04-27 19:42:18 +03006441 vmx_segment_cache_clear(vmx);
6442
Avi Kivity5706be02008-08-20 15:07:31 +03006443 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006444 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006445 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006446
6447 seg_setup(VCPU_SREG_DS);
6448 seg_setup(VCPU_SREG_ES);
6449 seg_setup(VCPU_SREG_FS);
6450 seg_setup(VCPU_SREG_GS);
6451 seg_setup(VCPU_SREG_SS);
6452
6453 vmcs_write16(GUEST_TR_SELECTOR, 0);
6454 vmcs_writel(GUEST_TR_BASE, 0);
6455 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6456 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6457
6458 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6459 vmcs_writel(GUEST_LDTR_BASE, 0);
6460 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6461 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6462
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006463 if (!init_event) {
6464 vmcs_write32(GUEST_SYSENTER_CS, 0);
6465 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6466 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6467 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6468 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006469
Wanpeng Lic37c2872017-11-20 14:52:21 -08006470 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006471 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006472
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006473 vmcs_writel(GUEST_GDTR_BASE, 0);
6474 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6475
6476 vmcs_writel(GUEST_IDTR_BASE, 0);
6477 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6478
Anthony Liguori443381a2010-12-06 10:53:38 -06006479 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006480 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006481 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006482 if (kvm_mpx_supported())
6483 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006484
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006485 setup_msrs(vmx);
6486
Avi Kivity6aa8b732006-12-10 02:21:36 -08006487 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6488
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006489 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006490 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006491 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006492 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006493 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006494 vmcs_write32(TPR_THRESHOLD, 0);
6495 }
6496
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006497 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006498
Sheng Yang2384d2b2008-01-17 15:14:33 +08006499 if (vmx->vpid != 0)
6500 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6501
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006502 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006503 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006504 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006505 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006506 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006507
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006508 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006509
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006510 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006511 if (init_event)
6512 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006513}
6514
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006515/*
6516 * In nested virtualization, check if L1 asked to exit on external interrupts.
6517 * For most existing hypervisors, this will always return true.
6518 */
6519static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6520{
6521 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6522 PIN_BASED_EXT_INTR_MASK;
6523}
6524
Bandan Das77b0f5d2014-04-19 18:17:45 -04006525/*
6526 * In nested virtualization, check if L1 has set
6527 * VM_EXIT_ACK_INTR_ON_EXIT
6528 */
6529static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6530{
6531 return get_vmcs12(vcpu)->vm_exit_controls &
6532 VM_EXIT_ACK_INTR_ON_EXIT;
6533}
6534
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006535static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6536{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006537 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006538}
6539
Jan Kiszkac9a79532014-03-07 20:03:15 +01006540static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006541{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006542 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6543 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006544}
6545
Jan Kiszkac9a79532014-03-07 20:03:15 +01006546static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006547{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006548 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006549 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006550 enable_irq_window(vcpu);
6551 return;
6552 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006553
Paolo Bonzini47c01522016-12-19 11:44:07 +01006554 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6555 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006556}
6557
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006558static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006559{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006560 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006561 uint32_t intr;
6562 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006563
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006564 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006565
Avi Kivityfa89a812008-09-01 15:57:51 +03006566 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006567 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006568 int inc_eip = 0;
6569 if (vcpu->arch.interrupt.soft)
6570 inc_eip = vcpu->arch.event_exit_inst_len;
6571 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006572 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006573 return;
6574 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006575 intr = irq | INTR_INFO_VALID_MASK;
6576 if (vcpu->arch.interrupt.soft) {
6577 intr |= INTR_TYPE_SOFT_INTR;
6578 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6579 vmx->vcpu.arch.event_exit_inst_len);
6580 } else
6581 intr |= INTR_TYPE_EXT_INTR;
6582 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006583
6584 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006585}
6586
Sheng Yangf08864b2008-05-15 18:23:25 +08006587static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6588{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006589 struct vcpu_vmx *vmx = to_vmx(vcpu);
6590
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006591 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006592 /*
6593 * Tracking the NMI-blocked state in software is built upon
6594 * finding the next open IRQ window. This, in turn, depends on
6595 * well-behaving guests: They have to keep IRQs disabled at
6596 * least as long as the NMI handler runs. Otherwise we may
6597 * cause NMI nesting, maybe breaking the guest. But as this is
6598 * highly unlikely, we can live with the residual risk.
6599 */
6600 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6601 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6602 }
6603
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006604 ++vcpu->stat.nmi_injections;
6605 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006606
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006607 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006608 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006609 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006610 return;
6611 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006612
Sheng Yangf08864b2008-05-15 18:23:25 +08006613 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6614 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006615
6616 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006617}
6618
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006619static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6620{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006621 struct vcpu_vmx *vmx = to_vmx(vcpu);
6622 bool masked;
6623
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006624 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006625 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006626 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006627 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006628 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6629 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6630 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006631}
6632
6633static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6634{
6635 struct vcpu_vmx *vmx = to_vmx(vcpu);
6636
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006637 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006638 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6639 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6640 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6641 }
6642 } else {
6643 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6644 if (masked)
6645 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6646 GUEST_INTR_STATE_NMI);
6647 else
6648 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6649 GUEST_INTR_STATE_NMI);
6650 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006651}
6652
Jan Kiszka2505dc92013-04-14 12:12:47 +02006653static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6654{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006655 if (to_vmx(vcpu)->nested.nested_run_pending)
6656 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006657
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006658 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006659 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6660 return 0;
6661
Jan Kiszka2505dc92013-04-14 12:12:47 +02006662 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6663 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6664 | GUEST_INTR_STATE_NMI));
6665}
6666
Gleb Natapov78646122009-03-23 12:12:11 +02006667static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6668{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006669 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6670 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006671 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6672 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006673}
6674
Izik Eiduscbc94022007-10-25 00:29:55 +02006675static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6676{
6677 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006678
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006679 if (enable_unrestricted_guest)
6680 return 0;
6681
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006682 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6683 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006684 if (ret)
6685 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006686 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006687 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006688}
6689
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006690static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6691{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006692 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006693 return 0;
6694}
6695
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006696static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006697{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006698 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006699 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006700 /*
6701 * Update instruction length as we may reinject the exception
6702 * from user space while in guest debugging mode.
6703 */
6704 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6705 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006706 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006707 return false;
6708 /* fall through */
6709 case DB_VECTOR:
6710 if (vcpu->guest_debug &
6711 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6712 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006713 /* fall through */
6714 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006715 case OF_VECTOR:
6716 case BR_VECTOR:
6717 case UD_VECTOR:
6718 case DF_VECTOR:
6719 case SS_VECTOR:
6720 case GP_VECTOR:
6721 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006722 return true;
6723 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006724 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006725 return false;
6726}
6727
6728static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6729 int vec, u32 err_code)
6730{
6731 /*
6732 * Instruction with address size override prefix opcode 0x67
6733 * Cause the #SS fault with 0 error code in VM86 mode.
6734 */
6735 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6736 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6737 if (vcpu->arch.halt_request) {
6738 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006739 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006740 }
6741 return 1;
6742 }
6743 return 0;
6744 }
6745
6746 /*
6747 * Forward all other exceptions that are valid in real mode.
6748 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6749 * the required debugging infrastructure rework.
6750 */
6751 kvm_queue_exception(vcpu, vec);
6752 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753}
6754
Andi Kleena0861c02009-06-08 17:37:09 +08006755/*
6756 * Trigger machine check on the host. We assume all the MSRs are already set up
6757 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6758 * We pass a fake environment to the machine check handler because we want
6759 * the guest to be always treated like user space, no matter what context
6760 * it used internally.
6761 */
6762static void kvm_machine_check(void)
6763{
6764#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6765 struct pt_regs regs = {
6766 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6767 .flags = X86_EFLAGS_IF,
6768 };
6769
6770 do_machine_check(&regs, 0);
6771#endif
6772}
6773
Avi Kivity851ba692009-08-24 11:10:17 +03006774static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006775{
6776 /* already handled by vcpu_run */
6777 return 1;
6778}
6779
Avi Kivity851ba692009-08-24 11:10:17 +03006780static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006781{
Avi Kivity1155f762007-11-22 11:30:47 +02006782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006783 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006784 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006785 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786 u32 vect_info;
6787 enum emulation_result er;
6788
Avi Kivity1155f762007-11-22 11:30:47 +02006789 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006790 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006791
Andi Kleena0861c02009-06-08 17:37:09 +08006792 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006793 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006794
Jim Mattsonef85b672016-12-12 11:01:37 -08006795 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006796 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006797
Wanpeng Li082d06e2018-04-03 16:28:48 -07006798 if (is_invalid_opcode(intr_info))
6799 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006800
Avi Kivity6aa8b732006-12-10 02:21:36 -08006801 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006802 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006803 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006804
Liran Alon9e869482018-03-12 13:12:51 +02006805 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6806 WARN_ON_ONCE(!enable_vmware_backdoor);
6807 er = emulate_instruction(vcpu,
6808 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6809 if (er == EMULATE_USER_EXIT)
6810 return 0;
6811 else if (er != EMULATE_DONE)
6812 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6813 return 1;
6814 }
6815
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006816 /*
6817 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6818 * MMIO, it is better to report an internal error.
6819 * See the comments in vmx_handle_exit.
6820 */
6821 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6822 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6823 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6824 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006825 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006826 vcpu->run->internal.data[0] = vect_info;
6827 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006828 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006829 return 0;
6830 }
6831
Avi Kivity6aa8b732006-12-10 02:21:36 -08006832 if (is_page_fault(intr_info)) {
6833 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006834 /* EPT won't cause page fault directly */
6835 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006836 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837 }
6838
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006839 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006840
6841 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6842 return handle_rmode_exception(vcpu, ex_no, error_code);
6843
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006844 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006845 case AC_VECTOR:
6846 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6847 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006848 case DB_VECTOR:
6849 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6850 if (!(vcpu->guest_debug &
6851 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006852 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006853 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006854 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006855 skip_emulated_instruction(vcpu);
6856
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006857 kvm_queue_exception(vcpu, DB_VECTOR);
6858 return 1;
6859 }
6860 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6861 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6862 /* fall through */
6863 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006864 /*
6865 * Update instruction length as we may reinject #BP from
6866 * user space while in guest debugging mode. Reading it for
6867 * #DB as well causes no harm, it is not used in that case.
6868 */
6869 vmx->vcpu.arch.event_exit_inst_len =
6870 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006871 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006872 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006873 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6874 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006875 break;
6876 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006877 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6878 kvm_run->ex.exception = ex_no;
6879 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006881 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006882 return 0;
6883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006886{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006887 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006888 return 1;
6889}
6890
Avi Kivity851ba692009-08-24 11:10:17 +03006891static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006892{
Avi Kivity851ba692009-08-24 11:10:17 +03006893 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006894 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006895 return 0;
6896}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006897
Avi Kivity851ba692009-08-24 11:10:17 +03006898static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899{
He, Qingbfdaab02007-09-12 14:18:28 +08006900 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006901 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006902 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006903
He, Qingbfdaab02007-09-12 14:18:28 +08006904 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006905 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006906
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006907 ++vcpu->stat.io_exits;
6908
Sean Christopherson432baf62018-03-08 08:57:26 -08006909 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006910 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006911
6912 port = exit_qualification >> 16;
6913 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006914 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006915
Sean Christophersondca7f122018-03-08 08:57:27 -08006916 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006917}
6918
Ingo Molnar102d8322007-02-19 14:37:47 +02006919static void
6920vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6921{
6922 /*
6923 * Patch in the VMCALL instruction:
6924 */
6925 hypercall[0] = 0x0f;
6926 hypercall[1] = 0x01;
6927 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006928}
6929
Guo Chao0fa06072012-06-28 15:16:19 +08006930/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006931static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6932{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006933 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006934 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6935 unsigned long orig_val = val;
6936
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006937 /*
6938 * We get here when L2 changed cr0 in a way that did not change
6939 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006940 * but did change L0 shadowed bits. So we first calculate the
6941 * effective cr0 value that L1 would like to write into the
6942 * hardware. It consists of the L2-owned bits from the new
6943 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006944 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006945 val = (val & ~vmcs12->cr0_guest_host_mask) |
6946 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6947
David Matlack38991522016-11-29 18:14:08 -08006948 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006949 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006950
6951 if (kvm_set_cr0(vcpu, val))
6952 return 1;
6953 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006954 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006955 } else {
6956 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006957 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006958 return 1;
David Matlack38991522016-11-29 18:14:08 -08006959
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006960 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006961 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006962}
6963
6964static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6965{
6966 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006967 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6968 unsigned long orig_val = val;
6969
6970 /* analogously to handle_set_cr0 */
6971 val = (val & ~vmcs12->cr4_guest_host_mask) |
6972 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6973 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006974 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006975 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006976 return 0;
6977 } else
6978 return kvm_set_cr4(vcpu, val);
6979}
6980
Paolo Bonzini0367f202016-07-12 10:44:55 +02006981static int handle_desc(struct kvm_vcpu *vcpu)
6982{
6983 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6984 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6985}
6986
Avi Kivity851ba692009-08-24 11:10:17 +03006987static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006989 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006990 int cr;
6991 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006992 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006993 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006994
He, Qingbfdaab02007-09-12 14:18:28 +08006995 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006996 cr = exit_qualification & 15;
6997 reg = (exit_qualification >> 8) & 15;
6998 switch ((exit_qualification >> 4) & 3) {
6999 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007000 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007001 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007002 switch (cr) {
7003 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007004 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007005 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007006 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007007 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007008 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007009 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007010 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007011 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007012 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007013 case 8: {
7014 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007015 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007016 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007017 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007018 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007019 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007020 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007021 return ret;
7022 /*
7023 * TODO: we might be squashing a
7024 * KVM_GUESTDBG_SINGLESTEP-triggered
7025 * KVM_EXIT_DEBUG here.
7026 */
Avi Kivity851ba692009-08-24 11:10:17 +03007027 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007028 return 0;
7029 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007030 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007032 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007033 WARN_ONCE(1, "Guest should always own CR0.TS");
7034 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007035 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007036 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007037 case 1: /*mov from cr*/
7038 switch (cr) {
7039 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007040 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007041 val = kvm_read_cr3(vcpu);
7042 kvm_register_write(vcpu, reg, val);
7043 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007044 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007045 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007046 val = kvm_get_cr8(vcpu);
7047 kvm_register_write(vcpu, reg, val);
7048 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007049 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050 }
7051 break;
7052 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007053 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007054 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007055 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007056
Kyle Huey6affcbe2016-11-29 12:40:40 -08007057 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058 default:
7059 break;
7060 }
Avi Kivity851ba692009-08-24 11:10:17 +03007061 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007062 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007063 (int)(exit_qualification >> 4) & 3, cr);
7064 return 0;
7065}
7066
Avi Kivity851ba692009-08-24 11:10:17 +03007067static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007068{
He, Qingbfdaab02007-09-12 14:18:28 +08007069 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007070 int dr, dr7, reg;
7071
7072 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7073 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7074
7075 /* First, if DR does not exist, trigger UD */
7076 if (!kvm_require_dr(vcpu, dr))
7077 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078
Jan Kiszkaf2483412010-01-20 18:20:20 +01007079 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007080 if (!kvm_require_cpl(vcpu, 0))
7081 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007082 dr7 = vmcs_readl(GUEST_DR7);
7083 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007084 /*
7085 * As the vm-exit takes precedence over the debug trap, we
7086 * need to emulate the latter, either for the host or the
7087 * guest debugging itself.
7088 */
7089 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007090 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007091 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007092 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007093 vcpu->run->debug.arch.exception = DB_VECTOR;
7094 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007095 return 0;
7096 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007097 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007098 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007099 kvm_queue_exception(vcpu, DB_VECTOR);
7100 return 1;
7101 }
7102 }
7103
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007104 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007105 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7106 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007107
7108 /*
7109 * No more DR vmexits; force a reload of the debug registers
7110 * and reenter on this instruction. The next vmexit will
7111 * retrieve the full state of the debug registers.
7112 */
7113 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7114 return 1;
7115 }
7116
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007117 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7118 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007119 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007120
7121 if (kvm_get_dr(vcpu, dr, &val))
7122 return 1;
7123 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007124 } else
Nadav Amit57773922014-06-18 17:19:23 +03007125 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007126 return 1;
7127
Kyle Huey6affcbe2016-11-29 12:40:40 -08007128 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007129}
7130
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007131static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7132{
7133 return vcpu->arch.dr6;
7134}
7135
7136static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7137{
7138}
7139
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007140static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7141{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007142 get_debugreg(vcpu->arch.db[0], 0);
7143 get_debugreg(vcpu->arch.db[1], 1);
7144 get_debugreg(vcpu->arch.db[2], 2);
7145 get_debugreg(vcpu->arch.db[3], 3);
7146 get_debugreg(vcpu->arch.dr6, 6);
7147 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7148
7149 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007150 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007151}
7152
Gleb Natapov020df072010-04-13 10:05:23 +03007153static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7154{
7155 vmcs_writel(GUEST_DR7, val);
7156}
7157
Avi Kivity851ba692009-08-24 11:10:17 +03007158static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159{
Kyle Huey6a908b62016-11-29 12:40:37 -08007160 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007161}
7162
Avi Kivity851ba692009-08-24 11:10:17 +03007163static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007164{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007165 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007166 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007167
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007168 msr_info.index = ecx;
7169 msr_info.host_initiated = false;
7170 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007171 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007172 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007173 return 1;
7174 }
7175
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007176 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007177
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007179 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7180 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007181 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007182}
7183
Avi Kivity851ba692009-08-24 11:10:17 +03007184static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007185{
Will Auld8fe8ab42012-11-29 12:42:12 -08007186 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007187 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7188 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7189 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007190
Will Auld8fe8ab42012-11-29 12:42:12 -08007191 msr.data = data;
7192 msr.index = ecx;
7193 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007194 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007195 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007196 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007197 return 1;
7198 }
7199
Avi Kivity59200272010-01-25 19:47:02 +02007200 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007201 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007202}
7203
Avi Kivity851ba692009-08-24 11:10:17 +03007204static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007205{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007206 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007207 return 1;
7208}
7209
Avi Kivity851ba692009-08-24 11:10:17 +03007210static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007211{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007212 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7213 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007214
Avi Kivity3842d132010-07-27 12:30:24 +03007215 kvm_make_request(KVM_REQ_EVENT, vcpu);
7216
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007217 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007218 return 1;
7219}
7220
Avi Kivity851ba692009-08-24 11:10:17 +03007221static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007222{
Avi Kivityd3bef152007-06-05 15:53:05 +03007223 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007224}
7225
Avi Kivity851ba692009-08-24 11:10:17 +03007226static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007227{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007228 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007229}
7230
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007231static int handle_invd(struct kvm_vcpu *vcpu)
7232{
Andre Przywara51d8b662010-12-21 11:12:02 +01007233 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007234}
7235
Avi Kivity851ba692009-08-24 11:10:17 +03007236static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007237{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007238 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007239
7240 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007241 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007242}
7243
Avi Kivityfee84b02011-11-10 14:57:25 +02007244static int handle_rdpmc(struct kvm_vcpu *vcpu)
7245{
7246 int err;
7247
7248 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007249 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007250}
7251
Avi Kivity851ba692009-08-24 11:10:17 +03007252static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007253{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007255}
7256
Dexuan Cui2acf9232010-06-10 11:27:12 +08007257static int handle_xsetbv(struct kvm_vcpu *vcpu)
7258{
7259 u64 new_bv = kvm_read_edx_eax(vcpu);
7260 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7261
7262 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007264 return 1;
7265}
7266
Wanpeng Lif53cd632014-12-02 19:14:58 +08007267static int handle_xsaves(struct kvm_vcpu *vcpu)
7268{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007269 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007270 WARN(1, "this should never happen\n");
7271 return 1;
7272}
7273
7274static int handle_xrstors(struct kvm_vcpu *vcpu)
7275{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007276 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007277 WARN(1, "this should never happen\n");
7278 return 1;
7279}
7280
Avi Kivity851ba692009-08-24 11:10:17 +03007281static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007282{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007283 if (likely(fasteoi)) {
7284 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7285 int access_type, offset;
7286
7287 access_type = exit_qualification & APIC_ACCESS_TYPE;
7288 offset = exit_qualification & APIC_ACCESS_OFFSET;
7289 /*
7290 * Sane guest uses MOV to write EOI, with written value
7291 * not cared. So make a short-circuit here by avoiding
7292 * heavy instruction emulation.
7293 */
7294 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7295 (offset == APIC_EOI)) {
7296 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007297 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007298 }
7299 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007300 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007301}
7302
Yang Zhangc7c9c562013-01-25 10:18:51 +08007303static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7304{
7305 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7306 int vector = exit_qualification & 0xff;
7307
7308 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7309 kvm_apic_set_eoi_accelerated(vcpu, vector);
7310 return 1;
7311}
7312
Yang Zhang83d4c282013-01-25 10:18:49 +08007313static int handle_apic_write(struct kvm_vcpu *vcpu)
7314{
7315 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7316 u32 offset = exit_qualification & 0xfff;
7317
7318 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7319 kvm_apic_write_nodecode(vcpu, offset);
7320 return 1;
7321}
7322
Avi Kivity851ba692009-08-24 11:10:17 +03007323static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007324{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007325 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007326 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007327 bool has_error_code = false;
7328 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007329 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007330 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007331
7332 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007333 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007334 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007335
7336 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7337
7338 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007339 if (reason == TASK_SWITCH_GATE && idt_v) {
7340 switch (type) {
7341 case INTR_TYPE_NMI_INTR:
7342 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007343 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007344 break;
7345 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007346 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007347 kvm_clear_interrupt_queue(vcpu);
7348 break;
7349 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007350 if (vmx->idt_vectoring_info &
7351 VECTORING_INFO_DELIVER_CODE_MASK) {
7352 has_error_code = true;
7353 error_code =
7354 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7355 }
7356 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007357 case INTR_TYPE_SOFT_EXCEPTION:
7358 kvm_clear_exception_queue(vcpu);
7359 break;
7360 default:
7361 break;
7362 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007363 }
Izik Eidus37817f22008-03-24 23:14:53 +02007364 tss_selector = exit_qualification;
7365
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007366 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7367 type != INTR_TYPE_EXT_INTR &&
7368 type != INTR_TYPE_NMI_INTR))
7369 skip_emulated_instruction(vcpu);
7370
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007371 if (kvm_task_switch(vcpu, tss_selector,
7372 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7373 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007374 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7375 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7376 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007377 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007378 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007379
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007380 /*
7381 * TODO: What about debug traps on tss switch?
7382 * Are we supposed to inject them and update dr6?
7383 */
7384
7385 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007386}
7387
Avi Kivity851ba692009-08-24 11:10:17 +03007388static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007389{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007390 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007391 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007392 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007393
Sheng Yangf9c617f2009-03-25 10:08:52 +08007394 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007395
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007396 /*
7397 * EPT violation happened while executing iret from NMI,
7398 * "blocked by NMI" bit has to be set before next VM entry.
7399 * There are errata that may cause this bit to not be set:
7400 * AAK134, BY25.
7401 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007402 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007403 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007404 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007405 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7406
Sheng Yang14394422008-04-28 12:24:45 +08007407 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007408 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007409
Junaid Shahid27959a42016-12-06 16:46:10 -08007410 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007411 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007412 ? PFERR_USER_MASK : 0;
7413 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007414 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007415 ? PFERR_WRITE_MASK : 0;
7416 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007417 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007418 ? PFERR_FETCH_MASK : 0;
7419 /* ept page table entry is present? */
7420 error_code |= (exit_qualification &
7421 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7422 EPT_VIOLATION_EXECUTABLE))
7423 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007424
Paolo Bonzinieebed242016-11-28 14:39:58 +01007425 error_code |= (exit_qualification & 0x100) != 0 ?
7426 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007427
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007428 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007429 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007430}
7431
Avi Kivity851ba692009-08-24 11:10:17 +03007432static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007433{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007434 gpa_t gpa;
7435
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007436 /*
7437 * A nested guest cannot optimize MMIO vmexits, because we have an
7438 * nGPA here instead of the required GPA.
7439 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007440 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007441 if (!is_guest_mode(vcpu) &&
7442 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007443 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007444 /*
7445 * Doing kvm_skip_emulated_instruction() depends on undefined
7446 * behavior: Intel's manual doesn't mandate
7447 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7448 * occurs and while on real hardware it was observed to be set,
7449 * other hypervisors (namely Hyper-V) don't set it, we end up
7450 * advancing IP with some random value. Disable fast mmio when
7451 * running nested and keep it for real hardware in hope that
7452 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7453 */
7454 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7455 return kvm_skip_emulated_instruction(vcpu);
7456 else
7457 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7458 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007459 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007460
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007461 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007462}
7463
Avi Kivity851ba692009-08-24 11:10:17 +03007464static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007465{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007466 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007467 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7468 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007469 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007470 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007471
7472 return 1;
7473}
7474
Mohammed Gamal80ced182009-09-01 12:48:18 +02007475static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007476{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007477 struct vcpu_vmx *vmx = to_vmx(vcpu);
7478 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007479 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007480 u32 cpu_exec_ctrl;
7481 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007482 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007483
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007484 /*
7485 * We should never reach the point where we are emulating L2
7486 * due to invalid guest state as that means we incorrectly
7487 * allowed a nested VMEntry with an invalid vmcs12.
7488 */
7489 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7490
Avi Kivity49e9d552010-09-19 14:34:08 +02007491 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7492 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007493
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007494 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007495 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007496 return handle_interrupt_window(&vmx->vcpu);
7497
Radim Krčmář72875d82017-04-26 22:32:19 +02007498 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007499 return 1;
7500
Liran Alon9b8ae632017-11-05 16:56:34 +02007501 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007502
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007503 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007504 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007505 ret = 0;
7506 goto out;
7507 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007508
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007509 if (err != EMULATE_DONE)
7510 goto emulation_error;
7511
7512 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7513 vcpu->arch.exception.pending)
7514 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007515
Gleb Natapov8d76c492013-05-08 18:38:44 +03007516 if (vcpu->arch.halt_request) {
7517 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007518 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007519 goto out;
7520 }
7521
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007522 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007523 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007524 if (need_resched())
7525 schedule();
7526 }
7527
Mohammed Gamal80ced182009-09-01 12:48:18 +02007528out:
7529 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007530
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007531emulation_error:
7532 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7533 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7534 vcpu->run->internal.ndata = 0;
7535 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007536}
7537
7538static void grow_ple_window(struct kvm_vcpu *vcpu)
7539{
7540 struct vcpu_vmx *vmx = to_vmx(vcpu);
7541 int old = vmx->ple_window;
7542
Babu Mogerc8e88712018-03-16 16:37:24 -04007543 vmx->ple_window = __grow_ple_window(old, ple_window,
7544 ple_window_grow,
7545 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007546
7547 if (vmx->ple_window != old)
7548 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007549
7550 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007551}
7552
7553static void shrink_ple_window(struct kvm_vcpu *vcpu)
7554{
7555 struct vcpu_vmx *vmx = to_vmx(vcpu);
7556 int old = vmx->ple_window;
7557
Babu Mogerc8e88712018-03-16 16:37:24 -04007558 vmx->ple_window = __shrink_ple_window(old, ple_window,
7559 ple_window_shrink,
7560 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007561
7562 if (vmx->ple_window != old)
7563 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007564
7565 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007566}
7567
7568/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007569 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7570 */
7571static void wakeup_handler(void)
7572{
7573 struct kvm_vcpu *vcpu;
7574 int cpu = smp_processor_id();
7575
7576 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7577 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7578 blocked_vcpu_list) {
7579 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7580
7581 if (pi_test_on(pi_desc) == 1)
7582 kvm_vcpu_kick(vcpu);
7583 }
7584 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7585}
7586
Peng Haoe01bca22018-04-07 05:47:32 +08007587static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007588{
7589 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7590 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7591 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7592 0ull, VMX_EPT_EXECUTABLE_MASK,
7593 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007594 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007595
7596 ept_set_mmio_spte_mask();
7597 kvm_enable_tdp();
7598}
7599
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600static __init int hardware_setup(void)
7601{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007602 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007603
7604 rdmsrl_safe(MSR_EFER, &host_efer);
7605
7606 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7607 kvm_define_shared_msr(i, vmx_msr_index[i]);
7608
Radim Krčmář23611332016-09-29 22:41:33 +02007609 for (i = 0; i < VMX_BITMAP_NR; i++) {
7610 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7611 if (!vmx_bitmap[i])
7612 goto out;
7613 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007614
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007615 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7616 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7617
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007618 if (setup_vmcs_config(&vmcs_config) < 0) {
7619 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007620 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007621 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007622
7623 if (boot_cpu_has(X86_FEATURE_NX))
7624 kvm_enable_efer_bits(EFER_NX);
7625
Wanpeng Li08d839c2017-03-23 05:30:08 -07007626 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7627 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007628 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007629
Tiejun Chenf2c76482014-10-28 10:14:47 +08007630 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007631 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007632 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007633 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007634 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007635
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007636 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007637 enable_ept_ad_bits = 0;
7638
Wanpeng Li8ad81822017-10-09 15:51:53 -07007639 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007640 enable_unrestricted_guest = 0;
7641
Paolo Bonziniad15a292015-01-30 16:18:49 +01007642 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007643 flexpriority_enabled = 0;
7644
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007645 if (!cpu_has_virtual_nmis())
7646 enable_vnmi = 0;
7647
Paolo Bonziniad15a292015-01-30 16:18:49 +01007648 /*
7649 * set_apic_access_page_addr() is used to reload apic access
7650 * page upon invalidation. No need to do anything if not
7651 * using the APIC_ACCESS_ADDR VMCS field.
7652 */
7653 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007654 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007655
7656 if (!cpu_has_vmx_tpr_shadow())
7657 kvm_x86_ops->update_cr8_intercept = NULL;
7658
7659 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7660 kvm_disable_largepages();
7661
Wanpeng Li0f107682017-09-28 18:06:24 -07007662 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007663 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007664 ple_window = 0;
7665 ple_window_grow = 0;
7666 ple_window_max = 0;
7667 ple_window_shrink = 0;
7668 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007669
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007670 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007671 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007672 kvm_x86_ops->sync_pir_to_irr = NULL;
7673 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007674
Haozhong Zhang64903d62015-10-20 15:39:09 +08007675 if (cpu_has_vmx_tsc_scaling()) {
7676 kvm_has_tsc_control = true;
7677 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7678 kvm_tsc_scaling_ratio_frac_bits = 48;
7679 }
7680
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007681 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7682
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007683 if (enable_ept)
7684 vmx_enable_tdp();
7685 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007686 kvm_disable_tdp();
7687
Kai Huang843e4332015-01-28 10:54:28 +08007688 /*
7689 * Only enable PML when hardware supports PML feature, and both EPT
7690 * and EPT A/D bit features are enabled -- PML depends on them to work.
7691 */
7692 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7693 enable_pml = 0;
7694
7695 if (!enable_pml) {
7696 kvm_x86_ops->slot_enable_log_dirty = NULL;
7697 kvm_x86_ops->slot_disable_log_dirty = NULL;
7698 kvm_x86_ops->flush_log_dirty = NULL;
7699 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7700 }
7701
Yunhong Jiang64672c92016-06-13 14:19:59 -07007702 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7703 u64 vmx_msr;
7704
7705 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7706 cpu_preemption_timer_multi =
7707 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7708 } else {
7709 kvm_x86_ops->set_hv_timer = NULL;
7710 kvm_x86_ops->cancel_hv_timer = NULL;
7711 }
7712
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007713 if (!cpu_has_vmx_shadow_vmcs())
7714 enable_shadow_vmcs = 0;
7715 if (enable_shadow_vmcs)
7716 init_vmcs_shadow_fields();
7717
Feng Wubf9f6ac2015-09-18 22:29:55 +08007718 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007719 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007720
Ashok Rajc45dcc72016-06-22 14:59:56 +08007721 kvm_mce_cap_supported |= MCG_LMCE_P;
7722
Tiejun Chenf2c76482014-10-28 10:14:47 +08007723 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007724
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007725out:
Radim Krčmář23611332016-09-29 22:41:33 +02007726 for (i = 0; i < VMX_BITMAP_NR; i++)
7727 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007728
7729 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007730}
7731
7732static __exit void hardware_unsetup(void)
7733{
Radim Krčmář23611332016-09-29 22:41:33 +02007734 int i;
7735
7736 for (i = 0; i < VMX_BITMAP_NR; i++)
7737 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007738
Tiejun Chenf2c76482014-10-28 10:14:47 +08007739 free_kvm_area();
7740}
7741
Avi Kivity6aa8b732006-12-10 02:21:36 -08007742/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007743 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7744 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7745 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007746static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007747{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007748 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007749 grow_ple_window(vcpu);
7750
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007751 /*
7752 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7753 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7754 * never set PAUSE_EXITING and just set PLE if supported,
7755 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7756 */
7757 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007758 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007759}
7760
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007761static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007762{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007763 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007764}
7765
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007766static int handle_mwait(struct kvm_vcpu *vcpu)
7767{
7768 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7769 return handle_nop(vcpu);
7770}
7771
Jim Mattson45ec3682017-08-23 16:32:04 -07007772static int handle_invalid_op(struct kvm_vcpu *vcpu)
7773{
7774 kvm_queue_exception(vcpu, UD_VECTOR);
7775 return 1;
7776}
7777
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007778static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7779{
7780 return 1;
7781}
7782
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007783static int handle_monitor(struct kvm_vcpu *vcpu)
7784{
7785 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7786 return handle_nop(vcpu);
7787}
7788
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007789/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007790 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7791 * set the success or error code of an emulated VMX instruction, as specified
7792 * by Vol 2B, VMX Instruction Reference, "Conventions".
7793 */
7794static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7795{
7796 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7797 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7798 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7799}
7800
7801static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7802{
7803 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7804 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7805 X86_EFLAGS_SF | X86_EFLAGS_OF))
7806 | X86_EFLAGS_CF);
7807}
7808
Abel Gordon145c28d2013-04-18 14:36:55 +03007809static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007810 u32 vm_instruction_error)
7811{
7812 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7813 /*
7814 * failValid writes the error number to the current VMCS, which
7815 * can't be done there isn't a current VMCS.
7816 */
7817 nested_vmx_failInvalid(vcpu);
7818 return;
7819 }
7820 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7821 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7822 X86_EFLAGS_SF | X86_EFLAGS_OF))
7823 | X86_EFLAGS_ZF);
7824 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7825 /*
7826 * We don't need to force a shadow sync because
7827 * VM_INSTRUCTION_ERROR is not shadowed
7828 */
7829}
Abel Gordon145c28d2013-04-18 14:36:55 +03007830
Wincy Vanff651cb2014-12-11 08:52:58 +03007831static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7832{
7833 /* TODO: not to reset guest simply here. */
7834 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007835 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007836}
7837
Jan Kiszkaf41245002014-03-07 20:03:13 +01007838static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7839{
7840 struct vcpu_vmx *vmx =
7841 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7842
7843 vmx->nested.preemption_timer_expired = true;
7844 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7845 kvm_vcpu_kick(&vmx->vcpu);
7846
7847 return HRTIMER_NORESTART;
7848}
7849
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007850/*
Bandan Das19677e32014-05-06 02:19:15 -04007851 * Decode the memory-address operand of a vmx instruction, as recorded on an
7852 * exit caused by such an instruction (run by a guest hypervisor).
7853 * On success, returns 0. When the operand is invalid, returns 1 and throws
7854 * #UD or #GP.
7855 */
7856static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7857 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007858 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007859{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007860 gva_t off;
7861 bool exn;
7862 struct kvm_segment s;
7863
Bandan Das19677e32014-05-06 02:19:15 -04007864 /*
7865 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7866 * Execution", on an exit, vmx_instruction_info holds most of the
7867 * addressing components of the operand. Only the displacement part
7868 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7869 * For how an actual address is calculated from all these components,
7870 * refer to Vol. 1, "Operand Addressing".
7871 */
7872 int scaling = vmx_instruction_info & 3;
7873 int addr_size = (vmx_instruction_info >> 7) & 7;
7874 bool is_reg = vmx_instruction_info & (1u << 10);
7875 int seg_reg = (vmx_instruction_info >> 15) & 7;
7876 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7877 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7878 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7879 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7880
7881 if (is_reg) {
7882 kvm_queue_exception(vcpu, UD_VECTOR);
7883 return 1;
7884 }
7885
7886 /* Addr = segment_base + offset */
7887 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007888 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007889 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007890 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007891 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007892 off += kvm_register_read(vcpu, index_reg)<<scaling;
7893 vmx_get_segment(vcpu, &s, seg_reg);
7894 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007895
7896 if (addr_size == 1) /* 32 bit */
7897 *ret &= 0xffffffff;
7898
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007899 /* Checks for #GP/#SS exceptions. */
7900 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007901 if (is_long_mode(vcpu)) {
7902 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7903 * non-canonical form. This is the only check on the memory
7904 * destination for long mode!
7905 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007906 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007907 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007908 /* Protected mode: apply checks for segment validity in the
7909 * following order:
7910 * - segment type check (#GP(0) may be thrown)
7911 * - usability check (#GP(0)/#SS(0))
7912 * - limit check (#GP(0)/#SS(0))
7913 */
7914 if (wr)
7915 /* #GP(0) if the destination operand is located in a
7916 * read-only data segment or any code segment.
7917 */
7918 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7919 else
7920 /* #GP(0) if the source operand is located in an
7921 * execute-only code segment
7922 */
7923 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007924 if (exn) {
7925 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7926 return 1;
7927 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007928 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7929 */
7930 exn = (s.unusable != 0);
7931 /* Protected mode: #GP(0)/#SS(0) if the memory
7932 * operand is outside the segment limit.
7933 */
7934 exn = exn || (off + sizeof(u64) > s.limit);
7935 }
7936 if (exn) {
7937 kvm_queue_exception_e(vcpu,
7938 seg_reg == VCPU_SREG_SS ?
7939 SS_VECTOR : GP_VECTOR,
7940 0);
7941 return 1;
7942 }
7943
Bandan Das19677e32014-05-06 02:19:15 -04007944 return 0;
7945}
7946
Radim Krčmářcbf71272017-05-19 15:48:51 +02007947static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007948{
7949 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007950 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007951
7952 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007953 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007954 return 1;
7955
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007956 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007957 kvm_inject_page_fault(vcpu, &e);
7958 return 1;
7959 }
7960
Bandan Das3573e222014-05-06 02:19:16 -04007961 return 0;
7962}
7963
Jim Mattsone29acc52016-11-30 12:03:43 -08007964static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7965{
7966 struct vcpu_vmx *vmx = to_vmx(vcpu);
7967 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007968 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007969
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007970 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7971 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007972 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007973
7974 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7975 if (!vmx->nested.cached_vmcs12)
7976 goto out_cached_vmcs12;
7977
7978 if (enable_shadow_vmcs) {
7979 shadow_vmcs = alloc_vmcs();
7980 if (!shadow_vmcs)
7981 goto out_shadow_vmcs;
7982 /* mark vmcs as shadow */
7983 shadow_vmcs->revision_id |= (1u << 31);
7984 /* init shadow vmcs */
7985 vmcs_clear(shadow_vmcs);
7986 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7987 }
7988
Jim Mattsone29acc52016-11-30 12:03:43 -08007989 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7990 HRTIMER_MODE_REL_PINNED);
7991 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7992
7993 vmx->nested.vmxon = true;
7994 return 0;
7995
7996out_shadow_vmcs:
7997 kfree(vmx->nested.cached_vmcs12);
7998
7999out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008000 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008001
Jim Mattsonde3a0022017-11-27 17:22:25 -06008002out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008003 return -ENOMEM;
8004}
8005
Bandan Das3573e222014-05-06 02:19:16 -04008006/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008007 * Emulate the VMXON instruction.
8008 * Currently, we just remember that VMX is active, and do not save or even
8009 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8010 * do not currently need to store anything in that guest-allocated memory
8011 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8012 * argument is different from the VMXON pointer (which the spec says they do).
8013 */
8014static int handle_vmon(struct kvm_vcpu *vcpu)
8015{
Jim Mattsone29acc52016-11-30 12:03:43 -08008016 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008017 gpa_t vmptr;
8018 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008019 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008020 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8021 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008022
Jim Mattson70f3aac2017-04-26 08:53:46 -07008023 /*
8024 * The Intel VMX Instruction Reference lists a bunch of bits that are
8025 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8026 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8027 * Otherwise, we should fail with #UD. But most faulting conditions
8028 * have already been checked by hardware, prior to the VM-exit for
8029 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8030 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008031 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008032 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008033 kvm_queue_exception(vcpu, UD_VECTOR);
8034 return 1;
8035 }
8036
Felix Wilhelm727ba742018-06-11 09:43:44 +02008037 /* CPL=0 must be checked manually. */
8038 if (vmx_get_cpl(vcpu)) {
8039 kvm_queue_exception(vcpu, UD_VECTOR);
8040 return 1;
8041 }
8042
Abel Gordon145c28d2013-04-18 14:36:55 +03008043 if (vmx->nested.vmxon) {
8044 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008045 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008046 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008047
Haozhong Zhang3b840802016-06-22 14:59:54 +08008048 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008049 != VMXON_NEEDED_FEATURES) {
8050 kvm_inject_gp(vcpu, 0);
8051 return 1;
8052 }
8053
Radim Krčmářcbf71272017-05-19 15:48:51 +02008054 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008055 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008056
8057 /*
8058 * SDM 3: 24.11.5
8059 * The first 4 bytes of VMXON region contain the supported
8060 * VMCS revision identifier
8061 *
8062 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8063 * which replaces physical address width with 32
8064 */
8065 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8066 nested_vmx_failInvalid(vcpu);
8067 return kvm_skip_emulated_instruction(vcpu);
8068 }
8069
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008070 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8071 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008072 nested_vmx_failInvalid(vcpu);
8073 return kvm_skip_emulated_instruction(vcpu);
8074 }
8075 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8076 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008077 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008078 nested_vmx_failInvalid(vcpu);
8079 return kvm_skip_emulated_instruction(vcpu);
8080 }
8081 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008082 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008083
8084 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008085 ret = enter_vmx_operation(vcpu);
8086 if (ret)
8087 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008088
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008089 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008090 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008091}
8092
8093/*
8094 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8095 * for running VMX instructions (except VMXON, whose prerequisites are
8096 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008097 * Note that many of these exceptions have priority over VM exits, so they
8098 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008099 */
8100static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8101{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008102 if (vmx_get_cpl(vcpu)) {
8103 kvm_queue_exception(vcpu, UD_VECTOR);
8104 return 0;
8105 }
8106
Jim Mattson70f3aac2017-04-26 08:53:46 -07008107 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008108 kvm_queue_exception(vcpu, UD_VECTOR);
8109 return 0;
8110 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008111 return 1;
8112}
8113
David Matlack8ca44e82017-08-01 14:00:39 -07008114static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8115{
8116 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8117 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8118}
8119
Abel Gordone7953d72013-04-18 14:37:55 +03008120static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8121{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008122 if (vmx->nested.current_vmptr == -1ull)
8123 return;
8124
Abel Gordon012f83c2013-04-18 14:39:25 +03008125 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008126 /* copy to memory all shadowed fields in case
8127 they were modified */
8128 copy_shadow_to_vmcs12(vmx);
8129 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008130 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008131 }
Wincy Van705699a2015-02-03 23:58:17 +08008132 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008133
8134 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008135 kvm_vcpu_write_guest_page(&vmx->vcpu,
8136 vmx->nested.current_vmptr >> PAGE_SHIFT,
8137 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008138
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008139 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008140}
8141
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008142/*
8143 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8144 * just stops using VMX.
8145 */
8146static void free_nested(struct vcpu_vmx *vmx)
8147{
Wanpeng Lib7455822017-11-22 14:04:00 -08008148 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008149 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008150
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008151 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008152 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008153 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008154 vmx->nested.posted_intr_nv = -1;
8155 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008156 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008157 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008158 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8159 free_vmcs(vmx->vmcs01.shadow_vmcs);
8160 vmx->vmcs01.shadow_vmcs = NULL;
8161 }
David Matlack4f2777b2016-07-13 17:16:37 -07008162 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008163 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008164 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008165 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008166 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008167 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008168 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008169 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008170 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008171 }
Wincy Van705699a2015-02-03 23:58:17 +08008172 if (vmx->nested.pi_desc_page) {
8173 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008174 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008175 vmx->nested.pi_desc_page = NULL;
8176 vmx->nested.pi_desc = NULL;
8177 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008178
Jim Mattsonde3a0022017-11-27 17:22:25 -06008179 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008180}
8181
8182/* Emulate the VMXOFF instruction */
8183static int handle_vmoff(struct kvm_vcpu *vcpu)
8184{
8185 if (!nested_vmx_check_permission(vcpu))
8186 return 1;
8187 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008188 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008189 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008190}
8191
Nadav Har'El27d6c862011-05-25 23:06:59 +03008192/* Emulate the VMCLEAR instruction */
8193static int handle_vmclear(struct kvm_vcpu *vcpu)
8194{
8195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008196 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008197 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008198
8199 if (!nested_vmx_check_permission(vcpu))
8200 return 1;
8201
Radim Krčmářcbf71272017-05-19 15:48:51 +02008202 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008203 return 1;
8204
Radim Krčmářcbf71272017-05-19 15:48:51 +02008205 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8206 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8207 return kvm_skip_emulated_instruction(vcpu);
8208 }
8209
8210 if (vmptr == vmx->nested.vmxon_ptr) {
8211 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8212 return kvm_skip_emulated_instruction(vcpu);
8213 }
8214
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008215 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008216 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008217
Jim Mattson587d7e722017-03-02 12:41:48 -08008218 kvm_vcpu_write_guest(vcpu,
8219 vmptr + offsetof(struct vmcs12, launch_state),
8220 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008221
Nadav Har'El27d6c862011-05-25 23:06:59 +03008222 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008223 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008224}
8225
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008226static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8227
8228/* Emulate the VMLAUNCH instruction */
8229static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8230{
8231 return nested_vmx_run(vcpu, true);
8232}
8233
8234/* Emulate the VMRESUME instruction */
8235static int handle_vmresume(struct kvm_vcpu *vcpu)
8236{
8237
8238 return nested_vmx_run(vcpu, false);
8239}
8240
Nadav Har'El49f705c2011-05-25 23:08:30 +03008241/*
8242 * Read a vmcs12 field. Since these can have varying lengths and we return
8243 * one type, we chose the biggest type (u64) and zero-extend the return value
8244 * to that size. Note that the caller, handle_vmread, might need to use only
8245 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8246 * 64-bit fields are to be returned).
8247 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008248static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8249 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008250{
8251 short offset = vmcs_field_to_offset(field);
8252 char *p;
8253
8254 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008255 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008256
8257 p = ((char *)(get_vmcs12(vcpu))) + offset;
8258
Jim Mattsond37f4262017-12-22 12:12:16 -08008259 switch (vmcs_field_width(field)) {
8260 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008261 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008262 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008263 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008264 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008265 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008266 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008267 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008268 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008269 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008270 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008271 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008272 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008273 WARN_ON(1);
8274 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008275 }
8276}
8277
Abel Gordon20b97fe2013-04-18 14:36:25 +03008278
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008279static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8280 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008281 short offset = vmcs_field_to_offset(field);
8282 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8283 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008284 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008285
Jim Mattsond37f4262017-12-22 12:12:16 -08008286 switch (vmcs_field_width(field)) {
8287 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008288 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008289 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008290 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008291 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008292 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008293 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008294 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008295 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008296 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008297 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008298 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008299 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008300 WARN_ON(1);
8301 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008302 }
8303
8304}
8305
Jim Mattsonf4160e42018-05-29 09:11:33 -07008306/*
8307 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8308 * they have been modified by the L1 guest. Note that the "read-only"
8309 * VM-exit information fields are actually writable if the vCPU is
8310 * configured to support "VMWRITE to any supported field in the VMCS."
8311 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008312static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8313{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008314 const u16 *fields[] = {
8315 shadow_read_write_fields,
8316 shadow_read_only_fields
8317 };
8318 const int max_fields[] = {
8319 max_shadow_read_write_fields,
8320 max_shadow_read_only_fields
8321 };
8322 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008323 unsigned long field;
8324 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008325 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008326
Jan Kiszka282da872014-10-08 18:05:39 +02008327 preempt_disable();
8328
Abel Gordon16f5b902013-04-18 14:38:25 +03008329 vmcs_load(shadow_vmcs);
8330
Jim Mattsonf4160e42018-05-29 09:11:33 -07008331 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8332 for (i = 0; i < max_fields[q]; i++) {
8333 field = fields[q][i];
8334 field_value = __vmcs_readl(field);
8335 vmcs12_write_any(&vmx->vcpu, field, field_value);
8336 }
8337 /*
8338 * Skip the VM-exit information fields if they are read-only.
8339 */
8340 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8341 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008342 }
8343
8344 vmcs_clear(shadow_vmcs);
8345 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008346
8347 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008348}
8349
Abel Gordonc3114422013-04-18 14:38:55 +03008350static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8351{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008352 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008353 shadow_read_write_fields,
8354 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008355 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008356 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008357 max_shadow_read_write_fields,
8358 max_shadow_read_only_fields
8359 };
8360 int i, q;
8361 unsigned long field;
8362 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008363 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008364
8365 vmcs_load(shadow_vmcs);
8366
Mathias Krausec2bae892013-06-26 20:36:21 +02008367 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008368 for (i = 0; i < max_fields[q]; i++) {
8369 field = fields[q][i];
8370 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008371 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008372 }
8373 }
8374
8375 vmcs_clear(shadow_vmcs);
8376 vmcs_load(vmx->loaded_vmcs->vmcs);
8377}
8378
Nadav Har'El49f705c2011-05-25 23:08:30 +03008379/*
8380 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8381 * used before) all generate the same failure when it is missing.
8382 */
8383static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8384{
8385 struct vcpu_vmx *vmx = to_vmx(vcpu);
8386 if (vmx->nested.current_vmptr == -1ull) {
8387 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008388 return 0;
8389 }
8390 return 1;
8391}
8392
8393static int handle_vmread(struct kvm_vcpu *vcpu)
8394{
8395 unsigned long field;
8396 u64 field_value;
8397 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8398 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8399 gva_t gva = 0;
8400
Kyle Hueyeb277562016-11-29 12:40:39 -08008401 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008402 return 1;
8403
Kyle Huey6affcbe2016-11-29 12:40:40 -08008404 if (!nested_vmx_check_vmcs12(vcpu))
8405 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008406
Nadav Har'El49f705c2011-05-25 23:08:30 +03008407 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008408 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008409 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008410 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008411 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008412 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008413 }
8414 /*
8415 * Now copy part of this value to register or memory, as requested.
8416 * Note that the number of bits actually copied is 32 or 64 depending
8417 * on the guest's mode (32 or 64 bit), not on the given field's length.
8418 */
8419 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008420 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421 field_value);
8422 } else {
8423 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008424 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008426 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008427 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8428 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008429 }
8430
8431 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008432 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008433}
8434
8435
8436static int handle_vmwrite(struct kvm_vcpu *vcpu)
8437{
8438 unsigned long field;
8439 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8442 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008443
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444 /* The value to write might be 32 or 64 bits, depending on L1's long
8445 * mode, and eventually we need to write that into a field of several
8446 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008447 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008448 * bits into the vmcs12 field.
8449 */
8450 u64 field_value = 0;
8451 struct x86_exception e;
8452
Kyle Hueyeb277562016-11-29 12:40:39 -08008453 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008454 return 1;
8455
Kyle Huey6affcbe2016-11-29 12:40:40 -08008456 if (!nested_vmx_check_vmcs12(vcpu))
8457 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008458
Nadav Har'El49f705c2011-05-25 23:08:30 +03008459 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008460 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008461 (((vmx_instruction_info) >> 3) & 0xf));
8462 else {
8463 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008464 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008465 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008466 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8467 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008468 kvm_inject_page_fault(vcpu, &e);
8469 return 1;
8470 }
8471 }
8472
8473
Nadav Amit27e6fb52014-06-18 17:19:26 +03008474 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008475 /*
8476 * If the vCPU supports "VMWRITE to any supported field in the
8477 * VMCS," then the "read-only" fields are actually read/write.
8478 */
8479 if (vmcs_field_readonly(field) &&
8480 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008481 nested_vmx_failValid(vcpu,
8482 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008483 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008484 }
8485
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008486 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008488 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008489 }
8490
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008491 switch (field) {
8492#define SHADOW_FIELD_RW(x) case x:
8493#include "vmx_shadow_fields.h"
8494 /*
8495 * The fields that can be updated by L1 without a vmexit are
8496 * always updated in the vmcs02, the others go down the slow
8497 * path of prepare_vmcs02.
8498 */
8499 break;
8500 default:
8501 vmx->nested.dirty_vmcs12 = true;
8502 break;
8503 }
8504
Nadav Har'El49f705c2011-05-25 23:08:30 +03008505 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008506 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008507}
8508
Jim Mattsona8bc2842016-11-30 12:03:44 -08008509static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8510{
8511 vmx->nested.current_vmptr = vmptr;
8512 if (enable_shadow_vmcs) {
8513 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8514 SECONDARY_EXEC_SHADOW_VMCS);
8515 vmcs_write64(VMCS_LINK_POINTER,
8516 __pa(vmx->vmcs01.shadow_vmcs));
8517 vmx->nested.sync_shadow_vmcs = true;
8518 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008519 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008520}
8521
Nadav Har'El63846662011-05-25 23:07:29 +03008522/* Emulate the VMPTRLD instruction */
8523static int handle_vmptrld(struct kvm_vcpu *vcpu)
8524{
8525 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008526 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008527
8528 if (!nested_vmx_check_permission(vcpu))
8529 return 1;
8530
Radim Krčmářcbf71272017-05-19 15:48:51 +02008531 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008532 return 1;
8533
Radim Krčmářcbf71272017-05-19 15:48:51 +02008534 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8535 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8536 return kvm_skip_emulated_instruction(vcpu);
8537 }
8538
8539 if (vmptr == vmx->nested.vmxon_ptr) {
8540 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8541 return kvm_skip_emulated_instruction(vcpu);
8542 }
8543
Nadav Har'El63846662011-05-25 23:07:29 +03008544 if (vmx->nested.current_vmptr != vmptr) {
8545 struct vmcs12 *new_vmcs12;
8546 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008547 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8548 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008549 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008550 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008551 }
8552 new_vmcs12 = kmap(page);
8553 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8554 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008555 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008556 nested_vmx_failValid(vcpu,
8557 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008558 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008559 }
Nadav Har'El63846662011-05-25 23:07:29 +03008560
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008561 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008562 /*
8563 * Load VMCS12 from guest memory since it is not already
8564 * cached.
8565 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008566 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8567 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008568 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008569
Jim Mattsona8bc2842016-11-30 12:03:44 -08008570 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008571 }
8572
8573 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008574 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008575}
8576
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008577/* Emulate the VMPTRST instruction */
8578static int handle_vmptrst(struct kvm_vcpu *vcpu)
8579{
8580 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8581 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8582 gva_t vmcs_gva;
8583 struct x86_exception e;
8584
8585 if (!nested_vmx_check_permission(vcpu))
8586 return 1;
8587
8588 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008589 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008590 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008591 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008592 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8593 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8594 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008595 kvm_inject_page_fault(vcpu, &e);
8596 return 1;
8597 }
8598 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008599 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008600}
8601
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008602/* Emulate the INVEPT instruction */
8603static int handle_invept(struct kvm_vcpu *vcpu)
8604{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008605 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008606 u32 vmx_instruction_info, types;
8607 unsigned long type;
8608 gva_t gva;
8609 struct x86_exception e;
8610 struct {
8611 u64 eptp, gpa;
8612 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008613
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008614 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008615 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008616 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008617 kvm_queue_exception(vcpu, UD_VECTOR);
8618 return 1;
8619 }
8620
8621 if (!nested_vmx_check_permission(vcpu))
8622 return 1;
8623
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008624 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008625 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008626
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008627 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008628
Jim Mattson85c856b2016-10-26 08:38:38 -07008629 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008630 nested_vmx_failValid(vcpu,
8631 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008632 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008633 }
8634
8635 /* According to the Intel VMX instruction reference, the memory
8636 * operand is read even if it isn't needed (e.g., for type==global)
8637 */
8638 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008639 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008640 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008641 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008642 kvm_inject_page_fault(vcpu, &e);
8643 return 1;
8644 }
8645
8646 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008647 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008648 /*
8649 * TODO: track mappings and invalidate
8650 * single context requests appropriately
8651 */
8652 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008653 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008654 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008655 nested_vmx_succeed(vcpu);
8656 break;
8657 default:
8658 BUG_ON(1);
8659 break;
8660 }
8661
Kyle Huey6affcbe2016-11-29 12:40:40 -08008662 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008663}
8664
Petr Matouseka642fc32014-09-23 20:22:30 +02008665static int handle_invvpid(struct kvm_vcpu *vcpu)
8666{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008667 struct vcpu_vmx *vmx = to_vmx(vcpu);
8668 u32 vmx_instruction_info;
8669 unsigned long type, types;
8670 gva_t gva;
8671 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008672 struct {
8673 u64 vpid;
8674 u64 gla;
8675 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008676
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008677 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008678 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008679 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008680 kvm_queue_exception(vcpu, UD_VECTOR);
8681 return 1;
8682 }
8683
8684 if (!nested_vmx_check_permission(vcpu))
8685 return 1;
8686
8687 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8688 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8689
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008690 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008691 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008692
Jim Mattson85c856b2016-10-26 08:38:38 -07008693 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008694 nested_vmx_failValid(vcpu,
8695 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008696 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008697 }
8698
8699 /* according to the intel vmx instruction reference, the memory
8700 * operand is read even if it isn't needed (e.g., for type==global)
8701 */
8702 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8703 vmx_instruction_info, false, &gva))
8704 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008705 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008706 kvm_inject_page_fault(vcpu, &e);
8707 return 1;
8708 }
Jim Mattson40352602017-06-28 09:37:37 -07008709 if (operand.vpid >> 16) {
8710 nested_vmx_failValid(vcpu,
8711 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8712 return kvm_skip_emulated_instruction(vcpu);
8713 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008714
8715 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008716 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008717 if (!operand.vpid ||
8718 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008719 nested_vmx_failValid(vcpu,
8720 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8721 return kvm_skip_emulated_instruction(vcpu);
8722 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008723 if (cpu_has_vmx_invvpid_individual_addr() &&
8724 vmx->nested.vpid02) {
8725 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8726 vmx->nested.vpid02, operand.gla);
8727 } else
8728 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8729 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008730 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008731 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008732 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008733 nested_vmx_failValid(vcpu,
8734 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008735 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008736 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008737 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008738 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008739 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008740 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008741 break;
8742 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008743 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008744 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008745 }
8746
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008747 nested_vmx_succeed(vcpu);
8748
Kyle Huey6affcbe2016-11-29 12:40:40 -08008749 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008750}
8751
Kai Huang843e4332015-01-28 10:54:28 +08008752static int handle_pml_full(struct kvm_vcpu *vcpu)
8753{
8754 unsigned long exit_qualification;
8755
8756 trace_kvm_pml_full(vcpu->vcpu_id);
8757
8758 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8759
8760 /*
8761 * PML buffer FULL happened while executing iret from NMI,
8762 * "blocked by NMI" bit has to be set before next VM entry.
8763 */
8764 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008765 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008766 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8767 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8768 GUEST_INTR_STATE_NMI);
8769
8770 /*
8771 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8772 * here.., and there's no userspace involvement needed for PML.
8773 */
8774 return 1;
8775}
8776
Yunhong Jiang64672c92016-06-13 14:19:59 -07008777static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8778{
8779 kvm_lapic_expired_hv_timer(vcpu);
8780 return 1;
8781}
8782
Bandan Das41ab9372017-08-03 15:54:43 -04008783static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8784{
8785 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008786 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8787
8788 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008789 switch (address & VMX_EPTP_MT_MASK) {
8790 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008791 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008792 return false;
8793 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008794 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008795 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008796 return false;
8797 break;
8798 default:
8799 return false;
8800 }
8801
David Hildenbrandbb97a012017-08-10 23:15:28 +02008802 /* only 4 levels page-walk length are valid */
8803 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008804 return false;
8805
8806 /* Reserved bits should not be set */
8807 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8808 return false;
8809
8810 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008811 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008812 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008813 return false;
8814 }
8815
8816 return true;
8817}
8818
8819static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8820 struct vmcs12 *vmcs12)
8821{
8822 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8823 u64 address;
8824 bool accessed_dirty;
8825 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8826
8827 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8828 !nested_cpu_has_ept(vmcs12))
8829 return 1;
8830
8831 if (index >= VMFUNC_EPTP_ENTRIES)
8832 return 1;
8833
8834
8835 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8836 &address, index * 8, 8))
8837 return 1;
8838
David Hildenbrandbb97a012017-08-10 23:15:28 +02008839 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008840
8841 /*
8842 * If the (L2) guest does a vmfunc to the currently
8843 * active ept pointer, we don't have to do anything else
8844 */
8845 if (vmcs12->ept_pointer != address) {
8846 if (!valid_ept_address(vcpu, address))
8847 return 1;
8848
8849 kvm_mmu_unload(vcpu);
8850 mmu->ept_ad = accessed_dirty;
8851 mmu->base_role.ad_disabled = !accessed_dirty;
8852 vmcs12->ept_pointer = address;
8853 /*
8854 * TODO: Check what's the correct approach in case
8855 * mmu reload fails. Currently, we just let the next
8856 * reload potentially fail
8857 */
8858 kvm_mmu_reload(vcpu);
8859 }
8860
8861 return 0;
8862}
8863
Bandan Das2a499e42017-08-03 15:54:41 -04008864static int handle_vmfunc(struct kvm_vcpu *vcpu)
8865{
Bandan Das27c42a12017-08-03 15:54:42 -04008866 struct vcpu_vmx *vmx = to_vmx(vcpu);
8867 struct vmcs12 *vmcs12;
8868 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8869
8870 /*
8871 * VMFUNC is only supported for nested guests, but we always enable the
8872 * secondary control for simplicity; for non-nested mode, fake that we
8873 * didn't by injecting #UD.
8874 */
8875 if (!is_guest_mode(vcpu)) {
8876 kvm_queue_exception(vcpu, UD_VECTOR);
8877 return 1;
8878 }
8879
8880 vmcs12 = get_vmcs12(vcpu);
8881 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8882 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008883
8884 switch (function) {
8885 case 0:
8886 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8887 goto fail;
8888 break;
8889 default:
8890 goto fail;
8891 }
8892 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008893
8894fail:
8895 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8896 vmcs_read32(VM_EXIT_INTR_INFO),
8897 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008898 return 1;
8899}
8900
Nadav Har'El0140cae2011-05-25 23:06:28 +03008901/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008902 * The exit handlers return 1 if the exit was handled fully and guest execution
8903 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8904 * to be done to userspace and return 0.
8905 */
Mathias Krause772e0312012-08-30 01:30:19 +02008906static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008907 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8908 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008909 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008910 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008911 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008912 [EXIT_REASON_CR_ACCESS] = handle_cr,
8913 [EXIT_REASON_DR_ACCESS] = handle_dr,
8914 [EXIT_REASON_CPUID] = handle_cpuid,
8915 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8916 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8917 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8918 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008919 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008920 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008921 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008922 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008923 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008924 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008925 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008926 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008927 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008928 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008929 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008930 [EXIT_REASON_VMOFF] = handle_vmoff,
8931 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008932 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8933 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008934 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008935 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008936 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008937 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008938 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008939 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008940 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8941 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008942 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8943 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008944 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008945 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008946 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008947 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008948 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008949 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008950 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008951 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008952 [EXIT_REASON_XSAVES] = handle_xsaves,
8953 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008954 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008955 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008956 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008957};
8958
8959static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008960 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008961
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008962static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8963 struct vmcs12 *vmcs12)
8964{
8965 unsigned long exit_qualification;
8966 gpa_t bitmap, last_bitmap;
8967 unsigned int port;
8968 int size;
8969 u8 b;
8970
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008971 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008972 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008973
8974 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8975
8976 port = exit_qualification >> 16;
8977 size = (exit_qualification & 7) + 1;
8978
8979 last_bitmap = (gpa_t)-1;
8980 b = -1;
8981
8982 while (size > 0) {
8983 if (port < 0x8000)
8984 bitmap = vmcs12->io_bitmap_a;
8985 else if (port < 0x10000)
8986 bitmap = vmcs12->io_bitmap_b;
8987 else
Joe Perches1d804d02015-03-30 16:46:09 -07008988 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008989 bitmap += (port & 0x7fff) / 8;
8990
8991 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008992 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008993 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008994 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008995 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008996
8997 port++;
8998 size--;
8999 last_bitmap = bitmap;
9000 }
9001
Joe Perches1d804d02015-03-30 16:46:09 -07009002 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009003}
9004
Nadav Har'El644d7112011-05-25 23:12:35 +03009005/*
9006 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9007 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9008 * disinterest in the current event (read or write a specific MSR) by using an
9009 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9010 */
9011static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9012 struct vmcs12 *vmcs12, u32 exit_reason)
9013{
9014 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9015 gpa_t bitmap;
9016
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009017 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009018 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009019
9020 /*
9021 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9022 * for the four combinations of read/write and low/high MSR numbers.
9023 * First we need to figure out which of the four to use:
9024 */
9025 bitmap = vmcs12->msr_bitmap;
9026 if (exit_reason == EXIT_REASON_MSR_WRITE)
9027 bitmap += 2048;
9028 if (msr_index >= 0xc0000000) {
9029 msr_index -= 0xc0000000;
9030 bitmap += 1024;
9031 }
9032
9033 /* Then read the msr_index'th bit from this bitmap: */
9034 if (msr_index < 1024*8) {
9035 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009036 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009037 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009038 return 1 & (b >> (msr_index & 7));
9039 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009040 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009041}
9042
9043/*
9044 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9045 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9046 * intercept (via guest_host_mask etc.) the current event.
9047 */
9048static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9049 struct vmcs12 *vmcs12)
9050{
9051 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9052 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009053 int reg;
9054 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009055
9056 switch ((exit_qualification >> 4) & 3) {
9057 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009058 reg = (exit_qualification >> 8) & 15;
9059 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009060 switch (cr) {
9061 case 0:
9062 if (vmcs12->cr0_guest_host_mask &
9063 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009064 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009065 break;
9066 case 3:
9067 if ((vmcs12->cr3_target_count >= 1 &&
9068 vmcs12->cr3_target_value0 == val) ||
9069 (vmcs12->cr3_target_count >= 2 &&
9070 vmcs12->cr3_target_value1 == val) ||
9071 (vmcs12->cr3_target_count >= 3 &&
9072 vmcs12->cr3_target_value2 == val) ||
9073 (vmcs12->cr3_target_count >= 4 &&
9074 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009075 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009076 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009078 break;
9079 case 4:
9080 if (vmcs12->cr4_guest_host_mask &
9081 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009082 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009083 break;
9084 case 8:
9085 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009086 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009087 break;
9088 }
9089 break;
9090 case 2: /* clts */
9091 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9092 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009093 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009094 break;
9095 case 1: /* mov from cr */
9096 switch (cr) {
9097 case 3:
9098 if (vmcs12->cpu_based_vm_exec_control &
9099 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009100 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009101 break;
9102 case 8:
9103 if (vmcs12->cpu_based_vm_exec_control &
9104 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009105 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009106 break;
9107 }
9108 break;
9109 case 3: /* lmsw */
9110 /*
9111 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9112 * cr0. Other attempted changes are ignored, with no exit.
9113 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009114 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009115 if (vmcs12->cr0_guest_host_mask & 0xe &
9116 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009117 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009118 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9119 !(vmcs12->cr0_read_shadow & 0x1) &&
9120 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009121 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009122 break;
9123 }
Joe Perches1d804d02015-03-30 16:46:09 -07009124 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009125}
9126
9127/*
9128 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9129 * should handle it ourselves in L0 (and then continue L2). Only call this
9130 * when in is_guest_mode (L2).
9131 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009132static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009133{
Nadav Har'El644d7112011-05-25 23:12:35 +03009134 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9135 struct vcpu_vmx *vmx = to_vmx(vcpu);
9136 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9137
Jim Mattson4f350c62017-09-14 16:31:44 -07009138 if (vmx->nested.nested_run_pending)
9139 return false;
9140
9141 if (unlikely(vmx->fail)) {
9142 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9143 vmcs_read32(VM_INSTRUCTION_ERROR));
9144 return true;
9145 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009146
David Matlackc9f04402017-08-01 14:00:40 -07009147 /*
9148 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009149 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9150 * Page). The CPU may write to these pages via their host
9151 * physical address while L2 is running, bypassing any
9152 * address-translation-based dirty tracking (e.g. EPT write
9153 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009154 *
9155 * Mark them dirty on every exit from L2 to prevent them from
9156 * getting out of sync with dirty tracking.
9157 */
9158 nested_mark_vmcs12_pages_dirty(vcpu);
9159
Jim Mattson4f350c62017-09-14 16:31:44 -07009160 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9161 vmcs_readl(EXIT_QUALIFICATION),
9162 vmx->idt_vectoring_info,
9163 intr_info,
9164 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9165 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009166
9167 switch (exit_reason) {
9168 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009169 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009170 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009171 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009172 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009173 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009174 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009175 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009176 else if (is_debug(intr_info) &&
9177 vcpu->guest_debug &
9178 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9179 return false;
9180 else if (is_breakpoint(intr_info) &&
9181 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9182 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009183 return vmcs12->exception_bitmap &
9184 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9185 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009186 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009188 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009189 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009190 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009191 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009192 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009194 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009195 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009196 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009197 case EXIT_REASON_HLT:
9198 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9199 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009200 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009201 case EXIT_REASON_INVLPG:
9202 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9203 case EXIT_REASON_RDPMC:
9204 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009205 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009206 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009207 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009208 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009209 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009210 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9211 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9212 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9213 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9214 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9215 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009216 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009217 /*
9218 * VMX instructions trap unconditionally. This allows L1 to
9219 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9220 */
Joe Perches1d804d02015-03-30 16:46:09 -07009221 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009222 case EXIT_REASON_CR_ACCESS:
9223 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9224 case EXIT_REASON_DR_ACCESS:
9225 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9226 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009227 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009228 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9229 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009230 case EXIT_REASON_MSR_READ:
9231 case EXIT_REASON_MSR_WRITE:
9232 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9233 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009234 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009235 case EXIT_REASON_MWAIT_INSTRUCTION:
9236 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009237 case EXIT_REASON_MONITOR_TRAP_FLAG:
9238 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009239 case EXIT_REASON_MONITOR_INSTRUCTION:
9240 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9241 case EXIT_REASON_PAUSE_INSTRUCTION:
9242 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9243 nested_cpu_has2(vmcs12,
9244 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9245 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009246 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009247 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009248 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009249 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009250 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009251 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009252 /*
9253 * The controls for "virtualize APIC accesses," "APIC-
9254 * register virtualization," and "virtual-interrupt
9255 * delivery" only come from vmcs12.
9256 */
Joe Perches1d804d02015-03-30 16:46:09 -07009257 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009258 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009259 /*
9260 * L0 always deals with the EPT violation. If nested EPT is
9261 * used, and the nested mmu code discovers that the address is
9262 * missing in the guest EPT table (EPT12), the EPT violation
9263 * will be injected with nested_ept_inject_page_fault()
9264 */
Joe Perches1d804d02015-03-30 16:46:09 -07009265 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009266 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009267 /*
9268 * L2 never uses directly L1's EPT, but rather L0's own EPT
9269 * table (shadow on EPT) or a merged EPT table that L0 built
9270 * (EPT on EPT). So any problems with the structure of the
9271 * table is L0's fault.
9272 */
Joe Perches1d804d02015-03-30 16:46:09 -07009273 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009274 case EXIT_REASON_INVPCID:
9275 return
9276 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9277 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009278 case EXIT_REASON_WBINVD:
9279 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9280 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009281 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009282 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9283 /*
9284 * This should never happen, since it is not possible to
9285 * set XSS to a non-zero value---neither in L1 nor in L2.
9286 * If if it were, XSS would have to be checked against
9287 * the XSS exit bitmap in vmcs12.
9288 */
9289 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009290 case EXIT_REASON_PREEMPTION_TIMER:
9291 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009292 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009293 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009294 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009295 case EXIT_REASON_VMFUNC:
9296 /* VM functions are emulated through L2->L0 vmexits. */
9297 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009298 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009299 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009300 }
9301}
9302
Paolo Bonzini7313c692017-07-27 10:31:25 +02009303static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9304{
9305 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9306
9307 /*
9308 * At this point, the exit interruption info in exit_intr_info
9309 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9310 * we need to query the in-kernel LAPIC.
9311 */
9312 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9313 if ((exit_intr_info &
9314 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9315 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9316 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9317 vmcs12->vm_exit_intr_error_code =
9318 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9319 }
9320
9321 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9322 vmcs_readl(EXIT_QUALIFICATION));
9323 return 1;
9324}
9325
Avi Kivity586f9602010-11-18 13:09:54 +02009326static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9327{
9328 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9329 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9330}
9331
Kai Huanga3eaa862015-11-04 13:46:05 +08009332static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009333{
Kai Huanga3eaa862015-11-04 13:46:05 +08009334 if (vmx->pml_pg) {
9335 __free_page(vmx->pml_pg);
9336 vmx->pml_pg = NULL;
9337 }
Kai Huang843e4332015-01-28 10:54:28 +08009338}
9339
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009340static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009341{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009342 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009343 u64 *pml_buf;
9344 u16 pml_idx;
9345
9346 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9347
9348 /* Do nothing if PML buffer is empty */
9349 if (pml_idx == (PML_ENTITY_NUM - 1))
9350 return;
9351
9352 /* PML index always points to next available PML buffer entity */
9353 if (pml_idx >= PML_ENTITY_NUM)
9354 pml_idx = 0;
9355 else
9356 pml_idx++;
9357
9358 pml_buf = page_address(vmx->pml_pg);
9359 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9360 u64 gpa;
9361
9362 gpa = pml_buf[pml_idx];
9363 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009364 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009365 }
9366
9367 /* reset PML index */
9368 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9369}
9370
9371/*
9372 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9373 * Called before reporting dirty_bitmap to userspace.
9374 */
9375static void kvm_flush_pml_buffers(struct kvm *kvm)
9376{
9377 int i;
9378 struct kvm_vcpu *vcpu;
9379 /*
9380 * We only need to kick vcpu out of guest mode here, as PML buffer
9381 * is flushed at beginning of all VMEXITs, and it's obvious that only
9382 * vcpus running in guest are possible to have unflushed GPAs in PML
9383 * buffer.
9384 */
9385 kvm_for_each_vcpu(i, vcpu, kvm)
9386 kvm_vcpu_kick(vcpu);
9387}
9388
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009389static void vmx_dump_sel(char *name, uint32_t sel)
9390{
9391 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009392 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009393 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9394 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9395 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9396}
9397
9398static void vmx_dump_dtsel(char *name, uint32_t limit)
9399{
9400 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9401 name, vmcs_read32(limit),
9402 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9403}
9404
9405static void dump_vmcs(void)
9406{
9407 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9408 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9409 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9410 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9411 u32 secondary_exec_control = 0;
9412 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009413 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009414 int i, n;
9415
9416 if (cpu_has_secondary_exec_ctrls())
9417 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9418
9419 pr_err("*** Guest State ***\n");
9420 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9421 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9422 vmcs_readl(CR0_GUEST_HOST_MASK));
9423 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9424 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9425 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9426 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9427 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9428 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009429 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9430 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9431 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9432 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009433 }
9434 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9435 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9436 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9437 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9438 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9439 vmcs_readl(GUEST_SYSENTER_ESP),
9440 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9441 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9442 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9443 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9444 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9445 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9446 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9447 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9448 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9449 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9450 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9451 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9452 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009453 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9454 efer, vmcs_read64(GUEST_IA32_PAT));
9455 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9456 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009457 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009458 if (cpu_has_load_perf_global_ctrl &&
9459 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009460 pr_err("PerfGlobCtl = 0x%016llx\n",
9461 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009462 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009463 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009464 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9465 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9466 vmcs_read32(GUEST_ACTIVITY_STATE));
9467 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9468 pr_err("InterruptStatus = %04x\n",
9469 vmcs_read16(GUEST_INTR_STATUS));
9470
9471 pr_err("*** Host State ***\n");
9472 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9473 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9474 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9475 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9476 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9477 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9478 vmcs_read16(HOST_TR_SELECTOR));
9479 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9480 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9481 vmcs_readl(HOST_TR_BASE));
9482 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9483 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9484 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9485 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9486 vmcs_readl(HOST_CR4));
9487 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9488 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9489 vmcs_read32(HOST_IA32_SYSENTER_CS),
9490 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9491 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009492 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9493 vmcs_read64(HOST_IA32_EFER),
9494 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009495 if (cpu_has_load_perf_global_ctrl &&
9496 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009497 pr_err("PerfGlobCtl = 0x%016llx\n",
9498 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009499
9500 pr_err("*** Control State ***\n");
9501 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9502 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9503 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9504 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9505 vmcs_read32(EXCEPTION_BITMAP),
9506 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9507 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9508 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9509 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9510 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9511 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9512 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9513 vmcs_read32(VM_EXIT_INTR_INFO),
9514 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9515 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9516 pr_err(" reason=%08x qualification=%016lx\n",
9517 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9518 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9519 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9520 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009521 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009522 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009523 pr_err("TSC Multiplier = 0x%016llx\n",
9524 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009525 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9526 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9527 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9528 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9529 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009530 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009531 n = vmcs_read32(CR3_TARGET_COUNT);
9532 for (i = 0; i + 1 < n; i += 4)
9533 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9534 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9535 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9536 if (i < n)
9537 pr_err("CR3 target%u=%016lx\n",
9538 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9539 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9540 pr_err("PLE Gap=%08x Window=%08x\n",
9541 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9542 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9543 pr_err("Virtual processor ID = 0x%04x\n",
9544 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9545}
9546
Avi Kivity6aa8b732006-12-10 02:21:36 -08009547/*
9548 * The guest has exited. See if we can fix it or if we need userspace
9549 * assistance.
9550 */
Avi Kivity851ba692009-08-24 11:10:17 +03009551static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009552{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009553 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009554 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009555 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009556
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009557 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9558
Kai Huang843e4332015-01-28 10:54:28 +08009559 /*
9560 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9561 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9562 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9563 * mode as if vcpus is in root mode, the PML buffer must has been
9564 * flushed already.
9565 */
9566 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009567 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009568
Mohammed Gamal80ced182009-09-01 12:48:18 +02009569 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009570 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009571 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009572
Paolo Bonzini7313c692017-07-27 10:31:25 +02009573 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9574 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009575
Mohammed Gamal51207022010-05-31 22:40:54 +03009576 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009577 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009578 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9579 vcpu->run->fail_entry.hardware_entry_failure_reason
9580 = exit_reason;
9581 return 0;
9582 }
9583
Avi Kivity29bd8a72007-09-10 17:27:03 +03009584 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009585 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9586 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009587 = vmcs_read32(VM_INSTRUCTION_ERROR);
9588 return 0;
9589 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009590
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009591 /*
9592 * Note:
9593 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9594 * delivery event since it indicates guest is accessing MMIO.
9595 * The vm-exit can be triggered again after return to guest that
9596 * will cause infinite loop.
9597 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009598 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009599 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009600 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009601 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009602 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9603 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9604 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009605 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009606 vcpu->run->internal.data[0] = vectoring_info;
9607 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009608 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9609 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9610 vcpu->run->internal.ndata++;
9611 vcpu->run->internal.data[3] =
9612 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9613 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009614 return 0;
9615 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009616
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009617 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009618 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9619 if (vmx_interrupt_allowed(vcpu)) {
9620 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9621 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9622 vcpu->arch.nmi_pending) {
9623 /*
9624 * This CPU don't support us in finding the end of an
9625 * NMI-blocked window if the guest runs with IRQs
9626 * disabled. So we pull the trigger after 1 s of
9627 * futile waiting, but inform the user about this.
9628 */
9629 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9630 "state on VCPU %d after 1 s timeout\n",
9631 __func__, vcpu->vcpu_id);
9632 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9633 }
9634 }
9635
Avi Kivity6aa8b732006-12-10 02:21:36 -08009636 if (exit_reason < kvm_vmx_max_exit_handlers
9637 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009638 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009639 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009640 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9641 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009642 kvm_queue_exception(vcpu, UD_VECTOR);
9643 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009644 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009645}
9646
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009647/*
9648 * Software based L1D cache flush which is used when microcode providing
9649 * the cache control MSR is not loaded.
9650 *
9651 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9652 * flush it is required to read in 64 KiB because the replacement algorithm
9653 * is not exactly LRU. This could be sized at runtime via topology
9654 * information but as all relevant affected CPUs have 32KiB L1D cache size
9655 * there is no point in doing so.
9656 */
9657#define L1D_CACHE_ORDER 4
9658static void *vmx_l1d_flush_pages;
9659
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009660static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009661{
9662 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009663
9664 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009665 * This code is only executed when the the flush mode is 'cond' or
9666 * 'always'
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009667 *
Thomas Gleixner2f055942018-07-13 16:23:17 +02009668 * If 'flush always', keep the flush bit set, otherwise clear
9669 * it. The flush bit gets set again either from vcpu_run() or from
9670 * one of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009671 */
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02009672 if (static_branch_unlikely(&vmx_l1d_flush_always))
9673 vcpu->arch.l1tf_flush_l1d = true;
9674 else
9675 vcpu->arch.l1tf_flush_l1d = false;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009676
9677 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009678
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009679 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9680 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9681 return;
9682 }
9683
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009684 asm volatile(
9685 /* First ensure the pages are in the TLB */
9686 "xorl %%eax, %%eax\n"
9687 ".Lpopulate_tlb:\n\t"
9688 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9689 "addl $4096, %%eax\n\t"
9690 "cmpl %%eax, %[size]\n\t"
9691 "jne .Lpopulate_tlb\n\t"
9692 "xorl %%eax, %%eax\n\t"
9693 "cpuid\n\t"
9694 /* Now fill the cache */
9695 "xorl %%eax, %%eax\n"
9696 ".Lfill_cache:\n"
9697 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9698 "addl $64, %%eax\n\t"
9699 "cmpl %%eax, %[size]\n\t"
9700 "jne .Lfill_cache\n\t"
9701 "lfence\n"
9702 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9703 [size] "r" (size)
9704 : "eax", "ebx", "ecx", "edx");
9705}
9706
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009707static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009708{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009709 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9710
9711 if (is_guest_mode(vcpu) &&
9712 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9713 return;
9714
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009715 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009716 vmcs_write32(TPR_THRESHOLD, 0);
9717 return;
9718 }
9719
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009720 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009721}
9722
Jim Mattson8d860bb2018-05-09 16:56:05 -04009723static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009724{
9725 u32 sec_exec_control;
9726
Jim Mattson8d860bb2018-05-09 16:56:05 -04009727 if (!lapic_in_kernel(vcpu))
9728 return;
9729
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009730 /* Postpone execution until vmcs01 is the current VMCS. */
9731 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009732 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009733 return;
9734 }
9735
Paolo Bonzini35754c92015-07-29 12:05:37 +02009736 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009737 return;
9738
9739 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009740 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9741 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009742
Jim Mattson8d860bb2018-05-09 16:56:05 -04009743 switch (kvm_get_apic_mode(vcpu)) {
9744 case LAPIC_MODE_INVALID:
9745 WARN_ONCE(true, "Invalid local APIC state");
9746 case LAPIC_MODE_DISABLED:
9747 break;
9748 case LAPIC_MODE_XAPIC:
9749 if (flexpriority_enabled) {
9750 sec_exec_control |=
9751 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9752 vmx_flush_tlb(vcpu, true);
9753 }
9754 break;
9755 case LAPIC_MODE_X2APIC:
9756 if (cpu_has_vmx_virtualize_x2apic_mode())
9757 sec_exec_control |=
9758 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9759 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009760 }
9761 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9762
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009763 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009764}
9765
Tang Chen38b99172014-09-24 15:57:54 +08009766static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9767{
Jim Mattsonab5df312018-05-09 17:02:03 -04009768 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009769 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009770 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009771 }
Tang Chen38b99172014-09-24 15:57:54 +08009772}
9773
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009774static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009775{
9776 u16 status;
9777 u8 old;
9778
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009779 if (max_isr == -1)
9780 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009781
9782 status = vmcs_read16(GUEST_INTR_STATUS);
9783 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009784 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009785 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009786 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009787 vmcs_write16(GUEST_INTR_STATUS, status);
9788 }
9789}
9790
9791static void vmx_set_rvi(int vector)
9792{
9793 u16 status;
9794 u8 old;
9795
Wei Wang4114c272014-11-05 10:53:43 +08009796 if (vector == -1)
9797 vector = 0;
9798
Yang Zhangc7c9c562013-01-25 10:18:51 +08009799 status = vmcs_read16(GUEST_INTR_STATUS);
9800 old = (u8)status & 0xff;
9801 if ((u8)vector != old) {
9802 status &= ~0xff;
9803 status |= (u8)vector;
9804 vmcs_write16(GUEST_INTR_STATUS, status);
9805 }
9806}
9807
9808static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9809{
Liran Alon851c1a182017-12-24 18:12:56 +02009810 /*
9811 * When running L2, updating RVI is only relevant when
9812 * vmcs12 virtual-interrupt-delivery enabled.
9813 * However, it can be enabled only when L1 also
9814 * intercepts external-interrupts and in that case
9815 * we should not update vmcs02 RVI but instead intercept
9816 * interrupt. Therefore, do nothing when running L2.
9817 */
9818 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009819 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009820}
9821
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009822static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009823{
9824 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009825 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009826 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009827
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009828 WARN_ON(!vcpu->arch.apicv_active);
9829 if (pi_test_on(&vmx->pi_desc)) {
9830 pi_clear_on(&vmx->pi_desc);
9831 /*
9832 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9833 * But on x86 this is just a compiler barrier anyway.
9834 */
9835 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009836 max_irr_updated =
9837 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9838
9839 /*
9840 * If we are running L2 and L1 has a new pending interrupt
9841 * which can be injected, we should re-evaluate
9842 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009843 * If L1 intercepts external-interrupts, we should
9844 * exit from L2 to L1. Otherwise, interrupt should be
9845 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009846 */
Liran Alon851c1a182017-12-24 18:12:56 +02009847 if (is_guest_mode(vcpu) && max_irr_updated) {
9848 if (nested_exit_on_intr(vcpu))
9849 kvm_vcpu_exiting_guest_mode(vcpu);
9850 else
9851 kvm_make_request(KVM_REQ_EVENT, vcpu);
9852 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009853 } else {
9854 max_irr = kvm_lapic_find_highest_irr(vcpu);
9855 }
9856 vmx_hwapic_irr_update(vcpu, max_irr);
9857 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009858}
9859
Andrey Smetanin63086302015-11-10 15:36:32 +03009860static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009861{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009862 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009863 return;
9864
Yang Zhangc7c9c562013-01-25 10:18:51 +08009865 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9866 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9867 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9868 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9869}
9870
Paolo Bonzini967235d2016-12-19 14:03:45 +01009871static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9872{
9873 struct vcpu_vmx *vmx = to_vmx(vcpu);
9874
9875 pi_clear_on(&vmx->pi_desc);
9876 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9877}
9878
Avi Kivity51aa01d2010-07-20 14:31:20 +03009879static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009880{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009881 u32 exit_intr_info = 0;
9882 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009883
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009884 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9885 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009886 return;
9887
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009888 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9889 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9890 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009891
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009892 /* if exit due to PF check for async PF */
9893 if (is_page_fault(exit_intr_info))
9894 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9895
Andi Kleena0861c02009-06-08 17:37:09 +08009896 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009897 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9898 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009899 kvm_machine_check();
9900
Gleb Natapov20f65982009-05-11 13:35:55 +03009901 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009902 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009903 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009904 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009905 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009906 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009907}
Gleb Natapov20f65982009-05-11 13:35:55 +03009908
Yang Zhanga547c6d2013-04-11 19:25:10 +08009909static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9910{
9911 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9912
Yang Zhanga547c6d2013-04-11 19:25:10 +08009913 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9914 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9915 unsigned int vector;
9916 unsigned long entry;
9917 gate_desc *desc;
9918 struct vcpu_vmx *vmx = to_vmx(vcpu);
9919#ifdef CONFIG_X86_64
9920 unsigned long tmp;
9921#endif
9922
9923 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9924 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009925 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009926 asm volatile(
9927#ifdef CONFIG_X86_64
9928 "mov %%" _ASM_SP ", %[sp]\n\t"
9929 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9930 "push $%c[ss]\n\t"
9931 "push %[sp]\n\t"
9932#endif
9933 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009934 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009935 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009936 :
9937#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009938 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009939#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009940 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009941 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009942 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009943 [ss]"i"(__KERNEL_DS),
9944 [cs]"i"(__KERNEL_CS)
9945 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009946 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009947 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009948}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009949STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009950
Tom Lendackybc226f02018-05-10 22:06:39 +02009951static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009952{
Tom Lendackybc226f02018-05-10 22:06:39 +02009953 switch (index) {
9954 case MSR_IA32_SMBASE:
9955 /*
9956 * We cannot do SMM unless we can run the guest in big
9957 * real mode.
9958 */
9959 return enable_unrestricted_guest || emulate_invalid_guest_state;
9960 case MSR_AMD64_VIRT_SPEC_CTRL:
9961 /* This is AMD only. */
9962 return false;
9963 default:
9964 return true;
9965 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009966}
9967
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009968static bool vmx_mpx_supported(void)
9969{
9970 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9971 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9972}
9973
Wanpeng Li55412b22014-12-02 19:21:30 +08009974static bool vmx_xsaves_supported(void)
9975{
9976 return vmcs_config.cpu_based_2nd_exec_ctrl &
9977 SECONDARY_EXEC_XSAVES;
9978}
9979
Avi Kivity51aa01d2010-07-20 14:31:20 +03009980static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9981{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009982 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009983 bool unblock_nmi;
9984 u8 vector;
9985 bool idtv_info_valid;
9986
9987 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009988
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009989 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009990 if (vmx->loaded_vmcs->nmi_known_unmasked)
9991 return;
9992 /*
9993 * Can't use vmx->exit_intr_info since we're not sure what
9994 * the exit reason is.
9995 */
9996 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9997 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9998 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9999 /*
10000 * SDM 3: 27.7.1.2 (September 2008)
10001 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10002 * a guest IRET fault.
10003 * SDM 3: 23.2.2 (September 2008)
10004 * Bit 12 is undefined in any of the following cases:
10005 * If the VM exit sets the valid bit in the IDT-vectoring
10006 * information field.
10007 * If the VM exit is due to a double fault.
10008 */
10009 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10010 vector != DF_VECTOR && !idtv_info_valid)
10011 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10012 GUEST_INTR_STATE_NMI);
10013 else
10014 vmx->loaded_vmcs->nmi_known_unmasked =
10015 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10016 & GUEST_INTR_STATE_NMI);
10017 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10018 vmx->loaded_vmcs->vnmi_blocked_time +=
10019 ktime_to_ns(ktime_sub(ktime_get(),
10020 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010021}
10022
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010023static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010024 u32 idt_vectoring_info,
10025 int instr_len_field,
10026 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010027{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010028 u8 vector;
10029 int type;
10030 bool idtv_info_valid;
10031
10032 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010033
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010034 vcpu->arch.nmi_injected = false;
10035 kvm_clear_exception_queue(vcpu);
10036 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010037
10038 if (!idtv_info_valid)
10039 return;
10040
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010041 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010042
Avi Kivity668f6122008-07-02 09:28:55 +030010043 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10044 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010045
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010046 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010047 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010048 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010049 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010050 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010051 * Clear bit "block by NMI" before VM entry if a NMI
10052 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010053 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010054 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010055 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010056 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010057 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010058 /* fall through */
10059 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010060 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010061 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010062 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010063 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010064 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010065 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010066 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010067 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010068 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010069 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010070 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010071 break;
10072 default:
10073 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010074 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010075}
10076
Avi Kivity83422e12010-07-20 14:43:23 +030010077static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10078{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010079 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010080 VM_EXIT_INSTRUCTION_LEN,
10081 IDT_VECTORING_ERROR_CODE);
10082}
10083
Avi Kivityb463a6f2010-07-20 15:06:17 +030010084static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10085{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010086 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010087 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10088 VM_ENTRY_INSTRUCTION_LEN,
10089 VM_ENTRY_EXCEPTION_ERROR_CODE);
10090
10091 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10092}
10093
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010094static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10095{
10096 int i, nr_msrs;
10097 struct perf_guest_switch_msr *msrs;
10098
10099 msrs = perf_guest_get_msrs(&nr_msrs);
10100
10101 if (!msrs)
10102 return;
10103
10104 for (i = 0; i < nr_msrs; i++)
10105 if (msrs[i].host == msrs[i].guest)
10106 clear_atomic_switch_msr(vmx, msrs[i].msr);
10107 else
10108 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010109 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010110}
10111
Jiang Biao33365e72016-11-03 15:03:37 +080010112static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010113{
10114 struct vcpu_vmx *vmx = to_vmx(vcpu);
10115 u64 tscl;
10116 u32 delta_tsc;
10117
10118 if (vmx->hv_deadline_tsc == -1)
10119 return;
10120
10121 tscl = rdtsc();
10122 if (vmx->hv_deadline_tsc > tscl)
10123 /* sure to be 32 bit only because checked on set_hv_timer */
10124 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10125 cpu_preemption_timer_multi);
10126 else
10127 delta_tsc = 0;
10128
10129 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10130}
10131
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010132static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010133{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010134 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010135 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010136
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010137 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010138 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010139 vmx->loaded_vmcs->soft_vnmi_blocked))
10140 vmx->loaded_vmcs->entry_time = ktime_get();
10141
Avi Kivity104f2262010-11-18 13:12:52 +020010142 /* Don't enter VMX if guest state is invalid, let the exit handler
10143 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010144 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010145 return;
10146
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010147 if (vmx->ple_window_dirty) {
10148 vmx->ple_window_dirty = false;
10149 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10150 }
10151
Abel Gordon012f83c2013-04-18 14:39:25 +030010152 if (vmx->nested.sync_shadow_vmcs) {
10153 copy_vmcs12_to_shadow(vmx);
10154 vmx->nested.sync_shadow_vmcs = false;
10155 }
10156
Avi Kivity104f2262010-11-18 13:12:52 +020010157 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10158 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10159 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10160 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10161
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010162 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010163 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010164 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010165 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010166 }
10167
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010168 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010169 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010170 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010171 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010172 }
10173
Avi Kivity104f2262010-11-18 13:12:52 +020010174 /* When single-stepping over STI and MOV SS, we must clear the
10175 * corresponding interruptibility bits in the guest state. Otherwise
10176 * vmentry fails as it then expects bit 14 (BS) in pending debug
10177 * exceptions being set, but that's not correct for the guest debugging
10178 * case. */
10179 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10180 vmx_set_interrupt_shadow(vcpu, 0);
10181
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010182 if (static_cpu_has(X86_FEATURE_PKU) &&
10183 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10184 vcpu->arch.pkru != vmx->host_pkru)
10185 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010186
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010187 atomic_switch_perf_msrs(vmx);
10188
Yunhong Jiang64672c92016-06-13 14:19:59 -070010189 vmx_arm_hv_timer(vcpu);
10190
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010191 /*
10192 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10193 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10194 * is no need to worry about the conditional branch over the wrmsr
10195 * being speculatively taken.
10196 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010197 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010198
Nadav Har'Eld462b812011-05-24 15:26:10 +030010199 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010200
10201 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10202 (unsigned long)&current_evmcs->host_rsp : 0;
10203
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010204 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10205 if (vcpu->arch.l1tf_flush_l1d)
10206 vmx_l1d_flush(vcpu);
10207 }
10208
Avi Kivity104f2262010-11-18 13:12:52 +020010209 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010210 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010211 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10212 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10213 "push %%" _ASM_CX " \n\t"
10214 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010215 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010216 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010217 /* Avoid VMWRITE when Enlightened VMCS is in use */
10218 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10219 "jz 2f \n\t"
10220 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10221 "jmp 1f \n\t"
10222 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010223 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010224 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010225 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010226 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10227 "mov %%cr2, %%" _ASM_DX " \n\t"
10228 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010229 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010230 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010231 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010232 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010233 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010234 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010235 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10236 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10237 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10238 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10239 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10240 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010241#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010242 "mov %c[r8](%0), %%r8 \n\t"
10243 "mov %c[r9](%0), %%r9 \n\t"
10244 "mov %c[r10](%0), %%r10 \n\t"
10245 "mov %c[r11](%0), %%r11 \n\t"
10246 "mov %c[r12](%0), %%r12 \n\t"
10247 "mov %c[r13](%0), %%r13 \n\t"
10248 "mov %c[r14](%0), %%r14 \n\t"
10249 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010250#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010251 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010252
Avi Kivity6aa8b732006-12-10 02:21:36 -080010253 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010254 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010255 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010256 "jmp 2f \n\t"
10257 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10258 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010259 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010260 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010261 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010262 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010263 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10264 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10265 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10266 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10267 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10268 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10269 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010270#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010271 "mov %%r8, %c[r8](%0) \n\t"
10272 "mov %%r9, %c[r9](%0) \n\t"
10273 "mov %%r10, %c[r10](%0) \n\t"
10274 "mov %%r11, %c[r11](%0) \n\t"
10275 "mov %%r12, %c[r12](%0) \n\t"
10276 "mov %%r13, %c[r13](%0) \n\t"
10277 "mov %%r14, %c[r14](%0) \n\t"
10278 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010279 "xor %%r8d, %%r8d \n\t"
10280 "xor %%r9d, %%r9d \n\t"
10281 "xor %%r10d, %%r10d \n\t"
10282 "xor %%r11d, %%r11d \n\t"
10283 "xor %%r12d, %%r12d \n\t"
10284 "xor %%r13d, %%r13d \n\t"
10285 "xor %%r14d, %%r14d \n\t"
10286 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010287#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010288 "mov %%cr2, %%" _ASM_AX " \n\t"
10289 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010290
Jim Mattson0cb5b302018-01-03 14:31:38 -080010291 "xor %%eax, %%eax \n\t"
10292 "xor %%ebx, %%ebx \n\t"
10293 "xor %%esi, %%esi \n\t"
10294 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010295 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010296 ".pushsection .rodata \n\t"
10297 ".global vmx_return \n\t"
10298 "vmx_return: " _ASM_PTR " 2b \n\t"
10299 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010300 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010301 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010302 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010303 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010304 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10305 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10306 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10307 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10308 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10309 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10310 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010311#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010312 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10313 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10314 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10315 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10316 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10317 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10318 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10319 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010320#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010321 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10322 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010323 : "cc", "memory"
10324#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010325 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010326 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010327#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010328 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010329#endif
10330 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010331
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010332 /*
10333 * We do not use IBRS in the kernel. If this vCPU has used the
10334 * SPEC_CTRL MSR it may have left it on; save the value and
10335 * turn it off. This is much more efficient than blindly adding
10336 * it to the atomic save/restore list. Especially as the former
10337 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10338 *
10339 * For non-nested case:
10340 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10341 * save it.
10342 *
10343 * For nested case:
10344 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10345 * save it.
10346 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010347 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010348 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010349
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010350 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010351
David Woodhouse117cc7a2018-01-12 11:11:27 +000010352 /* Eliminate branch target predictions from guest mode */
10353 vmexit_fill_RSB();
10354
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010355 /* All fields are clean at this point */
10356 if (static_branch_unlikely(&enable_evmcs))
10357 current_evmcs->hv_clean_fields |=
10358 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10359
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010360 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010361 if (vmx->host_debugctlmsr)
10362 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010363
Avi Kivityaa67f602012-08-01 16:48:03 +030010364#ifndef CONFIG_X86_64
10365 /*
10366 * The sysexit path does not restore ds/es, so we must set them to
10367 * a reasonable value ourselves.
10368 *
10369 * We can't defer this to vmx_load_host_state() since that function
10370 * may be executed in interrupt context, which saves and restore segments
10371 * around it, nullifying its effect.
10372 */
10373 loadsegment(ds, __USER_DS);
10374 loadsegment(es, __USER_DS);
10375#endif
10376
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010377 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010378 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010379 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010380 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010381 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010382 vcpu->arch.regs_dirty = 0;
10383
Gleb Natapove0b890d2013-09-25 12:51:33 +030010384 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010385 * eager fpu is enabled if PKEY is supported and CR4 is switched
10386 * back on host, so it is safe to read guest PKRU from current
10387 * XSAVE.
10388 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010389 if (static_cpu_has(X86_FEATURE_PKU) &&
10390 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10391 vcpu->arch.pkru = __read_pkru();
10392 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010393 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010394 }
10395
Gleb Natapove0b890d2013-09-25 12:51:33 +030010396 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010397 vmx->idt_vectoring_info = 0;
10398
10399 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10400 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10401 return;
10402
10403 vmx->loaded_vmcs->launched = 1;
10404 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010405
Avi Kivity51aa01d2010-07-20 14:31:20 +030010406 vmx_complete_atomic_exit(vmx);
10407 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010408 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010409}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010410STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010411
Sean Christopherson434a1e92018-03-20 12:17:18 -070010412static struct kvm *vmx_vm_alloc(void)
10413{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010414 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010415 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010416}
10417
10418static void vmx_vm_free(struct kvm *kvm)
10419{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010420 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010421}
10422
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010423static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010424{
10425 struct vcpu_vmx *vmx = to_vmx(vcpu);
10426 int cpu;
10427
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010428 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010429 return;
10430
10431 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010432 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010433 vmx_vcpu_put(vcpu);
10434 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010435 put_cpu();
10436}
10437
Jim Mattson2f1fe812016-07-08 15:36:06 -070010438/*
10439 * Ensure that the current vmcs of the logical processor is the
10440 * vmcs01 of the vcpu before calling free_nested().
10441 */
10442static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10443{
10444 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010445
Christoffer Dallec7660c2017-12-04 21:35:23 +010010446 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010447 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010448 free_nested(vmx);
10449 vcpu_put(vcpu);
10450}
10451
Avi Kivity6aa8b732006-12-10 02:21:36 -080010452static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10453{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010454 struct vcpu_vmx *vmx = to_vmx(vcpu);
10455
Kai Huang843e4332015-01-28 10:54:28 +080010456 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010457 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010458 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010459 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010460 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010461 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010462 kfree(vmx->guest_msrs);
10463 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010464 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010465}
10466
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010467static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010468{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010469 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010470 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010471 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010472 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010473
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010474 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010475 return ERR_PTR(-ENOMEM);
10476
Wanpeng Li991e7a02015-09-16 17:30:05 +080010477 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010478
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010479 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10480 if (err)
10481 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010482
Peter Feiner4e595162016-07-07 14:49:58 -070010483 err = -ENOMEM;
10484
10485 /*
10486 * If PML is turned on, failure on enabling PML just results in failure
10487 * of creating the vcpu, therefore we can simplify PML logic (by
10488 * avoiding dealing with cases, such as enabling PML partially on vcpus
10489 * for the guest, etc.
10490 */
10491 if (enable_pml) {
10492 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10493 if (!vmx->pml_pg)
10494 goto uninit_vcpu;
10495 }
10496
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010497 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010498 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10499 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010500
Peter Feiner4e595162016-07-07 14:49:58 -070010501 if (!vmx->guest_msrs)
10502 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010503
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010504 err = alloc_loaded_vmcs(&vmx->vmcs01);
10505 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010506 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010507
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010508 msr_bitmap = vmx->vmcs01.msr_bitmap;
10509 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10510 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10511 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10512 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10513 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10514 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10515 vmx->msr_bitmap_mode = 0;
10516
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010517 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010518 cpu = get_cpu();
10519 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010520 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010521 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010522 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010523 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010524 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010525 err = alloc_apic_access_page(kvm);
10526 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010527 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010528 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010529
Sean Christophersone90008d2018-03-05 12:04:37 -080010530 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010531 err = init_rmode_identity_map(kvm);
10532 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010533 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010534 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010535
Wanpeng Li5c614b32015-10-13 09:18:36 -070010536 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010537 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10538 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010539 vmx->nested.vpid02 = allocate_vpid();
10540 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010541
Wincy Van705699a2015-02-03 23:58:17 +080010542 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010543 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010544
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010545 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10546
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010547 /*
10548 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10549 * or POSTED_INTR_WAKEUP_VECTOR.
10550 */
10551 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10552 vmx->pi_desc.sn = 1;
10553
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010554 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010555
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010556free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010557 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010558 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010559free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010560 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010561free_pml:
10562 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010563uninit_vcpu:
10564 kvm_vcpu_uninit(&vmx->vcpu);
10565free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010566 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010567 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010568 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010569}
10570
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010571#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10572
Wanpeng Lib31c1142018-03-12 04:53:04 -070010573static int vmx_vm_init(struct kvm *kvm)
10574{
10575 if (!ple_gap)
10576 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010577
10578 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10579 if (nosmt) {
10580 pr_err(L1TF_MSG);
10581 return -EOPNOTSUPP;
10582 }
10583 pr_warn(L1TF_MSG);
10584 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010585 return 0;
10586}
10587
Yang, Sheng002c7f72007-07-31 14:23:01 +030010588static void __init vmx_check_processor_compat(void *rtn)
10589{
10590 struct vmcs_config vmcs_conf;
10591
10592 *(int *)rtn = 0;
10593 if (setup_vmcs_config(&vmcs_conf) < 0)
10594 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010595 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010596 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10597 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10598 smp_processor_id());
10599 *(int *)rtn = -EIO;
10600 }
10601}
10602
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010603static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010604{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010605 u8 cache;
10606 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010607
Sheng Yang522c68c2009-04-27 20:35:43 +080010608 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010609 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010610 * 2. EPT with VT-d:
10611 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010612 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010613 * b. VT-d with snooping control feature: snooping control feature of
10614 * VT-d engine can guarantee the cache correctness. Just set it
10615 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010616 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010617 * consistent with host MTRR
10618 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010619 if (is_mmio) {
10620 cache = MTRR_TYPE_UNCACHABLE;
10621 goto exit;
10622 }
10623
10624 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010625 ipat = VMX_EPT_IPAT_BIT;
10626 cache = MTRR_TYPE_WRBACK;
10627 goto exit;
10628 }
10629
10630 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10631 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010632 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010633 cache = MTRR_TYPE_WRBACK;
10634 else
10635 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010636 goto exit;
10637 }
10638
Xiao Guangrongff536042015-06-15 16:55:22 +080010639 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010640
10641exit:
10642 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010643}
10644
Sheng Yang17cc3932010-01-05 19:02:27 +080010645static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010646{
Sheng Yang878403b2010-01-05 19:02:29 +080010647 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10648 return PT_DIRECTORY_LEVEL;
10649 else
10650 /* For shadow and EPT supported 1GB page */
10651 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010652}
10653
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010654static void vmcs_set_secondary_exec_control(u32 new_ctl)
10655{
10656 /*
10657 * These bits in the secondary execution controls field
10658 * are dynamic, the others are mostly based on the hypervisor
10659 * architecture and the guest's CPUID. Do not touch the
10660 * dynamic bits.
10661 */
10662 u32 mask =
10663 SECONDARY_EXEC_SHADOW_VMCS |
10664 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010665 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10666 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010667
10668 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10669
10670 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10671 (new_ctl & ~mask) | (cur_ctl & mask));
10672}
10673
David Matlack8322ebb2016-11-29 18:14:09 -080010674/*
10675 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10676 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10677 */
10678static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10679{
10680 struct vcpu_vmx *vmx = to_vmx(vcpu);
10681 struct kvm_cpuid_entry2 *entry;
10682
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010683 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10684 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010685
10686#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10687 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010688 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010689} while (0)
10690
10691 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10692 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10693 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10694 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10695 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10696 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10697 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10698 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10699 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10700 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10701 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10702 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10703 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10704 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10705 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10706
10707 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10708 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10709 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10710 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10711 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010712 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010713
10714#undef cr4_fixed1_update
10715}
10716
Sheng Yang0e851882009-12-18 16:48:46 +080010717static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10718{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010719 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010720
Paolo Bonzini80154d72017-08-24 13:55:35 +020010721 if (cpu_has_secondary_exec_ctrls()) {
10722 vmx_compute_secondary_exec_control(vmx);
10723 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010724 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010725
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010726 if (nested_vmx_allowed(vcpu))
10727 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10728 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10729 else
10730 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10731 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010732
10733 if (nested_vmx_allowed(vcpu))
10734 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010735}
10736
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010737static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10738{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010739 if (func == 1 && nested)
10740 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010741}
10742
Yang Zhang25d92082013-08-06 12:00:32 +030010743static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10744 struct x86_exception *fault)
10745{
Jan Kiszka533558b2014-01-04 18:47:20 +010010746 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010748 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010749 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010750
Bandan Dasc5f983f2017-05-05 15:25:14 -040010751 if (vmx->nested.pml_full) {
10752 exit_reason = EXIT_REASON_PML_FULL;
10753 vmx->nested.pml_full = false;
10754 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10755 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010756 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010757 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010758 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010759
10760 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010761 vmcs12->guest_physical_address = fault->address;
10762}
10763
Peter Feiner995f00a2017-06-30 17:26:32 -070010764static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10765{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010766 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010767}
10768
Nadav Har'El155a97a2013-08-05 11:07:16 +030010769/* Callbacks for nested_ept_init_mmu_context: */
10770
10771static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10772{
10773 /* return the page table to be shadowed - in our case, EPT12 */
10774 return get_vmcs12(vcpu)->ept_pointer;
10775}
10776
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010777static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010778{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010779 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010780 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010781 return 1;
10782
10783 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010784 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010785 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010786 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010787 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010788 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10789 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10790 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10791
10792 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010793 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010794}
10795
10796static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10797{
10798 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10799}
10800
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010801static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10802 u16 error_code)
10803{
10804 bool inequality, bit;
10805
10806 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10807 inequality =
10808 (error_code & vmcs12->page_fault_error_code_mask) !=
10809 vmcs12->page_fault_error_code_match;
10810 return inequality ^ bit;
10811}
10812
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010813static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10814 struct x86_exception *fault)
10815{
10816 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10817
10818 WARN_ON(!is_guest_mode(vcpu));
10819
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010820 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10821 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010822 vmcs12->vm_exit_intr_error_code = fault->error_code;
10823 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10824 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10825 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10826 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010827 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010828 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010829 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010830}
10831
Paolo Bonzinic9923842017-12-13 14:16:30 +010010832static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10833 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010834
10835static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010836 struct vmcs12 *vmcs12)
10837{
10838 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010839 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010840 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010841
10842 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010843 /*
10844 * Translate L1 physical address to host physical
10845 * address for vmcs02. Keep the page pinned, so this
10846 * physical address remains valid. We keep a reference
10847 * to it so we can release it later.
10848 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010849 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010850 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010851 vmx->nested.apic_access_page = NULL;
10852 }
10853 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010854 /*
10855 * If translation failed, no matter: This feature asks
10856 * to exit when accessing the given address, and if it
10857 * can never be accessed, this feature won't do
10858 * anything anyway.
10859 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010860 if (!is_error_page(page)) {
10861 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010862 hpa = page_to_phys(vmx->nested.apic_access_page);
10863 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10864 } else {
10865 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10866 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10867 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010868 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010869
10870 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010871 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010872 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010873 vmx->nested.virtual_apic_page = NULL;
10874 }
10875 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010876
10877 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010878 * If translation failed, VM entry will fail because
10879 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10880 * Failing the vm entry is _not_ what the processor
10881 * does but it's basically the only possibility we
10882 * have. We could still enter the guest if CR8 load
10883 * exits are enabled, CR8 store exits are enabled, and
10884 * virtualize APIC access is disabled; in this case
10885 * the processor would never use the TPR shadow and we
10886 * could simply clear the bit from the execution
10887 * control. But such a configuration is useless, so
10888 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010889 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010890 if (!is_error_page(page)) {
10891 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010892 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10893 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10894 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010895 }
10896
Wincy Van705699a2015-02-03 23:58:17 +080010897 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010898 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10899 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010900 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010901 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010902 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010903 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10904 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010905 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010906 vmx->nested.pi_desc_page = page;
10907 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010908 vmx->nested.pi_desc =
10909 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10910 (unsigned long)(vmcs12->posted_intr_desc_addr &
10911 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010912 vmcs_write64(POSTED_INTR_DESC_ADDR,
10913 page_to_phys(vmx->nested.pi_desc_page) +
10914 (unsigned long)(vmcs12->posted_intr_desc_addr &
10915 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010916 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010917 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010918 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10919 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010920 else
10921 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10922 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010923}
10924
Jan Kiszkaf41245002014-03-07 20:03:13 +010010925static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10926{
10927 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10928 struct vcpu_vmx *vmx = to_vmx(vcpu);
10929
10930 if (vcpu->arch.virtual_tsc_khz == 0)
10931 return;
10932
10933 /* Make sure short timeouts reliably trigger an immediate vmexit.
10934 * hrtimer_start does not guarantee this. */
10935 if (preemption_timeout <= 1) {
10936 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10937 return;
10938 }
10939
10940 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10941 preemption_timeout *= 1000000;
10942 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10943 hrtimer_start(&vmx->nested.preemption_timer,
10944 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10945}
10946
Jim Mattson56a20512017-07-06 16:33:06 -070010947static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10948 struct vmcs12 *vmcs12)
10949{
10950 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10951 return 0;
10952
10953 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10954 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10955 return -EINVAL;
10956
10957 return 0;
10958}
10959
Wincy Van3af18d92015-02-03 23:49:31 +080010960static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10961 struct vmcs12 *vmcs12)
10962{
Wincy Van3af18d92015-02-03 23:49:31 +080010963 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10964 return 0;
10965
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010966 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010967 return -EINVAL;
10968
10969 return 0;
10970}
10971
Jim Mattson712b12d2017-08-24 13:24:47 -070010972static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10973 struct vmcs12 *vmcs12)
10974{
10975 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10976 return 0;
10977
10978 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10979 return -EINVAL;
10980
10981 return 0;
10982}
10983
Wincy Van3af18d92015-02-03 23:49:31 +080010984/*
10985 * Merge L0's and L1's MSR bitmap, return false to indicate that
10986 * we do not use the hardware.
10987 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010988static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10989 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010990{
Wincy Van82f0dd42015-02-03 23:57:18 +080010991 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010992 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010993 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010994 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010995 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010996 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010997 *
10998 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10999 * ensures that we do not accidentally generate an L02 MSR bitmap
11000 * from the L12 MSR bitmap that is too permissive.
11001 * 2. That L1 or L2s have actually used the MSR. This avoids
11002 * unnecessarily merging of the bitmap if the MSR is unused. This
11003 * works properly because we only update the L01 MSR bitmap lazily.
11004 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11005 * updated to reflect this when L1 (or its L2s) actually write to
11006 * the MSR.
11007 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011008 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11009 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011010
Paolo Bonzinic9923842017-12-13 14:16:30 +010011011 /* Nothing to do if the MSR bitmap is not in use. */
11012 if (!cpu_has_vmx_msr_bitmap() ||
11013 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11014 return false;
11015
Ashok Raj15d45072018-02-01 22:59:43 +010011016 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011017 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011018 return false;
11019
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011020 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11021 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011022 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011023
Radim Krčmářd048c092016-08-08 20:16:22 +020011024 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011025 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11026 /*
11027 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11028 * just lets the processor take the value from the virtual-APIC page;
11029 * take those 256 bits directly from the L1 bitmap.
11030 */
11031 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11032 unsigned word = msr / BITS_PER_LONG;
11033 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11034 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011035 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011036 } else {
11037 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11038 unsigned word = msr / BITS_PER_LONG;
11039 msr_bitmap_l0[word] = ~0;
11040 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11041 }
11042 }
11043
11044 nested_vmx_disable_intercept_for_msr(
11045 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011046 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011047 MSR_TYPE_W);
11048
11049 if (nested_cpu_has_vid(vmcs12)) {
11050 nested_vmx_disable_intercept_for_msr(
11051 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011052 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011053 MSR_TYPE_W);
11054 nested_vmx_disable_intercept_for_msr(
11055 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011056 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011057 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011058 }
Ashok Raj15d45072018-02-01 22:59:43 +010011059
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011060 if (spec_ctrl)
11061 nested_vmx_disable_intercept_for_msr(
11062 msr_bitmap_l1, msr_bitmap_l0,
11063 MSR_IA32_SPEC_CTRL,
11064 MSR_TYPE_R | MSR_TYPE_W);
11065
Ashok Raj15d45072018-02-01 22:59:43 +010011066 if (pred_cmd)
11067 nested_vmx_disable_intercept_for_msr(
11068 msr_bitmap_l1, msr_bitmap_l0,
11069 MSR_IA32_PRED_CMD,
11070 MSR_TYPE_W);
11071
Wincy Vanf2b93282015-02-03 23:56:03 +080011072 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011073 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011074
11075 return true;
11076}
11077
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011078static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11079 struct vmcs12 *vmcs12)
11080{
11081 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11082 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11083 return -EINVAL;
11084 else
11085 return 0;
11086}
11087
Wincy Vanf2b93282015-02-03 23:56:03 +080011088static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11089 struct vmcs12 *vmcs12)
11090{
Wincy Van82f0dd42015-02-03 23:57:18 +080011091 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011092 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011093 !nested_cpu_has_vid(vmcs12) &&
11094 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011095 return 0;
11096
11097 /*
11098 * If virtualize x2apic mode is enabled,
11099 * virtualize apic access must be disabled.
11100 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011101 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11102 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011103 return -EINVAL;
11104
Wincy Van608406e2015-02-03 23:57:51 +080011105 /*
11106 * If virtual interrupt delivery is enabled,
11107 * we must exit on external interrupts.
11108 */
11109 if (nested_cpu_has_vid(vmcs12) &&
11110 !nested_exit_on_intr(vcpu))
11111 return -EINVAL;
11112
Wincy Van705699a2015-02-03 23:58:17 +080011113 /*
11114 * bits 15:8 should be zero in posted_intr_nv,
11115 * the descriptor address has been already checked
11116 * in nested_get_vmcs12_pages.
11117 */
11118 if (nested_cpu_has_posted_intr(vmcs12) &&
11119 (!nested_cpu_has_vid(vmcs12) ||
11120 !nested_exit_intr_ack_set(vcpu) ||
11121 vmcs12->posted_intr_nv & 0xff00))
11122 return -EINVAL;
11123
Wincy Vanf2b93282015-02-03 23:56:03 +080011124 /* tpr shadow is needed by all apicv features. */
11125 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11126 return -EINVAL;
11127
11128 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011129}
11130
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011131static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11132 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011133 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011134{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011135 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011136 u64 count, addr;
11137
11138 if (vmcs12_read_any(vcpu, count_field, &count) ||
11139 vmcs12_read_any(vcpu, addr_field, &addr)) {
11140 WARN_ON(1);
11141 return -EINVAL;
11142 }
11143 if (count == 0)
11144 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011145 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011146 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11147 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011148 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011149 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11150 addr_field, maxphyaddr, count, addr);
11151 return -EINVAL;
11152 }
11153 return 0;
11154}
11155
11156static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11157 struct vmcs12 *vmcs12)
11158{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011159 if (vmcs12->vm_exit_msr_load_count == 0 &&
11160 vmcs12->vm_exit_msr_store_count == 0 &&
11161 vmcs12->vm_entry_msr_load_count == 0)
11162 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011163 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011164 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011165 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011166 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011167 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011168 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011169 return -EINVAL;
11170 return 0;
11171}
11172
Bandan Dasc5f983f2017-05-05 15:25:14 -040011173static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11174 struct vmcs12 *vmcs12)
11175{
11176 u64 address = vmcs12->pml_address;
11177 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11178
11179 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11180 if (!nested_cpu_has_ept(vmcs12) ||
11181 !IS_ALIGNED(address, 4096) ||
11182 address >> maxphyaddr)
11183 return -EINVAL;
11184 }
11185
11186 return 0;
11187}
11188
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011189static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11190 struct vmx_msr_entry *e)
11191{
11192 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011193 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011194 return -EINVAL;
11195 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11196 e->index == MSR_IA32_UCODE_REV)
11197 return -EINVAL;
11198 if (e->reserved != 0)
11199 return -EINVAL;
11200 return 0;
11201}
11202
11203static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11204 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011205{
11206 if (e->index == MSR_FS_BASE ||
11207 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011208 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11209 nested_vmx_msr_check_common(vcpu, e))
11210 return -EINVAL;
11211 return 0;
11212}
11213
11214static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11215 struct vmx_msr_entry *e)
11216{
11217 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11218 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011219 return -EINVAL;
11220 return 0;
11221}
11222
11223/*
11224 * Load guest's/host's msr at nested entry/exit.
11225 * return 0 for success, entry index for failure.
11226 */
11227static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11228{
11229 u32 i;
11230 struct vmx_msr_entry e;
11231 struct msr_data msr;
11232
11233 msr.host_initiated = false;
11234 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011235 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11236 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011237 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011238 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11239 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011240 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011241 }
11242 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011243 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011244 "%s check failed (%u, 0x%x, 0x%x)\n",
11245 __func__, i, e.index, e.reserved);
11246 goto fail;
11247 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011248 msr.index = e.index;
11249 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011250 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011251 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011252 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11253 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011254 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011255 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011256 }
11257 return 0;
11258fail:
11259 return i + 1;
11260}
11261
11262static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11263{
11264 u32 i;
11265 struct vmx_msr_entry e;
11266
11267 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011268 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011269 if (kvm_vcpu_read_guest(vcpu,
11270 gpa + i * sizeof(e),
11271 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011272 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011273 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11274 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011275 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011276 }
11277 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011278 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011279 "%s check failed (%u, 0x%x, 0x%x)\n",
11280 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011281 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011282 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011283 msr_info.host_initiated = false;
11284 msr_info.index = e.index;
11285 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011286 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011287 "%s cannot read MSR (%u, 0x%x)\n",
11288 __func__, i, e.index);
11289 return -EINVAL;
11290 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011291 if (kvm_vcpu_write_guest(vcpu,
11292 gpa + i * sizeof(e) +
11293 offsetof(struct vmx_msr_entry, value),
11294 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011295 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011296 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011297 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011298 return -EINVAL;
11299 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011300 }
11301 return 0;
11302}
11303
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011304static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11305{
11306 unsigned long invalid_mask;
11307
11308 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11309 return (val & invalid_mask) == 0;
11310}
11311
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011312/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011313 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11314 * emulating VM entry into a guest with EPT enabled.
11315 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11316 * is assigned to entry_failure_code on failure.
11317 */
11318static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011319 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011320{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011321 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011322 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011323 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11324 return 1;
11325 }
11326
11327 /*
11328 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11329 * must not be dereferenced.
11330 */
11331 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11332 !nested_ept) {
11333 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11334 *entry_failure_code = ENTRY_FAIL_PDPTE;
11335 return 1;
11336 }
11337 }
11338
11339 vcpu->arch.cr3 = cr3;
11340 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11341 }
11342
11343 kvm_mmu_reset_context(vcpu);
11344 return 0;
11345}
11346
Jim Mattson6514dc32018-04-26 16:09:12 -070011347static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011348{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011349 struct vcpu_vmx *vmx = to_vmx(vcpu);
11350
11351 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11352 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11353 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11354 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11355 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11356 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11357 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11358 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11359 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11360 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11361 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11362 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11363 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11364 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11365 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11366 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11367 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11368 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11369 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11370 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11371 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11372 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11373 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11374 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11375 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11376 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11377 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11378 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11379 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11380 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11381 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011382
11383 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11384 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11385 vmcs12->guest_pending_dbg_exceptions);
11386 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11387 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11388
11389 if (nested_cpu_has_xsaves(vmcs12))
11390 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11391 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11392
11393 if (cpu_has_vmx_posted_intr())
11394 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11395
11396 /*
11397 * Whether page-faults are trapped is determined by a combination of
11398 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11399 * If enable_ept, L0 doesn't care about page faults and we should
11400 * set all of these to L1's desires. However, if !enable_ept, L0 does
11401 * care about (at least some) page faults, and because it is not easy
11402 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11403 * to exit on each and every L2 page fault. This is done by setting
11404 * MASK=MATCH=0 and (see below) EB.PF=1.
11405 * Note that below we don't need special code to set EB.PF beyond the
11406 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11407 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11408 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11409 */
11410 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11411 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11412 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11413 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11414
11415 /* All VMFUNCs are currently emulated through L0 vmexits. */
11416 if (cpu_has_vmx_vmfunc())
11417 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11418
11419 if (cpu_has_vmx_apicv()) {
11420 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11421 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11422 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11423 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11424 }
11425
11426 /*
11427 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11428 * Some constant fields are set here by vmx_set_constant_host_state().
11429 * Other fields are different per CPU, and will be set later when
11430 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11431 */
11432 vmx_set_constant_host_state(vmx);
11433
11434 /*
11435 * Set the MSR load/store lists to match L0's settings.
11436 */
11437 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011438 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11439 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11440 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11441 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011442
11443 set_cr4_guest_host_mask(vmx);
11444
11445 if (vmx_mpx_supported())
11446 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11447
11448 if (enable_vpid) {
11449 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11450 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11451 else
11452 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11453 }
11454
11455 /*
11456 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11457 */
11458 if (enable_ept) {
11459 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11460 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11461 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11462 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11463 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011464
11465 if (cpu_has_vmx_msr_bitmap())
11466 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011467}
11468
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011469/*
11470 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11471 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011472 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011473 * guest in a way that will both be appropriate to L1's requests, and our
11474 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11475 * function also has additional necessary side-effects, like setting various
11476 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011477 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11478 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011479 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011480static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011481 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011482{
11483 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011484 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011486 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011487 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011488 vmx->nested.dirty_vmcs12 = false;
11489 }
11490
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011491 /*
11492 * First, the fields that are shadowed. This must be kept in sync
11493 * with vmx_shadow_fields.h.
11494 */
11495
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011496 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011497 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011498 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011499 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11500 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011501
11502 /*
11503 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11504 * HOST_FS_BASE, HOST_GS_BASE.
11505 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011506
Jim Mattson6514dc32018-04-26 16:09:12 -070011507 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011508 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011509 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11510 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11511 } else {
11512 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11513 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11514 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011515 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011516 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11517 vmcs12->vm_entry_intr_info_field);
11518 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11519 vmcs12->vm_entry_exception_error_code);
11520 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11521 vmcs12->vm_entry_instruction_len);
11522 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11523 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011524 vmx->loaded_vmcs->nmi_known_unmasked =
11525 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011526 } else {
11527 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11528 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011529 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011530
Jan Kiszkaf41245002014-03-07 20:03:13 +010011531 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011532
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011533 /* Preemption timer setting is only taken from vmcs01. */
11534 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11535 exec_control |= vmcs_config.pin_based_exec_ctrl;
11536 if (vmx->hv_deadline_tsc == -1)
11537 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11538
11539 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011540 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011541 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11542 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011543 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011544 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011545 }
Wincy Van705699a2015-02-03 23:58:17 +080011546
Jan Kiszkaf41245002014-03-07 20:03:13 +010011547 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011548
Jan Kiszkaf41245002014-03-07 20:03:13 +010011549 vmx->nested.preemption_timer_expired = false;
11550 if (nested_cpu_has_preemption_timer(vmcs12))
11551 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011552
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011553 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011554 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011555
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011556 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011557 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011558 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011559 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011560 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011561 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011562 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11563 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011564 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011565 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11566 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11567 ~SECONDARY_EXEC_ENABLE_PML;
11568 exec_control |= vmcs12_exec_ctrl;
11569 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011570
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011571 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011572 vmcs_write16(GUEST_INTR_STATUS,
11573 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011574
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011575 /*
11576 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11577 * nested_get_vmcs12_pages will either fix it up or
11578 * remove the VM execution control.
11579 */
11580 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11581 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11582
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011583 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11584 }
11585
Jim Mattson83bafef2016-10-04 10:48:38 -070011586 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011587 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11588 * entry, but only if the current (host) sp changed from the value
11589 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11590 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11591 * here we just force the write to happen on entry.
11592 */
11593 vmx->host_rsp = 0;
11594
11595 exec_control = vmx_exec_control(vmx); /* L0's desires */
11596 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11597 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11598 exec_control &= ~CPU_BASED_TPR_SHADOW;
11599 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011600
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011601 /*
11602 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11603 * nested_get_vmcs12_pages can't fix it up, the illegal value
11604 * will result in a VM entry failure.
11605 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011606 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011607 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011608 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011609 } else {
11610#ifdef CONFIG_X86_64
11611 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11612 CPU_BASED_CR8_STORE_EXITING;
11613#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011614 }
11615
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011616 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011617 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11618 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011619 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011620 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11621 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11622
11623 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11624
11625 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11626 * bitwise-or of what L1 wants to trap for L2, and what we want to
11627 * trap. Note that CR0.TS also needs updating - we do this later.
11628 */
11629 update_exception_bitmap(vcpu);
11630 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11631 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11632
Nadav Har'El8049d652013-08-05 11:07:06 +030011633 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11634 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11635 * bits are further modified by vmx_set_efer() below.
11636 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011637 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011638
11639 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11640 * emulated by vmx_set_efer(), below.
11641 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011642 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011643 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11644 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011645 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11646
Jim Mattson6514dc32018-04-26 16:09:12 -070011647 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011648 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011649 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011650 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011651 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011652 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011653 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011654
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011655 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11656
Peter Feinerc95ba922016-08-17 09:36:47 -070011657 if (kvm_has_tsc_control)
11658 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011659
11660 if (enable_vpid) {
11661 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011662 * There is no direct mapping between vpid02 and vpid12, the
11663 * vpid02 is per-vCPU for L0 and reused while the value of
11664 * vpid12 is changed w/ one invvpid during nested vmentry.
11665 * The vpid12 is allocated by L1 for L2, so it will not
11666 * influence global bitmap(for vpid01 and vpid02 allocation)
11667 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011668 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011669 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011670 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11671 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011672 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011673 }
11674 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011675 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011676 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011677 }
11678
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011679 if (enable_pml) {
11680 /*
11681 * Conceptually we want to copy the PML address and index from
11682 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11683 * since we always flush the log on each vmexit, this happens
11684 * to be equivalent to simply resetting the fields in vmcs02.
11685 */
11686 ASSERT(vmx->pml_pg);
11687 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11688 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11689 }
11690
Nadav Har'El155a97a2013-08-05 11:07:16 +030011691 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011692 if (nested_ept_init_mmu_context(vcpu)) {
11693 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11694 return 1;
11695 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011696 } else if (nested_cpu_has2(vmcs12,
11697 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011698 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011699 }
11700
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011701 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011702 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11703 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011704 * The CR0_READ_SHADOW is what L2 should have expected to read given
11705 * the specifications by L1; It's not enough to take
11706 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11707 * have more bits than L1 expected.
11708 */
11709 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11710 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11711
11712 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11713 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11714
Jim Mattson6514dc32018-04-26 16:09:12 -070011715 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011716 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011717 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11718 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11719 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11720 else
11721 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11722 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11723 vmx_set_efer(vcpu, vcpu->arch.efer);
11724
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011725 /*
11726 * Guest state is invalid and unrestricted guest is disabled,
11727 * which means L1 attempted VMEntry to L2 with invalid state.
11728 * Fail the VMEntry.
11729 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011730 if (vmx->emulation_required) {
11731 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011732 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011733 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011734
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011735 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011736 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011737 entry_failure_code))
11738 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011739
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011740 if (!enable_ept)
11741 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11742
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011743 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11744 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011745 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011746}
11747
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011748static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11749{
11750 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11751 nested_cpu_has_virtual_nmis(vmcs12))
11752 return -EINVAL;
11753
11754 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11755 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11756 return -EINVAL;
11757
11758 return 0;
11759}
11760
Jim Mattsonca0bde22016-11-30 12:03:46 -080011761static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11762{
11763 struct vcpu_vmx *vmx = to_vmx(vcpu);
11764
11765 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11766 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11767 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11768
Jim Mattson56a20512017-07-06 16:33:06 -070011769 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11771
Jim Mattsonca0bde22016-11-30 12:03:46 -080011772 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11773 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11774
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011775 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11776 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11777
Jim Mattson712b12d2017-08-24 13:24:47 -070011778 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11779 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11780
Jim Mattsonca0bde22016-11-30 12:03:46 -080011781 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11782 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11783
11784 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11785 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11786
Bandan Dasc5f983f2017-05-05 15:25:14 -040011787 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11788 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11789
Jim Mattsonca0bde22016-11-30 12:03:46 -080011790 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011791 vmx->nested.msrs.procbased_ctls_low,
11792 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011793 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11794 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011795 vmx->nested.msrs.secondary_ctls_low,
11796 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011797 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011798 vmx->nested.msrs.pinbased_ctls_low,
11799 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011800 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011801 vmx->nested.msrs.exit_ctls_low,
11802 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011803 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011804 vmx->nested.msrs.entry_ctls_low,
11805 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011806 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11807
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011808 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011809 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11810
Bandan Das41ab9372017-08-03 15:54:43 -040011811 if (nested_cpu_has_vmfunc(vmcs12)) {
11812 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011813 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011814 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11815
11816 if (nested_cpu_has_eptp_switching(vmcs12)) {
11817 if (!nested_cpu_has_ept(vmcs12) ||
11818 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11819 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11820 }
11821 }
Bandan Das27c42a12017-08-03 15:54:42 -040011822
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011823 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11824 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11825
Jim Mattsonca0bde22016-11-30 12:03:46 -080011826 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11827 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11828 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11829 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11830
11831 return 0;
11832}
11833
11834static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11835 u32 *exit_qual)
11836{
11837 bool ia32e;
11838
11839 *exit_qual = ENTRY_FAIL_DEFAULT;
11840
11841 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11842 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11843 return 1;
11844
11845 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11846 vmcs12->vmcs_link_pointer != -1ull) {
11847 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11848 return 1;
11849 }
11850
11851 /*
11852 * If the load IA32_EFER VM-entry control is 1, the following checks
11853 * are performed on the field for the IA32_EFER MSR:
11854 * - Bits reserved in the IA32_EFER MSR must be 0.
11855 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11856 * the IA-32e mode guest VM-exit control. It must also be identical
11857 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11858 * CR0.PG) is 1.
11859 */
11860 if (to_vmx(vcpu)->nested.nested_run_pending &&
11861 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11862 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11863 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11864 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11865 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11866 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11867 return 1;
11868 }
11869
11870 /*
11871 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11872 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11873 * the values of the LMA and LME bits in the field must each be that of
11874 * the host address-space size VM-exit control.
11875 */
11876 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11877 ia32e = (vmcs12->vm_exit_controls &
11878 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11879 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11880 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11881 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11882 return 1;
11883 }
11884
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011885 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11886 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11887 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11888 return 1;
11889
Jim Mattsonca0bde22016-11-30 12:03:46 -080011890 return 0;
11891}
11892
Jim Mattson6514dc32018-04-26 16:09:12 -070011893static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011894{
11895 struct vcpu_vmx *vmx = to_vmx(vcpu);
11896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011897 u32 msr_entry_idx;
11898 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011899 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011900
Jim Mattson858e25c2016-11-30 12:03:47 -080011901 enter_guest_mode(vcpu);
11902
11903 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11904 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11905
Jim Mattsonde3a0022017-11-27 17:22:25 -060011906 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011907 vmx_segment_cache_clear(vmx);
11908
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011909 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11910 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11911
11912 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011913 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011914 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011915
11916 nested_get_vmcs12_pages(vcpu, vmcs12);
11917
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011918 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011919 msr_entry_idx = nested_vmx_load_msr(vcpu,
11920 vmcs12->vm_entry_msr_load_addr,
11921 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011922 if (msr_entry_idx)
11923 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011924
Jim Mattson858e25c2016-11-30 12:03:47 -080011925 /*
11926 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11927 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11928 * returned as far as L1 is concerned. It will only return (and set
11929 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11930 */
11931 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011932
11933fail:
11934 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11935 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11936 leave_guest_mode(vcpu);
11937 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11938 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11939 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011940}
11941
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011942/*
11943 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11944 * for running an L2 nested guest.
11945 */
11946static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11947{
11948 struct vmcs12 *vmcs12;
11949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011950 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011951 u32 exit_qual;
11952 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011953
Kyle Hueyeb277562016-11-29 12:40:39 -080011954 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011955 return 1;
11956
Kyle Hueyeb277562016-11-29 12:40:39 -080011957 if (!nested_vmx_check_vmcs12(vcpu))
11958 goto out;
11959
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011960 vmcs12 = get_vmcs12(vcpu);
11961
Abel Gordon012f83c2013-04-18 14:39:25 +030011962 if (enable_shadow_vmcs)
11963 copy_shadow_to_vmcs12(vmx);
11964
Nadav Har'El7c177932011-05-25 23:12:04 +030011965 /*
11966 * The nested entry process starts with enforcing various prerequisites
11967 * on vmcs12 as required by the Intel SDM, and act appropriately when
11968 * they fail: As the SDM explains, some conditions should cause the
11969 * instruction to fail, while others will cause the instruction to seem
11970 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11971 * To speed up the normal (success) code path, we should avoid checking
11972 * for misconfigurations which will anyway be caught by the processor
11973 * when using the merged vmcs02.
11974 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011975 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11976 nested_vmx_failValid(vcpu,
11977 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11978 goto out;
11979 }
11980
Nadav Har'El7c177932011-05-25 23:12:04 +030011981 if (vmcs12->launch_state == launch) {
11982 nested_vmx_failValid(vcpu,
11983 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11984 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011985 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011986 }
11987
Jim Mattsonca0bde22016-11-30 12:03:46 -080011988 ret = check_vmentry_prereqs(vcpu, vmcs12);
11989 if (ret) {
11990 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011991 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011992 }
11993
Nadav Har'El7c177932011-05-25 23:12:04 +030011994 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011995 * After this point, the trap flag no longer triggers a singlestep trap
11996 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11997 * This is not 100% correct; for performance reasons, we delegate most
11998 * of the checks on host state to the processor. If those fail,
11999 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012000 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012001 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012002
Jim Mattsonca0bde22016-11-30 12:03:46 -080012003 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12004 if (ret) {
12005 nested_vmx_entry_failure(vcpu, vmcs12,
12006 EXIT_REASON_INVALID_STATE, exit_qual);
12007 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012008 }
12009
12010 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012011 * We're finally done with prerequisite checking, and can start with
12012 * the nested entry.
12013 */
12014
Jim Mattson6514dc32018-04-26 16:09:12 -070012015 vmx->nested.nested_run_pending = 1;
12016 ret = enter_vmx_non_root_mode(vcpu);
12017 if (ret) {
12018 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012019 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012020 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012021
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012022 /* Hide L1D cache contents from the nested guest. */
12023 vmx->vcpu.arch.l1tf_flush_l1d = true;
12024
Chao Gao135a06c2018-02-11 10:06:30 +080012025 /*
12026 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12027 * by event injection, halt vcpu.
12028 */
12029 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012030 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12031 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012032 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012033 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012034 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012035
12036out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012037 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012038}
12039
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012040/*
12041 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12042 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12043 * This function returns the new value we should put in vmcs12.guest_cr0.
12044 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12045 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12046 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12047 * didn't trap the bit, because if L1 did, so would L0).
12048 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12049 * been modified by L2, and L1 knows it. So just leave the old value of
12050 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12051 * isn't relevant, because if L0 traps this bit it can set it to anything.
12052 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12053 * changed these bits, and therefore they need to be updated, but L0
12054 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12055 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12056 */
12057static inline unsigned long
12058vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12059{
12060 return
12061 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12062 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12063 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12064 vcpu->arch.cr0_guest_owned_bits));
12065}
12066
12067static inline unsigned long
12068vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12069{
12070 return
12071 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12072 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12073 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12074 vcpu->arch.cr4_guest_owned_bits));
12075}
12076
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012077static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12078 struct vmcs12 *vmcs12)
12079{
12080 u32 idt_vectoring;
12081 unsigned int nr;
12082
Wanpeng Li664f8e22017-08-24 03:35:09 -070012083 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012084 nr = vcpu->arch.exception.nr;
12085 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12086
12087 if (kvm_exception_is_soft(nr)) {
12088 vmcs12->vm_exit_instruction_len =
12089 vcpu->arch.event_exit_inst_len;
12090 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12091 } else
12092 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12093
12094 if (vcpu->arch.exception.has_error_code) {
12095 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12096 vmcs12->idt_vectoring_error_code =
12097 vcpu->arch.exception.error_code;
12098 }
12099
12100 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012101 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012102 vmcs12->idt_vectoring_info_field =
12103 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012104 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012105 nr = vcpu->arch.interrupt.nr;
12106 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12107
12108 if (vcpu->arch.interrupt.soft) {
12109 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12110 vmcs12->vm_entry_instruction_len =
12111 vcpu->arch.event_exit_inst_len;
12112 } else
12113 idt_vectoring |= INTR_TYPE_EXT_INTR;
12114
12115 vmcs12->idt_vectoring_info_field = idt_vectoring;
12116 }
12117}
12118
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012119static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12120{
12121 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012122 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012123 bool block_nested_events =
12124 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012125
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012126 if (vcpu->arch.exception.pending &&
12127 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012128 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012129 return -EBUSY;
12130 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012131 return 0;
12132 }
12133
Jan Kiszkaf41245002014-03-07 20:03:13 +010012134 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12135 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012136 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012137 return -EBUSY;
12138 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12139 return 0;
12140 }
12141
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012142 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012143 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012144 return -EBUSY;
12145 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12146 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12147 INTR_INFO_VALID_MASK, 0);
12148 /*
12149 * The NMI-triggered VM exit counts as injection:
12150 * clear this one and block further NMIs.
12151 */
12152 vcpu->arch.nmi_pending = 0;
12153 vmx_set_nmi_mask(vcpu, true);
12154 return 0;
12155 }
12156
12157 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12158 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012159 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012160 return -EBUSY;
12161 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012162 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012163 }
12164
David Hildenbrand6342c502017-01-25 11:58:58 +010012165 vmx_complete_nested_posted_interrupt(vcpu);
12166 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012167}
12168
Jan Kiszkaf41245002014-03-07 20:03:13 +010012169static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12170{
12171 ktime_t remaining =
12172 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12173 u64 value;
12174
12175 if (ktime_to_ns(remaining) <= 0)
12176 return 0;
12177
12178 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12179 do_div(value, 1000000);
12180 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12181}
12182
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012183/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012184 * Update the guest state fields of vmcs12 to reflect changes that
12185 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12186 * VM-entry controls is also updated, since this is really a guest
12187 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012188 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012189static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012190{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012191 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12192 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12193
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012194 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12195 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12196 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12197
12198 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12199 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12200 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12201 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12202 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12203 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12204 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12205 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12206 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12207 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12208 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12209 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12210 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12211 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12212 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12213 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12214 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12215 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12216 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12217 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12218 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12219 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12220 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12221 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12222 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12223 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12224 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12225 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12226 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12227 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12228 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12229 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12230 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12231 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12232 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12233 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12234
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012235 vmcs12->guest_interruptibility_info =
12236 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12237 vmcs12->guest_pending_dbg_exceptions =
12238 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012239 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12240 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12241 else
12242 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012243
Jan Kiszkaf41245002014-03-07 20:03:13 +010012244 if (nested_cpu_has_preemption_timer(vmcs12)) {
12245 if (vmcs12->vm_exit_controls &
12246 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12247 vmcs12->vmx_preemption_timer_value =
12248 vmx_get_preemption_timer_value(vcpu);
12249 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12250 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012251
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012252 /*
12253 * In some cases (usually, nested EPT), L2 is allowed to change its
12254 * own CR3 without exiting. If it has changed it, we must keep it.
12255 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12256 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12257 *
12258 * Additionally, restore L2's PDPTR to vmcs12.
12259 */
12260 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012261 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012262 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12263 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12264 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12265 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12266 }
12267
Jim Mattsond281e132017-06-01 12:44:46 -070012268 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012269
Wincy Van608406e2015-02-03 23:57:51 +080012270 if (nested_cpu_has_vid(vmcs12))
12271 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12272
Jan Kiszkac18911a2013-03-13 16:06:41 +010012273 vmcs12->vm_entry_controls =
12274 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012275 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012276
Jan Kiszka2996fca2014-06-16 13:59:43 +020012277 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12278 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12279 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12280 }
12281
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012282 /* TODO: These cannot have changed unless we have MSR bitmaps and
12283 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012284 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012285 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012286 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12287 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012288 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12289 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12290 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012291 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012292 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012293}
12294
12295/*
12296 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12297 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12298 * and this function updates it to reflect the changes to the guest state while
12299 * L2 was running (and perhaps made some exits which were handled directly by L0
12300 * without going back to L1), and to reflect the exit reason.
12301 * Note that we do not have to copy here all VMCS fields, just those that
12302 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12303 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12304 * which already writes to vmcs12 directly.
12305 */
12306static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12307 u32 exit_reason, u32 exit_intr_info,
12308 unsigned long exit_qualification)
12309{
12310 /* update guest state fields: */
12311 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012312
12313 /* update exit information fields: */
12314
Jan Kiszka533558b2014-01-04 18:47:20 +010012315 vmcs12->vm_exit_reason = exit_reason;
12316 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012317 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012318
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012319 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012320 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12321 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12322
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012323 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012324 vmcs12->launch_state = 1;
12325
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012326 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12327 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012328 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012329
12330 /*
12331 * Transfer the event that L0 or L1 may wanted to inject into
12332 * L2 to IDT_VECTORING_INFO_FIELD.
12333 */
12334 vmcs12_save_pending_event(vcpu, vmcs12);
12335 }
12336
12337 /*
12338 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12339 * preserved above and would only end up incorrectly in L1.
12340 */
12341 vcpu->arch.nmi_injected = false;
12342 kvm_clear_exception_queue(vcpu);
12343 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012344}
12345
Wanpeng Li5af41572017-11-05 16:54:49 -080012346static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12347 struct vmcs12 *vmcs12)
12348{
12349 u32 entry_failure_code;
12350
12351 nested_ept_uninit_mmu_context(vcpu);
12352
12353 /*
12354 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12355 * couldn't have changed.
12356 */
12357 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12358 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12359
12360 if (!enable_ept)
12361 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12362}
12363
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012364/*
12365 * A part of what we need to when the nested L2 guest exits and we want to
12366 * run its L1 parent, is to reset L1's guest state to the host state specified
12367 * in vmcs12.
12368 * This function is to be called not only on normal nested exit, but also on
12369 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12370 * Failures During or After Loading Guest State").
12371 * This function should be called when the active VMCS is L1's (vmcs01).
12372 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012373static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12374 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012375{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012376 struct kvm_segment seg;
12377
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012378 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12379 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012380 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012381 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12382 else
12383 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12384 vmx_set_efer(vcpu, vcpu->arch.efer);
12385
12386 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12387 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012388 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012389 /*
12390 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012391 * actually changed, because vmx_set_cr0 refers to efer set above.
12392 *
12393 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12394 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012395 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012396 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012397 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012398
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012399 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012400 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012401 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012402
Wanpeng Li5af41572017-11-05 16:54:49 -080012403 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012404
Liran Alon6f1e03b2018-05-22 17:16:14 +030012405 /*
12406 * If vmcs01 don't use VPID, CPU flushes TLB on every
12407 * VMEntry/VMExit. Thus, no need to flush TLB.
12408 *
12409 * If vmcs12 uses VPID, TLB entries populated by L2 are
12410 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12411 * with vmx->vpid. Thus, no need to flush TLB.
12412 *
12413 * Therefore, flush TLB only in case vmcs01 uses VPID and
12414 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12415 * are both tagged with vmx->vpid.
12416 */
12417 if (enable_vpid &&
12418 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012419 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012420 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012421
12422 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12423 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12424 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12425 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12426 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012427 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12428 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012429
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012430 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12431 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12432 vmcs_write64(GUEST_BNDCFGS, 0);
12433
Jan Kiszka44811c02013-08-04 17:17:27 +020012434 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012435 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012436 vcpu->arch.pat = vmcs12->host_ia32_pat;
12437 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012438 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12439 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12440 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012441
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012442 /* Set L1 segment info according to Intel SDM
12443 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12444 seg = (struct kvm_segment) {
12445 .base = 0,
12446 .limit = 0xFFFFFFFF,
12447 .selector = vmcs12->host_cs_selector,
12448 .type = 11,
12449 .present = 1,
12450 .s = 1,
12451 .g = 1
12452 };
12453 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12454 seg.l = 1;
12455 else
12456 seg.db = 1;
12457 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12458 seg = (struct kvm_segment) {
12459 .base = 0,
12460 .limit = 0xFFFFFFFF,
12461 .type = 3,
12462 .present = 1,
12463 .s = 1,
12464 .db = 1,
12465 .g = 1
12466 };
12467 seg.selector = vmcs12->host_ds_selector;
12468 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12469 seg.selector = vmcs12->host_es_selector;
12470 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12471 seg.selector = vmcs12->host_ss_selector;
12472 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12473 seg.selector = vmcs12->host_fs_selector;
12474 seg.base = vmcs12->host_fs_base;
12475 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12476 seg.selector = vmcs12->host_gs_selector;
12477 seg.base = vmcs12->host_gs_base;
12478 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12479 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012480 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012481 .limit = 0x67,
12482 .selector = vmcs12->host_tr_selector,
12483 .type = 11,
12484 .present = 1
12485 };
12486 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12487
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012488 kvm_set_dr(vcpu, 7, 0x400);
12489 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012490
Wincy Van3af18d92015-02-03 23:49:31 +080012491 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012492 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012493
Wincy Vanff651cb2014-12-11 08:52:58 +030012494 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12495 vmcs12->vm_exit_msr_load_count))
12496 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012497}
12498
12499/*
12500 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12501 * and modify vmcs12 to make it see what it would expect to see there if
12502 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12503 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012504static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12505 u32 exit_intr_info,
12506 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012507{
12508 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012509 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12510
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012511 /* trying to cancel vmlaunch/vmresume is a bug */
12512 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12513
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012514 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012515 * The only expected VM-instruction error is "VM entry with
12516 * invalid control field(s)." Anything else indicates a
12517 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012518 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012519 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12520 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12521
12522 leave_guest_mode(vcpu);
12523
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012524 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12525 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12526
Jim Mattson4f350c62017-09-14 16:31:44 -070012527 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012528 if (exit_reason == -1)
12529 sync_vmcs12(vcpu, vmcs12);
12530 else
12531 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12532 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012533
12534 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12535 vmcs12->vm_exit_msr_store_count))
12536 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012537 }
12538
Jim Mattson4f350c62017-09-14 16:31:44 -070012539 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012540 vm_entry_controls_reset_shadow(vmx);
12541 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012542 vmx_segment_cache_clear(vmx);
12543
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012544 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012545 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12546 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012547 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012548 if (vmx->hv_deadline_tsc == -1)
12549 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12550 PIN_BASED_VMX_PREEMPTION_TIMER);
12551 else
12552 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12553 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012554 if (kvm_has_tsc_control)
12555 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012556
Jim Mattson8d860bb2018-05-09 16:56:05 -040012557 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12558 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12559 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012560 } else if (!nested_cpu_has_ept(vmcs12) &&
12561 nested_cpu_has2(vmcs12,
12562 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012563 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012564 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012565
12566 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12567 vmx->host_rsp = 0;
12568
12569 /* Unpin physical memory we referred to in vmcs02 */
12570 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012571 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012572 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012573 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012574 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012575 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012576 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012577 }
Wincy Van705699a2015-02-03 23:58:17 +080012578 if (vmx->nested.pi_desc_page) {
12579 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012580 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012581 vmx->nested.pi_desc_page = NULL;
12582 vmx->nested.pi_desc = NULL;
12583 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012584
12585 /*
Tang Chen38b99172014-09-24 15:57:54 +080012586 * We are now running in L2, mmu_notifier will force to reload the
12587 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12588 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012589 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012590
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012591 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012592 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012593
12594 /* in case we halted in L2 */
12595 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012596
12597 if (likely(!vmx->fail)) {
12598 /*
12599 * TODO: SDM says that with acknowledge interrupt on
12600 * exit, bit 31 of the VM-exit interrupt information
12601 * (valid interrupt) is always set to 1 on
12602 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12603 * need kvm_cpu_has_interrupt(). See the commit
12604 * message for details.
12605 */
12606 if (nested_exit_intr_ack_set(vcpu) &&
12607 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12608 kvm_cpu_has_interrupt(vcpu)) {
12609 int irq = kvm_cpu_get_interrupt(vcpu);
12610 WARN_ON(irq < 0);
12611 vmcs12->vm_exit_intr_info = irq |
12612 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12613 }
12614
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012615 if (exit_reason != -1)
12616 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12617 vmcs12->exit_qualification,
12618 vmcs12->idt_vectoring_info_field,
12619 vmcs12->vm_exit_intr_info,
12620 vmcs12->vm_exit_intr_error_code,
12621 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012622
12623 load_vmcs12_host_state(vcpu, vmcs12);
12624
12625 return;
12626 }
12627
12628 /*
12629 * After an early L2 VM-entry failure, we're now back
12630 * in L1 which thinks it just finished a VMLAUNCH or
12631 * VMRESUME instruction, so we need to set the failure
12632 * flag and the VM-instruction error field of the VMCS
12633 * accordingly.
12634 */
12635 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012636
12637 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12638
Jim Mattson4f350c62017-09-14 16:31:44 -070012639 /*
12640 * The emulated instruction was already skipped in
12641 * nested_vmx_run, but the updated RIP was never
12642 * written back to the vmcs01.
12643 */
12644 skip_emulated_instruction(vcpu);
12645 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012646}
12647
Nadav Har'El7c177932011-05-25 23:12:04 +030012648/*
Jan Kiszka42124922014-01-04 18:47:19 +010012649 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12650 */
12651static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12652{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012653 if (is_guest_mode(vcpu)) {
12654 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012655 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012656 }
Jan Kiszka42124922014-01-04 18:47:19 +010012657 free_nested(to_vmx(vcpu));
12658}
12659
12660/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012661 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12662 * 23.7 "VM-entry failures during or after loading guest state" (this also
12663 * lists the acceptable exit-reason and exit-qualification parameters).
12664 * It should only be called before L2 actually succeeded to run, and when
12665 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12666 */
12667static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12668 struct vmcs12 *vmcs12,
12669 u32 reason, unsigned long qualification)
12670{
12671 load_vmcs12_host_state(vcpu, vmcs12);
12672 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12673 vmcs12->exit_qualification = qualification;
12674 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012675 if (enable_shadow_vmcs)
12676 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012677}
12678
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012679static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12680 struct x86_instruction_info *info,
12681 enum x86_intercept_stage stage)
12682{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012683 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12684 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12685
12686 /*
12687 * RDPID causes #UD if disabled through secondary execution controls.
12688 * Because it is marked as EmulateOnUD, we need to intercept it here.
12689 */
12690 if (info->intercept == x86_intercept_rdtscp &&
12691 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12692 ctxt->exception.vector = UD_VECTOR;
12693 ctxt->exception.error_code_valid = false;
12694 return X86EMUL_PROPAGATE_FAULT;
12695 }
12696
12697 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012698 return X86EMUL_CONTINUE;
12699}
12700
Yunhong Jiang64672c92016-06-13 14:19:59 -070012701#ifdef CONFIG_X86_64
12702/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12703static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12704 u64 divisor, u64 *result)
12705{
12706 u64 low = a << shift, high = a >> (64 - shift);
12707
12708 /* To avoid the overflow on divq */
12709 if (high >= divisor)
12710 return 1;
12711
12712 /* Low hold the result, high hold rem which is discarded */
12713 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12714 "rm" (divisor), "0" (low), "1" (high));
12715 *result = low;
12716
12717 return 0;
12718}
12719
12720static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12721{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012722 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012723 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012724
12725 if (kvm_mwait_in_guest(vcpu->kvm))
12726 return -EOPNOTSUPP;
12727
12728 vmx = to_vmx(vcpu);
12729 tscl = rdtsc();
12730 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12731 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012732 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12733
12734 if (delta_tsc > lapic_timer_advance_cycles)
12735 delta_tsc -= lapic_timer_advance_cycles;
12736 else
12737 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012738
12739 /* Convert to host delta tsc if tsc scaling is enabled */
12740 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12741 u64_shl_div_u64(delta_tsc,
12742 kvm_tsc_scaling_ratio_frac_bits,
12743 vcpu->arch.tsc_scaling_ratio,
12744 &delta_tsc))
12745 return -ERANGE;
12746
12747 /*
12748 * If the delta tsc can't fit in the 32 bit after the multi shift,
12749 * we can't use the preemption timer.
12750 * It's possible that it fits on later vmentries, but checking
12751 * on every vmentry is costly so we just use an hrtimer.
12752 */
12753 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12754 return -ERANGE;
12755
12756 vmx->hv_deadline_tsc = tscl + delta_tsc;
12757 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12758 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012759
12760 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012761}
12762
12763static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12764{
12765 struct vcpu_vmx *vmx = to_vmx(vcpu);
12766 vmx->hv_deadline_tsc = -1;
12767 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12768 PIN_BASED_VMX_PREEMPTION_TIMER);
12769}
12770#endif
12771
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012772static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012773{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012774 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012775 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012776}
12777
Kai Huang843e4332015-01-28 10:54:28 +080012778static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12779 struct kvm_memory_slot *slot)
12780{
12781 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12782 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12783}
12784
12785static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12786 struct kvm_memory_slot *slot)
12787{
12788 kvm_mmu_slot_set_dirty(kvm, slot);
12789}
12790
12791static void vmx_flush_log_dirty(struct kvm *kvm)
12792{
12793 kvm_flush_pml_buffers(kvm);
12794}
12795
Bandan Dasc5f983f2017-05-05 15:25:14 -040012796static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12797{
12798 struct vmcs12 *vmcs12;
12799 struct vcpu_vmx *vmx = to_vmx(vcpu);
12800 gpa_t gpa;
12801 struct page *page = NULL;
12802 u64 *pml_address;
12803
12804 if (is_guest_mode(vcpu)) {
12805 WARN_ON_ONCE(vmx->nested.pml_full);
12806
12807 /*
12808 * Check if PML is enabled for the nested guest.
12809 * Whether eptp bit 6 is set is already checked
12810 * as part of A/D emulation.
12811 */
12812 vmcs12 = get_vmcs12(vcpu);
12813 if (!nested_cpu_has_pml(vmcs12))
12814 return 0;
12815
Dan Carpenter47698862017-05-10 22:43:17 +030012816 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012817 vmx->nested.pml_full = true;
12818 return 1;
12819 }
12820
12821 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12822
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012823 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12824 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012825 return 0;
12826
12827 pml_address = kmap(page);
12828 pml_address[vmcs12->guest_pml_index--] = gpa;
12829 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012830 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012831 }
12832
12833 return 0;
12834}
12835
Kai Huang843e4332015-01-28 10:54:28 +080012836static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12837 struct kvm_memory_slot *memslot,
12838 gfn_t offset, unsigned long mask)
12839{
12840 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12841}
12842
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012843static void __pi_post_block(struct kvm_vcpu *vcpu)
12844{
12845 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12846 struct pi_desc old, new;
12847 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012848
12849 do {
12850 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012851 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12852 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012853
12854 dest = cpu_physical_id(vcpu->cpu);
12855
12856 if (x2apic_enabled())
12857 new.ndst = dest;
12858 else
12859 new.ndst = (dest << 8) & 0xFF00;
12860
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012861 /* set 'NV' to 'notification vector' */
12862 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012863 } while (cmpxchg64(&pi_desc->control, old.control,
12864 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012865
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012866 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12867 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012868 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012869 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012870 vcpu->pre_pcpu = -1;
12871 }
12872}
12873
Feng Wuefc64402015-09-18 22:29:51 +080012874/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012875 * This routine does the following things for vCPU which is going
12876 * to be blocked if VT-d PI is enabled.
12877 * - Store the vCPU to the wakeup list, so when interrupts happen
12878 * we can find the right vCPU to wake up.
12879 * - Change the Posted-interrupt descriptor as below:
12880 * 'NDST' <-- vcpu->pre_pcpu
12881 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12882 * - If 'ON' is set during this process, which means at least one
12883 * interrupt is posted for this vCPU, we cannot block it, in
12884 * this case, return 1, otherwise, return 0.
12885 *
12886 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012887static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012888{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012889 unsigned int dest;
12890 struct pi_desc old, new;
12891 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12892
12893 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012894 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12895 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012896 return 0;
12897
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012898 WARN_ON(irqs_disabled());
12899 local_irq_disable();
12900 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12901 vcpu->pre_pcpu = vcpu->cpu;
12902 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12903 list_add_tail(&vcpu->blocked_vcpu_list,
12904 &per_cpu(blocked_vcpu_on_cpu,
12905 vcpu->pre_pcpu));
12906 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12907 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012908
12909 do {
12910 old.control = new.control = pi_desc->control;
12911
Feng Wubf9f6ac2015-09-18 22:29:55 +080012912 WARN((pi_desc->sn == 1),
12913 "Warning: SN field of posted-interrupts "
12914 "is set before blocking\n");
12915
12916 /*
12917 * Since vCPU can be preempted during this process,
12918 * vcpu->cpu could be different with pre_pcpu, we
12919 * need to set pre_pcpu as the destination of wakeup
12920 * notification event, then we can find the right vCPU
12921 * to wakeup in wakeup handler if interrupts happen
12922 * when the vCPU is in blocked state.
12923 */
12924 dest = cpu_physical_id(vcpu->pre_pcpu);
12925
12926 if (x2apic_enabled())
12927 new.ndst = dest;
12928 else
12929 new.ndst = (dest << 8) & 0xFF00;
12930
12931 /* set 'NV' to 'wakeup vector' */
12932 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012933 } while (cmpxchg64(&pi_desc->control, old.control,
12934 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012935
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012936 /* We should not block the vCPU if an interrupt is posted for it. */
12937 if (pi_test_on(pi_desc) == 1)
12938 __pi_post_block(vcpu);
12939
12940 local_irq_enable();
12941 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012942}
12943
Yunhong Jiangbc225122016-06-13 14:19:58 -070012944static int vmx_pre_block(struct kvm_vcpu *vcpu)
12945{
12946 if (pi_pre_block(vcpu))
12947 return 1;
12948
Yunhong Jiang64672c92016-06-13 14:19:59 -070012949 if (kvm_lapic_hv_timer_in_use(vcpu))
12950 kvm_lapic_switch_to_sw_timer(vcpu);
12951
Yunhong Jiangbc225122016-06-13 14:19:58 -070012952 return 0;
12953}
12954
12955static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012956{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012957 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012958 return;
12959
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012960 WARN_ON(irqs_disabled());
12961 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012962 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012963 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012964}
12965
Yunhong Jiangbc225122016-06-13 14:19:58 -070012966static void vmx_post_block(struct kvm_vcpu *vcpu)
12967{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012968 if (kvm_x86_ops->set_hv_timer)
12969 kvm_lapic_switch_to_hv_timer(vcpu);
12970
Yunhong Jiangbc225122016-06-13 14:19:58 -070012971 pi_post_block(vcpu);
12972}
12973
Feng Wubf9f6ac2015-09-18 22:29:55 +080012974/*
Feng Wuefc64402015-09-18 22:29:51 +080012975 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12976 *
12977 * @kvm: kvm
12978 * @host_irq: host irq of the interrupt
12979 * @guest_irq: gsi of the interrupt
12980 * @set: set or unset PI
12981 * returns 0 on success, < 0 on failure
12982 */
12983static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12984 uint32_t guest_irq, bool set)
12985{
12986 struct kvm_kernel_irq_routing_entry *e;
12987 struct kvm_irq_routing_table *irq_rt;
12988 struct kvm_lapic_irq irq;
12989 struct kvm_vcpu *vcpu;
12990 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012991 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012992
12993 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012994 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12995 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012996 return 0;
12997
12998 idx = srcu_read_lock(&kvm->irq_srcu);
12999 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013000 if (guest_irq >= irq_rt->nr_rt_entries ||
13001 hlist_empty(&irq_rt->map[guest_irq])) {
13002 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13003 guest_irq, irq_rt->nr_rt_entries);
13004 goto out;
13005 }
Feng Wuefc64402015-09-18 22:29:51 +080013006
13007 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13008 if (e->type != KVM_IRQ_ROUTING_MSI)
13009 continue;
13010 /*
13011 * VT-d PI cannot support posting multicast/broadcast
13012 * interrupts to a vCPU, we still use interrupt remapping
13013 * for these kind of interrupts.
13014 *
13015 * For lowest-priority interrupts, we only support
13016 * those with single CPU as the destination, e.g. user
13017 * configures the interrupts via /proc/irq or uses
13018 * irqbalance to make the interrupts single-CPU.
13019 *
13020 * We will support full lowest-priority interrupt later.
13021 */
13022
Radim Krčmář371313132016-07-12 22:09:27 +020013023 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013024 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13025 /*
13026 * Make sure the IRTE is in remapped mode if
13027 * we don't handle it in posted mode.
13028 */
13029 ret = irq_set_vcpu_affinity(host_irq, NULL);
13030 if (ret < 0) {
13031 printk(KERN_INFO
13032 "failed to back to remapped mode, irq: %u\n",
13033 host_irq);
13034 goto out;
13035 }
13036
Feng Wuefc64402015-09-18 22:29:51 +080013037 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013038 }
Feng Wuefc64402015-09-18 22:29:51 +080013039
13040 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13041 vcpu_info.vector = irq.vector;
13042
hu huajun2698d822018-04-11 15:16:40 +080013043 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013044 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13045
13046 if (set)
13047 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013048 else
Feng Wuefc64402015-09-18 22:29:51 +080013049 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013050
13051 if (ret < 0) {
13052 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13053 __func__);
13054 goto out;
13055 }
13056 }
13057
13058 ret = 0;
13059out:
13060 srcu_read_unlock(&kvm->irq_srcu, idx);
13061 return ret;
13062}
13063
Ashok Rajc45dcc72016-06-22 14:59:56 +080013064static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13065{
13066 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13067 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13068 FEATURE_CONTROL_LMCE;
13069 else
13070 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13071 ~FEATURE_CONTROL_LMCE;
13072}
13073
Ladi Prosek72d7b372017-10-11 16:54:41 +020013074static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13075{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013076 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13077 if (to_vmx(vcpu)->nested.nested_run_pending)
13078 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013079 return 1;
13080}
13081
Ladi Prosek0234bf82017-10-11 16:54:40 +020013082static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13083{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013084 struct vcpu_vmx *vmx = to_vmx(vcpu);
13085
13086 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13087 if (vmx->nested.smm.guest_mode)
13088 nested_vmx_vmexit(vcpu, -1, 0, 0);
13089
13090 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13091 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013092 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013093 return 0;
13094}
13095
13096static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13097{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013098 struct vcpu_vmx *vmx = to_vmx(vcpu);
13099 int ret;
13100
13101 if (vmx->nested.smm.vmxon) {
13102 vmx->nested.vmxon = true;
13103 vmx->nested.smm.vmxon = false;
13104 }
13105
13106 if (vmx->nested.smm.guest_mode) {
13107 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013108 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013109 vcpu->arch.hflags |= HF_SMM_MASK;
13110 if (ret)
13111 return ret;
13112
13113 vmx->nested.smm.guest_mode = false;
13114 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013115 return 0;
13116}
13117
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013118static int enable_smi_window(struct kvm_vcpu *vcpu)
13119{
13120 return 0;
13121}
13122
Kees Cook404f6aa2016-08-08 16:29:06 -070013123static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013124 .cpu_has_kvm_support = cpu_has_kvm_support,
13125 .disabled_by_bios = vmx_disabled_by_bios,
13126 .hardware_setup = hardware_setup,
13127 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013128 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013129 .hardware_enable = hardware_enable,
13130 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013131 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013132 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013133
Wanpeng Lib31c1142018-03-12 04:53:04 -070013134 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013135 .vm_alloc = vmx_vm_alloc,
13136 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013137
Avi Kivity6aa8b732006-12-10 02:21:36 -080013138 .vcpu_create = vmx_create_vcpu,
13139 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013140 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013141
Avi Kivity04d2cc72007-09-10 18:10:54 +030013142 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013143 .vcpu_load = vmx_vcpu_load,
13144 .vcpu_put = vmx_vcpu_put,
13145
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013146 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013147 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013148 .get_msr = vmx_get_msr,
13149 .set_msr = vmx_set_msr,
13150 .get_segment_base = vmx_get_segment_base,
13151 .get_segment = vmx_get_segment,
13152 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013153 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013154 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013155 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013156 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013157 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013158 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013159 .set_cr3 = vmx_set_cr3,
13160 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013161 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013162 .get_idt = vmx_get_idt,
13163 .set_idt = vmx_set_idt,
13164 .get_gdt = vmx_get_gdt,
13165 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013166 .get_dr6 = vmx_get_dr6,
13167 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013168 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013169 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013170 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013171 .get_rflags = vmx_get_rflags,
13172 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013173
Avi Kivity6aa8b732006-12-10 02:21:36 -080013174 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013175
Avi Kivity6aa8b732006-12-10 02:21:36 -080013176 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013177 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013178 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013179 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13180 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013181 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013182 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013183 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013184 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013185 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013186 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013187 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013188 .get_nmi_mask = vmx_get_nmi_mask,
13189 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013190 .enable_nmi_window = enable_nmi_window,
13191 .enable_irq_window = enable_irq_window,
13192 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013193 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013194 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013195 .get_enable_apicv = vmx_get_enable_apicv,
13196 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013197 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013198 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013199 .hwapic_irr_update = vmx_hwapic_irr_update,
13200 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013201 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13202 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013203
Izik Eiduscbc94022007-10-25 00:29:55 +020013204 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013205 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013206 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013207 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013208
Avi Kivity586f9602010-11-18 13:09:54 +020013209 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013210
Sheng Yang17cc3932010-01-05 19:02:27 +080013211 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013212
13213 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013214
13215 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013216 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013217
13218 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013219
13220 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013221
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013222 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013223 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013224
13225 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013226
13227 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013228 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013229 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013230 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013231 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013232
13233 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013234
13235 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013236
13237 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13238 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13239 .flush_log_dirty = vmx_flush_log_dirty,
13240 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013241 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013242
Feng Wubf9f6ac2015-09-18 22:29:55 +080013243 .pre_block = vmx_pre_block,
13244 .post_block = vmx_post_block,
13245
Wei Huang25462f72015-06-19 15:45:05 +020013246 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013247
13248 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013249
13250#ifdef CONFIG_X86_64
13251 .set_hv_timer = vmx_set_hv_timer,
13252 .cancel_hv_timer = vmx_cancel_hv_timer,
13253#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013254
13255 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013256
Ladi Prosek72d7b372017-10-11 16:54:41 +020013257 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013258 .pre_enter_smm = vmx_pre_enter_smm,
13259 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013260 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013261};
13262
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013263static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013264{
13265 if (vmx_l1d_flush_pages) {
13266 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13267 vmx_l1d_flush_pages = NULL;
13268 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013269 /* Restore state so sysfs ignores VMX */
13270 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013271}
13272
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013273static void vmx_exit(void)
13274{
13275#ifdef CONFIG_KEXEC_CORE
13276 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13277 synchronize_rcu();
13278#endif
13279
13280 kvm_exit();
13281
13282#if IS_ENABLED(CONFIG_HYPERV)
13283 if (static_branch_unlikely(&enable_evmcs)) {
13284 int cpu;
13285 struct hv_vp_assist_page *vp_ap;
13286 /*
13287 * Reset everything to support using non-enlightened VMCS
13288 * access later (e.g. when we reload the module with
13289 * enlightened_vmcs=0)
13290 */
13291 for_each_online_cpu(cpu) {
13292 vp_ap = hv_get_vp_assist_page(cpu);
13293
13294 if (!vp_ap)
13295 continue;
13296
13297 vp_ap->current_nested_vmcs = 0;
13298 vp_ap->enlighten_vmentry = 0;
13299 }
13300
13301 static_branch_disable(&enable_evmcs);
13302 }
13303#endif
13304 vmx_cleanup_l1d_flush();
13305}
13306module_exit(vmx_exit);
13307
Avi Kivity6aa8b732006-12-10 02:21:36 -080013308static int __init vmx_init(void)
13309{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013310 int r;
13311
13312#if IS_ENABLED(CONFIG_HYPERV)
13313 /*
13314 * Enlightened VMCS usage should be recommended and the host needs
13315 * to support eVMCS v1 or above. We can also disable eVMCS support
13316 * with module parameter.
13317 */
13318 if (enlightened_vmcs &&
13319 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13320 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13321 KVM_EVMCS_VERSION) {
13322 int cpu;
13323
13324 /* Check that we have assist pages on all online CPUs */
13325 for_each_online_cpu(cpu) {
13326 if (!hv_get_vp_assist_page(cpu)) {
13327 enlightened_vmcs = false;
13328 break;
13329 }
13330 }
13331
13332 if (enlightened_vmcs) {
13333 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13334 static_branch_enable(&enable_evmcs);
13335 }
13336 } else {
13337 enlightened_vmcs = false;
13338 }
13339#endif
13340
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013341 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13342 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013343 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013344 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013345
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013346 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013347 * Must be called after kvm_init() so enable_ept is properly set
13348 * up. Hand the parameter mitigation value in which was stored in
13349 * the pre module init parser. If no parameter was given, it will
13350 * contain 'auto' which will be turned into the default 'cond'
13351 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013352 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013353 if (boot_cpu_has(X86_BUG_L1TF)) {
13354 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13355 if (r) {
13356 vmx_exit();
13357 return r;
13358 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013359 }
13360
Dave Young2965faa2015-09-09 15:38:55 -070013361#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013362 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13363 crash_vmclear_local_loaded_vmcss);
13364#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013365 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013366
He, Qingfdef3ad2007-04-30 09:45:24 +030013367 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013368}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013369module_init(vmx_init);