blob: 37c7ff5811846503365c861699a7c31f252c10f7 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
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 Kiszkaf4124502014-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);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200196
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200197/* Storage for pre module init parameter parsing */
198static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200199
200static const struct {
201 const char *option;
202 enum vmx_l1d_flush_state cmd;
203} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200204 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200205 {"never", VMENTER_L1D_FLUSH_NEVER},
206 {"cond", VMENTER_L1D_FLUSH_COND},
207 {"always", VMENTER_L1D_FLUSH_ALWAYS},
208};
209
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200210#define L1D_CACHE_ORDER 4
211static void *vmx_l1d_flush_pages;
212
213static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
214{
215 struct page *page;
216
217 /* If set to 'auto' select 'cond' */
218 if (l1tf == VMENTER_L1D_FLUSH_AUTO)
219 l1tf = VMENTER_L1D_FLUSH_COND;
220
221 if (!enable_ept) {
222 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
223 return 0;
224 }
225
226 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
227 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
228 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
229 if (!page)
230 return -ENOMEM;
231 vmx_l1d_flush_pages = page_address(page);
232 }
233
234 l1tf_vmx_mitigation = l1tf;
235
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200236 if (l1tf == VMENTER_L1D_FLUSH_NEVER)
237 return 0;
238
239 static_branch_enable(&vmx_l1d_should_flush);
240 if (l1tf == VMENTER_L1D_FLUSH_ALWAYS)
241 static_branch_enable(&vmx_l1d_flush_always);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200242 return 0;
243}
244
245static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200246{
247 unsigned int i;
248
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200249 if (s) {
250 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
251 if (!strcmp(s, vmentry_l1d_param[i].option))
252 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200253 }
254 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200255 return -EINVAL;
256}
257
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200258static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
259{
260 int l1tf;
261
262 if (!boot_cpu_has(X86_BUG_L1TF))
263 return 0;
264
265 l1tf = vmentry_l1d_flush_parse(s);
266 if (l1tf < 0)
267 return l1tf;
268
269 /*
270 * Has vmx_init() run already? If not then this is the pre init
271 * parameter parsing. In that case just store the value and let
272 * vmx_init() do the proper setup after enable_ept has been
273 * established.
274 */
275 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
276 vmentry_l1d_flush_param = l1tf;
277 return 0;
278 }
279
280 return vmx_setup_l1d_flush(l1tf);
281}
282
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200283static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
284{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200285 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200286}
287
288static const struct kernel_param_ops vmentry_l1d_flush_ops = {
289 .set = vmentry_l1d_flush_set,
290 .get = vmentry_l1d_flush_get,
291};
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200292module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, S_IRUGO);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200293
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700294struct kvm_vmx {
295 struct kvm kvm;
296
297 unsigned int tss_addr;
298 bool ept_identity_pagetable_done;
299 gpa_t ept_identity_map_addr;
300};
301
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200302#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300303
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400304struct vmcs {
305 u32 revision_id;
306 u32 abort;
307 char data[0];
308};
309
Nadav Har'Eld462b812011-05-24 15:26:10 +0300310/*
311 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
312 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
313 * loaded on this CPU (so we can clear them if the CPU goes down).
314 */
315struct loaded_vmcs {
316 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700317 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300318 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200319 bool launched;
320 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200321 unsigned long vmcs_host_cr3; /* May not match real cr3 */
322 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100323 /* Support for vnmi-less CPUs */
324 int soft_vnmi_blocked;
325 ktime_t entry_time;
326 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100327 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300328 struct list_head loaded_vmcss_on_cpu_link;
329};
330
Avi Kivity26bb0982009-09-07 11:14:12 +0300331struct shared_msr_entry {
332 unsigned index;
333 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200334 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300335};
336
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300337/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300338 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
339 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
340 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
341 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
342 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
343 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600344 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300345 * underlying hardware which will be used to run L2.
346 * This structure is packed to ensure that its layout is identical across
347 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700348 *
349 * IMPORTANT: Changing the layout of existing fields in this structure
350 * will break save/restore compatibility with older kvm releases. When
351 * adding new fields, either use space in the reserved padding* arrays
352 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300353 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300354typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300355struct __packed vmcs12 {
356 /* According to the Intel spec, a VMCS region must start with the
357 * following two fields. Then follow implementation-specific data.
358 */
359 u32 revision_id;
360 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300361
Nadav Har'El27d6c862011-05-25 23:06:59 +0300362 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
363 u32 padding[7]; /* room for future expansion */
364
Nadav Har'El22bd0352011-05-25 23:05:57 +0300365 u64 io_bitmap_a;
366 u64 io_bitmap_b;
367 u64 msr_bitmap;
368 u64 vm_exit_msr_store_addr;
369 u64 vm_exit_msr_load_addr;
370 u64 vm_entry_msr_load_addr;
371 u64 tsc_offset;
372 u64 virtual_apic_page_addr;
373 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800374 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300375 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800376 u64 eoi_exit_bitmap0;
377 u64 eoi_exit_bitmap1;
378 u64 eoi_exit_bitmap2;
379 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800380 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300381 u64 guest_physical_address;
382 u64 vmcs_link_pointer;
383 u64 guest_ia32_debugctl;
384 u64 guest_ia32_pat;
385 u64 guest_ia32_efer;
386 u64 guest_ia32_perf_global_ctrl;
387 u64 guest_pdptr0;
388 u64 guest_pdptr1;
389 u64 guest_pdptr2;
390 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100391 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300392 u64 host_ia32_pat;
393 u64 host_ia32_efer;
394 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700395 u64 vmread_bitmap;
396 u64 vmwrite_bitmap;
397 u64 vm_function_control;
398 u64 eptp_list_address;
399 u64 pml_address;
400 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300401 /*
402 * To allow migration of L1 (complete with its L2 guests) between
403 * machines of different natural widths (32 or 64 bit), we cannot have
404 * unsigned long fields with no explict size. We use u64 (aliased
405 * natural_width) instead. Luckily, x86 is little-endian.
406 */
407 natural_width cr0_guest_host_mask;
408 natural_width cr4_guest_host_mask;
409 natural_width cr0_read_shadow;
410 natural_width cr4_read_shadow;
411 natural_width cr3_target_value0;
412 natural_width cr3_target_value1;
413 natural_width cr3_target_value2;
414 natural_width cr3_target_value3;
415 natural_width exit_qualification;
416 natural_width guest_linear_address;
417 natural_width guest_cr0;
418 natural_width guest_cr3;
419 natural_width guest_cr4;
420 natural_width guest_es_base;
421 natural_width guest_cs_base;
422 natural_width guest_ss_base;
423 natural_width guest_ds_base;
424 natural_width guest_fs_base;
425 natural_width guest_gs_base;
426 natural_width guest_ldtr_base;
427 natural_width guest_tr_base;
428 natural_width guest_gdtr_base;
429 natural_width guest_idtr_base;
430 natural_width guest_dr7;
431 natural_width guest_rsp;
432 natural_width guest_rip;
433 natural_width guest_rflags;
434 natural_width guest_pending_dbg_exceptions;
435 natural_width guest_sysenter_esp;
436 natural_width guest_sysenter_eip;
437 natural_width host_cr0;
438 natural_width host_cr3;
439 natural_width host_cr4;
440 natural_width host_fs_base;
441 natural_width host_gs_base;
442 natural_width host_tr_base;
443 natural_width host_gdtr_base;
444 natural_width host_idtr_base;
445 natural_width host_ia32_sysenter_esp;
446 natural_width host_ia32_sysenter_eip;
447 natural_width host_rsp;
448 natural_width host_rip;
449 natural_width paddingl[8]; /* room for future expansion */
450 u32 pin_based_vm_exec_control;
451 u32 cpu_based_vm_exec_control;
452 u32 exception_bitmap;
453 u32 page_fault_error_code_mask;
454 u32 page_fault_error_code_match;
455 u32 cr3_target_count;
456 u32 vm_exit_controls;
457 u32 vm_exit_msr_store_count;
458 u32 vm_exit_msr_load_count;
459 u32 vm_entry_controls;
460 u32 vm_entry_msr_load_count;
461 u32 vm_entry_intr_info_field;
462 u32 vm_entry_exception_error_code;
463 u32 vm_entry_instruction_len;
464 u32 tpr_threshold;
465 u32 secondary_vm_exec_control;
466 u32 vm_instruction_error;
467 u32 vm_exit_reason;
468 u32 vm_exit_intr_info;
469 u32 vm_exit_intr_error_code;
470 u32 idt_vectoring_info_field;
471 u32 idt_vectoring_error_code;
472 u32 vm_exit_instruction_len;
473 u32 vmx_instruction_info;
474 u32 guest_es_limit;
475 u32 guest_cs_limit;
476 u32 guest_ss_limit;
477 u32 guest_ds_limit;
478 u32 guest_fs_limit;
479 u32 guest_gs_limit;
480 u32 guest_ldtr_limit;
481 u32 guest_tr_limit;
482 u32 guest_gdtr_limit;
483 u32 guest_idtr_limit;
484 u32 guest_es_ar_bytes;
485 u32 guest_cs_ar_bytes;
486 u32 guest_ss_ar_bytes;
487 u32 guest_ds_ar_bytes;
488 u32 guest_fs_ar_bytes;
489 u32 guest_gs_ar_bytes;
490 u32 guest_ldtr_ar_bytes;
491 u32 guest_tr_ar_bytes;
492 u32 guest_interruptibility_info;
493 u32 guest_activity_state;
494 u32 guest_sysenter_cs;
495 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100496 u32 vmx_preemption_timer_value;
497 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300498 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800499 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300500 u16 guest_es_selector;
501 u16 guest_cs_selector;
502 u16 guest_ss_selector;
503 u16 guest_ds_selector;
504 u16 guest_fs_selector;
505 u16 guest_gs_selector;
506 u16 guest_ldtr_selector;
507 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800508 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300509 u16 host_es_selector;
510 u16 host_cs_selector;
511 u16 host_ss_selector;
512 u16 host_ds_selector;
513 u16 host_fs_selector;
514 u16 host_gs_selector;
515 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700516 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300517};
518
519/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700520 * For save/restore compatibility, the vmcs12 field offsets must not change.
521 */
522#define CHECK_OFFSET(field, loc) \
523 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
524 "Offset of " #field " in struct vmcs12 has changed.")
525
526static inline void vmx_check_vmcs12_offsets(void) {
527 CHECK_OFFSET(revision_id, 0);
528 CHECK_OFFSET(abort, 4);
529 CHECK_OFFSET(launch_state, 8);
530 CHECK_OFFSET(io_bitmap_a, 40);
531 CHECK_OFFSET(io_bitmap_b, 48);
532 CHECK_OFFSET(msr_bitmap, 56);
533 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
534 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
535 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
536 CHECK_OFFSET(tsc_offset, 88);
537 CHECK_OFFSET(virtual_apic_page_addr, 96);
538 CHECK_OFFSET(apic_access_addr, 104);
539 CHECK_OFFSET(posted_intr_desc_addr, 112);
540 CHECK_OFFSET(ept_pointer, 120);
541 CHECK_OFFSET(eoi_exit_bitmap0, 128);
542 CHECK_OFFSET(eoi_exit_bitmap1, 136);
543 CHECK_OFFSET(eoi_exit_bitmap2, 144);
544 CHECK_OFFSET(eoi_exit_bitmap3, 152);
545 CHECK_OFFSET(xss_exit_bitmap, 160);
546 CHECK_OFFSET(guest_physical_address, 168);
547 CHECK_OFFSET(vmcs_link_pointer, 176);
548 CHECK_OFFSET(guest_ia32_debugctl, 184);
549 CHECK_OFFSET(guest_ia32_pat, 192);
550 CHECK_OFFSET(guest_ia32_efer, 200);
551 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
552 CHECK_OFFSET(guest_pdptr0, 216);
553 CHECK_OFFSET(guest_pdptr1, 224);
554 CHECK_OFFSET(guest_pdptr2, 232);
555 CHECK_OFFSET(guest_pdptr3, 240);
556 CHECK_OFFSET(guest_bndcfgs, 248);
557 CHECK_OFFSET(host_ia32_pat, 256);
558 CHECK_OFFSET(host_ia32_efer, 264);
559 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
560 CHECK_OFFSET(vmread_bitmap, 280);
561 CHECK_OFFSET(vmwrite_bitmap, 288);
562 CHECK_OFFSET(vm_function_control, 296);
563 CHECK_OFFSET(eptp_list_address, 304);
564 CHECK_OFFSET(pml_address, 312);
565 CHECK_OFFSET(cr0_guest_host_mask, 344);
566 CHECK_OFFSET(cr4_guest_host_mask, 352);
567 CHECK_OFFSET(cr0_read_shadow, 360);
568 CHECK_OFFSET(cr4_read_shadow, 368);
569 CHECK_OFFSET(cr3_target_value0, 376);
570 CHECK_OFFSET(cr3_target_value1, 384);
571 CHECK_OFFSET(cr3_target_value2, 392);
572 CHECK_OFFSET(cr3_target_value3, 400);
573 CHECK_OFFSET(exit_qualification, 408);
574 CHECK_OFFSET(guest_linear_address, 416);
575 CHECK_OFFSET(guest_cr0, 424);
576 CHECK_OFFSET(guest_cr3, 432);
577 CHECK_OFFSET(guest_cr4, 440);
578 CHECK_OFFSET(guest_es_base, 448);
579 CHECK_OFFSET(guest_cs_base, 456);
580 CHECK_OFFSET(guest_ss_base, 464);
581 CHECK_OFFSET(guest_ds_base, 472);
582 CHECK_OFFSET(guest_fs_base, 480);
583 CHECK_OFFSET(guest_gs_base, 488);
584 CHECK_OFFSET(guest_ldtr_base, 496);
585 CHECK_OFFSET(guest_tr_base, 504);
586 CHECK_OFFSET(guest_gdtr_base, 512);
587 CHECK_OFFSET(guest_idtr_base, 520);
588 CHECK_OFFSET(guest_dr7, 528);
589 CHECK_OFFSET(guest_rsp, 536);
590 CHECK_OFFSET(guest_rip, 544);
591 CHECK_OFFSET(guest_rflags, 552);
592 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
593 CHECK_OFFSET(guest_sysenter_esp, 568);
594 CHECK_OFFSET(guest_sysenter_eip, 576);
595 CHECK_OFFSET(host_cr0, 584);
596 CHECK_OFFSET(host_cr3, 592);
597 CHECK_OFFSET(host_cr4, 600);
598 CHECK_OFFSET(host_fs_base, 608);
599 CHECK_OFFSET(host_gs_base, 616);
600 CHECK_OFFSET(host_tr_base, 624);
601 CHECK_OFFSET(host_gdtr_base, 632);
602 CHECK_OFFSET(host_idtr_base, 640);
603 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
604 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
605 CHECK_OFFSET(host_rsp, 664);
606 CHECK_OFFSET(host_rip, 672);
607 CHECK_OFFSET(pin_based_vm_exec_control, 744);
608 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
609 CHECK_OFFSET(exception_bitmap, 752);
610 CHECK_OFFSET(page_fault_error_code_mask, 756);
611 CHECK_OFFSET(page_fault_error_code_match, 760);
612 CHECK_OFFSET(cr3_target_count, 764);
613 CHECK_OFFSET(vm_exit_controls, 768);
614 CHECK_OFFSET(vm_exit_msr_store_count, 772);
615 CHECK_OFFSET(vm_exit_msr_load_count, 776);
616 CHECK_OFFSET(vm_entry_controls, 780);
617 CHECK_OFFSET(vm_entry_msr_load_count, 784);
618 CHECK_OFFSET(vm_entry_intr_info_field, 788);
619 CHECK_OFFSET(vm_entry_exception_error_code, 792);
620 CHECK_OFFSET(vm_entry_instruction_len, 796);
621 CHECK_OFFSET(tpr_threshold, 800);
622 CHECK_OFFSET(secondary_vm_exec_control, 804);
623 CHECK_OFFSET(vm_instruction_error, 808);
624 CHECK_OFFSET(vm_exit_reason, 812);
625 CHECK_OFFSET(vm_exit_intr_info, 816);
626 CHECK_OFFSET(vm_exit_intr_error_code, 820);
627 CHECK_OFFSET(idt_vectoring_info_field, 824);
628 CHECK_OFFSET(idt_vectoring_error_code, 828);
629 CHECK_OFFSET(vm_exit_instruction_len, 832);
630 CHECK_OFFSET(vmx_instruction_info, 836);
631 CHECK_OFFSET(guest_es_limit, 840);
632 CHECK_OFFSET(guest_cs_limit, 844);
633 CHECK_OFFSET(guest_ss_limit, 848);
634 CHECK_OFFSET(guest_ds_limit, 852);
635 CHECK_OFFSET(guest_fs_limit, 856);
636 CHECK_OFFSET(guest_gs_limit, 860);
637 CHECK_OFFSET(guest_ldtr_limit, 864);
638 CHECK_OFFSET(guest_tr_limit, 868);
639 CHECK_OFFSET(guest_gdtr_limit, 872);
640 CHECK_OFFSET(guest_idtr_limit, 876);
641 CHECK_OFFSET(guest_es_ar_bytes, 880);
642 CHECK_OFFSET(guest_cs_ar_bytes, 884);
643 CHECK_OFFSET(guest_ss_ar_bytes, 888);
644 CHECK_OFFSET(guest_ds_ar_bytes, 892);
645 CHECK_OFFSET(guest_fs_ar_bytes, 896);
646 CHECK_OFFSET(guest_gs_ar_bytes, 900);
647 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
648 CHECK_OFFSET(guest_tr_ar_bytes, 908);
649 CHECK_OFFSET(guest_interruptibility_info, 912);
650 CHECK_OFFSET(guest_activity_state, 916);
651 CHECK_OFFSET(guest_sysenter_cs, 920);
652 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
653 CHECK_OFFSET(vmx_preemption_timer_value, 928);
654 CHECK_OFFSET(virtual_processor_id, 960);
655 CHECK_OFFSET(posted_intr_nv, 962);
656 CHECK_OFFSET(guest_es_selector, 964);
657 CHECK_OFFSET(guest_cs_selector, 966);
658 CHECK_OFFSET(guest_ss_selector, 968);
659 CHECK_OFFSET(guest_ds_selector, 970);
660 CHECK_OFFSET(guest_fs_selector, 972);
661 CHECK_OFFSET(guest_gs_selector, 974);
662 CHECK_OFFSET(guest_ldtr_selector, 976);
663 CHECK_OFFSET(guest_tr_selector, 978);
664 CHECK_OFFSET(guest_intr_status, 980);
665 CHECK_OFFSET(host_es_selector, 982);
666 CHECK_OFFSET(host_cs_selector, 984);
667 CHECK_OFFSET(host_ss_selector, 986);
668 CHECK_OFFSET(host_ds_selector, 988);
669 CHECK_OFFSET(host_fs_selector, 990);
670 CHECK_OFFSET(host_gs_selector, 992);
671 CHECK_OFFSET(host_tr_selector, 994);
672 CHECK_OFFSET(guest_pml_index, 996);
673}
674
675/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300676 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
677 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
678 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700679 *
680 * IMPORTANT: Changing this value will break save/restore compatibility with
681 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300682 */
683#define VMCS12_REVISION 0x11e57ed0
684
685/*
686 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
687 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
688 * current implementation, 4K are reserved to avoid future complications.
689 */
690#define VMCS12_SIZE 0x1000
691
692/*
Jim Mattson5b157062017-12-22 12:11:12 -0800693 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
694 * supported VMCS12 field encoding.
695 */
696#define VMCS12_MAX_FIELD_INDEX 0x17
697
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100698struct nested_vmx_msrs {
699 /*
700 * We only store the "true" versions of the VMX capability MSRs. We
701 * generate the "non-true" versions by setting the must-be-1 bits
702 * according to the SDM.
703 */
704 u32 procbased_ctls_low;
705 u32 procbased_ctls_high;
706 u32 secondary_ctls_low;
707 u32 secondary_ctls_high;
708 u32 pinbased_ctls_low;
709 u32 pinbased_ctls_high;
710 u32 exit_ctls_low;
711 u32 exit_ctls_high;
712 u32 entry_ctls_low;
713 u32 entry_ctls_high;
714 u32 misc_low;
715 u32 misc_high;
716 u32 ept_caps;
717 u32 vpid_caps;
718 u64 basic;
719 u64 cr0_fixed0;
720 u64 cr0_fixed1;
721 u64 cr4_fixed0;
722 u64 cr4_fixed1;
723 u64 vmcs_enum;
724 u64 vmfunc_controls;
725};
726
Jim Mattson5b157062017-12-22 12:11:12 -0800727/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300728 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
729 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
730 */
731struct nested_vmx {
732 /* Has the level1 guest done vmxon? */
733 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400734 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400735 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300736
737 /* The guest-physical address of the current VMCS L1 keeps for L2 */
738 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700739 /*
740 * Cache of the guest's VMCS, existing outside of guest memory.
741 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700742 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700743 */
744 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300745 /*
746 * Indicates if the shadow vmcs must be updated with the
747 * data hold by vmcs12
748 */
749 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100750 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300751
Jim Mattson8d860bb2018-05-09 16:56:05 -0400752 bool change_vmcs01_virtual_apic_mode;
753
Nadav Har'El644d7112011-05-25 23:12:35 +0300754 /* L2 must run next, and mustn't decide to exit to L1. */
755 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600756
757 struct loaded_vmcs vmcs02;
758
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300759 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600760 * Guest pages referred to in the vmcs02 with host-physical
761 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300762 */
763 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800764 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800765 struct page *pi_desc_page;
766 struct pi_desc *pi_desc;
767 bool pi_pending;
768 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100769
770 struct hrtimer preemption_timer;
771 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200772
773 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
774 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800775
Wanpeng Li5c614b32015-10-13 09:18:36 -0700776 u16 vpid02;
777 u16 last_vpid;
778
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100779 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200780
781 /* SMM related state */
782 struct {
783 /* in VMX operation on SMM entry? */
784 bool vmxon;
785 /* in guest mode on SMM entry? */
786 bool guest_mode;
787 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300788};
789
Yang Zhang01e439b2013-04-11 19:25:12 +0800790#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800791#define POSTED_INTR_SN 1
792
Yang Zhang01e439b2013-04-11 19:25:12 +0800793/* Posted-Interrupt Descriptor */
794struct pi_desc {
795 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800796 union {
797 struct {
798 /* bit 256 - Outstanding Notification */
799 u16 on : 1,
800 /* bit 257 - Suppress Notification */
801 sn : 1,
802 /* bit 271:258 - Reserved */
803 rsvd_1 : 14;
804 /* bit 279:272 - Notification Vector */
805 u8 nv;
806 /* bit 287:280 - Reserved */
807 u8 rsvd_2;
808 /* bit 319:288 - Notification Destination */
809 u32 ndst;
810 };
811 u64 control;
812 };
813 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800814} __aligned(64);
815
Yang Zhanga20ed542013-04-11 19:25:15 +0800816static bool pi_test_and_set_on(struct pi_desc *pi_desc)
817{
818 return test_and_set_bit(POSTED_INTR_ON,
819 (unsigned long *)&pi_desc->control);
820}
821
822static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
823{
824 return test_and_clear_bit(POSTED_INTR_ON,
825 (unsigned long *)&pi_desc->control);
826}
827
828static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
829{
830 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
831}
832
Feng Wuebbfc762015-09-18 22:29:46 +0800833static inline void pi_clear_sn(struct pi_desc *pi_desc)
834{
835 return clear_bit(POSTED_INTR_SN,
836 (unsigned long *)&pi_desc->control);
837}
838
839static inline void pi_set_sn(struct pi_desc *pi_desc)
840{
841 return set_bit(POSTED_INTR_SN,
842 (unsigned long *)&pi_desc->control);
843}
844
Paolo Bonziniad361092016-09-20 16:15:05 +0200845static inline void pi_clear_on(struct pi_desc *pi_desc)
846{
847 clear_bit(POSTED_INTR_ON,
848 (unsigned long *)&pi_desc->control);
849}
850
Feng Wuebbfc762015-09-18 22:29:46 +0800851static inline int pi_test_on(struct pi_desc *pi_desc)
852{
853 return test_bit(POSTED_INTR_ON,
854 (unsigned long *)&pi_desc->control);
855}
856
857static inline int pi_test_sn(struct pi_desc *pi_desc)
858{
859 return test_bit(POSTED_INTR_SN,
860 (unsigned long *)&pi_desc->control);
861}
862
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400863struct vmx_msrs {
864 unsigned int nr;
865 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
866};
867
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400868struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000869 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300870 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300871 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100872 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300873 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200874 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200875 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300876 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400877 int nmsrs;
878 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800879 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400880#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300881 u64 msr_host_kernel_gs_base;
882 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400883#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100884
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100885 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100886 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100887
Gleb Natapov2961e8762013-11-25 15:37:13 +0200888 u32 vm_entry_controls_shadow;
889 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200890 u32 secondary_exec_control;
891
Nadav Har'Eld462b812011-05-24 15:26:10 +0300892 /*
893 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
894 * non-nested (L1) guest, it always points to vmcs01. For a nested
895 * guest (L2), it points to a different VMCS.
896 */
897 struct loaded_vmcs vmcs01;
898 struct loaded_vmcs *loaded_vmcs;
899 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300900 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400901 struct vmx_msrs guest;
902 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300903 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400904 struct {
905 int loaded;
906 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300907#ifdef CONFIG_X86_64
908 u16 ds_sel, es_sel;
909#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200910 int gs_ldt_reload_needed;
911 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000912 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400913 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200914 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300915 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300916 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300917 struct kvm_segment segs[8];
918 } rmode;
919 struct {
920 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300921 struct kvm_save_segment {
922 u16 selector;
923 unsigned long base;
924 u32 limit;
925 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300926 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300927 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800928 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300929 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200930
Andi Kleena0861c02009-06-08 17:37:09 +0800931 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800932
Yang Zhang01e439b2013-04-11 19:25:12 +0800933 /* Posted interrupt descriptor */
934 struct pi_desc pi_desc;
935
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300936 /* Support for a guest hypervisor (nested VMX) */
937 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200938
939 /* Dynamic PLE window. */
940 int ple_window;
941 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800942
943 /* Support for PML */
944#define PML_ENTITY_NUM 512
945 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800946
Yunhong Jiang64672c92016-06-13 14:19:59 -0700947 /* apic deadline value in host tsc */
948 u64 hv_deadline_tsc;
949
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800950 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800951
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800952 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800953
Wanpeng Li74c55932017-11-29 01:31:20 -0800954 unsigned long host_debugctlmsr;
955
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800956 /*
957 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
958 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
959 * in msr_ia32_feature_control_valid_bits.
960 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800961 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800962 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400963};
964
Avi Kivity2fb92db2011-04-27 19:42:18 +0300965enum segment_cache_field {
966 SEG_FIELD_SEL = 0,
967 SEG_FIELD_BASE = 1,
968 SEG_FIELD_LIMIT = 2,
969 SEG_FIELD_AR = 3,
970
971 SEG_FIELD_NR = 4
972};
973
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700974static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
975{
976 return container_of(kvm, struct kvm_vmx, kvm);
977}
978
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
980{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000981 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400982}
983
Feng Wuefc64402015-09-18 22:29:51 +0800984static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
985{
986 return &(to_vmx(vcpu)->pi_desc);
987}
988
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800989#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300990#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800991#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
992#define FIELD64(number, name) \
993 FIELD(number, name), \
994 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300995
Abel Gordon4607c2d2013-04-18 14:35:55 +0300996
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100997static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100998#define SHADOW_FIELD_RO(x) x,
999#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001000};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001001static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001002 ARRAY_SIZE(shadow_read_only_fields);
1003
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001004static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001005#define SHADOW_FIELD_RW(x) x,
1006#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001007};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001008static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001009 ARRAY_SIZE(shadow_read_write_fields);
1010
Mathias Krause772e0312012-08-30 01:30:19 +02001011static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001012 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001013 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001014 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1015 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1016 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1017 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1018 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1019 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1020 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1021 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001022 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001023 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001024 FIELD(HOST_ES_SELECTOR, host_es_selector),
1025 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1026 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1027 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1028 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1029 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1030 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1031 FIELD64(IO_BITMAP_A, io_bitmap_a),
1032 FIELD64(IO_BITMAP_B, io_bitmap_b),
1033 FIELD64(MSR_BITMAP, msr_bitmap),
1034 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1035 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1036 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001037 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001038 FIELD64(TSC_OFFSET, tsc_offset),
1039 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1040 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001041 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001042 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001043 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001044 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1045 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1046 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1047 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001048 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001049 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1050 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001051 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001052 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1053 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1054 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1055 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1056 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1057 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1058 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1059 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1060 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1061 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001062 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001063 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1064 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1065 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1066 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1067 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1068 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1069 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1070 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1071 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1072 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1073 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1074 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1075 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1076 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1077 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1078 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1079 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1080 FIELD(TPR_THRESHOLD, tpr_threshold),
1081 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1082 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1083 FIELD(VM_EXIT_REASON, vm_exit_reason),
1084 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1085 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1086 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1087 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1088 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1089 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1090 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1091 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1092 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1093 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1094 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1095 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1096 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1097 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1098 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1099 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1100 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1101 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1102 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1103 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1104 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1105 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1106 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1107 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1108 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1109 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1110 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1111 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001112 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001113 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1114 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1115 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1116 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1117 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1118 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1119 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1120 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1121 FIELD(EXIT_QUALIFICATION, exit_qualification),
1122 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1123 FIELD(GUEST_CR0, guest_cr0),
1124 FIELD(GUEST_CR3, guest_cr3),
1125 FIELD(GUEST_CR4, guest_cr4),
1126 FIELD(GUEST_ES_BASE, guest_es_base),
1127 FIELD(GUEST_CS_BASE, guest_cs_base),
1128 FIELD(GUEST_SS_BASE, guest_ss_base),
1129 FIELD(GUEST_DS_BASE, guest_ds_base),
1130 FIELD(GUEST_FS_BASE, guest_fs_base),
1131 FIELD(GUEST_GS_BASE, guest_gs_base),
1132 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1133 FIELD(GUEST_TR_BASE, guest_tr_base),
1134 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1135 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1136 FIELD(GUEST_DR7, guest_dr7),
1137 FIELD(GUEST_RSP, guest_rsp),
1138 FIELD(GUEST_RIP, guest_rip),
1139 FIELD(GUEST_RFLAGS, guest_rflags),
1140 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1141 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1142 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1143 FIELD(HOST_CR0, host_cr0),
1144 FIELD(HOST_CR3, host_cr3),
1145 FIELD(HOST_CR4, host_cr4),
1146 FIELD(HOST_FS_BASE, host_fs_base),
1147 FIELD(HOST_GS_BASE, host_gs_base),
1148 FIELD(HOST_TR_BASE, host_tr_base),
1149 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1150 FIELD(HOST_IDTR_BASE, host_idtr_base),
1151 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1152 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1153 FIELD(HOST_RSP, host_rsp),
1154 FIELD(HOST_RIP, host_rip),
1155};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001156
1157static inline short vmcs_field_to_offset(unsigned long field)
1158{
Dan Williams085331d2018-01-31 17:47:03 -08001159 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1160 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001161 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001162
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001163 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001164 return -ENOENT;
1165
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001166 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001167 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001168 return -ENOENT;
1169
Linus Torvalds15303ba2018-02-10 13:16:35 -08001170 index = array_index_nospec(index, size);
1171 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001172 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001173 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001174 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001175}
1176
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001177static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1178{
David Matlack4f2777b2016-07-13 17:16:37 -07001179 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001180}
1181
Peter Feiner995f00a2017-06-30 17:26:32 -07001182static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001183static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001184static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001185static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001186static void vmx_set_segment(struct kvm_vcpu *vcpu,
1187 struct kvm_segment *var, int seg);
1188static void vmx_get_segment(struct kvm_vcpu *vcpu,
1189 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001190static bool guest_state_valid(struct kvm_vcpu *vcpu);
1191static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001192static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001193static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1194static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1195static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1196 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001197static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001198static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1199 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001200
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1202static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001203/*
1204 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1205 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1206 */
1207static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208
Feng Wubf9f6ac2015-09-18 22:29:55 +08001209/*
1210 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1211 * can find which vCPU should be waken up.
1212 */
1213static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1214static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1215
Radim Krčmář23611332016-09-29 22:41:33 +02001216enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001217 VMX_VMREAD_BITMAP,
1218 VMX_VMWRITE_BITMAP,
1219 VMX_BITMAP_NR
1220};
1221
1222static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1223
Radim Krčmář23611332016-09-29 22:41:33 +02001224#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1225#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001226
Avi Kivity110312c2010-12-21 12:54:20 +02001227static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001228static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001229
Sheng Yang2384d2b2008-01-17 15:14:33 +08001230static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1231static DEFINE_SPINLOCK(vmx_vpid_lock);
1232
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001233static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234 int size;
1235 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001236 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001238 u32 pin_based_exec_ctrl;
1239 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001240 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001241 u32 vmexit_ctrl;
1242 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001243 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001244} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245
Hannes Ederefff9e52008-11-28 17:02:06 +01001246static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001247 u32 ept;
1248 u32 vpid;
1249} vmx_capability;
1250
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251#define VMX_SEGMENT_FIELD(seg) \
1252 [VCPU_SREG_##seg] = { \
1253 .selector = GUEST_##seg##_SELECTOR, \
1254 .base = GUEST_##seg##_BASE, \
1255 .limit = GUEST_##seg##_LIMIT, \
1256 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1257 }
1258
Mathias Krause772e0312012-08-30 01:30:19 +02001259static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 unsigned selector;
1261 unsigned base;
1262 unsigned limit;
1263 unsigned ar_bytes;
1264} kvm_vmx_segment_fields[] = {
1265 VMX_SEGMENT_FIELD(CS),
1266 VMX_SEGMENT_FIELD(DS),
1267 VMX_SEGMENT_FIELD(ES),
1268 VMX_SEGMENT_FIELD(FS),
1269 VMX_SEGMENT_FIELD(GS),
1270 VMX_SEGMENT_FIELD(SS),
1271 VMX_SEGMENT_FIELD(TR),
1272 VMX_SEGMENT_FIELD(LDTR),
1273};
1274
Avi Kivity26bb0982009-09-07 11:14:12 +03001275static u64 host_efer;
1276
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001277static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1278
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001279/*
Brian Gerst8c065852010-07-17 09:03:26 -04001280 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001281 * away by decrementing the array size.
1282 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001284#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001285 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001287 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001290DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1291
1292#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1293
1294#define KVM_EVMCS_VERSION 1
1295
1296#if IS_ENABLED(CONFIG_HYPERV)
1297static bool __read_mostly enlightened_vmcs = true;
1298module_param(enlightened_vmcs, bool, 0444);
1299
1300static inline void evmcs_write64(unsigned long field, u64 value)
1301{
1302 u16 clean_field;
1303 int offset = get_evmcs_offset(field, &clean_field);
1304
1305 if (offset < 0)
1306 return;
1307
1308 *(u64 *)((char *)current_evmcs + offset) = value;
1309
1310 current_evmcs->hv_clean_fields &= ~clean_field;
1311}
1312
1313static inline void evmcs_write32(unsigned long field, u32 value)
1314{
1315 u16 clean_field;
1316 int offset = get_evmcs_offset(field, &clean_field);
1317
1318 if (offset < 0)
1319 return;
1320
1321 *(u32 *)((char *)current_evmcs + offset) = value;
1322 current_evmcs->hv_clean_fields &= ~clean_field;
1323}
1324
1325static inline void evmcs_write16(unsigned long field, u16 value)
1326{
1327 u16 clean_field;
1328 int offset = get_evmcs_offset(field, &clean_field);
1329
1330 if (offset < 0)
1331 return;
1332
1333 *(u16 *)((char *)current_evmcs + offset) = value;
1334 current_evmcs->hv_clean_fields &= ~clean_field;
1335}
1336
1337static inline u64 evmcs_read64(unsigned long field)
1338{
1339 int offset = get_evmcs_offset(field, NULL);
1340
1341 if (offset < 0)
1342 return 0;
1343
1344 return *(u64 *)((char *)current_evmcs + offset);
1345}
1346
1347static inline u32 evmcs_read32(unsigned long field)
1348{
1349 int offset = get_evmcs_offset(field, NULL);
1350
1351 if (offset < 0)
1352 return 0;
1353
1354 return *(u32 *)((char *)current_evmcs + offset);
1355}
1356
1357static inline u16 evmcs_read16(unsigned long field)
1358{
1359 int offset = get_evmcs_offset(field, NULL);
1360
1361 if (offset < 0)
1362 return 0;
1363
1364 return *(u16 *)((char *)current_evmcs + offset);
1365}
1366
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001367static inline void evmcs_touch_msr_bitmap(void)
1368{
1369 if (unlikely(!current_evmcs))
1370 return;
1371
1372 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1373 current_evmcs->hv_clean_fields &=
1374 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1375}
1376
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001377static void evmcs_load(u64 phys_addr)
1378{
1379 struct hv_vp_assist_page *vp_ap =
1380 hv_get_vp_assist_page(smp_processor_id());
1381
1382 vp_ap->current_nested_vmcs = phys_addr;
1383 vp_ap->enlighten_vmentry = 1;
1384}
1385
1386static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1387{
1388 /*
1389 * Enlightened VMCSv1 doesn't support these:
1390 *
1391 * POSTED_INTR_NV = 0x00000002,
1392 * GUEST_INTR_STATUS = 0x00000810,
1393 * APIC_ACCESS_ADDR = 0x00002014,
1394 * POSTED_INTR_DESC_ADDR = 0x00002016,
1395 * EOI_EXIT_BITMAP0 = 0x0000201c,
1396 * EOI_EXIT_BITMAP1 = 0x0000201e,
1397 * EOI_EXIT_BITMAP2 = 0x00002020,
1398 * EOI_EXIT_BITMAP3 = 0x00002022,
1399 */
1400 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1401 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1402 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1403 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1404 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1405 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1406 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1407
1408 /*
1409 * GUEST_PML_INDEX = 0x00000812,
1410 * PML_ADDRESS = 0x0000200e,
1411 */
1412 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1413
1414 /* VM_FUNCTION_CONTROL = 0x00002018, */
1415 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1416
1417 /*
1418 * EPTP_LIST_ADDRESS = 0x00002024,
1419 * VMREAD_BITMAP = 0x00002026,
1420 * VMWRITE_BITMAP = 0x00002028,
1421 */
1422 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1423
1424 /*
1425 * TSC_MULTIPLIER = 0x00002032,
1426 */
1427 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1428
1429 /*
1430 * PLE_GAP = 0x00004020,
1431 * PLE_WINDOW = 0x00004022,
1432 */
1433 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1434
1435 /*
1436 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1437 */
1438 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1439
1440 /*
1441 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1442 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1443 */
1444 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1445 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1446
1447 /*
1448 * Currently unsupported in KVM:
1449 * GUEST_IA32_RTIT_CTL = 0x00002814,
1450 */
1451}
1452#else /* !IS_ENABLED(CONFIG_HYPERV) */
1453static inline void evmcs_write64(unsigned long field, u64 value) {}
1454static inline void evmcs_write32(unsigned long field, u32 value) {}
1455static inline void evmcs_write16(unsigned long field, u16 value) {}
1456static inline u64 evmcs_read64(unsigned long field) { return 0; }
1457static inline u32 evmcs_read32(unsigned long field) { return 0; }
1458static inline u16 evmcs_read16(unsigned long field) { return 0; }
1459static inline void evmcs_load(u64 phys_addr) {}
1460static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001461static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001462#endif /* IS_ENABLED(CONFIG_HYPERV) */
1463
Jan Kiszka5bb16012016-02-09 20:14:21 +01001464static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465{
1466 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1467 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001468 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1469}
1470
Jan Kiszka6f054852016-02-09 20:15:18 +01001471static inline bool is_debug(u32 intr_info)
1472{
1473 return is_exception_n(intr_info, DB_VECTOR);
1474}
1475
1476static inline bool is_breakpoint(u32 intr_info)
1477{
1478 return is_exception_n(intr_info, BP_VECTOR);
1479}
1480
Jan Kiszka5bb16012016-02-09 20:14:21 +01001481static inline bool is_page_fault(u32 intr_info)
1482{
1483 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485
Gui Jianfeng31299942010-03-15 17:29:09 +08001486static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001487{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001488 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001489}
1490
Gui Jianfeng31299942010-03-15 17:29:09 +08001491static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001492{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001493 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001494}
1495
Liran Alon9e869482018-03-12 13:12:51 +02001496static inline bool is_gp_fault(u32 intr_info)
1497{
1498 return is_exception_n(intr_info, GP_VECTOR);
1499}
1500
Gui Jianfeng31299942010-03-15 17:29:09 +08001501static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502{
1503 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1504 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1505}
1506
Gui Jianfeng31299942010-03-15 17:29:09 +08001507static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001508{
1509 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1510 INTR_INFO_VALID_MASK)) ==
1511 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1512}
1513
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001514/* Undocumented: icebp/int1 */
1515static inline bool is_icebp(u32 intr_info)
1516{
1517 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1518 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1519}
1520
Gui Jianfeng31299942010-03-15 17:29:09 +08001521static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001522{
Sheng Yang04547152009-04-01 15:52:31 +08001523 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001524}
1525
Gui Jianfeng31299942010-03-15 17:29:09 +08001526static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001527{
Sheng Yang04547152009-04-01 15:52:31 +08001528 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001529}
1530
Paolo Bonzini35754c92015-07-29 12:05:37 +02001531static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001532{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001533 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001534}
1535
Gui Jianfeng31299942010-03-15 17:29:09 +08001536static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001537{
Sheng Yang04547152009-04-01 15:52:31 +08001538 return vmcs_config.cpu_based_exec_ctrl &
1539 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001540}
1541
Avi Kivity774ead32007-12-26 13:57:04 +02001542static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001543{
Sheng Yang04547152009-04-01 15:52:31 +08001544 return vmcs_config.cpu_based_2nd_exec_ctrl &
1545 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1546}
1547
Yang Zhang8d146952013-01-25 10:18:50 +08001548static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1549{
1550 return vmcs_config.cpu_based_2nd_exec_ctrl &
1551 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1552}
1553
Yang Zhang83d4c282013-01-25 10:18:49 +08001554static inline bool cpu_has_vmx_apic_register_virt(void)
1555{
1556 return vmcs_config.cpu_based_2nd_exec_ctrl &
1557 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1558}
1559
Yang Zhangc7c9c562013-01-25 10:18:51 +08001560static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1561{
1562 return vmcs_config.cpu_based_2nd_exec_ctrl &
1563 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1564}
1565
Yunhong Jiang64672c92016-06-13 14:19:59 -07001566/*
1567 * Comment's format: document - errata name - stepping - processor name.
1568 * Refer from
1569 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1570 */
1571static u32 vmx_preemption_cpu_tfms[] = {
1572/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15730x000206E6,
1574/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1575/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1576/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15770x00020652,
1578/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15790x00020655,
1580/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1581/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1582/*
1583 * 320767.pdf - AAP86 - B1 -
1584 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1585 */
15860x000106E5,
1587/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15880x000106A0,
1589/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15900x000106A1,
1591/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15920x000106A4,
1593 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1594 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1595 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15960x000106A5,
1597};
1598
1599static inline bool cpu_has_broken_vmx_preemption_timer(void)
1600{
1601 u32 eax = cpuid_eax(0x00000001), i;
1602
1603 /* Clear the reserved bits */
1604 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001605 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001606 if (eax == vmx_preemption_cpu_tfms[i])
1607 return true;
1608
1609 return false;
1610}
1611
1612static inline bool cpu_has_vmx_preemption_timer(void)
1613{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001614 return vmcs_config.pin_based_exec_ctrl &
1615 PIN_BASED_VMX_PREEMPTION_TIMER;
1616}
1617
Yang Zhang01e439b2013-04-11 19:25:12 +08001618static inline bool cpu_has_vmx_posted_intr(void)
1619{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001620 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1621 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001622}
1623
1624static inline bool cpu_has_vmx_apicv(void)
1625{
1626 return cpu_has_vmx_apic_register_virt() &&
1627 cpu_has_vmx_virtual_intr_delivery() &&
1628 cpu_has_vmx_posted_intr();
1629}
1630
Sheng Yang04547152009-04-01 15:52:31 +08001631static inline bool cpu_has_vmx_flexpriority(void)
1632{
1633 return cpu_has_vmx_tpr_shadow() &&
1634 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001635}
1636
Marcelo Tosattie7997942009-06-11 12:07:40 -03001637static inline bool cpu_has_vmx_ept_execute_only(void)
1638{
Gui Jianfeng31299942010-03-15 17:29:09 +08001639 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001640}
1641
Marcelo Tosattie7997942009-06-11 12:07:40 -03001642static inline bool cpu_has_vmx_ept_2m_page(void)
1643{
Gui Jianfeng31299942010-03-15 17:29:09 +08001644 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001645}
1646
Sheng Yang878403b2010-01-05 19:02:29 +08001647static inline bool cpu_has_vmx_ept_1g_page(void)
1648{
Gui Jianfeng31299942010-03-15 17:29:09 +08001649 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001650}
1651
Sheng Yang4bc9b982010-06-02 14:05:24 +08001652static inline bool cpu_has_vmx_ept_4levels(void)
1653{
1654 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1655}
1656
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001657static inline bool cpu_has_vmx_ept_mt_wb(void)
1658{
1659 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1660}
1661
Yu Zhang855feb62017-08-24 20:27:55 +08001662static inline bool cpu_has_vmx_ept_5levels(void)
1663{
1664 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1665}
1666
Xudong Hao83c3a332012-05-28 19:33:35 +08001667static inline bool cpu_has_vmx_ept_ad_bits(void)
1668{
1669 return vmx_capability.ept & VMX_EPT_AD_BIT;
1670}
1671
Gui Jianfeng31299942010-03-15 17:29:09 +08001672static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001673{
Gui Jianfeng31299942010-03-15 17:29:09 +08001674 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001675}
1676
Gui Jianfeng31299942010-03-15 17:29:09 +08001677static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001678{
Gui Jianfeng31299942010-03-15 17:29:09 +08001679 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001680}
1681
Liran Aloncd9a4912018-05-22 17:16:15 +03001682static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1683{
1684 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1685}
1686
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001687static inline bool cpu_has_vmx_invvpid_single(void)
1688{
1689 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1690}
1691
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001692static inline bool cpu_has_vmx_invvpid_global(void)
1693{
1694 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1695}
1696
Wanpeng Li08d839c2017-03-23 05:30:08 -07001697static inline bool cpu_has_vmx_invvpid(void)
1698{
1699 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1700}
1701
Gui Jianfeng31299942010-03-15 17:29:09 +08001702static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001703{
Sheng Yang04547152009-04-01 15:52:31 +08001704 return vmcs_config.cpu_based_2nd_exec_ctrl &
1705 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001706}
1707
Gui Jianfeng31299942010-03-15 17:29:09 +08001708static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001709{
1710 return vmcs_config.cpu_based_2nd_exec_ctrl &
1711 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1712}
1713
Gui Jianfeng31299942010-03-15 17:29:09 +08001714static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001715{
1716 return vmcs_config.cpu_based_2nd_exec_ctrl &
1717 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1718}
1719
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001720static inline bool cpu_has_vmx_basic_inout(void)
1721{
1722 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1723}
1724
Paolo Bonzini35754c92015-07-29 12:05:37 +02001725static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001726{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001727 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001728}
1729
Gui Jianfeng31299942010-03-15 17:29:09 +08001730static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001731{
Sheng Yang04547152009-04-01 15:52:31 +08001732 return vmcs_config.cpu_based_2nd_exec_ctrl &
1733 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001734}
1735
Gui Jianfeng31299942010-03-15 17:29:09 +08001736static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001737{
1738 return vmcs_config.cpu_based_2nd_exec_ctrl &
1739 SECONDARY_EXEC_RDTSCP;
1740}
1741
Mao, Junjiead756a12012-07-02 01:18:48 +00001742static inline bool cpu_has_vmx_invpcid(void)
1743{
1744 return vmcs_config.cpu_based_2nd_exec_ctrl &
1745 SECONDARY_EXEC_ENABLE_INVPCID;
1746}
1747
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001748static inline bool cpu_has_virtual_nmis(void)
1749{
1750 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1751}
1752
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001753static inline bool cpu_has_vmx_wbinvd_exit(void)
1754{
1755 return vmcs_config.cpu_based_2nd_exec_ctrl &
1756 SECONDARY_EXEC_WBINVD_EXITING;
1757}
1758
Abel Gordonabc4fc52013-04-18 14:35:25 +03001759static inline bool cpu_has_vmx_shadow_vmcs(void)
1760{
1761 u64 vmx_msr;
1762 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1763 /* check if the cpu supports writing r/o exit information fields */
1764 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1765 return false;
1766
1767 return vmcs_config.cpu_based_2nd_exec_ctrl &
1768 SECONDARY_EXEC_SHADOW_VMCS;
1769}
1770
Kai Huang843e4332015-01-28 10:54:28 +08001771static inline bool cpu_has_vmx_pml(void)
1772{
1773 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1774}
1775
Haozhong Zhang64903d62015-10-20 15:39:09 +08001776static inline bool cpu_has_vmx_tsc_scaling(void)
1777{
1778 return vmcs_config.cpu_based_2nd_exec_ctrl &
1779 SECONDARY_EXEC_TSC_SCALING;
1780}
1781
Bandan Das2a499e42017-08-03 15:54:41 -04001782static inline bool cpu_has_vmx_vmfunc(void)
1783{
1784 return vmcs_config.cpu_based_2nd_exec_ctrl &
1785 SECONDARY_EXEC_ENABLE_VMFUNC;
1786}
1787
Sean Christopherson64f7a112018-04-30 10:01:06 -07001788static bool vmx_umip_emulated(void)
1789{
1790 return vmcs_config.cpu_based_2nd_exec_ctrl &
1791 SECONDARY_EXEC_DESC;
1792}
1793
Sheng Yang04547152009-04-01 15:52:31 +08001794static inline bool report_flexpriority(void)
1795{
1796 return flexpriority_enabled;
1797}
1798
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001799static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1800{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001801 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001802}
1803
Jim Mattsonf4160e42018-05-29 09:11:33 -07001804/*
1805 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1806 * to modify any valid field of the VMCS, or are the VM-exit
1807 * information fields read-only?
1808 */
1809static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1810{
1811 return to_vmx(vcpu)->nested.msrs.misc_low &
1812 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1813}
1814
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001815static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1816{
1817 return vmcs12->cpu_based_vm_exec_control & bit;
1818}
1819
1820static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1821{
1822 return (vmcs12->cpu_based_vm_exec_control &
1823 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1824 (vmcs12->secondary_vm_exec_control & bit);
1825}
1826
Jan Kiszkaf4124502014-03-07 20:03:13 +01001827static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1828{
1829 return vmcs12->pin_based_vm_exec_control &
1830 PIN_BASED_VMX_PREEMPTION_TIMER;
1831}
1832
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001833static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1834{
1835 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1836}
1837
1838static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1839{
1840 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1841}
1842
Nadav Har'El155a97a2013-08-05 11:07:16 +03001843static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1844{
1845 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1846}
1847
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001848static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1849{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001850 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001851}
1852
Bandan Dasc5f983f2017-05-05 15:25:14 -04001853static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1854{
1855 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1856}
1857
Wincy Vanf2b93282015-02-03 23:56:03 +08001858static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1859{
1860 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1861}
1862
Wanpeng Li5c614b32015-10-13 09:18:36 -07001863static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1864{
1865 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1866}
1867
Wincy Van82f0dd42015-02-03 23:57:18 +08001868static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1869{
1870 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1871}
1872
Wincy Van608406e2015-02-03 23:57:51 +08001873static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1874{
1875 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1876}
1877
Wincy Van705699a2015-02-03 23:58:17 +08001878static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1879{
1880 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1881}
1882
Bandan Das27c42a12017-08-03 15:54:42 -04001883static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1884{
1885 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1886}
1887
Bandan Das41ab9372017-08-03 15:54:43 -04001888static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1889{
1890 return nested_cpu_has_vmfunc(vmcs12) &&
1891 (vmcs12->vm_function_control &
1892 VMX_VMFUNC_EPTP_SWITCHING);
1893}
1894
Jim Mattsonef85b672016-12-12 11:01:37 -08001895static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001896{
1897 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001898 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001899}
1900
Jan Kiszka533558b2014-01-04 18:47:20 +01001901static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1902 u32 exit_intr_info,
1903 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001904static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1905 struct vmcs12 *vmcs12,
1906 u32 reason, unsigned long qualification);
1907
Rusty Russell8b9cf982007-07-30 16:31:43 +10001908static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001909{
1910 int i;
1911
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001912 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001913 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001914 return i;
1915 return -1;
1916}
1917
Sheng Yang2384d2b2008-01-17 15:14:33 +08001918static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1919{
1920 struct {
1921 u64 vpid : 16;
1922 u64 rsvd : 48;
1923 u64 gva;
1924 } operand = { vpid, 0, gva };
1925
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001926 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001927 /* CF==1 or ZF==1 --> rc = -1 */
1928 "; ja 1f ; ud2 ; 1:"
1929 : : "a"(&operand), "c"(ext) : "cc", "memory");
1930}
1931
Sheng Yang14394422008-04-28 12:24:45 +08001932static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1933{
1934 struct {
1935 u64 eptp, gpa;
1936 } operand = {eptp, gpa};
1937
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001938 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001939 /* CF==1 or ZF==1 --> rc = -1 */
1940 "; ja 1f ; ud2 ; 1:\n"
1941 : : "a" (&operand), "c" (ext) : "cc", "memory");
1942}
1943
Avi Kivity26bb0982009-09-07 11:14:12 +03001944static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001945{
1946 int i;
1947
Rusty Russell8b9cf982007-07-30 16:31:43 +10001948 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001949 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001950 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001951 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001952}
1953
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954static void vmcs_clear(struct vmcs *vmcs)
1955{
1956 u64 phys_addr = __pa(vmcs);
1957 u8 error;
1958
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001959 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001960 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 : "cc", "memory");
1962 if (error)
1963 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1964 vmcs, phys_addr);
1965}
1966
Nadav Har'Eld462b812011-05-24 15:26:10 +03001967static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1968{
1969 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001970 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1971 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001972 loaded_vmcs->cpu = -1;
1973 loaded_vmcs->launched = 0;
1974}
1975
Dongxiao Xu7725b892010-05-11 18:29:38 +08001976static void vmcs_load(struct vmcs *vmcs)
1977{
1978 u64 phys_addr = __pa(vmcs);
1979 u8 error;
1980
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001981 if (static_branch_unlikely(&enable_evmcs))
1982 return evmcs_load(phys_addr);
1983
Dongxiao Xu7725b892010-05-11 18:29:38 +08001984 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001985 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001986 : "cc", "memory");
1987 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001988 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001989 vmcs, phys_addr);
1990}
1991
Dave Young2965faa2015-09-09 15:38:55 -07001992#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001993/*
1994 * This bitmap is used to indicate whether the vmclear
1995 * operation is enabled on all cpus. All disabled by
1996 * default.
1997 */
1998static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1999
2000static inline void crash_enable_local_vmclear(int cpu)
2001{
2002 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2003}
2004
2005static inline void crash_disable_local_vmclear(int cpu)
2006{
2007 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2008}
2009
2010static inline int crash_local_vmclear_enabled(int cpu)
2011{
2012 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2013}
2014
2015static void crash_vmclear_local_loaded_vmcss(void)
2016{
2017 int cpu = raw_smp_processor_id();
2018 struct loaded_vmcs *v;
2019
2020 if (!crash_local_vmclear_enabled(cpu))
2021 return;
2022
2023 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2024 loaded_vmcss_on_cpu_link)
2025 vmcs_clear(v->vmcs);
2026}
2027#else
2028static inline void crash_enable_local_vmclear(int cpu) { }
2029static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002030#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002031
Nadav Har'Eld462b812011-05-24 15:26:10 +03002032static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002034 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002035 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036
Nadav Har'Eld462b812011-05-24 15:26:10 +03002037 if (loaded_vmcs->cpu != cpu)
2038 return; /* vcpu migration can race with cpu offline */
2039 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002041 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002042 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002043
2044 /*
2045 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2046 * is before setting loaded_vmcs->vcpu to -1 which is done in
2047 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2048 * then adds the vmcs into percpu list before it is deleted.
2049 */
2050 smp_wmb();
2051
Nadav Har'Eld462b812011-05-24 15:26:10 +03002052 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002053 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054}
2055
Nadav Har'Eld462b812011-05-24 15:26:10 +03002056static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002057{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002058 int cpu = loaded_vmcs->cpu;
2059
2060 if (cpu != -1)
2061 smp_call_function_single(cpu,
2062 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002063}
2064
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002065static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002066{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002067 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002068 return;
2069
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002070 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002071 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002072}
2073
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002074static inline void vpid_sync_vcpu_global(void)
2075{
2076 if (cpu_has_vmx_invvpid_global())
2077 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2078}
2079
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002080static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002081{
2082 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002083 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002084 else
2085 vpid_sync_vcpu_global();
2086}
2087
Sheng Yang14394422008-04-28 12:24:45 +08002088static inline void ept_sync_global(void)
2089{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002090 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002091}
2092
2093static inline void ept_sync_context(u64 eptp)
2094{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002095 if (cpu_has_vmx_invept_context())
2096 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2097 else
2098 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002099}
2100
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002101static __always_inline void vmcs_check16(unsigned long field)
2102{
2103 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2104 "16-bit accessor invalid for 64-bit field");
2105 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2106 "16-bit accessor invalid for 64-bit high field");
2107 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2108 "16-bit accessor invalid for 32-bit high field");
2109 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2110 "16-bit accessor invalid for natural width field");
2111}
2112
2113static __always_inline void vmcs_check32(unsigned long field)
2114{
2115 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2116 "32-bit accessor invalid for 16-bit field");
2117 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2118 "32-bit accessor invalid for natural width field");
2119}
2120
2121static __always_inline void vmcs_check64(unsigned long field)
2122{
2123 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2124 "64-bit accessor invalid for 16-bit field");
2125 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2126 "64-bit accessor invalid for 64-bit high field");
2127 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2128 "64-bit accessor invalid for 32-bit field");
2129 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2130 "64-bit accessor invalid for natural width field");
2131}
2132
2133static __always_inline void vmcs_checkl(unsigned long field)
2134{
2135 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2136 "Natural width accessor invalid for 16-bit field");
2137 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2138 "Natural width accessor invalid for 64-bit field");
2139 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2140 "Natural width accessor invalid for 64-bit high field");
2141 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2142 "Natural width accessor invalid for 32-bit field");
2143}
2144
2145static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146{
Avi Kivity5e520e62011-05-15 10:13:12 -04002147 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148
Avi Kivity5e520e62011-05-15 10:13:12 -04002149 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2150 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 return value;
2152}
2153
Avi Kivity96304212011-05-15 10:13:13 -04002154static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002156 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002157 if (static_branch_unlikely(&enable_evmcs))
2158 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002159 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160}
2161
Avi Kivity96304212011-05-15 10:13:13 -04002162static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002165 if (static_branch_unlikely(&enable_evmcs))
2166 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002167 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168}
2169
Avi Kivity96304212011-05-15 10:13:13 -04002170static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002172 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002173 if (static_branch_unlikely(&enable_evmcs))
2174 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002175#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002176 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002178 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179#endif
2180}
2181
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182static __always_inline unsigned long vmcs_readl(unsigned long field)
2183{
2184 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002185 if (static_branch_unlikely(&enable_evmcs))
2186 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002187 return __vmcs_readl(field);
2188}
2189
Avi Kivitye52de1b2007-01-05 16:36:56 -08002190static noinline void vmwrite_error(unsigned long field, unsigned long value)
2191{
2192 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2193 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2194 dump_stack();
2195}
2196
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002197static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198{
2199 u8 error;
2200
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002201 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002202 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002203 if (unlikely(error))
2204 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205}
2206
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002207static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002209 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002210 if (static_branch_unlikely(&enable_evmcs))
2211 return evmcs_write16(field, value);
2212
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002213 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002216static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002218 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002219 if (static_branch_unlikely(&enable_evmcs))
2220 return evmcs_write32(field, value);
2221
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002222 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223}
2224
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002225static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002227 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002228 if (static_branch_unlikely(&enable_evmcs))
2229 return evmcs_write64(field, value);
2230
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002231 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002232#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002234 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235#endif
2236}
2237
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002238static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002239{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002240 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002241 if (static_branch_unlikely(&enable_evmcs))
2242 return evmcs_write64(field, value);
2243
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002244 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002245}
2246
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002247static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002248{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2250 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002251 if (static_branch_unlikely(&enable_evmcs))
2252 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2253
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002254 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2255}
2256
2257static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2258{
2259 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2260 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002261 if (static_branch_unlikely(&enable_evmcs))
2262 return evmcs_write32(field, evmcs_read32(field) | mask);
2263
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002264 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002265}
2266
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002267static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2268{
2269 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2270}
2271
Gleb Natapov2961e8762013-11-25 15:37:13 +02002272static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2273{
2274 vmcs_write32(VM_ENTRY_CONTROLS, val);
2275 vmx->vm_entry_controls_shadow = val;
2276}
2277
2278static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2279{
2280 if (vmx->vm_entry_controls_shadow != val)
2281 vm_entry_controls_init(vmx, val);
2282}
2283
2284static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2285{
2286 return vmx->vm_entry_controls_shadow;
2287}
2288
2289
2290static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2291{
2292 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2293}
2294
2295static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2296{
2297 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2298}
2299
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002300static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2301{
2302 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2303}
2304
Gleb Natapov2961e8762013-11-25 15:37:13 +02002305static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2306{
2307 vmcs_write32(VM_EXIT_CONTROLS, val);
2308 vmx->vm_exit_controls_shadow = val;
2309}
2310
2311static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2312{
2313 if (vmx->vm_exit_controls_shadow != val)
2314 vm_exit_controls_init(vmx, val);
2315}
2316
2317static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2318{
2319 return vmx->vm_exit_controls_shadow;
2320}
2321
2322
2323static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2324{
2325 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2326}
2327
2328static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2329{
2330 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2331}
2332
Avi Kivity2fb92db2011-04-27 19:42:18 +03002333static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2334{
2335 vmx->segment_cache.bitmask = 0;
2336}
2337
2338static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2339 unsigned field)
2340{
2341 bool ret;
2342 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2343
2344 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2345 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2346 vmx->segment_cache.bitmask = 0;
2347 }
2348 ret = vmx->segment_cache.bitmask & mask;
2349 vmx->segment_cache.bitmask |= mask;
2350 return ret;
2351}
2352
2353static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2354{
2355 u16 *p = &vmx->segment_cache.seg[seg].selector;
2356
2357 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2358 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2359 return *p;
2360}
2361
2362static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2363{
2364 ulong *p = &vmx->segment_cache.seg[seg].base;
2365
2366 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2367 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2368 return *p;
2369}
2370
2371static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2372{
2373 u32 *p = &vmx->segment_cache.seg[seg].limit;
2374
2375 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2376 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2377 return *p;
2378}
2379
2380static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2381{
2382 u32 *p = &vmx->segment_cache.seg[seg].ar;
2383
2384 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2385 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2386 return *p;
2387}
2388
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002389static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2390{
2391 u32 eb;
2392
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002393 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002394 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002395 /*
2396 * Guest access to VMware backdoor ports could legitimately
2397 * trigger #GP because of TSS I/O permission bitmap.
2398 * We intercept those #GP and allow access to them anyway
2399 * as VMware does.
2400 */
2401 if (enable_vmware_backdoor)
2402 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002403 if ((vcpu->guest_debug &
2404 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2405 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2406 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002407 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002408 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002409 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002410 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002411
2412 /* When we are running a nested L2 guest and L1 specified for it a
2413 * certain exception bitmap, we must trap the same exceptions and pass
2414 * them to L1. When running L2, we will only handle the exceptions
2415 * specified above if L1 did not want them.
2416 */
2417 if (is_guest_mode(vcpu))
2418 eb |= get_vmcs12(vcpu)->exception_bitmap;
2419
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002420 vmcs_write32(EXCEPTION_BITMAP, eb);
2421}
2422
Ashok Raj15d45072018-02-01 22:59:43 +01002423/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002424 * Check if MSR is intercepted for currently loaded MSR bitmap.
2425 */
2426static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2427{
2428 unsigned long *msr_bitmap;
2429 int f = sizeof(unsigned long);
2430
2431 if (!cpu_has_vmx_msr_bitmap())
2432 return true;
2433
2434 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2435
2436 if (msr <= 0x1fff) {
2437 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2438 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2439 msr &= 0x1fff;
2440 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2441 }
2442
2443 return true;
2444}
2445
2446/*
Ashok Raj15d45072018-02-01 22:59:43 +01002447 * Check if MSR is intercepted for L01 MSR bitmap.
2448 */
2449static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2450{
2451 unsigned long *msr_bitmap;
2452 int f = sizeof(unsigned long);
2453
2454 if (!cpu_has_vmx_msr_bitmap())
2455 return true;
2456
2457 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2458
2459 if (msr <= 0x1fff) {
2460 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2461 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2462 msr &= 0x1fff;
2463 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2464 }
2465
2466 return true;
2467}
2468
Gleb Natapov2961e8762013-11-25 15:37:13 +02002469static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2470 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002471{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002472 vm_entry_controls_clearbit(vmx, entry);
2473 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002474}
2475
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002476static int find_msr(struct vmx_msrs *m, unsigned int msr)
2477{
2478 unsigned int i;
2479
2480 for (i = 0; i < m->nr; ++i) {
2481 if (m->val[i].index == msr)
2482 return i;
2483 }
2484 return -ENOENT;
2485}
2486
Avi Kivity61d2ef22010-04-28 16:40:38 +03002487static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2488{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002489 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002490 struct msr_autoload *m = &vmx->msr_autoload;
2491
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002492 switch (msr) {
2493 case MSR_EFER:
2494 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002495 clear_atomic_switch_msr_special(vmx,
2496 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002497 VM_EXIT_LOAD_IA32_EFER);
2498 return;
2499 }
2500 break;
2501 case MSR_CORE_PERF_GLOBAL_CTRL:
2502 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002503 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002504 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2505 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2506 return;
2507 }
2508 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002509 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002510 i = find_msr(&m->guest, msr);
2511 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002512 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002513 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002514 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002515 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002516
2517skip_guest:
2518 i = find_msr(&m->host, msr);
2519 if (i < 0)
2520 return;
2521
2522 --m->host.nr;
2523 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002524 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002525}
2526
Gleb Natapov2961e8762013-11-25 15:37:13 +02002527static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2528 unsigned long entry, unsigned long exit,
2529 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2530 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002531{
2532 vmcs_write64(guest_val_vmcs, guest_val);
2533 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002534 vm_entry_controls_setbit(vmx, entry);
2535 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002536}
2537
Avi Kivity61d2ef22010-04-28 16:40:38 +03002538static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002539 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002540{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002541 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002542 struct msr_autoload *m = &vmx->msr_autoload;
2543
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002544 switch (msr) {
2545 case MSR_EFER:
2546 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002547 add_atomic_switch_msr_special(vmx,
2548 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002549 VM_EXIT_LOAD_IA32_EFER,
2550 GUEST_IA32_EFER,
2551 HOST_IA32_EFER,
2552 guest_val, host_val);
2553 return;
2554 }
2555 break;
2556 case MSR_CORE_PERF_GLOBAL_CTRL:
2557 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002558 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002559 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2560 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2561 GUEST_IA32_PERF_GLOBAL_CTRL,
2562 HOST_IA32_PERF_GLOBAL_CTRL,
2563 guest_val, host_val);
2564 return;
2565 }
2566 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002567 case MSR_IA32_PEBS_ENABLE:
2568 /* PEBS needs a quiescent period after being disabled (to write
2569 * a record). Disabling PEBS through VMX MSR swapping doesn't
2570 * provide that period, so a CPU could write host's record into
2571 * guest's memory.
2572 */
2573 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002574 }
2575
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002576 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002577 if (!entry_only)
2578 j = find_msr(&m->host, msr);
2579
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002580 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002581 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002582 "Can't add msr %x\n", msr);
2583 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002584 }
2585 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002586 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002587 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002588 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002589 m->guest.val[i].index = msr;
2590 m->guest.val[i].value = guest_val;
2591
2592 if (entry_only)
2593 return;
2594
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002595 if (j < 0) {
2596 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002597 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002598 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002599 m->host.val[j].index = msr;
2600 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002601}
2602
Avi Kivity92c0d902009-10-29 11:00:16 +02002603static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002604{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002605 u64 guest_efer = vmx->vcpu.arch.efer;
2606 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002607
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002608 if (!enable_ept) {
2609 /*
2610 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2611 * host CPUID is more efficient than testing guest CPUID
2612 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2613 */
2614 if (boot_cpu_has(X86_FEATURE_SMEP))
2615 guest_efer |= EFER_NX;
2616 else if (!(guest_efer & EFER_NX))
2617 ignore_bits |= EFER_NX;
2618 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002619
Avi Kivity51c6cf62007-08-29 03:48:05 +03002620 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002621 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002622 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002623 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002624#ifdef CONFIG_X86_64
2625 ignore_bits |= EFER_LMA | EFER_LME;
2626 /* SCE is meaningful only in long mode on Intel */
2627 if (guest_efer & EFER_LMA)
2628 ignore_bits &= ~(u64)EFER_SCE;
2629#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002630
2631 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002632
2633 /*
2634 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2635 * On CPUs that support "load IA32_EFER", always switch EFER
2636 * atomically, since it's faster than switching it manually.
2637 */
2638 if (cpu_has_load_ia32_efer ||
2639 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002640 if (!(guest_efer & EFER_LMA))
2641 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002642 if (guest_efer != host_efer)
2643 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002644 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002645 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002646 } else {
2647 guest_efer &= ~ignore_bits;
2648 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002649
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002650 vmx->guest_msrs[efer_offset].data = guest_efer;
2651 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2652
2653 return true;
2654 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002655}
2656
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002657#ifdef CONFIG_X86_32
2658/*
2659 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2660 * VMCS rather than the segment table. KVM uses this helper to figure
2661 * out the current bases to poke them into the VMCS before entry.
2662 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002663static unsigned long segment_base(u16 selector)
2664{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002665 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002666 unsigned long v;
2667
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002668 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002669 return 0;
2670
Thomas Garnier45fc8752017-03-14 10:05:08 -07002671 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002672
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002673 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002674 u16 ldt_selector = kvm_read_ldt();
2675
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002676 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002677 return 0;
2678
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002679 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002680 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002681 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002682 return v;
2683}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002684#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002685
Avi Kivity04d2cc72007-09-10 18:10:54 +03002686static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002687{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002688 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002689#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002690 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002691#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002692 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002693
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002694 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002695 return;
2696
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002697 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002698 /*
2699 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2700 * allow segment selectors with cpl > 0 or ti == 1.
2701 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002702 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002703 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002704
2705#ifdef CONFIG_X86_64
2706 save_fsgs_for_kvm();
2707 vmx->host_state.fs_sel = current->thread.fsindex;
2708 vmx->host_state.gs_sel = current->thread.gsindex;
2709#else
Avi Kivity9581d442010-10-19 16:46:55 +02002710 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002711 savesegment(gs, vmx->host_state.gs_sel);
2712#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002713 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002714 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002715 vmx->host_state.fs_reload_needed = 0;
2716 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002717 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002718 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002719 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002720 if (!(vmx->host_state.gs_sel & 7))
2721 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002722 else {
2723 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002724 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002725 }
2726
2727#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002728 savesegment(ds, vmx->host_state.ds_sel);
2729 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002730
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002731 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002732 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002733
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002734 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002735 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002736 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002737#else
2738 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2739 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2740#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002741 if (boot_cpu_has(X86_FEATURE_MPX))
2742 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002743 for (i = 0; i < vmx->save_nmsrs; ++i)
2744 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002745 vmx->guest_msrs[i].data,
2746 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002747}
2748
Avi Kivitya9b21b62008-06-24 11:48:49 +03002749static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002750{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002751 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002752 return;
2753
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002754 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002755 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002756#ifdef CONFIG_X86_64
2757 if (is_long_mode(&vmx->vcpu))
2758 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2759#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002760 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002761 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002762#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002763 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002764#else
2765 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002766#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002767 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002768 if (vmx->host_state.fs_reload_needed)
2769 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002770#ifdef CONFIG_X86_64
2771 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2772 loadsegment(ds, vmx->host_state.ds_sel);
2773 loadsegment(es, vmx->host_state.es_sel);
2774 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002775#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002776 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002777#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002778 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002779#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002780 if (vmx->host_state.msr_host_bndcfgs)
2781 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002782 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002783}
2784
Avi Kivitya9b21b62008-06-24 11:48:49 +03002785static void vmx_load_host_state(struct vcpu_vmx *vmx)
2786{
2787 preempt_disable();
2788 __vmx_load_host_state(vmx);
2789 preempt_enable();
2790}
2791
Feng Wu28b835d2015-09-18 22:29:54 +08002792static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2793{
2794 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2795 struct pi_desc old, new;
2796 unsigned int dest;
2797
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002798 /*
2799 * In case of hot-plug or hot-unplug, we may have to undo
2800 * vmx_vcpu_pi_put even if there is no assigned device. And we
2801 * always keep PI.NDST up to date for simplicity: it makes the
2802 * code easier, and CPU migration is not a fast path.
2803 */
2804 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002805 return;
2806
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002807 /*
2808 * First handle the simple case where no cmpxchg is necessary; just
2809 * allow posting non-urgent interrupts.
2810 *
2811 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2812 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2813 * expects the VCPU to be on the blocked_vcpu_list that matches
2814 * PI.NDST.
2815 */
2816 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2817 vcpu->cpu == cpu) {
2818 pi_clear_sn(pi_desc);
2819 return;
2820 }
2821
2822 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002823 do {
2824 old.control = new.control = pi_desc->control;
2825
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002826 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002827
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002828 if (x2apic_enabled())
2829 new.ndst = dest;
2830 else
2831 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002832
Feng Wu28b835d2015-09-18 22:29:54 +08002833 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002834 } while (cmpxchg64(&pi_desc->control, old.control,
2835 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002836}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002837
Peter Feinerc95ba922016-08-17 09:36:47 -07002838static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2839{
2840 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2841 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2842}
2843
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844/*
2845 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2846 * vcpu mutex is already taken.
2847 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002848static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002850 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002851 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002853 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002854 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002855 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002856 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002857
2858 /*
2859 * Read loaded_vmcs->cpu should be before fetching
2860 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2861 * See the comments in __loaded_vmcs_clear().
2862 */
2863 smp_rmb();
2864
Nadav Har'Eld462b812011-05-24 15:26:10 +03002865 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2866 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002867 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002868 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002869 }
2870
2871 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2872 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2873 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002874 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002875 }
2876
2877 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002878 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002879 unsigned long sysenter_esp;
2880
2881 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002882
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 /*
2884 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002885 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002887 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002888 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002889 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002891 /*
2892 * VM exits change the host TR limit to 0x67 after a VM
2893 * exit. This is okay, since 0x67 covers everything except
2894 * the IO bitmap and have have code to handle the IO bitmap
2895 * being lost after a VM exit.
2896 */
2897 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2898
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2900 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002901
Nadav Har'Eld462b812011-05-24 15:26:10 +03002902 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 }
Feng Wu28b835d2015-09-18 22:29:54 +08002904
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002905 /* Setup TSC multiplier */
2906 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002907 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2908 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002909
Feng Wu28b835d2015-09-18 22:29:54 +08002910 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002911 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002912 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002913}
2914
2915static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2916{
2917 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2918
2919 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002920 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2921 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002922 return;
2923
2924 /* Set SN when the vCPU is preempted */
2925 if (vcpu->preempted)
2926 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927}
2928
2929static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2930{
Feng Wu28b835d2015-09-18 22:29:54 +08002931 vmx_vcpu_pi_put(vcpu);
2932
Avi Kivitya9b21b62008-06-24 11:48:49 +03002933 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934}
2935
Wanpeng Lif244dee2017-07-20 01:11:54 -07002936static bool emulation_required(struct kvm_vcpu *vcpu)
2937{
2938 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2939}
2940
Avi Kivityedcafe32009-12-30 18:07:40 +02002941static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2942
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002943/*
2944 * Return the cr0 value that a nested guest would read. This is a combination
2945 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2946 * its hypervisor (cr0_read_shadow).
2947 */
2948static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2949{
2950 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2951 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2952}
2953static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2954{
2955 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2956 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2957}
2958
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2960{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002961 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002962
Avi Kivity6de12732011-03-07 12:51:22 +02002963 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2964 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2965 rflags = vmcs_readl(GUEST_RFLAGS);
2966 if (to_vmx(vcpu)->rmode.vm86_active) {
2967 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2968 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2969 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2970 }
2971 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002972 }
Avi Kivity6de12732011-03-07 12:51:22 +02002973 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974}
2975
2976static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2977{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002978 unsigned long old_rflags = vmx_get_rflags(vcpu);
2979
Avi Kivity6de12732011-03-07 12:51:22 +02002980 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2981 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002982 if (to_vmx(vcpu)->rmode.vm86_active) {
2983 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002984 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002985 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002987
2988 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2989 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990}
2991
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002992static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002993{
2994 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2995 int ret = 0;
2996
2997 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002998 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002999 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003000 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003001
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003002 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003003}
3004
3005static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3006{
3007 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3008 u32 interruptibility = interruptibility_old;
3009
3010 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3011
Jan Kiszka48005f62010-02-19 19:38:07 +01003012 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003013 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003014 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003015 interruptibility |= GUEST_INTR_STATE_STI;
3016
3017 if ((interruptibility != interruptibility_old))
3018 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3019}
3020
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3022{
3023 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003025 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003027 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Glauber Costa2809f5d2009-05-12 16:21:05 -04003029 /* skipping an emulated instruction also counts */
3030 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031}
3032
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003033static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3034 unsigned long exit_qual)
3035{
3036 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3037 unsigned int nr = vcpu->arch.exception.nr;
3038 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3039
3040 if (vcpu->arch.exception.has_error_code) {
3041 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3042 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3043 }
3044
3045 if (kvm_exception_is_soft(nr))
3046 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3047 else
3048 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3049
3050 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3051 vmx_get_nmi_mask(vcpu))
3052 intr_info |= INTR_INFO_UNBLOCK_NMI;
3053
3054 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3055}
3056
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003057/*
3058 * KVM wants to inject page-faults which it got to the guest. This function
3059 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003060 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003061static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003062{
3063 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003064 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003065
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003066 if (nr == PF_VECTOR) {
3067 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003068 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003069 return 1;
3070 }
3071 /*
3072 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3073 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3074 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3075 * can be written only when inject_pending_event runs. This should be
3076 * conditional on a new capability---if the capability is disabled,
3077 * kvm_multiple_exception would write the ancillary information to
3078 * CR2 or DR6, for backwards ABI-compatibility.
3079 */
3080 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3081 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003082 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003083 return 1;
3084 }
3085 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003086 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003087 if (nr == DB_VECTOR)
3088 *exit_qual = vcpu->arch.dr6;
3089 else
3090 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003091 return 1;
3092 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003093 }
3094
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003095 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003096}
3097
Wanpeng Licaa057a2018-03-12 04:53:03 -07003098static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3099{
3100 /*
3101 * Ensure that we clear the HLT state in the VMCS. We don't need to
3102 * explicitly skip the instruction because if the HLT state is set,
3103 * then the instruction is already executing and RIP has already been
3104 * advanced.
3105 */
3106 if (kvm_hlt_in_guest(vcpu->kvm) &&
3107 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3108 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3109}
3110
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003111static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003112{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003113 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003114 unsigned nr = vcpu->arch.exception.nr;
3115 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003116 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003117 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003118
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003119 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003120 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003121 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3122 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003123
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003124 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003125 int inc_eip = 0;
3126 if (kvm_exception_is_soft(nr))
3127 inc_eip = vcpu->arch.event_exit_inst_len;
3128 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003129 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003130 return;
3131 }
3132
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003133 WARN_ON_ONCE(vmx->emulation_required);
3134
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003135 if (kvm_exception_is_soft(nr)) {
3136 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3137 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003138 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3139 } else
3140 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3141
3142 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003143
3144 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003145}
3146
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003147static bool vmx_rdtscp_supported(void)
3148{
3149 return cpu_has_vmx_rdtscp();
3150}
3151
Mao, Junjiead756a12012-07-02 01:18:48 +00003152static bool vmx_invpcid_supported(void)
3153{
3154 return cpu_has_vmx_invpcid() && enable_ept;
3155}
3156
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157/*
Eddie Donga75beee2007-05-17 18:55:15 +03003158 * Swap MSR entry in host/guest MSR entry array.
3159 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003160static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003161{
Avi Kivity26bb0982009-09-07 11:14:12 +03003162 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003163
3164 tmp = vmx->guest_msrs[to];
3165 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3166 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003167}
3168
3169/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003170 * Set up the vmcs to automatically save and restore system
3171 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3172 * mode, as fiddling with msrs is very expensive.
3173 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003174static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003175{
Avi Kivity26bb0982009-09-07 11:14:12 +03003176 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003177
Eddie Donga75beee2007-05-17 18:55:15 +03003178 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003179#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003180 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003181 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003182 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003183 move_msr_up(vmx, index, save_nmsrs++);
3184 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003185 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003186 move_msr_up(vmx, index, save_nmsrs++);
3187 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003188 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003189 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003190 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003191 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003192 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003193 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003194 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003195 * if efer.sce is enabled.
3196 */
Brian Gerst8c065852010-07-17 09:03:26 -04003197 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003198 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003199 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003200 }
Eddie Donga75beee2007-05-17 18:55:15 +03003201#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003202 index = __find_msr_index(vmx, MSR_EFER);
3203 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003204 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003205
Avi Kivity26bb0982009-09-07 11:14:12 +03003206 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003207
Yang Zhang8d146952013-01-25 10:18:50 +08003208 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003209 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003210}
3211
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003212static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003214 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003216 if (is_guest_mode(vcpu) &&
3217 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3218 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3219
3220 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221}
3222
3223/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003224 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003226static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003228 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003229 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003230 * We're here if L1 chose not to trap WRMSR to TSC. According
3231 * to the spec, this should set L1's TSC; The offset that L1
3232 * set for L2 remains unchanged, and still needs to be added
3233 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003234 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003235 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003236 /* recalculate vmcs02.TSC_OFFSET: */
3237 vmcs12 = get_vmcs12(vcpu);
3238 vmcs_write64(TSC_OFFSET, offset +
3239 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3240 vmcs12->tsc_offset : 0));
3241 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003242 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3243 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003244 vmcs_write64(TSC_OFFSET, offset);
3245 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246}
3247
Nadav Har'El801d3422011-05-25 23:02:23 +03003248/*
3249 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3250 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3251 * all guests if the "nested" module option is off, and can also be disabled
3252 * for a single guest by disabling its VMX cpuid bit.
3253 */
3254static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3255{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003256 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003257}
3258
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003260 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3261 * returned for the various VMX controls MSRs when nested VMX is enabled.
3262 * The same values should also be used to verify that vmcs12 control fields are
3263 * valid during nested entry from L1 to L2.
3264 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3265 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3266 * bit in the high half is on if the corresponding bit in the control field
3267 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003268 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003269static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003270{
Paolo Bonzini13893092018-02-26 13:40:09 +01003271 if (!nested) {
3272 memset(msrs, 0, sizeof(*msrs));
3273 return;
3274 }
3275
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003276 /*
3277 * Note that as a general rule, the high half of the MSRs (bits in
3278 * the control fields which may be 1) should be initialized by the
3279 * intersection of the underlying hardware's MSR (i.e., features which
3280 * can be supported) and the list of features we want to expose -
3281 * because they are known to be properly supported in our code.
3282 * Also, usually, the low half of the MSRs (bits which must be 1) can
3283 * be set to 0, meaning that L1 may turn off any of these bits. The
3284 * reason is that if one of these bits is necessary, it will appear
3285 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3286 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003287 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003288 * These rules have exceptions below.
3289 */
3290
3291 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003292 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003293 msrs->pinbased_ctls_low,
3294 msrs->pinbased_ctls_high);
3295 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003296 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003298 PIN_BASED_EXT_INTR_MASK |
3299 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003300 PIN_BASED_VIRTUAL_NMIS |
3301 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003302 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003303 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003304 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003305
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003306 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003307 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003308 msrs->exit_ctls_low,
3309 msrs->exit_ctls_high);
3310 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003311 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003312
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003314#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003315 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003316#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003317 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003318 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003319 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003320 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003321 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3322
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003323 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003324 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003325
Jan Kiszka2996fca2014-06-16 13:59:43 +02003326 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003327 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003328
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003329 /* entry controls */
3330 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003331 msrs->entry_ctls_low,
3332 msrs->entry_ctls_high);
3333 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003334 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003336#ifdef CONFIG_X86_64
3337 VM_ENTRY_IA32E_MODE |
3338#endif
3339 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003341 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003342 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003343 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003344
Jan Kiszka2996fca2014-06-16 13:59:43 +02003345 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003346 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003347
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003348 /* cpu-based controls */
3349 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003350 msrs->procbased_ctls_low,
3351 msrs->procbased_ctls_high);
3352 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003353 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003354 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003355 CPU_BASED_VIRTUAL_INTR_PENDING |
3356 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003357 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3358 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3359 CPU_BASED_CR3_STORE_EXITING |
3360#ifdef CONFIG_X86_64
3361 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3362#endif
3363 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003364 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3365 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3366 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3367 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003368 /*
3369 * We can allow some features even when not supported by the
3370 * hardware. For example, L1 can specify an MSR bitmap - and we
3371 * can use it to avoid exits to L1 - even when L0 runs L2
3372 * without MSR bitmaps.
3373 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003375 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003376 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003377
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003378 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003379 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003380 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3381
Paolo Bonzini80154d72017-08-24 13:55:35 +02003382 /*
3383 * secondary cpu-based controls. Do not include those that
3384 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3385 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003386 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 msrs->secondary_ctls_low,
3388 msrs->secondary_ctls_high);
3389 msrs->secondary_ctls_low = 0;
3390 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003391 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003392 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003393 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003394 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003395 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003396 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003397
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003398 if (enable_ept) {
3399 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003400 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003401 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003402 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003403 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003404 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003405 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003406 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 msrs->ept_caps &= vmx_capability.ept;
3408 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003409 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3410 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003411 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003412 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003413 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003414 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003415 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003416 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003417
Bandan Das27c42a12017-08-03 15:54:42 -04003418 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003419 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003420 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003421 /*
3422 * Advertise EPTP switching unconditionally
3423 * since we emulate it
3424 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003425 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003426 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003427 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003428 }
3429
Paolo Bonzinief697a72016-03-18 16:58:38 +01003430 /*
3431 * Old versions of KVM use the single-context version without
3432 * checking for support, so declare that it is supported even
3433 * though it is treated as global context. The alternative is
3434 * not failing the single-context invvpid, and it is worse.
3435 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003436 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003437 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003438 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003440 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003441 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003442
Radim Krčmář0790ec12015-03-17 14:02:32 +01003443 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003444 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003445 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3446
Jan Kiszkac18911a2013-03-13 16:06:41 +01003447 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003448 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003449 msrs->misc_low,
3450 msrs->misc_high);
3451 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3452 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003453 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003454 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003455 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003457
3458 /*
3459 * This MSR reports some information about VMX support. We
3460 * should return information about the VMX we emulate for the
3461 * guest, and the VMCS structure we give it - not about the
3462 * VMX support of the underlying hardware.
3463 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003464 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003465 VMCS12_REVISION |
3466 VMX_BASIC_TRUE_CTLS |
3467 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3468 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3469
3470 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003471 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003472
3473 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003474 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003475 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3476 * We picked the standard core2 setting.
3477 */
3478#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3479#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003480 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3481 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003482
3483 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003484 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3485 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003486
3487 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003488 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003489}
3490
David Matlack38991522016-11-29 18:14:08 -08003491/*
3492 * if fixed0[i] == 1: val[i] must be 1
3493 * if fixed1[i] == 0: val[i] must be 0
3494 */
3495static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3496{
3497 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003498}
3499
3500static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3501{
David Matlack38991522016-11-29 18:14:08 -08003502 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003503}
3504
3505static inline u64 vmx_control_msr(u32 low, u32 high)
3506{
3507 return low | ((u64)high << 32);
3508}
3509
David Matlack62cc6b9d2016-11-29 18:14:07 -08003510static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3511{
3512 superset &= mask;
3513 subset &= mask;
3514
3515 return (superset | subset) == superset;
3516}
3517
3518static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3519{
3520 const u64 feature_and_reserved =
3521 /* feature (except bit 48; see below) */
3522 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3523 /* reserved */
3524 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003525 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003526
3527 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3528 return -EINVAL;
3529
3530 /*
3531 * KVM does not emulate a version of VMX that constrains physical
3532 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3533 */
3534 if (data & BIT_ULL(48))
3535 return -EINVAL;
3536
3537 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3538 vmx_basic_vmcs_revision_id(data))
3539 return -EINVAL;
3540
3541 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3542 return -EINVAL;
3543
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003544 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003545 return 0;
3546}
3547
3548static int
3549vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3550{
3551 u64 supported;
3552 u32 *lowp, *highp;
3553
3554 switch (msr_index) {
3555 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3557 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003558 break;
3559 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003560 lowp = &vmx->nested.msrs.procbased_ctls_low;
3561 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003562 break;
3563 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 lowp = &vmx->nested.msrs.exit_ctls_low;
3565 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003566 break;
3567 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003568 lowp = &vmx->nested.msrs.entry_ctls_low;
3569 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003570 break;
3571 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003572 lowp = &vmx->nested.msrs.secondary_ctls_low;
3573 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003574 break;
3575 default:
3576 BUG();
3577 }
3578
3579 supported = vmx_control_msr(*lowp, *highp);
3580
3581 /* Check must-be-1 bits are still 1. */
3582 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3583 return -EINVAL;
3584
3585 /* Check must-be-0 bits are still 0. */
3586 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3587 return -EINVAL;
3588
3589 *lowp = data;
3590 *highp = data >> 32;
3591 return 0;
3592}
3593
3594static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3595{
3596 const u64 feature_and_reserved_bits =
3597 /* feature */
3598 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3599 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3600 /* reserved */
3601 GENMASK_ULL(13, 9) | BIT_ULL(31);
3602 u64 vmx_misc;
3603
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003604 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3605 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003606
3607 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3608 return -EINVAL;
3609
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003611 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3612 vmx_misc_preemption_timer_rate(data) !=
3613 vmx_misc_preemption_timer_rate(vmx_misc))
3614 return -EINVAL;
3615
3616 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3617 return -EINVAL;
3618
3619 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3620 return -EINVAL;
3621
3622 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3623 return -EINVAL;
3624
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003625 vmx->nested.msrs.misc_low = data;
3626 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003627
3628 /*
3629 * If L1 has read-only VM-exit information fields, use the
3630 * less permissive vmx_vmwrite_bitmap to specify write
3631 * permissions for the shadow VMCS.
3632 */
3633 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3634 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3635
David Matlack62cc6b9d2016-11-29 18:14:07 -08003636 return 0;
3637}
3638
3639static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3640{
3641 u64 vmx_ept_vpid_cap;
3642
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003643 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3644 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003645
3646 /* Every bit is either reserved or a feature bit. */
3647 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3648 return -EINVAL;
3649
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003650 vmx->nested.msrs.ept_caps = data;
3651 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003652 return 0;
3653}
3654
3655static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3656{
3657 u64 *msr;
3658
3659 switch (msr_index) {
3660 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003662 break;
3663 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003664 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003665 break;
3666 default:
3667 BUG();
3668 }
3669
3670 /*
3671 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3672 * must be 1 in the restored value.
3673 */
3674 if (!is_bitwise_subset(data, *msr, -1ULL))
3675 return -EINVAL;
3676
3677 *msr = data;
3678 return 0;
3679}
3680
3681/*
3682 * Called when userspace is restoring VMX MSRs.
3683 *
3684 * Returns 0 on success, non-0 otherwise.
3685 */
3686static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3687{
3688 struct vcpu_vmx *vmx = to_vmx(vcpu);
3689
Jim Mattsona943ac52018-05-29 09:11:32 -07003690 /*
3691 * Don't allow changes to the VMX capability MSRs while the vCPU
3692 * is in VMX operation.
3693 */
3694 if (vmx->nested.vmxon)
3695 return -EBUSY;
3696
David Matlack62cc6b9d2016-11-29 18:14:07 -08003697 switch (msr_index) {
3698 case MSR_IA32_VMX_BASIC:
3699 return vmx_restore_vmx_basic(vmx, data);
3700 case MSR_IA32_VMX_PINBASED_CTLS:
3701 case MSR_IA32_VMX_PROCBASED_CTLS:
3702 case MSR_IA32_VMX_EXIT_CTLS:
3703 case MSR_IA32_VMX_ENTRY_CTLS:
3704 /*
3705 * The "non-true" VMX capability MSRs are generated from the
3706 * "true" MSRs, so we do not support restoring them directly.
3707 *
3708 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3709 * should restore the "true" MSRs with the must-be-1 bits
3710 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3711 * DEFAULT SETTINGS".
3712 */
3713 return -EINVAL;
3714 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3715 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3716 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3717 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3718 case MSR_IA32_VMX_PROCBASED_CTLS2:
3719 return vmx_restore_control_msr(vmx, msr_index, data);
3720 case MSR_IA32_VMX_MISC:
3721 return vmx_restore_vmx_misc(vmx, data);
3722 case MSR_IA32_VMX_CR0_FIXED0:
3723 case MSR_IA32_VMX_CR4_FIXED0:
3724 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3725 case MSR_IA32_VMX_CR0_FIXED1:
3726 case MSR_IA32_VMX_CR4_FIXED1:
3727 /*
3728 * These MSRs are generated based on the vCPU's CPUID, so we
3729 * do not support restoring them directly.
3730 */
3731 return -EINVAL;
3732 case MSR_IA32_VMX_EPT_VPID_CAP:
3733 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3734 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003736 return 0;
3737 default:
3738 /*
3739 * The rest of the VMX capability MSRs do not support restore.
3740 */
3741 return -EINVAL;
3742 }
3743}
3744
Jan Kiszkacae50132014-01-04 18:47:22 +01003745/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003746static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003747{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003748 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003749 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003750 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003751 break;
3752 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3753 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003754 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003755 msrs->pinbased_ctls_low,
3756 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003757 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3758 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003759 break;
3760 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3761 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003762 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003763 msrs->procbased_ctls_low,
3764 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003765 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3766 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003767 break;
3768 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3769 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003770 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 msrs->exit_ctls_low,
3772 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003773 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3774 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003775 break;
3776 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3777 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003778 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003779 msrs->entry_ctls_low,
3780 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003781 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3782 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003783 break;
3784 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003785 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 msrs->misc_low,
3787 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003788 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003789 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003790 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003791 break;
3792 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003793 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003794 break;
3795 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003796 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003797 break;
3798 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003799 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003800 break;
3801 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003803 break;
3804 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003805 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 msrs->secondary_ctls_low,
3807 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003808 break;
3809 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 *pdata = msrs->ept_caps |
3811 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003813 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003814 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003815 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003816 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003817 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003818 }
3819
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003820 return 0;
3821}
3822
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003823static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3824 uint64_t val)
3825{
3826 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3827
3828 return !(val & ~valid_bits);
3829}
3830
Tom Lendacky801e4592018-02-21 13:39:51 -06003831static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3832{
Paolo Bonzini13893092018-02-26 13:40:09 +01003833 switch (msr->index) {
3834 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3835 if (!nested)
3836 return 1;
3837 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3838 default:
3839 return 1;
3840 }
3841
3842 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003843}
3844
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003845/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846 * Reads an msr value (of 'msr_index') into 'pdata'.
3847 * Returns 0 on success, non-0 otherwise.
3848 * Assumes vcpu_load() was already called.
3849 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003853 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003855 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003856#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003858 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859 break;
3860 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003861 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003863 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003864 vmx_load_host_state(vmx);
3865 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003866 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003867#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003869 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003870 case MSR_IA32_SPEC_CTRL:
3871 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003872 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3873 return 1;
3874
3875 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3876 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003877 case MSR_IA32_ARCH_CAPABILITIES:
3878 if (!msr_info->host_initiated &&
3879 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3880 return 1;
3881 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3882 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003884 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885 break;
3886 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003887 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 break;
3889 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003890 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003892 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003893 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003894 (!msr_info->host_initiated &&
3895 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003896 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003897 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003898 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003899 case MSR_IA32_MCG_EXT_CTL:
3900 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003901 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003902 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003903 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003904 msr_info->data = vcpu->arch.mcg_ext_ctl;
3905 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003906 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003907 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003908 break;
3909 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3910 if (!nested_vmx_allowed(vcpu))
3911 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003912 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3913 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003914 case MSR_IA32_XSS:
3915 if (!vmx_xsaves_supported())
3916 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003917 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003918 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003919 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003920 if (!msr_info->host_initiated &&
3921 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003922 return 1;
3923 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003925 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003926 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003927 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003928 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003930 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931 }
3932
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 return 0;
3934}
3935
Jan Kiszkacae50132014-01-04 18:47:22 +01003936static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3937
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938/*
3939 * Writes msr value into into the appropriate "register".
3940 * Returns 0 on success, non-0 otherwise.
3941 * Assumes vcpu_load() was already called.
3942 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003943static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003945 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003946 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003947 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003948 u32 msr_index = msr_info->index;
3949 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003950
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003952 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003953 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003954 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003955#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003957 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 vmcs_writel(GUEST_FS_BASE, data);
3959 break;
3960 case MSR_GS_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_GS_BASE, data);
3963 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003964 case MSR_KERNEL_GS_BASE:
3965 vmx_load_host_state(vmx);
3966 vmx->msr_guest_kernel_gs_base = data;
3967 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968#endif
3969 case MSR_IA32_SYSENTER_CS:
3970 vmcs_write32(GUEST_SYSENTER_CS, data);
3971 break;
3972 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003973 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003974 break;
3975 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003976 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003978 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003979 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003980 (!msr_info->host_initiated &&
3981 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003982 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003983 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003984 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003986 vmcs_write64(GUEST_BNDCFGS, data);
3987 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003988 case MSR_IA32_SPEC_CTRL:
3989 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003990 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3991 return 1;
3992
3993 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003994 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003995 return 1;
3996
3997 vmx->spec_ctrl = data;
3998
3999 if (!data)
4000 break;
4001
4002 /*
4003 * For non-nested:
4004 * When it's written (to non-zero) for the first time, pass
4005 * it through.
4006 *
4007 * For nested:
4008 * The handling of the MSR bitmap for L2 guests is done in
4009 * nested_vmx_merge_msr_bitmap. We should not touch the
4010 * vmcs02.msr_bitmap here since it gets completely overwritten
4011 * in the merging. We update the vmcs01 here for L1 as well
4012 * since it will end up touching the MSR anyway now.
4013 */
4014 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4015 MSR_IA32_SPEC_CTRL,
4016 MSR_TYPE_RW);
4017 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004018 case MSR_IA32_PRED_CMD:
4019 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004020 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4021 return 1;
4022
4023 if (data & ~PRED_CMD_IBPB)
4024 return 1;
4025
4026 if (!data)
4027 break;
4028
4029 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4030
4031 /*
4032 * For non-nested:
4033 * When it's written (to non-zero) for the first time, pass
4034 * it through.
4035 *
4036 * For nested:
4037 * The handling of the MSR bitmap for L2 guests is done in
4038 * nested_vmx_merge_msr_bitmap. We should not touch the
4039 * vmcs02.msr_bitmap here since it gets completely overwritten
4040 * in the merging.
4041 */
4042 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4043 MSR_TYPE_W);
4044 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004045 case MSR_IA32_ARCH_CAPABILITIES:
4046 if (!msr_info->host_initiated)
4047 return 1;
4048 vmx->arch_capabilities = data;
4049 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004050 case MSR_IA32_CR_PAT:
4051 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004052 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4053 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004054 vmcs_write64(GUEST_IA32_PAT, data);
4055 vcpu->arch.pat = data;
4056 break;
4057 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004058 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004059 break;
Will Auldba904632012-11-29 12:42:50 -08004060 case MSR_IA32_TSC_ADJUST:
4061 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004062 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004063 case MSR_IA32_MCG_EXT_CTL:
4064 if ((!msr_info->host_initiated &&
4065 !(to_vmx(vcpu)->msr_ia32_feature_control &
4066 FEATURE_CONTROL_LMCE)) ||
4067 (data & ~MCG_EXT_CTL_LMCE_EN))
4068 return 1;
4069 vcpu->arch.mcg_ext_ctl = data;
4070 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004071 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004072 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004073 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004074 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4075 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004076 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004077 if (msr_info->host_initiated && data == 0)
4078 vmx_leave_nested(vcpu);
4079 break;
4080 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004081 if (!msr_info->host_initiated)
4082 return 1; /* they are read-only */
4083 if (!nested_vmx_allowed(vcpu))
4084 return 1;
4085 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004086 case MSR_IA32_XSS:
4087 if (!vmx_xsaves_supported())
4088 return 1;
4089 /*
4090 * The only supported bit as of Skylake is bit 8, but
4091 * it is not supported on KVM.
4092 */
4093 if (data != 0)
4094 return 1;
4095 vcpu->arch.ia32_xss = data;
4096 if (vcpu->arch.ia32_xss != host_xss)
4097 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004098 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004099 else
4100 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4101 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004102 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004103 if (!msr_info->host_initiated &&
4104 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004105 return 1;
4106 /* Check reserved bit, higher 32 bits should be zero */
4107 if ((data >> 32) != 0)
4108 return 1;
4109 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004111 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004112 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004113 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004114 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004115 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4116 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004117 ret = kvm_set_shared_msr(msr->index, msr->data,
4118 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004119 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004120 if (ret)
4121 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004122 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004123 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004125 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 }
4127
Eddie Dong2cc51562007-05-21 07:28:09 +03004128 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129}
4130
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004131static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004133 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4134 switch (reg) {
4135 case VCPU_REGS_RSP:
4136 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4137 break;
4138 case VCPU_REGS_RIP:
4139 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4140 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004141 case VCPU_EXREG_PDPTR:
4142 if (enable_ept)
4143 ept_save_pdptrs(vcpu);
4144 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004145 default:
4146 break;
4147 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148}
4149
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150static __init int cpu_has_kvm_support(void)
4151{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004152 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153}
4154
4155static __init int vmx_disabled_by_bios(void)
4156{
4157 u64 msr;
4158
4159 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004160 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004161 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004162 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4163 && tboot_enabled())
4164 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004165 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004166 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004167 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004168 && !tboot_enabled()) {
4169 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004170 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004171 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004172 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004173 /* launched w/o TXT and VMX disabled */
4174 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4175 && !tboot_enabled())
4176 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004177 }
4178
4179 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180}
4181
Dongxiao Xu7725b892010-05-11 18:29:38 +08004182static void kvm_cpu_vmxon(u64 addr)
4183{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004184 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004185 intel_pt_handle_vmx(1);
4186
Dongxiao Xu7725b892010-05-11 18:29:38 +08004187 asm volatile (ASM_VMX_VMXON_RAX
4188 : : "a"(&addr), "m"(addr)
4189 : "memory", "cc");
4190}
4191
Radim Krčmář13a34e02014-08-28 15:13:03 +02004192static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193{
4194 int cpu = raw_smp_processor_id();
4195 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004196 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004198 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004199 return -EBUSY;
4200
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004201 /*
4202 * This can happen if we hot-added a CPU but failed to allocate
4203 * VP assist page for it.
4204 */
4205 if (static_branch_unlikely(&enable_evmcs) &&
4206 !hv_get_vp_assist_page(cpu))
4207 return -EFAULT;
4208
Nadav Har'Eld462b812011-05-24 15:26:10 +03004209 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004210 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4211 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004212
4213 /*
4214 * Now we can enable the vmclear operation in kdump
4215 * since the loaded_vmcss_on_cpu list on this cpu
4216 * has been initialized.
4217 *
4218 * Though the cpu is not in VMX operation now, there
4219 * is no problem to enable the vmclear operation
4220 * for the loaded_vmcss_on_cpu list is empty!
4221 */
4222 crash_enable_local_vmclear(cpu);
4223
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004225
4226 test_bits = FEATURE_CONTROL_LOCKED;
4227 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4228 if (tboot_enabled())
4229 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4230
4231 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004233 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4234 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004235 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004236 if (enable_ept)
4237 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004238
4239 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240}
4241
Nadav Har'Eld462b812011-05-24 15:26:10 +03004242static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004243{
4244 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004245 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004246
Nadav Har'Eld462b812011-05-24 15:26:10 +03004247 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4248 loaded_vmcss_on_cpu_link)
4249 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004250}
4251
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004252
4253/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4254 * tricks.
4255 */
4256static void kvm_cpu_vmxoff(void)
4257{
4258 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004259
4260 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004261 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004262}
4263
Radim Krčmář13a34e02014-08-28 15:13:03 +02004264static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004266 vmclear_local_loaded_vmcss();
4267 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268}
4269
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004270static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004271 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272{
4273 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004274 u32 ctl = ctl_min | ctl_opt;
4275
4276 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4277
4278 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4279 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4280
4281 /* Ensure minimum (required) set of control bits are supported. */
4282 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004283 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004284
4285 *result = ctl;
4286 return 0;
4287}
4288
Avi Kivity110312c2010-12-21 12:54:20 +02004289static __init bool allow_1_setting(u32 msr, u32 ctl)
4290{
4291 u32 vmx_msr_low, vmx_msr_high;
4292
4293 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4294 return vmx_msr_high & ctl;
4295}
4296
Yang, Sheng002c7f72007-07-31 14:23:01 +03004297static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004298{
4299 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004300 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004301 u32 _pin_based_exec_control = 0;
4302 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004303 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004304 u32 _vmexit_control = 0;
4305 u32 _vmentry_control = 0;
4306
Paolo Bonzini13893092018-02-26 13:40:09 +01004307 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304308 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004309#ifdef CONFIG_X86_64
4310 CPU_BASED_CR8_LOAD_EXITING |
4311 CPU_BASED_CR8_STORE_EXITING |
4312#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004313 CPU_BASED_CR3_LOAD_EXITING |
4314 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004315 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004316 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004317 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004318 CPU_BASED_MWAIT_EXITING |
4319 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004320 CPU_BASED_INVLPG_EXITING |
4321 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004322
Sheng Yangf78e0e22007-10-29 09:40:42 +08004323 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004324 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004325 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004326 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4327 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004328 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004329#ifdef CONFIG_X86_64
4330 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4331 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4332 ~CPU_BASED_CR8_STORE_EXITING;
4333#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004334 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004335 min2 = 0;
4336 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004337 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004338 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004339 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004340 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004341 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004342 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004343 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004344 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004345 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004346 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004347 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004348 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004349 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004350 SECONDARY_EXEC_RDSEED_EXITING |
4351 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004352 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004353 SECONDARY_EXEC_TSC_SCALING |
4354 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004355 if (adjust_vmx_controls(min2, opt2,
4356 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004357 &_cpu_based_2nd_exec_control) < 0)
4358 return -EIO;
4359 }
4360#ifndef CONFIG_X86_64
4361 if (!(_cpu_based_2nd_exec_control &
4362 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4363 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4364#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004365
4366 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4367 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004368 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004369 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4370 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004371
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004372 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4373 &vmx_capability.ept, &vmx_capability.vpid);
4374
Sheng Yangd56f5462008-04-25 10:13:16 +08004375 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004376 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4377 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004378 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4379 CPU_BASED_CR3_STORE_EXITING |
4380 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004381 } else if (vmx_capability.ept) {
4382 vmx_capability.ept = 0;
4383 pr_warn_once("EPT CAP should not exist if not support "
4384 "1-setting enable EPT VM-execution control\n");
4385 }
4386 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4387 vmx_capability.vpid) {
4388 vmx_capability.vpid = 0;
4389 pr_warn_once("VPID CAP should not exist if not support "
4390 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004391 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004392
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004393 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004394#ifdef CONFIG_X86_64
4395 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4396#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004397 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004398 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004399 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4400 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004401 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004402
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004403 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4404 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4405 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004406 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4407 &_pin_based_exec_control) < 0)
4408 return -EIO;
4409
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004410 if (cpu_has_broken_vmx_preemption_timer())
4411 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004412 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004413 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004414 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4415
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004416 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004417 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004418 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4419 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004420 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004422 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004423
4424 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4425 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004426 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004427
4428#ifdef CONFIG_X86_64
4429 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4430 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004431 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004432#endif
4433
4434 /* Require Write-Back (WB) memory type for VMCS accesses. */
4435 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004436 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004437
Yang, Sheng002c7f72007-07-31 14:23:01 +03004438 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004439 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004440 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004441
4442 /* KVM supports Enlightened VMCS v1 only */
4443 if (static_branch_unlikely(&enable_evmcs))
4444 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4445 else
4446 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004447
Yang, Sheng002c7f72007-07-31 14:23:01 +03004448 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4449 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004450 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004451 vmcs_conf->vmexit_ctrl = _vmexit_control;
4452 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004453
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004454 if (static_branch_unlikely(&enable_evmcs))
4455 evmcs_sanitize_exec_ctrls(vmcs_conf);
4456
Avi Kivity110312c2010-12-21 12:54:20 +02004457 cpu_has_load_ia32_efer =
4458 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4459 VM_ENTRY_LOAD_IA32_EFER)
4460 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4461 VM_EXIT_LOAD_IA32_EFER);
4462
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004463 cpu_has_load_perf_global_ctrl =
4464 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4465 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4466 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4467 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4468
4469 /*
4470 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004471 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004472 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4473 *
4474 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4475 *
4476 * AAK155 (model 26)
4477 * AAP115 (model 30)
4478 * AAT100 (model 37)
4479 * BC86,AAY89,BD102 (model 44)
4480 * BA97 (model 46)
4481 *
4482 */
4483 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4484 switch (boot_cpu_data.x86_model) {
4485 case 26:
4486 case 30:
4487 case 37:
4488 case 44:
4489 case 46:
4490 cpu_has_load_perf_global_ctrl = false;
4491 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4492 "does not work properly. Using workaround\n");
4493 break;
4494 default:
4495 break;
4496 }
4497 }
4498
Borislav Petkov782511b2016-04-04 22:25:03 +02004499 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004500 rdmsrl(MSR_IA32_XSS, host_xss);
4501
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004502 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004503}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504
4505static struct vmcs *alloc_vmcs_cpu(int cpu)
4506{
4507 int node = cpu_to_node(cpu);
4508 struct page *pages;
4509 struct vmcs *vmcs;
4510
Vlastimil Babka96db8002015-09-08 15:03:50 -07004511 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512 if (!pages)
4513 return NULL;
4514 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004515 memset(vmcs, 0, vmcs_config.size);
4516 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517 return vmcs;
4518}
4519
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520static void free_vmcs(struct vmcs *vmcs)
4521{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004522 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523}
4524
Nadav Har'Eld462b812011-05-24 15:26:10 +03004525/*
4526 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4527 */
4528static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4529{
4530 if (!loaded_vmcs->vmcs)
4531 return;
4532 loaded_vmcs_clear(loaded_vmcs);
4533 free_vmcs(loaded_vmcs->vmcs);
4534 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004535 if (loaded_vmcs->msr_bitmap)
4536 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004537 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004538}
4539
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004540static struct vmcs *alloc_vmcs(void)
4541{
4542 return alloc_vmcs_cpu(raw_smp_processor_id());
4543}
4544
4545static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4546{
4547 loaded_vmcs->vmcs = alloc_vmcs();
4548 if (!loaded_vmcs->vmcs)
4549 return -ENOMEM;
4550
4551 loaded_vmcs->shadow_vmcs = NULL;
4552 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004553
4554 if (cpu_has_vmx_msr_bitmap()) {
4555 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4556 if (!loaded_vmcs->msr_bitmap)
4557 goto out_vmcs;
4558 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004559
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004560 if (IS_ENABLED(CONFIG_HYPERV) &&
4561 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004562 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4563 struct hv_enlightened_vmcs *evmcs =
4564 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4565
4566 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4567 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004568 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004569 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004570
4571out_vmcs:
4572 free_loaded_vmcs(loaded_vmcs);
4573 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004574}
4575
Sam Ravnborg39959582007-06-01 00:47:13 -07004576static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577{
4578 int cpu;
4579
Zachary Amsden3230bb42009-09-29 11:38:37 -10004580 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004582 per_cpu(vmxarea, cpu) = NULL;
4583 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004584}
4585
Jim Mattsond37f4262017-12-22 12:12:16 -08004586enum vmcs_field_width {
4587 VMCS_FIELD_WIDTH_U16 = 0,
4588 VMCS_FIELD_WIDTH_U64 = 1,
4589 VMCS_FIELD_WIDTH_U32 = 2,
4590 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004591};
4592
Jim Mattsond37f4262017-12-22 12:12:16 -08004593static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004594{
4595 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004596 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004597 return (field >> 13) & 0x3 ;
4598}
4599
4600static inline int vmcs_field_readonly(unsigned long field)
4601{
4602 return (((field >> 10) & 0x3) == 1);
4603}
4604
Bandan Dasfe2b2012014-04-21 15:20:14 -04004605static void init_vmcs_shadow_fields(void)
4606{
4607 int i, j;
4608
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004609 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4610 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004611 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004612 (i + 1 == max_shadow_read_only_fields ||
4613 shadow_read_only_fields[i + 1] != field + 1))
4614 pr_err("Missing field from shadow_read_only_field %x\n",
4615 field + 1);
4616
4617 clear_bit(field, vmx_vmread_bitmap);
4618#ifdef CONFIG_X86_64
4619 if (field & 1)
4620 continue;
4621#endif
4622 if (j < i)
4623 shadow_read_only_fields[j] = field;
4624 j++;
4625 }
4626 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004627
4628 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004629 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004630 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004631 (i + 1 == max_shadow_read_write_fields ||
4632 shadow_read_write_fields[i + 1] != field + 1))
4633 pr_err("Missing field from shadow_read_write_field %x\n",
4634 field + 1);
4635
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004636 /*
4637 * PML and the preemption timer can be emulated, but the
4638 * processor cannot vmwrite to fields that don't exist
4639 * on bare metal.
4640 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004641 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004642 case GUEST_PML_INDEX:
4643 if (!cpu_has_vmx_pml())
4644 continue;
4645 break;
4646 case VMX_PREEMPTION_TIMER_VALUE:
4647 if (!cpu_has_vmx_preemption_timer())
4648 continue;
4649 break;
4650 case GUEST_INTR_STATUS:
4651 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004652 continue;
4653 break;
4654 default:
4655 break;
4656 }
4657
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004658 clear_bit(field, vmx_vmwrite_bitmap);
4659 clear_bit(field, vmx_vmread_bitmap);
4660#ifdef CONFIG_X86_64
4661 if (field & 1)
4662 continue;
4663#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004664 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004665 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004666 j++;
4667 }
4668 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004669}
4670
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671static __init int alloc_kvm_area(void)
4672{
4673 int cpu;
4674
Zachary Amsden3230bb42009-09-29 11:38:37 -10004675 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004676 struct vmcs *vmcs;
4677
4678 vmcs = alloc_vmcs_cpu(cpu);
4679 if (!vmcs) {
4680 free_kvm_area();
4681 return -ENOMEM;
4682 }
4683
4684 per_cpu(vmxarea, cpu) = vmcs;
4685 }
4686 return 0;
4687}
4688
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004689static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004690 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004692 if (!emulate_invalid_guest_state) {
4693 /*
4694 * CS and SS RPL should be equal during guest entry according
4695 * to VMX spec, but in reality it is not always so. Since vcpu
4696 * is in the middle of the transition from real mode to
4697 * protected mode it is safe to assume that RPL 0 is a good
4698 * default value.
4699 */
4700 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004701 save->selector &= ~SEGMENT_RPL_MASK;
4702 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004703 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004705 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706}
4707
4708static void enter_pmode(struct kvm_vcpu *vcpu)
4709{
4710 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004711 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712
Gleb Natapovd99e4152012-12-20 16:57:45 +02004713 /*
4714 * Update real mode segment cache. It may be not up-to-date if sement
4715 * register was written while vcpu was in a guest mode.
4716 */
4717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4718 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4723
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004724 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725
Avi Kivity2fb92db2011-04-27 19:42:18 +03004726 vmx_segment_cache_clear(vmx);
4727
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004728 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729
4730 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004731 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4732 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733 vmcs_writel(GUEST_RFLAGS, flags);
4734
Rusty Russell66aee912007-07-17 23:34:16 +10004735 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4736 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737
4738 update_exception_bitmap(vcpu);
4739
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004740 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4741 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4742 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4743 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4744 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4745 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004748static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749{
Mathias Krause772e0312012-08-30 01:30:19 +02004750 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004751 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752
Gleb Natapovd99e4152012-12-20 16:57:45 +02004753 var.dpl = 0x3;
4754 if (seg == VCPU_SREG_CS)
4755 var.type = 0x3;
4756
4757 if (!emulate_invalid_guest_state) {
4758 var.selector = var.base >> 4;
4759 var.base = var.base & 0xffff0;
4760 var.limit = 0xffff;
4761 var.g = 0;
4762 var.db = 0;
4763 var.present = 1;
4764 var.s = 1;
4765 var.l = 0;
4766 var.unusable = 0;
4767 var.type = 0x3;
4768 var.avl = 0;
4769 if (save->base & 0xf)
4770 printk_once(KERN_WARNING "kvm: segment base is not "
4771 "paragraph aligned when entering "
4772 "protected mode (seg=%d)", seg);
4773 }
4774
4775 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004776 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004777 vmcs_write32(sf->limit, var.limit);
4778 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004779}
4780
4781static void enter_rmode(struct kvm_vcpu *vcpu)
4782{
4783 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004784 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004785 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004787 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4788 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4789 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4790 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4791 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004794
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004795 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796
Gleb Natapov776e58e2011-03-13 12:34:27 +02004797 /*
4798 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004799 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004800 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004801 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004802 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4803 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004804
Avi Kivity2fb92db2011-04-27 19:42:18 +03004805 vmx_segment_cache_clear(vmx);
4806
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004807 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004808 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4810
4811 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004812 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004814 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815
4816 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004817 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818 update_exception_bitmap(vcpu);
4819
Gleb Natapovd99e4152012-12-20 16:57:45 +02004820 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4821 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4822 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4823 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4824 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4825 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004826
Eddie Dong8668a3c2007-10-10 14:26:45 +08004827 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828}
4829
Amit Shah401d10d2009-02-20 22:53:37 +05304830static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4831{
4832 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004833 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4834
4835 if (!msr)
4836 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304837
Avi Kivity44ea2b12009-09-06 15:55:37 +03004838 /*
4839 * Force kernel_gs_base reloading before EFER changes, as control
4840 * of this msr depends on is_long_mode().
4841 */
4842 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004843 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304844 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004845 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304846 msr->data = efer;
4847 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004848 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304849
4850 msr->data = efer & ~EFER_LME;
4851 }
4852 setup_msrs(vmx);
4853}
4854
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004855#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856
4857static void enter_lmode(struct kvm_vcpu *vcpu)
4858{
4859 u32 guest_tr_ar;
4860
Avi Kivity2fb92db2011-04-27 19:42:18 +03004861 vmx_segment_cache_clear(to_vmx(vcpu));
4862
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004864 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004865 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4866 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004868 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4869 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 }
Avi Kivityda38f432010-07-06 11:30:49 +03004871 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872}
4873
4874static void exit_lmode(struct kvm_vcpu *vcpu)
4875{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004876 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004877 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878}
4879
4880#endif
4881
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004882static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4883 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004884{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004885 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004886 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4887 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004888 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004889 } else {
4890 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004891 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004892}
4893
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004894static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004895{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004896 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004897}
4898
Avi Kivitye8467fd2009-12-29 18:43:06 +02004899static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4900{
4901 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4902
4903 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4904 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4905}
4906
Avi Kivityaff48ba2010-12-05 18:56:11 +02004907static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4908{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004909 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004910 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4911 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4912}
4913
Anthony Liguori25c4c272007-04-27 09:29:21 +03004914static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004915{
Avi Kivityfc78f512009-12-07 12:16:48 +02004916 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4917
4918 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4919 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004920}
4921
Sheng Yang14394422008-04-28 12:24:45 +08004922static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4923{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004924 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4925
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004926 if (!test_bit(VCPU_EXREG_PDPTR,
4927 (unsigned long *)&vcpu->arch.regs_dirty))
4928 return;
4929
Sheng Yang14394422008-04-28 12:24:45 +08004930 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004931 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4932 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4933 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4934 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004935 }
4936}
4937
Avi Kivity8f5d5492009-05-31 18:41:29 +03004938static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4939{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004940 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4941
Avi Kivity8f5d5492009-05-31 18:41:29 +03004942 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004943 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4944 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4945 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4946 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004947 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004948
4949 __set_bit(VCPU_EXREG_PDPTR,
4950 (unsigned long *)&vcpu->arch.regs_avail);
4951 __set_bit(VCPU_EXREG_PDPTR,
4952 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004953}
4954
David Matlack38991522016-11-29 18:14:08 -08004955static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4956{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004957 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4958 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004959 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4960
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004961 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004962 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4963 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4964 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4965
4966 return fixed_bits_valid(val, fixed0, fixed1);
4967}
4968
4969static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4970{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004971 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4972 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004973
4974 return fixed_bits_valid(val, fixed0, fixed1);
4975}
4976
4977static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4978{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004979 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4980 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004981
4982 return fixed_bits_valid(val, fixed0, fixed1);
4983}
4984
4985/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4986#define nested_guest_cr4_valid nested_cr4_valid
4987#define nested_host_cr4_valid nested_cr4_valid
4988
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004989static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004990
4991static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4992 unsigned long cr0,
4993 struct kvm_vcpu *vcpu)
4994{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004995 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4996 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004997 if (!(cr0 & X86_CR0_PG)) {
4998 /* From paging/starting to nonpaging */
4999 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005000 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005001 (CPU_BASED_CR3_LOAD_EXITING |
5002 CPU_BASED_CR3_STORE_EXITING));
5003 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005004 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005005 } else if (!is_paging(vcpu)) {
5006 /* From nonpaging to paging */
5007 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005008 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005009 ~(CPU_BASED_CR3_LOAD_EXITING |
5010 CPU_BASED_CR3_STORE_EXITING));
5011 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005012 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005013 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005014
5015 if (!(cr0 & X86_CR0_WP))
5016 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005017}
5018
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5020{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005022 unsigned long hw_cr0;
5023
Gleb Natapov50378782013-02-04 16:00:28 +02005024 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005025 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005026 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005027 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005028 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005029
Gleb Natapov218e7632013-01-21 15:36:45 +02005030 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5031 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032
Gleb Natapov218e7632013-01-21 15:36:45 +02005033 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5034 enter_rmode(vcpu);
5035 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005037#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005038 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005039 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005041 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 exit_lmode(vcpu);
5043 }
5044#endif
5045
Sean Christophersonb4d18512018-03-05 12:04:40 -08005046 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005047 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5048
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005050 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005051 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005052
5053 /* depends on vcpu->arch.cr0 to be set to a new value */
5054 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055}
5056
Yu Zhang855feb62017-08-24 20:27:55 +08005057static int get_ept_level(struct kvm_vcpu *vcpu)
5058{
5059 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5060 return 5;
5061 return 4;
5062}
5063
Peter Feiner995f00a2017-06-30 17:26:32 -07005064static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005065{
Yu Zhang855feb62017-08-24 20:27:55 +08005066 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005067
Yu Zhang855feb62017-08-24 20:27:55 +08005068 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005069
Peter Feiner995f00a2017-06-30 17:26:32 -07005070 if (enable_ept_ad_bits &&
5071 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005072 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005073 eptp |= (root_hpa & PAGE_MASK);
5074
5075 return eptp;
5076}
5077
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5079{
Sheng Yang14394422008-04-28 12:24:45 +08005080 unsigned long guest_cr3;
5081 u64 eptp;
5082
5083 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005084 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005085 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005086 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005087 if (enable_unrestricted_guest || is_paging(vcpu) ||
5088 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005089 guest_cr3 = kvm_read_cr3(vcpu);
5090 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005091 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005092 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005093 }
5094
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005095 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005096 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005097}
5098
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005099static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005101 /*
5102 * Pass through host's Machine Check Enable value to hw_cr4, which
5103 * is in force while we are in guest mode. Do not let guests control
5104 * this bit, even if host CR4.MCE == 0.
5105 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005106 unsigned long hw_cr4;
5107
5108 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5109 if (enable_unrestricted_guest)
5110 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5111 else if (to_vmx(vcpu)->rmode.vm86_active)
5112 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5113 else
5114 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005115
Sean Christopherson64f7a112018-04-30 10:01:06 -07005116 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5117 if (cr4 & X86_CR4_UMIP) {
5118 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005119 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005120 hw_cr4 &= ~X86_CR4_UMIP;
5121 } else if (!is_guest_mode(vcpu) ||
5122 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5123 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5124 SECONDARY_EXEC_DESC);
5125 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005126
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005127 if (cr4 & X86_CR4_VMXE) {
5128 /*
5129 * To use VMXON (and later other VMX instructions), a guest
5130 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5131 * So basically the check on whether to allow nested VMX
5132 * is here.
5133 */
5134 if (!nested_vmx_allowed(vcpu))
5135 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005136 }
David Matlack38991522016-11-29 18:14:08 -08005137
5138 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005139 return 1;
5140
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005141 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005142
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005143 if (!enable_unrestricted_guest) {
5144 if (enable_ept) {
5145 if (!is_paging(vcpu)) {
5146 hw_cr4 &= ~X86_CR4_PAE;
5147 hw_cr4 |= X86_CR4_PSE;
5148 } else if (!(cr4 & X86_CR4_PAE)) {
5149 hw_cr4 &= ~X86_CR4_PAE;
5150 }
5151 }
5152
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005153 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005154 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5155 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5156 * to be manually disabled when guest switches to non-paging
5157 * mode.
5158 *
5159 * If !enable_unrestricted_guest, the CPU is always running
5160 * with CR0.PG=1 and CR4 needs to be modified.
5161 * If enable_unrestricted_guest, the CPU automatically
5162 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005163 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005164 if (!is_paging(vcpu))
5165 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5166 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005167
Sheng Yang14394422008-04-28 12:24:45 +08005168 vmcs_writel(CR4_READ_SHADOW, cr4);
5169 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005170 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171}
5172
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173static void vmx_get_segment(struct kvm_vcpu *vcpu,
5174 struct kvm_segment *var, int seg)
5175{
Avi Kivitya9179492011-01-03 14:28:52 +02005176 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177 u32 ar;
5178
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005179 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005180 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005181 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005182 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005183 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005184 var->base = vmx_read_guest_seg_base(vmx, seg);
5185 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5186 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005187 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005188 var->base = vmx_read_guest_seg_base(vmx, seg);
5189 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5190 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5191 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005192 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005193 var->type = ar & 15;
5194 var->s = (ar >> 4) & 1;
5195 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005196 /*
5197 * Some userspaces do not preserve unusable property. Since usable
5198 * segment has to be present according to VMX spec we can use present
5199 * property to amend userspace bug by making unusable segment always
5200 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5201 * segment as unusable.
5202 */
5203 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204 var->avl = (ar >> 12) & 1;
5205 var->l = (ar >> 13) & 1;
5206 var->db = (ar >> 14) & 1;
5207 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208}
5209
Avi Kivitya9179492011-01-03 14:28:52 +02005210static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5211{
Avi Kivitya9179492011-01-03 14:28:52 +02005212 struct kvm_segment s;
5213
5214 if (to_vmx(vcpu)->rmode.vm86_active) {
5215 vmx_get_segment(vcpu, &s, seg);
5216 return s.base;
5217 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005218 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005219}
5220
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005221static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005222{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005223 struct vcpu_vmx *vmx = to_vmx(vcpu);
5224
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005225 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005226 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005227 else {
5228 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005229 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005230 }
Avi Kivity69c73022011-03-07 15:26:44 +02005231}
5232
Avi Kivity653e3102007-05-07 10:55:37 +03005233static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005234{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235 u32 ar;
5236
Avi Kivityf0495f92012-06-07 17:06:10 +03005237 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238 ar = 1 << 16;
5239 else {
5240 ar = var->type & 15;
5241 ar |= (var->s & 1) << 4;
5242 ar |= (var->dpl & 3) << 5;
5243 ar |= (var->present & 1) << 7;
5244 ar |= (var->avl & 1) << 12;
5245 ar |= (var->l & 1) << 13;
5246 ar |= (var->db & 1) << 14;
5247 ar |= (var->g & 1) << 15;
5248 }
Avi Kivity653e3102007-05-07 10:55:37 +03005249
5250 return ar;
5251}
5252
5253static void vmx_set_segment(struct kvm_vcpu *vcpu,
5254 struct kvm_segment *var, int seg)
5255{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005256 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005257 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005258
Avi Kivity2fb92db2011-04-27 19:42:18 +03005259 vmx_segment_cache_clear(vmx);
5260
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005261 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5262 vmx->rmode.segs[seg] = *var;
5263 if (seg == VCPU_SREG_TR)
5264 vmcs_write16(sf->selector, var->selector);
5265 else if (var->s)
5266 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005267 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005268 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005269
Avi Kivity653e3102007-05-07 10:55:37 +03005270 vmcs_writel(sf->base, var->base);
5271 vmcs_write32(sf->limit, var->limit);
5272 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005273
5274 /*
5275 * Fix the "Accessed" bit in AR field of segment registers for older
5276 * qemu binaries.
5277 * IA32 arch specifies that at the time of processor reset the
5278 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005279 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005280 * state vmexit when "unrestricted guest" mode is turned on.
5281 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5282 * tree. Newer qemu binaries with that qemu fix would not need this
5283 * kvm hack.
5284 */
5285 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005286 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005287
Gleb Natapovf924d662012-12-12 19:10:55 +02005288 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005289
5290out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005291 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292}
5293
Avi Kivity6aa8b732006-12-10 02:21:36 -08005294static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5295{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005296 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297
5298 *db = (ar >> 14) & 1;
5299 *l = (ar >> 13) & 1;
5300}
5301
Gleb Natapov89a27f42010-02-16 10:51:48 +02005302static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005303{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005304 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5305 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005306}
5307
Gleb Natapov89a27f42010-02-16 10:51:48 +02005308static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005310 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5311 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312}
5313
Gleb Natapov89a27f42010-02-16 10:51:48 +02005314static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005315{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005316 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5317 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005318}
5319
Gleb Natapov89a27f42010-02-16 10:51:48 +02005320static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005321{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005322 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5323 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005324}
5325
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005326static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5327{
5328 struct kvm_segment var;
5329 u32 ar;
5330
5331 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005332 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005333 if (seg == VCPU_SREG_CS)
5334 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005335 ar = vmx_segment_access_rights(&var);
5336
5337 if (var.base != (var.selector << 4))
5338 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005339 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005340 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005341 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005342 return false;
5343
5344 return true;
5345}
5346
5347static bool code_segment_valid(struct kvm_vcpu *vcpu)
5348{
5349 struct kvm_segment cs;
5350 unsigned int cs_rpl;
5351
5352 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005353 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005354
Avi Kivity1872a3f2009-01-04 23:26:52 +02005355 if (cs.unusable)
5356 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005357 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005358 return false;
5359 if (!cs.s)
5360 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005361 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362 if (cs.dpl > cs_rpl)
5363 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005364 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005365 if (cs.dpl != cs_rpl)
5366 return false;
5367 }
5368 if (!cs.present)
5369 return false;
5370
5371 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5372 return true;
5373}
5374
5375static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5376{
5377 struct kvm_segment ss;
5378 unsigned int ss_rpl;
5379
5380 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005381 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005382
Avi Kivity1872a3f2009-01-04 23:26:52 +02005383 if (ss.unusable)
5384 return true;
5385 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005386 return false;
5387 if (!ss.s)
5388 return false;
5389 if (ss.dpl != ss_rpl) /* DPL != RPL */
5390 return false;
5391 if (!ss.present)
5392 return false;
5393
5394 return true;
5395}
5396
5397static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5398{
5399 struct kvm_segment var;
5400 unsigned int rpl;
5401
5402 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005403 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005404
Avi Kivity1872a3f2009-01-04 23:26:52 +02005405 if (var.unusable)
5406 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005407 if (!var.s)
5408 return false;
5409 if (!var.present)
5410 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005411 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005412 if (var.dpl < rpl) /* DPL < RPL */
5413 return false;
5414 }
5415
5416 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5417 * rights flags
5418 */
5419 return true;
5420}
5421
5422static bool tr_valid(struct kvm_vcpu *vcpu)
5423{
5424 struct kvm_segment tr;
5425
5426 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5427
Avi Kivity1872a3f2009-01-04 23:26:52 +02005428 if (tr.unusable)
5429 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005430 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005431 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005432 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005433 return false;
5434 if (!tr.present)
5435 return false;
5436
5437 return true;
5438}
5439
5440static bool ldtr_valid(struct kvm_vcpu *vcpu)
5441{
5442 struct kvm_segment ldtr;
5443
5444 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5445
Avi Kivity1872a3f2009-01-04 23:26:52 +02005446 if (ldtr.unusable)
5447 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005448 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005449 return false;
5450 if (ldtr.type != 2)
5451 return false;
5452 if (!ldtr.present)
5453 return false;
5454
5455 return true;
5456}
5457
5458static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5459{
5460 struct kvm_segment cs, ss;
5461
5462 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5463 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5464
Nadav Amitb32a9912015-03-29 16:33:04 +03005465 return ((cs.selector & SEGMENT_RPL_MASK) ==
5466 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005467}
5468
5469/*
5470 * Check if guest state is valid. Returns true if valid, false if
5471 * not.
5472 * We assume that registers are always usable
5473 */
5474static bool guest_state_valid(struct kvm_vcpu *vcpu)
5475{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005476 if (enable_unrestricted_guest)
5477 return true;
5478
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005479 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005480 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005481 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5482 return false;
5483 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5484 return false;
5485 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5486 return false;
5487 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5488 return false;
5489 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5490 return false;
5491 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5492 return false;
5493 } else {
5494 /* protected mode guest state checks */
5495 if (!cs_ss_rpl_check(vcpu))
5496 return false;
5497 if (!code_segment_valid(vcpu))
5498 return false;
5499 if (!stack_segment_valid(vcpu))
5500 return false;
5501 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5502 return false;
5503 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5504 return false;
5505 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5506 return false;
5507 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5508 return false;
5509 if (!tr_valid(vcpu))
5510 return false;
5511 if (!ldtr_valid(vcpu))
5512 return false;
5513 }
5514 /* TODO:
5515 * - Add checks on RIP
5516 * - Add checks on RFLAGS
5517 */
5518
5519 return true;
5520}
5521
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005522static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5523{
5524 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5525}
5526
Mike Dayd77c26f2007-10-08 09:02:08 -04005527static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005528{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005529 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005530 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005531 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005533 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005534 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005535 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5536 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005537 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005538 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005539 r = kvm_write_guest_page(kvm, fn++, &data,
5540 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005541 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005542 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005543 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5544 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005545 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005546 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5547 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005548 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005549 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005550 r = kvm_write_guest_page(kvm, fn, &data,
5551 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5552 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005553out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005554 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005555 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556}
5557
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005558static int init_rmode_identity_map(struct kvm *kvm)
5559{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005560 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005561 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005562 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005563 u32 tmp;
5564
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005565 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005566 mutex_lock(&kvm->slots_lock);
5567
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005568 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005569 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005570
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005571 if (!kvm_vmx->ept_identity_map_addr)
5572 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5573 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005574
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005575 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005576 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005577 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005578 goto out2;
5579
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005580 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005581 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5582 if (r < 0)
5583 goto out;
5584 /* Set up identity-mapping pagetable for EPT in real mode */
5585 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5586 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5587 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5588 r = kvm_write_guest_page(kvm, identity_map_pfn,
5589 &tmp, i * sizeof(tmp), sizeof(tmp));
5590 if (r < 0)
5591 goto out;
5592 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005593 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005594
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005595out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005596 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005597
5598out2:
5599 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005600 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005601}
5602
Avi Kivity6aa8b732006-12-10 02:21:36 -08005603static void seg_setup(int seg)
5604{
Mathias Krause772e0312012-08-30 01:30:19 +02005605 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005606 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005607
5608 vmcs_write16(sf->selector, 0);
5609 vmcs_writel(sf->base, 0);
5610 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005611 ar = 0x93;
5612 if (seg == VCPU_SREG_CS)
5613 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005614
5615 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005616}
5617
Sheng Yangf78e0e22007-10-29 09:40:42 +08005618static int alloc_apic_access_page(struct kvm *kvm)
5619{
Xiao Guangrong44841412012-09-07 14:14:20 +08005620 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005621 int r = 0;
5622
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005623 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005624 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005625 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005626 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5627 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005628 if (r)
5629 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005630
Tang Chen73a6d942014-09-11 13:38:00 +08005631 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005632 if (is_error_page(page)) {
5633 r = -EFAULT;
5634 goto out;
5635 }
5636
Tang Chenc24ae0d2014-09-24 15:57:58 +08005637 /*
5638 * Do not pin the page in memory, so that memory hot-unplug
5639 * is able to migrate it.
5640 */
5641 put_page(page);
5642 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005643out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005644 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005645 return r;
5646}
5647
Wanpeng Li991e7a02015-09-16 17:30:05 +08005648static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005649{
5650 int vpid;
5651
Avi Kivity919818a2009-03-23 18:01:29 +02005652 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005653 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005654 spin_lock(&vmx_vpid_lock);
5655 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005656 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005657 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005658 else
5659 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005660 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005661 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005662}
5663
Wanpeng Li991e7a02015-09-16 17:30:05 +08005664static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005665{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005666 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005667 return;
5668 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005669 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005670 spin_unlock(&vmx_vpid_lock);
5671}
5672
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005673static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5674 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005675{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005676 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005677
5678 if (!cpu_has_vmx_msr_bitmap())
5679 return;
5680
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005681 if (static_branch_unlikely(&enable_evmcs))
5682 evmcs_touch_msr_bitmap();
5683
Sheng Yang25c5f222008-03-28 13:18:56 +08005684 /*
5685 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5686 * have the write-low and read-high bitmap offsets the wrong way round.
5687 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5688 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005689 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005690 if (type & MSR_TYPE_R)
5691 /* read-low */
5692 __clear_bit(msr, msr_bitmap + 0x000 / f);
5693
5694 if (type & MSR_TYPE_W)
5695 /* write-low */
5696 __clear_bit(msr, msr_bitmap + 0x800 / f);
5697
Sheng Yang25c5f222008-03-28 13:18:56 +08005698 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5699 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005700 if (type & MSR_TYPE_R)
5701 /* read-high */
5702 __clear_bit(msr, msr_bitmap + 0x400 / f);
5703
5704 if (type & MSR_TYPE_W)
5705 /* write-high */
5706 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5707
5708 }
5709}
5710
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005711static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5712 u32 msr, int type)
5713{
5714 int f = sizeof(unsigned long);
5715
5716 if (!cpu_has_vmx_msr_bitmap())
5717 return;
5718
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005719 if (static_branch_unlikely(&enable_evmcs))
5720 evmcs_touch_msr_bitmap();
5721
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005722 /*
5723 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5724 * have the write-low and read-high bitmap offsets the wrong way round.
5725 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5726 */
5727 if (msr <= 0x1fff) {
5728 if (type & MSR_TYPE_R)
5729 /* read-low */
5730 __set_bit(msr, msr_bitmap + 0x000 / f);
5731
5732 if (type & MSR_TYPE_W)
5733 /* write-low */
5734 __set_bit(msr, msr_bitmap + 0x800 / f);
5735
5736 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5737 msr &= 0x1fff;
5738 if (type & MSR_TYPE_R)
5739 /* read-high */
5740 __set_bit(msr, msr_bitmap + 0x400 / f);
5741
5742 if (type & MSR_TYPE_W)
5743 /* write-high */
5744 __set_bit(msr, msr_bitmap + 0xc00 / f);
5745
5746 }
5747}
5748
5749static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5750 u32 msr, int type, bool value)
5751{
5752 if (value)
5753 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5754 else
5755 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5756}
5757
Wincy Vanf2b93282015-02-03 23:56:03 +08005758/*
5759 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5760 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5761 */
5762static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5763 unsigned long *msr_bitmap_nested,
5764 u32 msr, int type)
5765{
5766 int f = sizeof(unsigned long);
5767
Wincy Vanf2b93282015-02-03 23:56:03 +08005768 /*
5769 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5770 * have the write-low and read-high bitmap offsets the wrong way round.
5771 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5772 */
5773 if (msr <= 0x1fff) {
5774 if (type & MSR_TYPE_R &&
5775 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5776 /* read-low */
5777 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5778
5779 if (type & MSR_TYPE_W &&
5780 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5781 /* write-low */
5782 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5783
5784 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5785 msr &= 0x1fff;
5786 if (type & MSR_TYPE_R &&
5787 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5788 /* read-high */
5789 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5790
5791 if (type & MSR_TYPE_W &&
5792 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5793 /* write-high */
5794 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5795
5796 }
5797}
5798
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005799static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005800{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005801 u8 mode = 0;
5802
5803 if (cpu_has_secondary_exec_ctrls() &&
5804 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5805 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5806 mode |= MSR_BITMAP_MODE_X2APIC;
5807 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5808 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5809 }
5810
5811 if (is_long_mode(vcpu))
5812 mode |= MSR_BITMAP_MODE_LM;
5813
5814 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005815}
5816
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005817#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5818
5819static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5820 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005821{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005822 int msr;
5823
5824 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5825 unsigned word = msr / BITS_PER_LONG;
5826 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5827 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005828 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005829
5830 if (mode & MSR_BITMAP_MODE_X2APIC) {
5831 /*
5832 * TPR reads and writes can be virtualized even if virtual interrupt
5833 * delivery is not in use.
5834 */
5835 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5836 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5837 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5838 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5839 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5840 }
5841 }
5842}
5843
5844static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5845{
5846 struct vcpu_vmx *vmx = to_vmx(vcpu);
5847 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5848 u8 mode = vmx_msr_bitmap_mode(vcpu);
5849 u8 changed = mode ^ vmx->msr_bitmap_mode;
5850
5851 if (!changed)
5852 return;
5853
5854 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5855 !(mode & MSR_BITMAP_MODE_LM));
5856
5857 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5858 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5859
5860 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005861}
5862
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005863static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005864{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005865 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005866}
5867
David Matlackc9f04402017-08-01 14:00:40 -07005868static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5869{
5870 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5871 gfn_t gfn;
5872
5873 /*
5874 * Don't need to mark the APIC access page dirty; it is never
5875 * written to by the CPU during APIC virtualization.
5876 */
5877
5878 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5879 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5880 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5881 }
5882
5883 if (nested_cpu_has_posted_intr(vmcs12)) {
5884 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5885 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5886 }
5887}
5888
5889
David Hildenbrand6342c502017-01-25 11:58:58 +01005890static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005891{
5892 struct vcpu_vmx *vmx = to_vmx(vcpu);
5893 int max_irr;
5894 void *vapic_page;
5895 u16 status;
5896
David Matlackc9f04402017-08-01 14:00:40 -07005897 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5898 return;
Wincy Van705699a2015-02-03 23:58:17 +08005899
David Matlackc9f04402017-08-01 14:00:40 -07005900 vmx->nested.pi_pending = false;
5901 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5902 return;
Wincy Van705699a2015-02-03 23:58:17 +08005903
David Matlackc9f04402017-08-01 14:00:40 -07005904 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5905 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005906 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005907 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5908 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005909 kunmap(vmx->nested.virtual_apic_page);
5910
5911 status = vmcs_read16(GUEST_INTR_STATUS);
5912 if ((u8)max_irr > ((u8)status & 0xff)) {
5913 status &= ~0xff;
5914 status |= (u8)max_irr;
5915 vmcs_write16(GUEST_INTR_STATUS, status);
5916 }
5917 }
David Matlackc9f04402017-08-01 14:00:40 -07005918
5919 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005920}
5921
Wincy Van06a55242017-04-28 13:13:59 +08005922static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5923 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005924{
5925#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005926 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5927
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005928 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005929 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005930 * The vector of interrupt to be delivered to vcpu had
5931 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005932 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005933 * Following cases will be reached in this block, and
5934 * we always send a notification event in all cases as
5935 * explained below.
5936 *
5937 * Case 1: vcpu keeps in non-root mode. Sending a
5938 * notification event posts the interrupt to vcpu.
5939 *
5940 * Case 2: vcpu exits to root mode and is still
5941 * runnable. PIR will be synced to vIRR before the
5942 * next vcpu entry. Sending a notification event in
5943 * this case has no effect, as vcpu is not in root
5944 * mode.
5945 *
5946 * Case 3: vcpu exits to root mode and is blocked.
5947 * vcpu_block() has already synced PIR to vIRR and
5948 * never blocks vcpu if vIRR is not cleared. Therefore,
5949 * a blocked vcpu here does not wait for any requested
5950 * interrupts in PIR, and sending a notification event
5951 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005952 */
Feng Wu28b835d2015-09-18 22:29:54 +08005953
Wincy Van06a55242017-04-28 13:13:59 +08005954 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005955 return true;
5956 }
5957#endif
5958 return false;
5959}
5960
Wincy Van705699a2015-02-03 23:58:17 +08005961static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5962 int vector)
5963{
5964 struct vcpu_vmx *vmx = to_vmx(vcpu);
5965
5966 if (is_guest_mode(vcpu) &&
5967 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005968 /*
5969 * If a posted intr is not recognized by hardware,
5970 * we will accomplish it in the next vmentry.
5971 */
5972 vmx->nested.pi_pending = true;
5973 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005974 /* the PIR and ON have been set by L1. */
5975 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5976 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005977 return 0;
5978 }
5979 return -1;
5980}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005981/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005982 * Send interrupt to vcpu via posted interrupt way.
5983 * 1. If target vcpu is running(non-root mode), send posted interrupt
5984 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5985 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5986 * interrupt from PIR in next vmentry.
5987 */
5988static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5989{
5990 struct vcpu_vmx *vmx = to_vmx(vcpu);
5991 int r;
5992
Wincy Van705699a2015-02-03 23:58:17 +08005993 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5994 if (!r)
5995 return;
5996
Yang Zhanga20ed542013-04-11 19:25:15 +08005997 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5998 return;
5999
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006000 /* If a previous notification has sent the IPI, nothing to do. */
6001 if (pi_test_and_set_on(&vmx->pi_desc))
6002 return;
6003
Wincy Van06a55242017-04-28 13:13:59 +08006004 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006005 kvm_vcpu_kick(vcpu);
6006}
6007
Avi Kivity6aa8b732006-12-10 02:21:36 -08006008/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006009 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6010 * will not change in the lifetime of the guest.
6011 * Note that host-state that does change is set elsewhere. E.g., host-state
6012 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6013 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006014static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006015{
6016 u32 low32, high32;
6017 unsigned long tmpl;
6018 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006019 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006020
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006021 cr0 = read_cr0();
6022 WARN_ON(cr0 & X86_CR0_TS);
6023 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006024
6025 /*
6026 * Save the most likely value for this task's CR3 in the VMCS.
6027 * We can't use __get_current_cr3_fast() because we're not atomic.
6028 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006029 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006030 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006031 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006032
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006033 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006034 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006035 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006036 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006037
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006038 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006039#ifdef CONFIG_X86_64
6040 /*
6041 * Load null selectors, so we can avoid reloading them in
6042 * __vmx_load_host_state(), in case userspace uses the null selectors
6043 * too (the expected case).
6044 */
6045 vmcs_write16(HOST_DS_SELECTOR, 0);
6046 vmcs_write16(HOST_ES_SELECTOR, 0);
6047#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006048 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6049 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006050#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006051 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6052 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6053
Juergen Gross87930012017-09-04 12:25:27 +02006054 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006055 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006056 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006057
Avi Kivity83287ea422012-09-16 15:10:57 +03006058 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006059
6060 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6061 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6062 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6063 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6064
6065 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6066 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6067 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6068 }
6069}
6070
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006071static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6072{
6073 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6074 if (enable_ept)
6075 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006076 if (is_guest_mode(&vmx->vcpu))
6077 vmx->vcpu.arch.cr4_guest_owned_bits &=
6078 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006079 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6080}
6081
Yang Zhang01e439b2013-04-11 19:25:12 +08006082static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6083{
6084 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6085
Andrey Smetanind62caab2015-11-10 15:36:33 +03006086 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006087 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006088
6089 if (!enable_vnmi)
6090 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6091
Yunhong Jiang64672c92016-06-13 14:19:59 -07006092 /* Enable the preemption timer dynamically */
6093 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006094 return pin_based_exec_ctrl;
6095}
6096
Andrey Smetanind62caab2015-11-10 15:36:33 +03006097static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6098{
6099 struct vcpu_vmx *vmx = to_vmx(vcpu);
6100
6101 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006102 if (cpu_has_secondary_exec_ctrls()) {
6103 if (kvm_vcpu_apicv_active(vcpu))
6104 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6105 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6106 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6107 else
6108 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6109 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6110 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6111 }
6112
6113 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006114 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006115}
6116
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006117static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6118{
6119 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006120
6121 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6122 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6123
Paolo Bonzini35754c92015-07-29 12:05:37 +02006124 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006125 exec_control &= ~CPU_BASED_TPR_SHADOW;
6126#ifdef CONFIG_X86_64
6127 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6128 CPU_BASED_CR8_LOAD_EXITING;
6129#endif
6130 }
6131 if (!enable_ept)
6132 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6133 CPU_BASED_CR3_LOAD_EXITING |
6134 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006135 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6136 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6137 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006138 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6139 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006140 return exec_control;
6141}
6142
Jim Mattson45ec3682017-08-23 16:32:04 -07006143static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006144{
Jim Mattson45ec3682017-08-23 16:32:04 -07006145 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006146 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006147}
6148
Jim Mattson75f4fc82017-08-23 16:32:03 -07006149static bool vmx_rdseed_supported(void)
6150{
6151 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006152 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006153}
6154
Paolo Bonzini80154d72017-08-24 13:55:35 +02006155static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006156{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006157 struct kvm_vcpu *vcpu = &vmx->vcpu;
6158
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006159 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006160
Paolo Bonzini80154d72017-08-24 13:55:35 +02006161 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006162 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6163 if (vmx->vpid == 0)
6164 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6165 if (!enable_ept) {
6166 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6167 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006168 /* Enable INVPCID for non-ept guests may cause performance regression. */
6169 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006170 }
6171 if (!enable_unrestricted_guest)
6172 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006173 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006174 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006175 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006176 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6177 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006178 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006179
6180 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6181 * in vmx_set_cr4. */
6182 exec_control &= ~SECONDARY_EXEC_DESC;
6183
Abel Gordonabc4fc52013-04-18 14:35:25 +03006184 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6185 (handle_vmptrld).
6186 We can NOT enable shadow_vmcs here because we don't have yet
6187 a current VMCS12
6188 */
6189 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006190
6191 if (!enable_pml)
6192 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006193
Paolo Bonzini3db13482017-08-24 14:48:03 +02006194 if (vmx_xsaves_supported()) {
6195 /* Exposing XSAVES only when XSAVE is exposed */
6196 bool xsaves_enabled =
6197 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6198 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6199
6200 if (!xsaves_enabled)
6201 exec_control &= ~SECONDARY_EXEC_XSAVES;
6202
6203 if (nested) {
6204 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006205 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006206 SECONDARY_EXEC_XSAVES;
6207 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006208 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006209 ~SECONDARY_EXEC_XSAVES;
6210 }
6211 }
6212
Paolo Bonzini80154d72017-08-24 13:55:35 +02006213 if (vmx_rdtscp_supported()) {
6214 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6215 if (!rdtscp_enabled)
6216 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6217
6218 if (nested) {
6219 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006220 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006221 SECONDARY_EXEC_RDTSCP;
6222 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006223 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006224 ~SECONDARY_EXEC_RDTSCP;
6225 }
6226 }
6227
6228 if (vmx_invpcid_supported()) {
6229 /* Exposing INVPCID only when PCID is exposed */
6230 bool invpcid_enabled =
6231 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6232 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6233
6234 if (!invpcid_enabled) {
6235 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6236 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6237 }
6238
6239 if (nested) {
6240 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006241 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006242 SECONDARY_EXEC_ENABLE_INVPCID;
6243 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006244 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006245 ~SECONDARY_EXEC_ENABLE_INVPCID;
6246 }
6247 }
6248
Jim Mattson45ec3682017-08-23 16:32:04 -07006249 if (vmx_rdrand_supported()) {
6250 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6251 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006252 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006253
6254 if (nested) {
6255 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006256 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006257 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006258 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006259 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006260 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006261 }
6262 }
6263
Jim Mattson75f4fc82017-08-23 16:32:03 -07006264 if (vmx_rdseed_supported()) {
6265 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6266 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006267 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006268
6269 if (nested) {
6270 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006271 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006272 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006273 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006274 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006275 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006276 }
6277 }
6278
Paolo Bonzini80154d72017-08-24 13:55:35 +02006279 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006280}
6281
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006282static void ept_set_mmio_spte_mask(void)
6283{
6284 /*
6285 * EPT Misconfigurations can be generated if the value of bits 2:0
6286 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006287 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006288 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6289 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006290}
6291
Wanpeng Lif53cd632014-12-02 19:14:58 +08006292#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006293/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006294 * Sets up the vmcs for emulated real mode.
6295 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006296static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006298#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006300#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006301 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006302
Abel Gordon4607c2d2013-04-18 14:35:55 +03006303 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006304 /*
6305 * At vCPU creation, "VMWRITE to any supported field
6306 * in the VMCS" is supported, so use the more
6307 * permissive vmx_vmread_bitmap to specify both read
6308 * and write permissions for the shadow VMCS.
6309 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006310 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006311 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006312 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006313 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006314 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006315
Avi Kivity6aa8b732006-12-10 02:21:36 -08006316 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6317
Avi Kivity6aa8b732006-12-10 02:21:36 -08006318 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006319 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006320 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006321
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006322 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323
Dan Williamsdfa169b2016-06-02 11:17:24 -07006324 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006325 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006326 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006327 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006328 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006329
Andrey Smetanind62caab2015-11-10 15:36:33 +03006330 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006331 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6332 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6333 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6334 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6335
6336 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006337
Li RongQing0bcf2612015-12-03 13:29:34 +08006338 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006339 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006340 }
6341
Wanpeng Lib31c1142018-03-12 04:53:04 -07006342 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006343 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006344 vmx->ple_window = ple_window;
6345 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006346 }
6347
Xiao Guangrongc3707952011-07-12 03:28:04 +08006348 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6349 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006350 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6351
Avi Kivity9581d442010-10-19 16:46:55 +02006352 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6353 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006354 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006355#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006356 rdmsrl(MSR_FS_BASE, a);
6357 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6358 rdmsrl(MSR_GS_BASE, a);
6359 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6360#else
6361 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6362 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6363#endif
6364
Bandan Das2a499e42017-08-03 15:54:41 -04006365 if (cpu_has_vmx_vmfunc())
6366 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6367
Eddie Dong2cc51562007-05-21 07:28:09 +03006368 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6369 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006370 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006371 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006372 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373
Radim Krčmář74545702015-04-27 15:11:25 +02006374 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6375 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006376
Paolo Bonzini03916db2014-07-24 14:21:57 +02006377 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006378 u32 index = vmx_msr_index[i];
6379 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006380 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006381
6382 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6383 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006384 if (wrmsr_safe(index, data_low, data_high) < 0)
6385 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006386 vmx->guest_msrs[j].index = i;
6387 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006388 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006389 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006390 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006391
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006392 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6393 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006394
6395 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396
6397 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006398 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006399
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006400 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6401 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6402
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006403 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006404
Wanpeng Lif53cd632014-12-02 19:14:58 +08006405 if (vmx_xsaves_supported())
6406 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6407
Peter Feiner4e595162016-07-07 14:49:58 -07006408 if (enable_pml) {
6409 ASSERT(vmx->pml_pg);
6410 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6411 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6412 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006413}
6414
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006415static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006416{
6417 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006418 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006419 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006420
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006421 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006422 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006423
Wanpeng Li518e7b92018-02-28 14:03:31 +08006424 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006425 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006426 kvm_set_cr8(vcpu, 0);
6427
6428 if (!init_event) {
6429 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6430 MSR_IA32_APICBASE_ENABLE;
6431 if (kvm_vcpu_is_reset_bsp(vcpu))
6432 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6433 apic_base_msr.host_initiated = true;
6434 kvm_set_apic_base(vcpu, &apic_base_msr);
6435 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006436
Avi Kivity2fb92db2011-04-27 19:42:18 +03006437 vmx_segment_cache_clear(vmx);
6438
Avi Kivity5706be02008-08-20 15:07:31 +03006439 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006440 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006441 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006442
6443 seg_setup(VCPU_SREG_DS);
6444 seg_setup(VCPU_SREG_ES);
6445 seg_setup(VCPU_SREG_FS);
6446 seg_setup(VCPU_SREG_GS);
6447 seg_setup(VCPU_SREG_SS);
6448
6449 vmcs_write16(GUEST_TR_SELECTOR, 0);
6450 vmcs_writel(GUEST_TR_BASE, 0);
6451 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6452 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6453
6454 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6455 vmcs_writel(GUEST_LDTR_BASE, 0);
6456 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6457 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6458
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006459 if (!init_event) {
6460 vmcs_write32(GUEST_SYSENTER_CS, 0);
6461 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6462 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6463 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6464 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006465
Wanpeng Lic37c2872017-11-20 14:52:21 -08006466 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006467 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006468
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006469 vmcs_writel(GUEST_GDTR_BASE, 0);
6470 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6471
6472 vmcs_writel(GUEST_IDTR_BASE, 0);
6473 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6474
Anthony Liguori443381a2010-12-06 10:53:38 -06006475 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006476 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006477 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006478 if (kvm_mpx_supported())
6479 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006480
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006481 setup_msrs(vmx);
6482
Avi Kivity6aa8b732006-12-10 02:21:36 -08006483 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6484
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006485 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006486 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006487 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006488 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006489 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006490 vmcs_write32(TPR_THRESHOLD, 0);
6491 }
6492
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006493 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006494
Sheng Yang2384d2b2008-01-17 15:14:33 +08006495 if (vmx->vpid != 0)
6496 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6497
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006498 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006499 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006500 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006501 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006502 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006503
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006504 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006505
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006506 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006507 if (init_event)
6508 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006509}
6510
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006511/*
6512 * In nested virtualization, check if L1 asked to exit on external interrupts.
6513 * For most existing hypervisors, this will always return true.
6514 */
6515static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6516{
6517 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6518 PIN_BASED_EXT_INTR_MASK;
6519}
6520
Bandan Das77b0f5d2014-04-19 18:17:45 -04006521/*
6522 * In nested virtualization, check if L1 has set
6523 * VM_EXIT_ACK_INTR_ON_EXIT
6524 */
6525static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6526{
6527 return get_vmcs12(vcpu)->vm_exit_controls &
6528 VM_EXIT_ACK_INTR_ON_EXIT;
6529}
6530
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006531static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6532{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006533 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006534}
6535
Jan Kiszkac9a79532014-03-07 20:03:15 +01006536static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006537{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006538 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6539 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006540}
6541
Jan Kiszkac9a79532014-03-07 20:03:15 +01006542static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006543{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006544 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006545 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006546 enable_irq_window(vcpu);
6547 return;
6548 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006549
Paolo Bonzini47c01522016-12-19 11:44:07 +01006550 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6551 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006552}
6553
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006554static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006555{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006556 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006557 uint32_t intr;
6558 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006559
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006560 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006561
Avi Kivityfa89a812008-09-01 15:57:51 +03006562 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006563 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006564 int inc_eip = 0;
6565 if (vcpu->arch.interrupt.soft)
6566 inc_eip = vcpu->arch.event_exit_inst_len;
6567 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006568 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006569 return;
6570 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006571 intr = irq | INTR_INFO_VALID_MASK;
6572 if (vcpu->arch.interrupt.soft) {
6573 intr |= INTR_TYPE_SOFT_INTR;
6574 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6575 vmx->vcpu.arch.event_exit_inst_len);
6576 } else
6577 intr |= INTR_TYPE_EXT_INTR;
6578 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006579
6580 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006581}
6582
Sheng Yangf08864b2008-05-15 18:23:25 +08006583static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6584{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006585 struct vcpu_vmx *vmx = to_vmx(vcpu);
6586
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006587 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006588 /*
6589 * Tracking the NMI-blocked state in software is built upon
6590 * finding the next open IRQ window. This, in turn, depends on
6591 * well-behaving guests: They have to keep IRQs disabled at
6592 * least as long as the NMI handler runs. Otherwise we may
6593 * cause NMI nesting, maybe breaking the guest. But as this is
6594 * highly unlikely, we can live with the residual risk.
6595 */
6596 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6597 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6598 }
6599
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006600 ++vcpu->stat.nmi_injections;
6601 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006602
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006603 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006604 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006605 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006606 return;
6607 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006608
Sheng Yangf08864b2008-05-15 18:23:25 +08006609 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6610 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006611
6612 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006613}
6614
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006615static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6616{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006617 struct vcpu_vmx *vmx = to_vmx(vcpu);
6618 bool masked;
6619
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006620 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006621 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006622 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006623 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006624 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6625 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6626 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006627}
6628
6629static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6630{
6631 struct vcpu_vmx *vmx = to_vmx(vcpu);
6632
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006633 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006634 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6635 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6636 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6637 }
6638 } else {
6639 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6640 if (masked)
6641 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6642 GUEST_INTR_STATE_NMI);
6643 else
6644 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6645 GUEST_INTR_STATE_NMI);
6646 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006647}
6648
Jan Kiszka2505dc92013-04-14 12:12:47 +02006649static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6650{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006651 if (to_vmx(vcpu)->nested.nested_run_pending)
6652 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006653
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006654 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006655 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6656 return 0;
6657
Jan Kiszka2505dc92013-04-14 12:12:47 +02006658 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6659 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6660 | GUEST_INTR_STATE_NMI));
6661}
6662
Gleb Natapov78646122009-03-23 12:12:11 +02006663static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6664{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006665 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6666 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006667 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6668 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006669}
6670
Izik Eiduscbc94022007-10-25 00:29:55 +02006671static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6672{
6673 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006674
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006675 if (enable_unrestricted_guest)
6676 return 0;
6677
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006678 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6679 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006680 if (ret)
6681 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006682 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006683 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006684}
6685
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006686static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6687{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006688 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006689 return 0;
6690}
6691
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006692static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006693{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006694 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006695 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006696 /*
6697 * Update instruction length as we may reinject the exception
6698 * from user space while in guest debugging mode.
6699 */
6700 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6701 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006702 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006703 return false;
6704 /* fall through */
6705 case DB_VECTOR:
6706 if (vcpu->guest_debug &
6707 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6708 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006709 /* fall through */
6710 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006711 case OF_VECTOR:
6712 case BR_VECTOR:
6713 case UD_VECTOR:
6714 case DF_VECTOR:
6715 case SS_VECTOR:
6716 case GP_VECTOR:
6717 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006718 return true;
6719 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006720 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006721 return false;
6722}
6723
6724static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6725 int vec, u32 err_code)
6726{
6727 /*
6728 * Instruction with address size override prefix opcode 0x67
6729 * Cause the #SS fault with 0 error code in VM86 mode.
6730 */
6731 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6732 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6733 if (vcpu->arch.halt_request) {
6734 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006735 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006736 }
6737 return 1;
6738 }
6739 return 0;
6740 }
6741
6742 /*
6743 * Forward all other exceptions that are valid in real mode.
6744 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6745 * the required debugging infrastructure rework.
6746 */
6747 kvm_queue_exception(vcpu, vec);
6748 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006749}
6750
Andi Kleena0861c02009-06-08 17:37:09 +08006751/*
6752 * Trigger machine check on the host. We assume all the MSRs are already set up
6753 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6754 * We pass a fake environment to the machine check handler because we want
6755 * the guest to be always treated like user space, no matter what context
6756 * it used internally.
6757 */
6758static void kvm_machine_check(void)
6759{
6760#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6761 struct pt_regs regs = {
6762 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6763 .flags = X86_EFLAGS_IF,
6764 };
6765
6766 do_machine_check(&regs, 0);
6767#endif
6768}
6769
Avi Kivity851ba692009-08-24 11:10:17 +03006770static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006771{
6772 /* already handled by vcpu_run */
6773 return 1;
6774}
6775
Avi Kivity851ba692009-08-24 11:10:17 +03006776static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006777{
Avi Kivity1155f762007-11-22 11:30:47 +02006778 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006779 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006780 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006781 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006782 u32 vect_info;
6783 enum emulation_result er;
6784
Avi Kivity1155f762007-11-22 11:30:47 +02006785 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006786 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006787
Andi Kleena0861c02009-06-08 17:37:09 +08006788 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006789 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006790
Jim Mattsonef85b672016-12-12 11:01:37 -08006791 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006792 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006793
Wanpeng Li082d06e2018-04-03 16:28:48 -07006794 if (is_invalid_opcode(intr_info))
6795 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006796
Avi Kivity6aa8b732006-12-10 02:21:36 -08006797 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006798 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006799 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006800
Liran Alon9e869482018-03-12 13:12:51 +02006801 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6802 WARN_ON_ONCE(!enable_vmware_backdoor);
6803 er = emulate_instruction(vcpu,
6804 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6805 if (er == EMULATE_USER_EXIT)
6806 return 0;
6807 else if (er != EMULATE_DONE)
6808 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6809 return 1;
6810 }
6811
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006812 /*
6813 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6814 * MMIO, it is better to report an internal error.
6815 * See the comments in vmx_handle_exit.
6816 */
6817 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6818 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6819 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6820 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006821 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006822 vcpu->run->internal.data[0] = vect_info;
6823 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006824 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006825 return 0;
6826 }
6827
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828 if (is_page_fault(intr_info)) {
6829 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006830 /* EPT won't cause page fault directly */
6831 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006832 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006833 }
6834
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006835 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006836
6837 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6838 return handle_rmode_exception(vcpu, ex_no, error_code);
6839
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006840 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006841 case AC_VECTOR:
6842 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6843 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006844 case DB_VECTOR:
6845 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6846 if (!(vcpu->guest_debug &
6847 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006848 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006849 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006850 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006851 skip_emulated_instruction(vcpu);
6852
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006853 kvm_queue_exception(vcpu, DB_VECTOR);
6854 return 1;
6855 }
6856 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6857 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6858 /* fall through */
6859 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006860 /*
6861 * Update instruction length as we may reinject #BP from
6862 * user space while in guest debugging mode. Reading it for
6863 * #DB as well causes no harm, it is not used in that case.
6864 */
6865 vmx->vcpu.arch.event_exit_inst_len =
6866 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006867 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006868 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006869 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6870 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006871 break;
6872 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006873 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6874 kvm_run->ex.exception = ex_no;
6875 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006878 return 0;
6879}
6880
Avi Kivity851ba692009-08-24 11:10:17 +03006881static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006882{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006883 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006884 return 1;
6885}
6886
Avi Kivity851ba692009-08-24 11:10:17 +03006887static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006888{
Avi Kivity851ba692009-08-24 11:10:17 +03006889 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006890 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006891 return 0;
6892}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893
Avi Kivity851ba692009-08-24 11:10:17 +03006894static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006895{
He, Qingbfdaab02007-09-12 14:18:28 +08006896 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006897 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006898 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899
He, Qingbfdaab02007-09-12 14:18:28 +08006900 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006901 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006902
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006903 ++vcpu->stat.io_exits;
6904
Sean Christopherson432baf62018-03-08 08:57:26 -08006905 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006906 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006907
6908 port = exit_qualification >> 16;
6909 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006910 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006911
Sean Christophersondca7f122018-03-08 08:57:27 -08006912 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913}
6914
Ingo Molnar102d8322007-02-19 14:37:47 +02006915static void
6916vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6917{
6918 /*
6919 * Patch in the VMCALL instruction:
6920 */
6921 hypercall[0] = 0x0f;
6922 hypercall[1] = 0x01;
6923 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006924}
6925
Guo Chao0fa06072012-06-28 15:16:19 +08006926/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006927static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6928{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006929 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006930 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6931 unsigned long orig_val = val;
6932
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006933 /*
6934 * We get here when L2 changed cr0 in a way that did not change
6935 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006936 * but did change L0 shadowed bits. So we first calculate the
6937 * effective cr0 value that L1 would like to write into the
6938 * hardware. It consists of the L2-owned bits from the new
6939 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006940 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006941 val = (val & ~vmcs12->cr0_guest_host_mask) |
6942 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6943
David Matlack38991522016-11-29 18:14:08 -08006944 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006945 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006946
6947 if (kvm_set_cr0(vcpu, val))
6948 return 1;
6949 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006950 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006951 } else {
6952 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006953 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006954 return 1;
David Matlack38991522016-11-29 18:14:08 -08006955
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006956 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006957 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006958}
6959
6960static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6961{
6962 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6964 unsigned long orig_val = val;
6965
6966 /* analogously to handle_set_cr0 */
6967 val = (val & ~vmcs12->cr4_guest_host_mask) |
6968 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6969 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006970 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006971 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006972 return 0;
6973 } else
6974 return kvm_set_cr4(vcpu, val);
6975}
6976
Paolo Bonzini0367f202016-07-12 10:44:55 +02006977static int handle_desc(struct kvm_vcpu *vcpu)
6978{
6979 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6980 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6981}
6982
Avi Kivity851ba692009-08-24 11:10:17 +03006983static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006984{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006985 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006986 int cr;
6987 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006988 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006989 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006990
He, Qingbfdaab02007-09-12 14:18:28 +08006991 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006992 cr = exit_qualification & 15;
6993 reg = (exit_qualification >> 8) & 15;
6994 switch ((exit_qualification >> 4) & 3) {
6995 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006996 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006997 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998 switch (cr) {
6999 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007000 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007001 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007002 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007003 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007004 err = kvm_set_cr3(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 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007007 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007008 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007009 case 8: {
7010 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007011 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007012 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007013 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007014 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007015 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007016 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007017 return ret;
7018 /*
7019 * TODO: we might be squashing a
7020 * KVM_GUESTDBG_SINGLESTEP-triggered
7021 * KVM_EXIT_DEBUG here.
7022 */
Avi Kivity851ba692009-08-24 11:10:17 +03007023 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007024 return 0;
7025 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007026 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007027 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007028 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007029 WARN_ONCE(1, "Guest should always own CR0.TS");
7030 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007031 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007032 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007033 case 1: /*mov from cr*/
7034 switch (cr) {
7035 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007036 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007037 val = kvm_read_cr3(vcpu);
7038 kvm_register_write(vcpu, reg, val);
7039 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007040 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007042 val = kvm_get_cr8(vcpu);
7043 kvm_register_write(vcpu, reg, val);
7044 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007045 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046 }
7047 break;
7048 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007049 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007050 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007051 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052
Kyle Huey6affcbe2016-11-29 12:40:40 -08007053 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054 default:
7055 break;
7056 }
Avi Kivity851ba692009-08-24 11:10:17 +03007057 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007058 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059 (int)(exit_qualification >> 4) & 3, cr);
7060 return 0;
7061}
7062
Avi Kivity851ba692009-08-24 11:10:17 +03007063static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064{
He, Qingbfdaab02007-09-12 14:18:28 +08007065 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007066 int dr, dr7, reg;
7067
7068 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7069 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7070
7071 /* First, if DR does not exist, trigger UD */
7072 if (!kvm_require_dr(vcpu, dr))
7073 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074
Jan Kiszkaf2483412010-01-20 18:20:20 +01007075 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007076 if (!kvm_require_cpl(vcpu, 0))
7077 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007078 dr7 = vmcs_readl(GUEST_DR7);
7079 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007080 /*
7081 * As the vm-exit takes precedence over the debug trap, we
7082 * need to emulate the latter, either for the host or the
7083 * guest debugging itself.
7084 */
7085 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007086 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007087 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007088 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007089 vcpu->run->debug.arch.exception = DB_VECTOR;
7090 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007091 return 0;
7092 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007093 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007094 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007095 kvm_queue_exception(vcpu, DB_VECTOR);
7096 return 1;
7097 }
7098 }
7099
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007100 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007101 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7102 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007103
7104 /*
7105 * No more DR vmexits; force a reload of the debug registers
7106 * and reenter on this instruction. The next vmexit will
7107 * retrieve the full state of the debug registers.
7108 */
7109 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7110 return 1;
7111 }
7112
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007113 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7114 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007115 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007116
7117 if (kvm_get_dr(vcpu, dr, &val))
7118 return 1;
7119 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007120 } else
Nadav Amit57773922014-06-18 17:19:23 +03007121 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007122 return 1;
7123
Kyle Huey6affcbe2016-11-29 12:40:40 -08007124 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007125}
7126
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007127static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7128{
7129 return vcpu->arch.dr6;
7130}
7131
7132static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7133{
7134}
7135
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007136static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7137{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007138 get_debugreg(vcpu->arch.db[0], 0);
7139 get_debugreg(vcpu->arch.db[1], 1);
7140 get_debugreg(vcpu->arch.db[2], 2);
7141 get_debugreg(vcpu->arch.db[3], 3);
7142 get_debugreg(vcpu->arch.dr6, 6);
7143 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7144
7145 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007146 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007147}
7148
Gleb Natapov020df072010-04-13 10:05:23 +03007149static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7150{
7151 vmcs_writel(GUEST_DR7, val);
7152}
7153
Avi Kivity851ba692009-08-24 11:10:17 +03007154static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155{
Kyle Huey6a908b62016-11-29 12:40:37 -08007156 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007157}
7158
Avi Kivity851ba692009-08-24 11:10:17 +03007159static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007160{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007161 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007162 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007163
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007164 msr_info.index = ecx;
7165 msr_info.host_initiated = false;
7166 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007167 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007168 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007169 return 1;
7170 }
7171
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007172 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007173
Avi Kivity6aa8b732006-12-10 02:21:36 -08007174 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007175 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7176 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007177 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178}
7179
Avi Kivity851ba692009-08-24 11:10:17 +03007180static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007181{
Will Auld8fe8ab42012-11-29 12:42:12 -08007182 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007183 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7184 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7185 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186
Will Auld8fe8ab42012-11-29 12:42:12 -08007187 msr.data = data;
7188 msr.index = ecx;
7189 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007190 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007191 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007192 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007193 return 1;
7194 }
7195
Avi Kivity59200272010-01-25 19:47:02 +02007196 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007197 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007198}
7199
Avi Kivity851ba692009-08-24 11:10:17 +03007200static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007201{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007202 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007203 return 1;
7204}
7205
Avi Kivity851ba692009-08-24 11:10:17 +03007206static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007207{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007208 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7209 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007210
Avi Kivity3842d132010-07-27 12:30:24 +03007211 kvm_make_request(KVM_REQ_EVENT, vcpu);
7212
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007213 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007214 return 1;
7215}
7216
Avi Kivity851ba692009-08-24 11:10:17 +03007217static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007218{
Avi Kivityd3bef152007-06-05 15:53:05 +03007219 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007220}
7221
Avi Kivity851ba692009-08-24 11:10:17 +03007222static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007223{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007224 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007225}
7226
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007227static int handle_invd(struct kvm_vcpu *vcpu)
7228{
Andre Przywara51d8b662010-12-21 11:12:02 +01007229 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007230}
7231
Avi Kivity851ba692009-08-24 11:10:17 +03007232static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007233{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007234 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007235
7236 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007237 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007238}
7239
Avi Kivityfee84b02011-11-10 14:57:25 +02007240static int handle_rdpmc(struct kvm_vcpu *vcpu)
7241{
7242 int err;
7243
7244 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007245 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007246}
7247
Avi Kivity851ba692009-08-24 11:10:17 +03007248static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007249{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007250 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007251}
7252
Dexuan Cui2acf9232010-06-10 11:27:12 +08007253static int handle_xsetbv(struct kvm_vcpu *vcpu)
7254{
7255 u64 new_bv = kvm_read_edx_eax(vcpu);
7256 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7257
7258 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007259 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007260 return 1;
7261}
7262
Wanpeng Lif53cd632014-12-02 19:14:58 +08007263static int handle_xsaves(struct kvm_vcpu *vcpu)
7264{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007265 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007266 WARN(1, "this should never happen\n");
7267 return 1;
7268}
7269
7270static int handle_xrstors(struct kvm_vcpu *vcpu)
7271{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007272 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007273 WARN(1, "this should never happen\n");
7274 return 1;
7275}
7276
Avi Kivity851ba692009-08-24 11:10:17 +03007277static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007278{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007279 if (likely(fasteoi)) {
7280 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7281 int access_type, offset;
7282
7283 access_type = exit_qualification & APIC_ACCESS_TYPE;
7284 offset = exit_qualification & APIC_ACCESS_OFFSET;
7285 /*
7286 * Sane guest uses MOV to write EOI, with written value
7287 * not cared. So make a short-circuit here by avoiding
7288 * heavy instruction emulation.
7289 */
7290 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7291 (offset == APIC_EOI)) {
7292 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007293 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007294 }
7295 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007296 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007297}
7298
Yang Zhangc7c9c562013-01-25 10:18:51 +08007299static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7300{
7301 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7302 int vector = exit_qualification & 0xff;
7303
7304 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7305 kvm_apic_set_eoi_accelerated(vcpu, vector);
7306 return 1;
7307}
7308
Yang Zhang83d4c282013-01-25 10:18:49 +08007309static int handle_apic_write(struct kvm_vcpu *vcpu)
7310{
7311 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7312 u32 offset = exit_qualification & 0xfff;
7313
7314 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7315 kvm_apic_write_nodecode(vcpu, offset);
7316 return 1;
7317}
7318
Avi Kivity851ba692009-08-24 11:10:17 +03007319static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007320{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007321 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007322 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007323 bool has_error_code = false;
7324 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007325 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007326 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007327
7328 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007329 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007330 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007331
7332 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7333
7334 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007335 if (reason == TASK_SWITCH_GATE && idt_v) {
7336 switch (type) {
7337 case INTR_TYPE_NMI_INTR:
7338 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007339 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007340 break;
7341 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007342 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007343 kvm_clear_interrupt_queue(vcpu);
7344 break;
7345 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007346 if (vmx->idt_vectoring_info &
7347 VECTORING_INFO_DELIVER_CODE_MASK) {
7348 has_error_code = true;
7349 error_code =
7350 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7351 }
7352 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007353 case INTR_TYPE_SOFT_EXCEPTION:
7354 kvm_clear_exception_queue(vcpu);
7355 break;
7356 default:
7357 break;
7358 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007359 }
Izik Eidus37817f22008-03-24 23:14:53 +02007360 tss_selector = exit_qualification;
7361
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007362 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7363 type != INTR_TYPE_EXT_INTR &&
7364 type != INTR_TYPE_NMI_INTR))
7365 skip_emulated_instruction(vcpu);
7366
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007367 if (kvm_task_switch(vcpu, tss_selector,
7368 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7369 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007370 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7371 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7372 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007373 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007374 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007375
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007376 /*
7377 * TODO: What about debug traps on tss switch?
7378 * Are we supposed to inject them and update dr6?
7379 */
7380
7381 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007382}
7383
Avi Kivity851ba692009-08-24 11:10:17 +03007384static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007385{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007386 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007387 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007388 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007389
Sheng Yangf9c617f2009-03-25 10:08:52 +08007390 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007391
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007392 /*
7393 * EPT violation happened while executing iret from NMI,
7394 * "blocked by NMI" bit has to be set before next VM entry.
7395 * There are errata that may cause this bit to not be set:
7396 * AAK134, BY25.
7397 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007398 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007399 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007400 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007401 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7402
Sheng Yang14394422008-04-28 12:24:45 +08007403 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007404 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007405
Junaid Shahid27959a42016-12-06 16:46:10 -08007406 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007407 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007408 ? PFERR_USER_MASK : 0;
7409 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007410 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007411 ? PFERR_WRITE_MASK : 0;
7412 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007413 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007414 ? PFERR_FETCH_MASK : 0;
7415 /* ept page table entry is present? */
7416 error_code |= (exit_qualification &
7417 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7418 EPT_VIOLATION_EXECUTABLE))
7419 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007420
Paolo Bonzinieebed242016-11-28 14:39:58 +01007421 error_code |= (exit_qualification & 0x100) != 0 ?
7422 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007423
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007424 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007425 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007426}
7427
Avi Kivity851ba692009-08-24 11:10:17 +03007428static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007429{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007430 gpa_t gpa;
7431
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007432 /*
7433 * A nested guest cannot optimize MMIO vmexits, because we have an
7434 * nGPA here instead of the required GPA.
7435 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007436 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007437 if (!is_guest_mode(vcpu) &&
7438 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007439 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007440 /*
7441 * Doing kvm_skip_emulated_instruction() depends on undefined
7442 * behavior: Intel's manual doesn't mandate
7443 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7444 * occurs and while on real hardware it was observed to be set,
7445 * other hypervisors (namely Hyper-V) don't set it, we end up
7446 * advancing IP with some random value. Disable fast mmio when
7447 * running nested and keep it for real hardware in hope that
7448 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7449 */
7450 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7451 return kvm_skip_emulated_instruction(vcpu);
7452 else
7453 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7454 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007455 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007456
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007457 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007458}
7459
Avi Kivity851ba692009-08-24 11:10:17 +03007460static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007461{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007462 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007463 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7464 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007465 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007466 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007467
7468 return 1;
7469}
7470
Mohammed Gamal80ced182009-09-01 12:48:18 +02007471static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007472{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007473 struct vcpu_vmx *vmx = to_vmx(vcpu);
7474 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007475 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007476 u32 cpu_exec_ctrl;
7477 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007478 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007479
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007480 /*
7481 * We should never reach the point where we are emulating L2
7482 * due to invalid guest state as that means we incorrectly
7483 * allowed a nested VMEntry with an invalid vmcs12.
7484 */
7485 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7486
Avi Kivity49e9d552010-09-19 14:34:08 +02007487 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7488 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007489
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007490 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007491 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007492 return handle_interrupt_window(&vmx->vcpu);
7493
Radim Krčmář72875d82017-04-26 22:32:19 +02007494 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007495 return 1;
7496
Liran Alon9b8ae632017-11-05 16:56:34 +02007497 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007498
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007499 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007500 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007501 ret = 0;
7502 goto out;
7503 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007504
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007505 if (err != EMULATE_DONE)
7506 goto emulation_error;
7507
7508 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7509 vcpu->arch.exception.pending)
7510 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007511
Gleb Natapov8d76c492013-05-08 18:38:44 +03007512 if (vcpu->arch.halt_request) {
7513 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007514 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007515 goto out;
7516 }
7517
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007518 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007519 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007520 if (need_resched())
7521 schedule();
7522 }
7523
Mohammed Gamal80ced182009-09-01 12:48:18 +02007524out:
7525 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007526
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007527emulation_error:
7528 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7529 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7530 vcpu->run->internal.ndata = 0;
7531 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007532}
7533
7534static void grow_ple_window(struct kvm_vcpu *vcpu)
7535{
7536 struct vcpu_vmx *vmx = to_vmx(vcpu);
7537 int old = vmx->ple_window;
7538
Babu Mogerc8e88712018-03-16 16:37:24 -04007539 vmx->ple_window = __grow_ple_window(old, ple_window,
7540 ple_window_grow,
7541 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007542
7543 if (vmx->ple_window != old)
7544 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007545
7546 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007547}
7548
7549static void shrink_ple_window(struct kvm_vcpu *vcpu)
7550{
7551 struct vcpu_vmx *vmx = to_vmx(vcpu);
7552 int old = vmx->ple_window;
7553
Babu Mogerc8e88712018-03-16 16:37:24 -04007554 vmx->ple_window = __shrink_ple_window(old, ple_window,
7555 ple_window_shrink,
7556 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007557
7558 if (vmx->ple_window != old)
7559 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007560
7561 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007562}
7563
7564/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007565 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7566 */
7567static void wakeup_handler(void)
7568{
7569 struct kvm_vcpu *vcpu;
7570 int cpu = smp_processor_id();
7571
7572 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7573 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7574 blocked_vcpu_list) {
7575 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7576
7577 if (pi_test_on(pi_desc) == 1)
7578 kvm_vcpu_kick(vcpu);
7579 }
7580 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7581}
7582
Peng Haoe01bca22018-04-07 05:47:32 +08007583static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007584{
7585 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7586 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7587 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7588 0ull, VMX_EPT_EXECUTABLE_MASK,
7589 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007590 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007591
7592 ept_set_mmio_spte_mask();
7593 kvm_enable_tdp();
7594}
7595
Tiejun Chenf2c76482014-10-28 10:14:47 +08007596static __init int hardware_setup(void)
7597{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007598 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007599
7600 rdmsrl_safe(MSR_EFER, &host_efer);
7601
7602 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7603 kvm_define_shared_msr(i, vmx_msr_index[i]);
7604
Radim Krčmář23611332016-09-29 22:41:33 +02007605 for (i = 0; i < VMX_BITMAP_NR; i++) {
7606 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7607 if (!vmx_bitmap[i])
7608 goto out;
7609 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007610
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007611 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7612 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7613
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007614 if (setup_vmcs_config(&vmcs_config) < 0) {
7615 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007616 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007617 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007618
7619 if (boot_cpu_has(X86_FEATURE_NX))
7620 kvm_enable_efer_bits(EFER_NX);
7621
Wanpeng Li08d839c2017-03-23 05:30:08 -07007622 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7623 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007624 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007625
Tiejun Chenf2c76482014-10-28 10:14:47 +08007626 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007627 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007628 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007629 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007630 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007631
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007632 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007633 enable_ept_ad_bits = 0;
7634
Wanpeng Li8ad81822017-10-09 15:51:53 -07007635 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007636 enable_unrestricted_guest = 0;
7637
Paolo Bonziniad15a292015-01-30 16:18:49 +01007638 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007639 flexpriority_enabled = 0;
7640
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007641 if (!cpu_has_virtual_nmis())
7642 enable_vnmi = 0;
7643
Paolo Bonziniad15a292015-01-30 16:18:49 +01007644 /*
7645 * set_apic_access_page_addr() is used to reload apic access
7646 * page upon invalidation. No need to do anything if not
7647 * using the APIC_ACCESS_ADDR VMCS field.
7648 */
7649 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007650 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007651
7652 if (!cpu_has_vmx_tpr_shadow())
7653 kvm_x86_ops->update_cr8_intercept = NULL;
7654
7655 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7656 kvm_disable_largepages();
7657
Wanpeng Li0f107682017-09-28 18:06:24 -07007658 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007660 ple_window = 0;
7661 ple_window_grow = 0;
7662 ple_window_max = 0;
7663 ple_window_shrink = 0;
7664 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007665
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007666 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007667 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007668 kvm_x86_ops->sync_pir_to_irr = NULL;
7669 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007670
Haozhong Zhang64903d62015-10-20 15:39:09 +08007671 if (cpu_has_vmx_tsc_scaling()) {
7672 kvm_has_tsc_control = true;
7673 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7674 kvm_tsc_scaling_ratio_frac_bits = 48;
7675 }
7676
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007677 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7678
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007679 if (enable_ept)
7680 vmx_enable_tdp();
7681 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007682 kvm_disable_tdp();
7683
Kai Huang843e4332015-01-28 10:54:28 +08007684 /*
7685 * Only enable PML when hardware supports PML feature, and both EPT
7686 * and EPT A/D bit features are enabled -- PML depends on them to work.
7687 */
7688 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7689 enable_pml = 0;
7690
7691 if (!enable_pml) {
7692 kvm_x86_ops->slot_enable_log_dirty = NULL;
7693 kvm_x86_ops->slot_disable_log_dirty = NULL;
7694 kvm_x86_ops->flush_log_dirty = NULL;
7695 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7696 }
7697
Yunhong Jiang64672c92016-06-13 14:19:59 -07007698 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7699 u64 vmx_msr;
7700
7701 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7702 cpu_preemption_timer_multi =
7703 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7704 } else {
7705 kvm_x86_ops->set_hv_timer = NULL;
7706 kvm_x86_ops->cancel_hv_timer = NULL;
7707 }
7708
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007709 if (!cpu_has_vmx_shadow_vmcs())
7710 enable_shadow_vmcs = 0;
7711 if (enable_shadow_vmcs)
7712 init_vmcs_shadow_fields();
7713
Feng Wubf9f6ac2015-09-18 22:29:55 +08007714 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007715 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007716
Ashok Rajc45dcc72016-06-22 14:59:56 +08007717 kvm_mce_cap_supported |= MCG_LMCE_P;
7718
Tiejun Chenf2c76482014-10-28 10:14:47 +08007719 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007720
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007721out:
Radim Krčmář23611332016-09-29 22:41:33 +02007722 for (i = 0; i < VMX_BITMAP_NR; i++)
7723 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007724
7725 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007726}
7727
7728static __exit void hardware_unsetup(void)
7729{
Radim Krčmář23611332016-09-29 22:41:33 +02007730 int i;
7731
7732 for (i = 0; i < VMX_BITMAP_NR; i++)
7733 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007734
Tiejun Chenf2c76482014-10-28 10:14:47 +08007735 free_kvm_area();
7736}
7737
Avi Kivity6aa8b732006-12-10 02:21:36 -08007738/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007739 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7740 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7741 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007742static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007743{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007744 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007745 grow_ple_window(vcpu);
7746
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007747 /*
7748 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7749 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7750 * never set PAUSE_EXITING and just set PLE if supported,
7751 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7752 */
7753 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007754 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007755}
7756
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007757static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007758{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007759 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007760}
7761
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007762static int handle_mwait(struct kvm_vcpu *vcpu)
7763{
7764 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7765 return handle_nop(vcpu);
7766}
7767
Jim Mattson45ec3682017-08-23 16:32:04 -07007768static int handle_invalid_op(struct kvm_vcpu *vcpu)
7769{
7770 kvm_queue_exception(vcpu, UD_VECTOR);
7771 return 1;
7772}
7773
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007774static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7775{
7776 return 1;
7777}
7778
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007779static int handle_monitor(struct kvm_vcpu *vcpu)
7780{
7781 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7782 return handle_nop(vcpu);
7783}
7784
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007785/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007786 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7787 * set the success or error code of an emulated VMX instruction, as specified
7788 * by Vol 2B, VMX Instruction Reference, "Conventions".
7789 */
7790static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7791{
7792 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7793 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7794 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7795}
7796
7797static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7798{
7799 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7800 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7801 X86_EFLAGS_SF | X86_EFLAGS_OF))
7802 | X86_EFLAGS_CF);
7803}
7804
Abel Gordon145c28d2013-04-18 14:36:55 +03007805static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007806 u32 vm_instruction_error)
7807{
7808 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7809 /*
7810 * failValid writes the error number to the current VMCS, which
7811 * can't be done there isn't a current VMCS.
7812 */
7813 nested_vmx_failInvalid(vcpu);
7814 return;
7815 }
7816 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7817 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7818 X86_EFLAGS_SF | X86_EFLAGS_OF))
7819 | X86_EFLAGS_ZF);
7820 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7821 /*
7822 * We don't need to force a shadow sync because
7823 * VM_INSTRUCTION_ERROR is not shadowed
7824 */
7825}
Abel Gordon145c28d2013-04-18 14:36:55 +03007826
Wincy Vanff651cb2014-12-11 08:52:58 +03007827static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7828{
7829 /* TODO: not to reset guest simply here. */
7830 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007831 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007832}
7833
Jan Kiszkaf4124502014-03-07 20:03:13 +01007834static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7835{
7836 struct vcpu_vmx *vmx =
7837 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7838
7839 vmx->nested.preemption_timer_expired = true;
7840 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7841 kvm_vcpu_kick(&vmx->vcpu);
7842
7843 return HRTIMER_NORESTART;
7844}
7845
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007846/*
Bandan Das19677e32014-05-06 02:19:15 -04007847 * Decode the memory-address operand of a vmx instruction, as recorded on an
7848 * exit caused by such an instruction (run by a guest hypervisor).
7849 * On success, returns 0. When the operand is invalid, returns 1 and throws
7850 * #UD or #GP.
7851 */
7852static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7853 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007854 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007855{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007856 gva_t off;
7857 bool exn;
7858 struct kvm_segment s;
7859
Bandan Das19677e32014-05-06 02:19:15 -04007860 /*
7861 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7862 * Execution", on an exit, vmx_instruction_info holds most of the
7863 * addressing components of the operand. Only the displacement part
7864 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7865 * For how an actual address is calculated from all these components,
7866 * refer to Vol. 1, "Operand Addressing".
7867 */
7868 int scaling = vmx_instruction_info & 3;
7869 int addr_size = (vmx_instruction_info >> 7) & 7;
7870 bool is_reg = vmx_instruction_info & (1u << 10);
7871 int seg_reg = (vmx_instruction_info >> 15) & 7;
7872 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7873 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7874 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7875 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7876
7877 if (is_reg) {
7878 kvm_queue_exception(vcpu, UD_VECTOR);
7879 return 1;
7880 }
7881
7882 /* Addr = segment_base + offset */
7883 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007884 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007885 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007886 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007887 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007888 off += kvm_register_read(vcpu, index_reg)<<scaling;
7889 vmx_get_segment(vcpu, &s, seg_reg);
7890 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007891
7892 if (addr_size == 1) /* 32 bit */
7893 *ret &= 0xffffffff;
7894
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007895 /* Checks for #GP/#SS exceptions. */
7896 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007897 if (is_long_mode(vcpu)) {
7898 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7899 * non-canonical form. This is the only check on the memory
7900 * destination for long mode!
7901 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007902 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007903 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007904 /* Protected mode: apply checks for segment validity in the
7905 * following order:
7906 * - segment type check (#GP(0) may be thrown)
7907 * - usability check (#GP(0)/#SS(0))
7908 * - limit check (#GP(0)/#SS(0))
7909 */
7910 if (wr)
7911 /* #GP(0) if the destination operand is located in a
7912 * read-only data segment or any code segment.
7913 */
7914 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7915 else
7916 /* #GP(0) if the source operand is located in an
7917 * execute-only code segment
7918 */
7919 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007920 if (exn) {
7921 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7922 return 1;
7923 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007924 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7925 */
7926 exn = (s.unusable != 0);
7927 /* Protected mode: #GP(0)/#SS(0) if the memory
7928 * operand is outside the segment limit.
7929 */
7930 exn = exn || (off + sizeof(u64) > s.limit);
7931 }
7932 if (exn) {
7933 kvm_queue_exception_e(vcpu,
7934 seg_reg == VCPU_SREG_SS ?
7935 SS_VECTOR : GP_VECTOR,
7936 0);
7937 return 1;
7938 }
7939
Bandan Das19677e32014-05-06 02:19:15 -04007940 return 0;
7941}
7942
Radim Krčmářcbf71272017-05-19 15:48:51 +02007943static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007944{
7945 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007946 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007947
7948 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007949 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007950 return 1;
7951
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007952 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007953 kvm_inject_page_fault(vcpu, &e);
7954 return 1;
7955 }
7956
Bandan Das3573e222014-05-06 02:19:16 -04007957 return 0;
7958}
7959
Jim Mattsone29acc52016-11-30 12:03:43 -08007960static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7961{
7962 struct vcpu_vmx *vmx = to_vmx(vcpu);
7963 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007964 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007965
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007966 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7967 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007968 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007969
7970 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7971 if (!vmx->nested.cached_vmcs12)
7972 goto out_cached_vmcs12;
7973
7974 if (enable_shadow_vmcs) {
7975 shadow_vmcs = alloc_vmcs();
7976 if (!shadow_vmcs)
7977 goto out_shadow_vmcs;
7978 /* mark vmcs as shadow */
7979 shadow_vmcs->revision_id |= (1u << 31);
7980 /* init shadow vmcs */
7981 vmcs_clear(shadow_vmcs);
7982 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7983 }
7984
Jim Mattsone29acc52016-11-30 12:03:43 -08007985 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7986 HRTIMER_MODE_REL_PINNED);
7987 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7988
7989 vmx->nested.vmxon = true;
7990 return 0;
7991
7992out_shadow_vmcs:
7993 kfree(vmx->nested.cached_vmcs12);
7994
7995out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007996 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007997
Jim Mattsonde3a0022017-11-27 17:22:25 -06007998out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007999 return -ENOMEM;
8000}
8001
Bandan Das3573e222014-05-06 02:19:16 -04008002/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008003 * Emulate the VMXON instruction.
8004 * Currently, we just remember that VMX is active, and do not save or even
8005 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8006 * do not currently need to store anything in that guest-allocated memory
8007 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8008 * argument is different from the VMXON pointer (which the spec says they do).
8009 */
8010static int handle_vmon(struct kvm_vcpu *vcpu)
8011{
Jim Mattsone29acc52016-11-30 12:03:43 -08008012 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008013 gpa_t vmptr;
8014 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008015 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008016 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8017 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008018
Jim Mattson70f3aac2017-04-26 08:53:46 -07008019 /*
8020 * The Intel VMX Instruction Reference lists a bunch of bits that are
8021 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8022 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8023 * Otherwise, we should fail with #UD. But most faulting conditions
8024 * have already been checked by hardware, prior to the VM-exit for
8025 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8026 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008027 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008028 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008029 kvm_queue_exception(vcpu, UD_VECTOR);
8030 return 1;
8031 }
8032
Felix Wilhelm727ba742018-06-11 09:43:44 +02008033 /* CPL=0 must be checked manually. */
8034 if (vmx_get_cpl(vcpu)) {
8035 kvm_queue_exception(vcpu, UD_VECTOR);
8036 return 1;
8037 }
8038
Abel Gordon145c28d2013-04-18 14:36:55 +03008039 if (vmx->nested.vmxon) {
8040 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008041 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008042 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008043
Haozhong Zhang3b840802016-06-22 14:59:54 +08008044 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008045 != VMXON_NEEDED_FEATURES) {
8046 kvm_inject_gp(vcpu, 0);
8047 return 1;
8048 }
8049
Radim Krčmářcbf71272017-05-19 15:48:51 +02008050 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008051 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008052
8053 /*
8054 * SDM 3: 24.11.5
8055 * The first 4 bytes of VMXON region contain the supported
8056 * VMCS revision identifier
8057 *
8058 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8059 * which replaces physical address width with 32
8060 */
8061 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8062 nested_vmx_failInvalid(vcpu);
8063 return kvm_skip_emulated_instruction(vcpu);
8064 }
8065
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008066 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8067 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008068 nested_vmx_failInvalid(vcpu);
8069 return kvm_skip_emulated_instruction(vcpu);
8070 }
8071 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8072 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008073 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008074 nested_vmx_failInvalid(vcpu);
8075 return kvm_skip_emulated_instruction(vcpu);
8076 }
8077 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008078 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008079
8080 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008081 ret = enter_vmx_operation(vcpu);
8082 if (ret)
8083 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008084
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008085 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008086 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008087}
8088
8089/*
8090 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8091 * for running VMX instructions (except VMXON, whose prerequisites are
8092 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008093 * Note that many of these exceptions have priority over VM exits, so they
8094 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008095 */
8096static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8097{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008098 if (vmx_get_cpl(vcpu)) {
8099 kvm_queue_exception(vcpu, UD_VECTOR);
8100 return 0;
8101 }
8102
Jim Mattson70f3aac2017-04-26 08:53:46 -07008103 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008104 kvm_queue_exception(vcpu, UD_VECTOR);
8105 return 0;
8106 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008107 return 1;
8108}
8109
David Matlack8ca44e82017-08-01 14:00:39 -07008110static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8111{
8112 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8113 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8114}
8115
Abel Gordone7953d72013-04-18 14:37:55 +03008116static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8117{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008118 if (vmx->nested.current_vmptr == -1ull)
8119 return;
8120
Abel Gordon012f83c2013-04-18 14:39:25 +03008121 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008122 /* copy to memory all shadowed fields in case
8123 they were modified */
8124 copy_shadow_to_vmcs12(vmx);
8125 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008126 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008127 }
Wincy Van705699a2015-02-03 23:58:17 +08008128 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008129
8130 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008131 kvm_vcpu_write_guest_page(&vmx->vcpu,
8132 vmx->nested.current_vmptr >> PAGE_SHIFT,
8133 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008134
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008135 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008136}
8137
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008138/*
8139 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8140 * just stops using VMX.
8141 */
8142static void free_nested(struct vcpu_vmx *vmx)
8143{
Wanpeng Lib7455822017-11-22 14:04:00 -08008144 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008145 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008146
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008147 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008148 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008149 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008150 vmx->nested.posted_intr_nv = -1;
8151 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008152 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008153 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008154 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8155 free_vmcs(vmx->vmcs01.shadow_vmcs);
8156 vmx->vmcs01.shadow_vmcs = NULL;
8157 }
David Matlack4f2777b2016-07-13 17:16:37 -07008158 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008159 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008160 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008161 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008162 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008163 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008164 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008165 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008166 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008167 }
Wincy Van705699a2015-02-03 23:58:17 +08008168 if (vmx->nested.pi_desc_page) {
8169 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008170 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008171 vmx->nested.pi_desc_page = NULL;
8172 vmx->nested.pi_desc = NULL;
8173 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008174
Jim Mattsonde3a0022017-11-27 17:22:25 -06008175 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008176}
8177
8178/* Emulate the VMXOFF instruction */
8179static int handle_vmoff(struct kvm_vcpu *vcpu)
8180{
8181 if (!nested_vmx_check_permission(vcpu))
8182 return 1;
8183 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008184 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008185 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008186}
8187
Nadav Har'El27d6c862011-05-25 23:06:59 +03008188/* Emulate the VMCLEAR instruction */
8189static int handle_vmclear(struct kvm_vcpu *vcpu)
8190{
8191 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008192 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008193 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008194
8195 if (!nested_vmx_check_permission(vcpu))
8196 return 1;
8197
Radim Krčmářcbf71272017-05-19 15:48:51 +02008198 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008199 return 1;
8200
Radim Krčmářcbf71272017-05-19 15:48:51 +02008201 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8202 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8203 return kvm_skip_emulated_instruction(vcpu);
8204 }
8205
8206 if (vmptr == vmx->nested.vmxon_ptr) {
8207 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8208 return kvm_skip_emulated_instruction(vcpu);
8209 }
8210
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008211 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008212 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008213
Jim Mattson587d7e722017-03-02 12:41:48 -08008214 kvm_vcpu_write_guest(vcpu,
8215 vmptr + offsetof(struct vmcs12, launch_state),
8216 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008217
Nadav Har'El27d6c862011-05-25 23:06:59 +03008218 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008219 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008220}
8221
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008222static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8223
8224/* Emulate the VMLAUNCH instruction */
8225static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8226{
8227 return nested_vmx_run(vcpu, true);
8228}
8229
8230/* Emulate the VMRESUME instruction */
8231static int handle_vmresume(struct kvm_vcpu *vcpu)
8232{
8233
8234 return nested_vmx_run(vcpu, false);
8235}
8236
Nadav Har'El49f705c2011-05-25 23:08:30 +03008237/*
8238 * Read a vmcs12 field. Since these can have varying lengths and we return
8239 * one type, we chose the biggest type (u64) and zero-extend the return value
8240 * to that size. Note that the caller, handle_vmread, might need to use only
8241 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8242 * 64-bit fields are to be returned).
8243 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008244static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8245 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008246{
8247 short offset = vmcs_field_to_offset(field);
8248 char *p;
8249
8250 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008251 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008252
8253 p = ((char *)(get_vmcs12(vcpu))) + offset;
8254
Jim Mattsond37f4262017-12-22 12:12:16 -08008255 switch (vmcs_field_width(field)) {
8256 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008257 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008258 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008259 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008260 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008261 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008262 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008263 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008265 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008266 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008267 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008268 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008269 WARN_ON(1);
8270 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008271 }
8272}
8273
Abel Gordon20b97fe2013-04-18 14:36:25 +03008274
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008275static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8276 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008277 short offset = vmcs_field_to_offset(field);
8278 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8279 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008280 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008281
Jim Mattsond37f4262017-12-22 12:12:16 -08008282 switch (vmcs_field_width(field)) {
8283 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008284 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008285 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008286 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008287 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008288 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008289 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008290 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008291 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008292 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008293 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008294 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008295 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008296 WARN_ON(1);
8297 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008298 }
8299
8300}
8301
Jim Mattsonf4160e42018-05-29 09:11:33 -07008302/*
8303 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8304 * they have been modified by the L1 guest. Note that the "read-only"
8305 * VM-exit information fields are actually writable if the vCPU is
8306 * configured to support "VMWRITE to any supported field in the VMCS."
8307 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008308static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8309{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008310 const u16 *fields[] = {
8311 shadow_read_write_fields,
8312 shadow_read_only_fields
8313 };
8314 const int max_fields[] = {
8315 max_shadow_read_write_fields,
8316 max_shadow_read_only_fields
8317 };
8318 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008319 unsigned long field;
8320 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008321 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008322
Jan Kiszka282da872014-10-08 18:05:39 +02008323 preempt_disable();
8324
Abel Gordon16f5b902013-04-18 14:38:25 +03008325 vmcs_load(shadow_vmcs);
8326
Jim Mattsonf4160e42018-05-29 09:11:33 -07008327 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8328 for (i = 0; i < max_fields[q]; i++) {
8329 field = fields[q][i];
8330 field_value = __vmcs_readl(field);
8331 vmcs12_write_any(&vmx->vcpu, field, field_value);
8332 }
8333 /*
8334 * Skip the VM-exit information fields if they are read-only.
8335 */
8336 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8337 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008338 }
8339
8340 vmcs_clear(shadow_vmcs);
8341 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008342
8343 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008344}
8345
Abel Gordonc3114422013-04-18 14:38:55 +03008346static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8347{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008348 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008349 shadow_read_write_fields,
8350 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008351 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008352 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008353 max_shadow_read_write_fields,
8354 max_shadow_read_only_fields
8355 };
8356 int i, q;
8357 unsigned long field;
8358 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008359 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008360
8361 vmcs_load(shadow_vmcs);
8362
Mathias Krausec2bae892013-06-26 20:36:21 +02008363 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008364 for (i = 0; i < max_fields[q]; i++) {
8365 field = fields[q][i];
8366 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008367 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008368 }
8369 }
8370
8371 vmcs_clear(shadow_vmcs);
8372 vmcs_load(vmx->loaded_vmcs->vmcs);
8373}
8374
Nadav Har'El49f705c2011-05-25 23:08:30 +03008375/*
8376 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8377 * used before) all generate the same failure when it is missing.
8378 */
8379static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8380{
8381 struct vcpu_vmx *vmx = to_vmx(vcpu);
8382 if (vmx->nested.current_vmptr == -1ull) {
8383 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008384 return 0;
8385 }
8386 return 1;
8387}
8388
8389static int handle_vmread(struct kvm_vcpu *vcpu)
8390{
8391 unsigned long field;
8392 u64 field_value;
8393 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8394 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8395 gva_t gva = 0;
8396
Kyle Hueyeb277562016-11-29 12:40:39 -08008397 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008398 return 1;
8399
Kyle Huey6affcbe2016-11-29 12:40:40 -08008400 if (!nested_vmx_check_vmcs12(vcpu))
8401 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008402
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008404 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008405 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008406 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008407 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008408 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008409 }
8410 /*
8411 * Now copy part of this value to register or memory, as requested.
8412 * Note that the number of bits actually copied is 32 or 64 depending
8413 * on the guest's mode (32 or 64 bit), not on the given field's length.
8414 */
8415 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008416 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008417 field_value);
8418 } else {
8419 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008420 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008422 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008423 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8424 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 }
8426
8427 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008428 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008429}
8430
8431
8432static int handle_vmwrite(struct kvm_vcpu *vcpu)
8433{
8434 unsigned long field;
8435 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008436 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008437 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8438 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008439
Nadav Har'El49f705c2011-05-25 23:08:30 +03008440 /* The value to write might be 32 or 64 bits, depending on L1's long
8441 * mode, and eventually we need to write that into a field of several
8442 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008443 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444 * bits into the vmcs12 field.
8445 */
8446 u64 field_value = 0;
8447 struct x86_exception e;
8448
Kyle Hueyeb277562016-11-29 12:40:39 -08008449 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 return 1;
8451
Kyle Huey6affcbe2016-11-29 12:40:40 -08008452 if (!nested_vmx_check_vmcs12(vcpu))
8453 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008454
Nadav Har'El49f705c2011-05-25 23:08:30 +03008455 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008456 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008457 (((vmx_instruction_info) >> 3) & 0xf));
8458 else {
8459 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008460 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008461 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008462 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8463 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008464 kvm_inject_page_fault(vcpu, &e);
8465 return 1;
8466 }
8467 }
8468
8469
Nadav Amit27e6fb52014-06-18 17:19:26 +03008470 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008471 /*
8472 * If the vCPU supports "VMWRITE to any supported field in the
8473 * VMCS," then the "read-only" fields are actually read/write.
8474 */
8475 if (vmcs_field_readonly(field) &&
8476 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008477 nested_vmx_failValid(vcpu,
8478 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008479 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008480 }
8481
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008482 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008483 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008484 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008485 }
8486
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008487 switch (field) {
8488#define SHADOW_FIELD_RW(x) case x:
8489#include "vmx_shadow_fields.h"
8490 /*
8491 * The fields that can be updated by L1 without a vmexit are
8492 * always updated in the vmcs02, the others go down the slow
8493 * path of prepare_vmcs02.
8494 */
8495 break;
8496 default:
8497 vmx->nested.dirty_vmcs12 = true;
8498 break;
8499 }
8500
Nadav Har'El49f705c2011-05-25 23:08:30 +03008501 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008502 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008503}
8504
Jim Mattsona8bc2842016-11-30 12:03:44 -08008505static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8506{
8507 vmx->nested.current_vmptr = vmptr;
8508 if (enable_shadow_vmcs) {
8509 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8510 SECONDARY_EXEC_SHADOW_VMCS);
8511 vmcs_write64(VMCS_LINK_POINTER,
8512 __pa(vmx->vmcs01.shadow_vmcs));
8513 vmx->nested.sync_shadow_vmcs = true;
8514 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008515 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008516}
8517
Nadav Har'El63846662011-05-25 23:07:29 +03008518/* Emulate the VMPTRLD instruction */
8519static int handle_vmptrld(struct kvm_vcpu *vcpu)
8520{
8521 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008522 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008523
8524 if (!nested_vmx_check_permission(vcpu))
8525 return 1;
8526
Radim Krčmářcbf71272017-05-19 15:48:51 +02008527 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008528 return 1;
8529
Radim Krčmářcbf71272017-05-19 15:48:51 +02008530 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8531 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8532 return kvm_skip_emulated_instruction(vcpu);
8533 }
8534
8535 if (vmptr == vmx->nested.vmxon_ptr) {
8536 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8537 return kvm_skip_emulated_instruction(vcpu);
8538 }
8539
Nadav Har'El63846662011-05-25 23:07:29 +03008540 if (vmx->nested.current_vmptr != vmptr) {
8541 struct vmcs12 *new_vmcs12;
8542 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008543 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8544 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008545 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008546 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008547 }
8548 new_vmcs12 = kmap(page);
8549 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8550 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008551 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008552 nested_vmx_failValid(vcpu,
8553 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008554 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008555 }
Nadav Har'El63846662011-05-25 23:07:29 +03008556
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008557 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008558 /*
8559 * Load VMCS12 from guest memory since it is not already
8560 * cached.
8561 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008562 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8563 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008564 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008565
Jim Mattsona8bc2842016-11-30 12:03:44 -08008566 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008567 }
8568
8569 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008570 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008571}
8572
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008573/* Emulate the VMPTRST instruction */
8574static int handle_vmptrst(struct kvm_vcpu *vcpu)
8575{
8576 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8577 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8578 gva_t vmcs_gva;
8579 struct x86_exception e;
8580
8581 if (!nested_vmx_check_permission(vcpu))
8582 return 1;
8583
8584 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008585 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008586 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008587 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008588 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8589 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8590 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008591 kvm_inject_page_fault(vcpu, &e);
8592 return 1;
8593 }
8594 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008595 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008596}
8597
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008598/* Emulate the INVEPT instruction */
8599static int handle_invept(struct kvm_vcpu *vcpu)
8600{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008601 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008602 u32 vmx_instruction_info, types;
8603 unsigned long type;
8604 gva_t gva;
8605 struct x86_exception e;
8606 struct {
8607 u64 eptp, gpa;
8608 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008609
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008610 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008611 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008612 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008613 kvm_queue_exception(vcpu, UD_VECTOR);
8614 return 1;
8615 }
8616
8617 if (!nested_vmx_check_permission(vcpu))
8618 return 1;
8619
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008620 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008621 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008622
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008623 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008624
Jim Mattson85c856b2016-10-26 08:38:38 -07008625 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008626 nested_vmx_failValid(vcpu,
8627 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008628 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008629 }
8630
8631 /* According to the Intel VMX instruction reference, the memory
8632 * operand is read even if it isn't needed (e.g., for type==global)
8633 */
8634 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008635 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008636 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008637 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 kvm_inject_page_fault(vcpu, &e);
8639 return 1;
8640 }
8641
8642 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008643 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008644 /*
8645 * TODO: track mappings and invalidate
8646 * single context requests appropriately
8647 */
8648 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008649 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008650 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008651 nested_vmx_succeed(vcpu);
8652 break;
8653 default:
8654 BUG_ON(1);
8655 break;
8656 }
8657
Kyle Huey6affcbe2016-11-29 12:40:40 -08008658 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008659}
8660
Petr Matouseka642fc32014-09-23 20:22:30 +02008661static int handle_invvpid(struct kvm_vcpu *vcpu)
8662{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008663 struct vcpu_vmx *vmx = to_vmx(vcpu);
8664 u32 vmx_instruction_info;
8665 unsigned long type, types;
8666 gva_t gva;
8667 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008668 struct {
8669 u64 vpid;
8670 u64 gla;
8671 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008672
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008673 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008674 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008675 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008676 kvm_queue_exception(vcpu, UD_VECTOR);
8677 return 1;
8678 }
8679
8680 if (!nested_vmx_check_permission(vcpu))
8681 return 1;
8682
8683 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8684 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8685
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008686 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008687 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008688
Jim Mattson85c856b2016-10-26 08:38:38 -07008689 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008690 nested_vmx_failValid(vcpu,
8691 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008692 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008693 }
8694
8695 /* according to the intel vmx instruction reference, the memory
8696 * operand is read even if it isn't needed (e.g., for type==global)
8697 */
8698 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8699 vmx_instruction_info, false, &gva))
8700 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008701 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008702 kvm_inject_page_fault(vcpu, &e);
8703 return 1;
8704 }
Jim Mattson40352602017-06-28 09:37:37 -07008705 if (operand.vpid >> 16) {
8706 nested_vmx_failValid(vcpu,
8707 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8708 return kvm_skip_emulated_instruction(vcpu);
8709 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008710
8711 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008712 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008713 if (!operand.vpid ||
8714 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008715 nested_vmx_failValid(vcpu,
8716 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8717 return kvm_skip_emulated_instruction(vcpu);
8718 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008719 if (cpu_has_vmx_invvpid_individual_addr() &&
8720 vmx->nested.vpid02) {
8721 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8722 vmx->nested.vpid02, operand.gla);
8723 } else
8724 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8725 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008726 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008727 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008728 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008729 nested_vmx_failValid(vcpu,
8730 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008731 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008732 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008733 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008734 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008735 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008736 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008737 break;
8738 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008739 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008740 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008741 }
8742
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008743 nested_vmx_succeed(vcpu);
8744
Kyle Huey6affcbe2016-11-29 12:40:40 -08008745 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008746}
8747
Kai Huang843e4332015-01-28 10:54:28 +08008748static int handle_pml_full(struct kvm_vcpu *vcpu)
8749{
8750 unsigned long exit_qualification;
8751
8752 trace_kvm_pml_full(vcpu->vcpu_id);
8753
8754 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8755
8756 /*
8757 * PML buffer FULL happened while executing iret from NMI,
8758 * "blocked by NMI" bit has to be set before next VM entry.
8759 */
8760 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008761 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008762 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8763 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8764 GUEST_INTR_STATE_NMI);
8765
8766 /*
8767 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8768 * here.., and there's no userspace involvement needed for PML.
8769 */
8770 return 1;
8771}
8772
Yunhong Jiang64672c92016-06-13 14:19:59 -07008773static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8774{
8775 kvm_lapic_expired_hv_timer(vcpu);
8776 return 1;
8777}
8778
Bandan Das41ab9372017-08-03 15:54:43 -04008779static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8780{
8781 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008782 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8783
8784 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008785 switch (address & VMX_EPTP_MT_MASK) {
8786 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008787 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008788 return false;
8789 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008790 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008791 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008792 return false;
8793 break;
8794 default:
8795 return false;
8796 }
8797
David Hildenbrandbb97a012017-08-10 23:15:28 +02008798 /* only 4 levels page-walk length are valid */
8799 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008800 return false;
8801
8802 /* Reserved bits should not be set */
8803 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8804 return false;
8805
8806 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008807 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008808 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008809 return false;
8810 }
8811
8812 return true;
8813}
8814
8815static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8816 struct vmcs12 *vmcs12)
8817{
8818 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8819 u64 address;
8820 bool accessed_dirty;
8821 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8822
8823 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8824 !nested_cpu_has_ept(vmcs12))
8825 return 1;
8826
8827 if (index >= VMFUNC_EPTP_ENTRIES)
8828 return 1;
8829
8830
8831 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8832 &address, index * 8, 8))
8833 return 1;
8834
David Hildenbrandbb97a012017-08-10 23:15:28 +02008835 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008836
8837 /*
8838 * If the (L2) guest does a vmfunc to the currently
8839 * active ept pointer, we don't have to do anything else
8840 */
8841 if (vmcs12->ept_pointer != address) {
8842 if (!valid_ept_address(vcpu, address))
8843 return 1;
8844
8845 kvm_mmu_unload(vcpu);
8846 mmu->ept_ad = accessed_dirty;
8847 mmu->base_role.ad_disabled = !accessed_dirty;
8848 vmcs12->ept_pointer = address;
8849 /*
8850 * TODO: Check what's the correct approach in case
8851 * mmu reload fails. Currently, we just let the next
8852 * reload potentially fail
8853 */
8854 kvm_mmu_reload(vcpu);
8855 }
8856
8857 return 0;
8858}
8859
Bandan Das2a499e42017-08-03 15:54:41 -04008860static int handle_vmfunc(struct kvm_vcpu *vcpu)
8861{
Bandan Das27c42a12017-08-03 15:54:42 -04008862 struct vcpu_vmx *vmx = to_vmx(vcpu);
8863 struct vmcs12 *vmcs12;
8864 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8865
8866 /*
8867 * VMFUNC is only supported for nested guests, but we always enable the
8868 * secondary control for simplicity; for non-nested mode, fake that we
8869 * didn't by injecting #UD.
8870 */
8871 if (!is_guest_mode(vcpu)) {
8872 kvm_queue_exception(vcpu, UD_VECTOR);
8873 return 1;
8874 }
8875
8876 vmcs12 = get_vmcs12(vcpu);
8877 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8878 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008879
8880 switch (function) {
8881 case 0:
8882 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8883 goto fail;
8884 break;
8885 default:
8886 goto fail;
8887 }
8888 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008889
8890fail:
8891 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8892 vmcs_read32(VM_EXIT_INTR_INFO),
8893 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008894 return 1;
8895}
8896
Nadav Har'El0140cae2011-05-25 23:06:28 +03008897/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008898 * The exit handlers return 1 if the exit was handled fully and guest execution
8899 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8900 * to be done to userspace and return 0.
8901 */
Mathias Krause772e0312012-08-30 01:30:19 +02008902static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008903 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8904 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008905 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008906 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008907 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008908 [EXIT_REASON_CR_ACCESS] = handle_cr,
8909 [EXIT_REASON_DR_ACCESS] = handle_dr,
8910 [EXIT_REASON_CPUID] = handle_cpuid,
8911 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8912 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8913 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8914 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008915 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008916 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008917 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008918 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008919 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008920 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008921 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008922 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008923 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008924 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008925 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008926 [EXIT_REASON_VMOFF] = handle_vmoff,
8927 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008928 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8929 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008930 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008931 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008932 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008933 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008934 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008935 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008936 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8937 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008938 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8939 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008940 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008941 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008942 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008943 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008944 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008945 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008946 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008947 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008948 [EXIT_REASON_XSAVES] = handle_xsaves,
8949 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008950 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008951 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008952 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008953};
8954
8955static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008956 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008957
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008958static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8959 struct vmcs12 *vmcs12)
8960{
8961 unsigned long exit_qualification;
8962 gpa_t bitmap, last_bitmap;
8963 unsigned int port;
8964 int size;
8965 u8 b;
8966
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008967 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008968 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008969
8970 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8971
8972 port = exit_qualification >> 16;
8973 size = (exit_qualification & 7) + 1;
8974
8975 last_bitmap = (gpa_t)-1;
8976 b = -1;
8977
8978 while (size > 0) {
8979 if (port < 0x8000)
8980 bitmap = vmcs12->io_bitmap_a;
8981 else if (port < 0x10000)
8982 bitmap = vmcs12->io_bitmap_b;
8983 else
Joe Perches1d804d02015-03-30 16:46:09 -07008984 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008985 bitmap += (port & 0x7fff) / 8;
8986
8987 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008988 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008989 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008990 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008991 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008992
8993 port++;
8994 size--;
8995 last_bitmap = bitmap;
8996 }
8997
Joe Perches1d804d02015-03-30 16:46:09 -07008998 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008999}
9000
Nadav Har'El644d7112011-05-25 23:12:35 +03009001/*
9002 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9003 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9004 * disinterest in the current event (read or write a specific MSR) by using an
9005 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9006 */
9007static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9008 struct vmcs12 *vmcs12, u32 exit_reason)
9009{
9010 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9011 gpa_t bitmap;
9012
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009013 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009014 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009015
9016 /*
9017 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9018 * for the four combinations of read/write and low/high MSR numbers.
9019 * First we need to figure out which of the four to use:
9020 */
9021 bitmap = vmcs12->msr_bitmap;
9022 if (exit_reason == EXIT_REASON_MSR_WRITE)
9023 bitmap += 2048;
9024 if (msr_index >= 0xc0000000) {
9025 msr_index -= 0xc0000000;
9026 bitmap += 1024;
9027 }
9028
9029 /* Then read the msr_index'th bit from this bitmap: */
9030 if (msr_index < 1024*8) {
9031 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009032 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009033 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009034 return 1 & (b >> (msr_index & 7));
9035 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009036 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009037}
9038
9039/*
9040 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9041 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9042 * intercept (via guest_host_mask etc.) the current event.
9043 */
9044static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9045 struct vmcs12 *vmcs12)
9046{
9047 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9048 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009049 int reg;
9050 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009051
9052 switch ((exit_qualification >> 4) & 3) {
9053 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009054 reg = (exit_qualification >> 8) & 15;
9055 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009056 switch (cr) {
9057 case 0:
9058 if (vmcs12->cr0_guest_host_mask &
9059 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009060 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 break;
9062 case 3:
9063 if ((vmcs12->cr3_target_count >= 1 &&
9064 vmcs12->cr3_target_value0 == val) ||
9065 (vmcs12->cr3_target_count >= 2 &&
9066 vmcs12->cr3_target_value1 == val) ||
9067 (vmcs12->cr3_target_count >= 3 &&
9068 vmcs12->cr3_target_value2 == val) ||
9069 (vmcs12->cr3_target_count >= 4 &&
9070 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009071 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009072 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009073 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009074 break;
9075 case 4:
9076 if (vmcs12->cr4_guest_host_mask &
9077 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009078 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009079 break;
9080 case 8:
9081 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009082 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009083 break;
9084 }
9085 break;
9086 case 2: /* clts */
9087 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9088 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009089 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009090 break;
9091 case 1: /* mov from cr */
9092 switch (cr) {
9093 case 3:
9094 if (vmcs12->cpu_based_vm_exec_control &
9095 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009096 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009097 break;
9098 case 8:
9099 if (vmcs12->cpu_based_vm_exec_control &
9100 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009101 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009102 break;
9103 }
9104 break;
9105 case 3: /* lmsw */
9106 /*
9107 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9108 * cr0. Other attempted changes are ignored, with no exit.
9109 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009110 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009111 if (vmcs12->cr0_guest_host_mask & 0xe &
9112 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009113 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009114 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9115 !(vmcs12->cr0_read_shadow & 0x1) &&
9116 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009117 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009118 break;
9119 }
Joe Perches1d804d02015-03-30 16:46:09 -07009120 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009121}
9122
9123/*
9124 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9125 * should handle it ourselves in L0 (and then continue L2). Only call this
9126 * when in is_guest_mode (L2).
9127 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009128static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009129{
Nadav Har'El644d7112011-05-25 23:12:35 +03009130 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9131 struct vcpu_vmx *vmx = to_vmx(vcpu);
9132 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9133
Jim Mattson4f350c62017-09-14 16:31:44 -07009134 if (vmx->nested.nested_run_pending)
9135 return false;
9136
9137 if (unlikely(vmx->fail)) {
9138 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9139 vmcs_read32(VM_INSTRUCTION_ERROR));
9140 return true;
9141 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009142
David Matlackc9f04402017-08-01 14:00:40 -07009143 /*
9144 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009145 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9146 * Page). The CPU may write to these pages via their host
9147 * physical address while L2 is running, bypassing any
9148 * address-translation-based dirty tracking (e.g. EPT write
9149 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009150 *
9151 * Mark them dirty on every exit from L2 to prevent them from
9152 * getting out of sync with dirty tracking.
9153 */
9154 nested_mark_vmcs12_pages_dirty(vcpu);
9155
Jim Mattson4f350c62017-09-14 16:31:44 -07009156 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9157 vmcs_readl(EXIT_QUALIFICATION),
9158 vmx->idt_vectoring_info,
9159 intr_info,
9160 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9161 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009162
9163 switch (exit_reason) {
9164 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009165 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009166 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009167 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009168 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009169 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009170 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009171 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009172 else if (is_debug(intr_info) &&
9173 vcpu->guest_debug &
9174 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9175 return false;
9176 else if (is_breakpoint(intr_info) &&
9177 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9178 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009179 return vmcs12->exception_bitmap &
9180 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9181 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009182 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009183 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009184 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009185 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009186 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009188 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009189 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009190 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009191 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 case EXIT_REASON_HLT:
9194 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9195 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009196 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009197 case EXIT_REASON_INVLPG:
9198 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9199 case EXIT_REASON_RDPMC:
9200 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009201 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009202 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009203 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009204 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009205 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009206 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9207 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9208 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9209 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9210 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9211 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009212 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009213 /*
9214 * VMX instructions trap unconditionally. This allows L1 to
9215 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9216 */
Joe Perches1d804d02015-03-30 16:46:09 -07009217 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009218 case EXIT_REASON_CR_ACCESS:
9219 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9220 case EXIT_REASON_DR_ACCESS:
9221 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9222 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009223 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009224 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9225 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009226 case EXIT_REASON_MSR_READ:
9227 case EXIT_REASON_MSR_WRITE:
9228 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9229 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009230 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009231 case EXIT_REASON_MWAIT_INSTRUCTION:
9232 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009233 case EXIT_REASON_MONITOR_TRAP_FLAG:
9234 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009235 case EXIT_REASON_MONITOR_INSTRUCTION:
9236 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9237 case EXIT_REASON_PAUSE_INSTRUCTION:
9238 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9239 nested_cpu_has2(vmcs12,
9240 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9241 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009242 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009243 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009244 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009245 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009246 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009247 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009248 /*
9249 * The controls for "virtualize APIC accesses," "APIC-
9250 * register virtualization," and "virtual-interrupt
9251 * delivery" only come from vmcs12.
9252 */
Joe Perches1d804d02015-03-30 16:46:09 -07009253 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009254 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009255 /*
9256 * L0 always deals with the EPT violation. If nested EPT is
9257 * used, and the nested mmu code discovers that the address is
9258 * missing in the guest EPT table (EPT12), the EPT violation
9259 * will be injected with nested_ept_inject_page_fault()
9260 */
Joe Perches1d804d02015-03-30 16:46:09 -07009261 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009262 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009263 /*
9264 * L2 never uses directly L1's EPT, but rather L0's own EPT
9265 * table (shadow on EPT) or a merged EPT table that L0 built
9266 * (EPT on EPT). So any problems with the structure of the
9267 * table is L0's fault.
9268 */
Joe Perches1d804d02015-03-30 16:46:09 -07009269 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009270 case EXIT_REASON_INVPCID:
9271 return
9272 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9273 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009274 case EXIT_REASON_WBINVD:
9275 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9276 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009277 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009278 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9279 /*
9280 * This should never happen, since it is not possible to
9281 * set XSS to a non-zero value---neither in L1 nor in L2.
9282 * If if it were, XSS would have to be checked against
9283 * the XSS exit bitmap in vmcs12.
9284 */
9285 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009286 case EXIT_REASON_PREEMPTION_TIMER:
9287 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009288 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009289 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009290 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009291 case EXIT_REASON_VMFUNC:
9292 /* VM functions are emulated through L2->L0 vmexits. */
9293 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009294 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009295 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009296 }
9297}
9298
Paolo Bonzini7313c692017-07-27 10:31:25 +02009299static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9300{
9301 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9302
9303 /*
9304 * At this point, the exit interruption info in exit_intr_info
9305 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9306 * we need to query the in-kernel LAPIC.
9307 */
9308 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9309 if ((exit_intr_info &
9310 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9311 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9312 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9313 vmcs12->vm_exit_intr_error_code =
9314 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9315 }
9316
9317 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9318 vmcs_readl(EXIT_QUALIFICATION));
9319 return 1;
9320}
9321
Avi Kivity586f9602010-11-18 13:09:54 +02009322static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9323{
9324 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9325 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9326}
9327
Kai Huanga3eaa862015-11-04 13:46:05 +08009328static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009329{
Kai Huanga3eaa862015-11-04 13:46:05 +08009330 if (vmx->pml_pg) {
9331 __free_page(vmx->pml_pg);
9332 vmx->pml_pg = NULL;
9333 }
Kai Huang843e4332015-01-28 10:54:28 +08009334}
9335
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009336static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009337{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009338 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009339 u64 *pml_buf;
9340 u16 pml_idx;
9341
9342 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9343
9344 /* Do nothing if PML buffer is empty */
9345 if (pml_idx == (PML_ENTITY_NUM - 1))
9346 return;
9347
9348 /* PML index always points to next available PML buffer entity */
9349 if (pml_idx >= PML_ENTITY_NUM)
9350 pml_idx = 0;
9351 else
9352 pml_idx++;
9353
9354 pml_buf = page_address(vmx->pml_pg);
9355 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9356 u64 gpa;
9357
9358 gpa = pml_buf[pml_idx];
9359 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009360 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009361 }
9362
9363 /* reset PML index */
9364 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9365}
9366
9367/*
9368 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9369 * Called before reporting dirty_bitmap to userspace.
9370 */
9371static void kvm_flush_pml_buffers(struct kvm *kvm)
9372{
9373 int i;
9374 struct kvm_vcpu *vcpu;
9375 /*
9376 * We only need to kick vcpu out of guest mode here, as PML buffer
9377 * is flushed at beginning of all VMEXITs, and it's obvious that only
9378 * vcpus running in guest are possible to have unflushed GPAs in PML
9379 * buffer.
9380 */
9381 kvm_for_each_vcpu(i, vcpu, kvm)
9382 kvm_vcpu_kick(vcpu);
9383}
9384
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009385static void vmx_dump_sel(char *name, uint32_t sel)
9386{
9387 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009388 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009389 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9390 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9391 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9392}
9393
9394static void vmx_dump_dtsel(char *name, uint32_t limit)
9395{
9396 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9397 name, vmcs_read32(limit),
9398 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9399}
9400
9401static void dump_vmcs(void)
9402{
9403 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9404 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9405 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9406 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9407 u32 secondary_exec_control = 0;
9408 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009409 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009410 int i, n;
9411
9412 if (cpu_has_secondary_exec_ctrls())
9413 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9414
9415 pr_err("*** Guest State ***\n");
9416 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9417 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9418 vmcs_readl(CR0_GUEST_HOST_MASK));
9419 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9420 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9421 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9422 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9423 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9424 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009425 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9426 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9427 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9428 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009429 }
9430 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9431 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9432 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9433 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9434 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9435 vmcs_readl(GUEST_SYSENTER_ESP),
9436 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9437 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9438 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9439 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9440 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9441 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9442 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9443 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9444 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9445 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9446 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9447 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9448 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009449 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9450 efer, vmcs_read64(GUEST_IA32_PAT));
9451 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9452 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009453 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009454 if (cpu_has_load_perf_global_ctrl &&
9455 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009456 pr_err("PerfGlobCtl = 0x%016llx\n",
9457 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009458 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009459 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009460 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9461 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9462 vmcs_read32(GUEST_ACTIVITY_STATE));
9463 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9464 pr_err("InterruptStatus = %04x\n",
9465 vmcs_read16(GUEST_INTR_STATUS));
9466
9467 pr_err("*** Host State ***\n");
9468 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9469 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9470 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9471 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9472 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9473 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9474 vmcs_read16(HOST_TR_SELECTOR));
9475 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9476 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9477 vmcs_readl(HOST_TR_BASE));
9478 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9479 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9480 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9481 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9482 vmcs_readl(HOST_CR4));
9483 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9484 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9485 vmcs_read32(HOST_IA32_SYSENTER_CS),
9486 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9487 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009488 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9489 vmcs_read64(HOST_IA32_EFER),
9490 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009491 if (cpu_has_load_perf_global_ctrl &&
9492 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009493 pr_err("PerfGlobCtl = 0x%016llx\n",
9494 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009495
9496 pr_err("*** Control State ***\n");
9497 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9498 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9499 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9500 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9501 vmcs_read32(EXCEPTION_BITMAP),
9502 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9503 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9504 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9505 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9506 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9507 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9508 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9509 vmcs_read32(VM_EXIT_INTR_INFO),
9510 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9511 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9512 pr_err(" reason=%08x qualification=%016lx\n",
9513 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9514 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9515 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9516 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009517 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009518 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009519 pr_err("TSC Multiplier = 0x%016llx\n",
9520 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009521 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9522 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9523 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9524 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9525 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009526 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009527 n = vmcs_read32(CR3_TARGET_COUNT);
9528 for (i = 0; i + 1 < n; i += 4)
9529 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9530 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9531 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9532 if (i < n)
9533 pr_err("CR3 target%u=%016lx\n",
9534 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9535 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9536 pr_err("PLE Gap=%08x Window=%08x\n",
9537 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9538 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9539 pr_err("Virtual processor ID = 0x%04x\n",
9540 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9541}
9542
Avi Kivity6aa8b732006-12-10 02:21:36 -08009543/*
9544 * The guest has exited. See if we can fix it or if we need userspace
9545 * assistance.
9546 */
Avi Kivity851ba692009-08-24 11:10:17 +03009547static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009548{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009549 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009550 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009551 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009552
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009553 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9554
Kai Huang843e4332015-01-28 10:54:28 +08009555 /*
9556 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9557 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9558 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9559 * mode as if vcpus is in root mode, the PML buffer must has been
9560 * flushed already.
9561 */
9562 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009563 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009564
Mohammed Gamal80ced182009-09-01 12:48:18 +02009565 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009566 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009567 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009568
Paolo Bonzini7313c692017-07-27 10:31:25 +02009569 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9570 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009571
Mohammed Gamal51207022010-05-31 22:40:54 +03009572 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009573 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009574 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9575 vcpu->run->fail_entry.hardware_entry_failure_reason
9576 = exit_reason;
9577 return 0;
9578 }
9579
Avi Kivity29bd8a72007-09-10 17:27:03 +03009580 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009581 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9582 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009583 = vmcs_read32(VM_INSTRUCTION_ERROR);
9584 return 0;
9585 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009586
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009587 /*
9588 * Note:
9589 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9590 * delivery event since it indicates guest is accessing MMIO.
9591 * The vm-exit can be triggered again after return to guest that
9592 * will cause infinite loop.
9593 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009594 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009595 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009596 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009597 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009598 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9599 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9600 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009601 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009602 vcpu->run->internal.data[0] = vectoring_info;
9603 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009604 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9605 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9606 vcpu->run->internal.ndata++;
9607 vcpu->run->internal.data[3] =
9608 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9609 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009610 return 0;
9611 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009612
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009613 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009614 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9615 if (vmx_interrupt_allowed(vcpu)) {
9616 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9617 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9618 vcpu->arch.nmi_pending) {
9619 /*
9620 * This CPU don't support us in finding the end of an
9621 * NMI-blocked window if the guest runs with IRQs
9622 * disabled. So we pull the trigger after 1 s of
9623 * futile waiting, but inform the user about this.
9624 */
9625 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9626 "state on VCPU %d after 1 s timeout\n",
9627 __func__, vcpu->vcpu_id);
9628 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9629 }
9630 }
9631
Avi Kivity6aa8b732006-12-10 02:21:36 -08009632 if (exit_reason < kvm_vmx_max_exit_handlers
9633 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009634 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009635 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009636 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9637 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009638 kvm_queue_exception(vcpu, UD_VECTOR);
9639 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009640 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009641}
9642
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009643/*
9644 * Software based L1D cache flush which is used when microcode providing
9645 * the cache control MSR is not loaded.
9646 *
9647 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9648 * flush it is required to read in 64 KiB because the replacement algorithm
9649 * is not exactly LRU. This could be sized at runtime via topology
9650 * information but as all relevant affected CPUs have 32KiB L1D cache size
9651 * there is no point in doing so.
9652 */
9653#define L1D_CACHE_ORDER 4
9654static void *vmx_l1d_flush_pages;
9655
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009656static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009657{
9658 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009659
9660 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009661 * This code is only executed when the the flush mode is 'cond' or
9662 * 'always'
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009663 *
Thomas Gleixner2f055942018-07-13 16:23:17 +02009664 * If 'flush always', keep the flush bit set, otherwise clear
9665 * it. The flush bit gets set again either from vcpu_run() or from
9666 * one of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009667 */
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02009668 if (static_branch_unlikely(&vmx_l1d_flush_always))
9669 vcpu->arch.l1tf_flush_l1d = true;
9670 else
9671 vcpu->arch.l1tf_flush_l1d = false;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009672
9673 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009674
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009675 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9676 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9677 return;
9678 }
9679
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009680 asm volatile(
9681 /* First ensure the pages are in the TLB */
9682 "xorl %%eax, %%eax\n"
9683 ".Lpopulate_tlb:\n\t"
9684 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9685 "addl $4096, %%eax\n\t"
9686 "cmpl %%eax, %[size]\n\t"
9687 "jne .Lpopulate_tlb\n\t"
9688 "xorl %%eax, %%eax\n\t"
9689 "cpuid\n\t"
9690 /* Now fill the cache */
9691 "xorl %%eax, %%eax\n"
9692 ".Lfill_cache:\n"
9693 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9694 "addl $64, %%eax\n\t"
9695 "cmpl %%eax, %[size]\n\t"
9696 "jne .Lfill_cache\n\t"
9697 "lfence\n"
9698 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9699 [size] "r" (size)
9700 : "eax", "ebx", "ecx", "edx");
9701}
9702
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009703static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009704{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009705 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9706
9707 if (is_guest_mode(vcpu) &&
9708 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9709 return;
9710
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009711 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009712 vmcs_write32(TPR_THRESHOLD, 0);
9713 return;
9714 }
9715
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009716 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009717}
9718
Jim Mattson8d860bb2018-05-09 16:56:05 -04009719static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009720{
9721 u32 sec_exec_control;
9722
Jim Mattson8d860bb2018-05-09 16:56:05 -04009723 if (!lapic_in_kernel(vcpu))
9724 return;
9725
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009726 /* Postpone execution until vmcs01 is the current VMCS. */
9727 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009728 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009729 return;
9730 }
9731
Paolo Bonzini35754c92015-07-29 12:05:37 +02009732 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009733 return;
9734
9735 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009736 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9737 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009738
Jim Mattson8d860bb2018-05-09 16:56:05 -04009739 switch (kvm_get_apic_mode(vcpu)) {
9740 case LAPIC_MODE_INVALID:
9741 WARN_ONCE(true, "Invalid local APIC state");
9742 case LAPIC_MODE_DISABLED:
9743 break;
9744 case LAPIC_MODE_XAPIC:
9745 if (flexpriority_enabled) {
9746 sec_exec_control |=
9747 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9748 vmx_flush_tlb(vcpu, true);
9749 }
9750 break;
9751 case LAPIC_MODE_X2APIC:
9752 if (cpu_has_vmx_virtualize_x2apic_mode())
9753 sec_exec_control |=
9754 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9755 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009756 }
9757 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9758
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009759 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009760}
9761
Tang Chen38b99172014-09-24 15:57:54 +08009762static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9763{
Jim Mattsonab5df312018-05-09 17:02:03 -04009764 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009765 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009766 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009767 }
Tang Chen38b99172014-09-24 15:57:54 +08009768}
9769
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009770static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009771{
9772 u16 status;
9773 u8 old;
9774
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009775 if (max_isr == -1)
9776 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009777
9778 status = vmcs_read16(GUEST_INTR_STATUS);
9779 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009780 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009781 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009782 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009783 vmcs_write16(GUEST_INTR_STATUS, status);
9784 }
9785}
9786
9787static void vmx_set_rvi(int vector)
9788{
9789 u16 status;
9790 u8 old;
9791
Wei Wang4114c272014-11-05 10:53:43 +08009792 if (vector == -1)
9793 vector = 0;
9794
Yang Zhangc7c9c562013-01-25 10:18:51 +08009795 status = vmcs_read16(GUEST_INTR_STATUS);
9796 old = (u8)status & 0xff;
9797 if ((u8)vector != old) {
9798 status &= ~0xff;
9799 status |= (u8)vector;
9800 vmcs_write16(GUEST_INTR_STATUS, status);
9801 }
9802}
9803
9804static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9805{
Liran Alon851c1a182017-12-24 18:12:56 +02009806 /*
9807 * When running L2, updating RVI is only relevant when
9808 * vmcs12 virtual-interrupt-delivery enabled.
9809 * However, it can be enabled only when L1 also
9810 * intercepts external-interrupts and in that case
9811 * we should not update vmcs02 RVI but instead intercept
9812 * interrupt. Therefore, do nothing when running L2.
9813 */
9814 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009815 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009816}
9817
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009818static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009819{
9820 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009821 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009822 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009823
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009824 WARN_ON(!vcpu->arch.apicv_active);
9825 if (pi_test_on(&vmx->pi_desc)) {
9826 pi_clear_on(&vmx->pi_desc);
9827 /*
9828 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9829 * But on x86 this is just a compiler barrier anyway.
9830 */
9831 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009832 max_irr_updated =
9833 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9834
9835 /*
9836 * If we are running L2 and L1 has a new pending interrupt
9837 * which can be injected, we should re-evaluate
9838 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009839 * If L1 intercepts external-interrupts, we should
9840 * exit from L2 to L1. Otherwise, interrupt should be
9841 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009842 */
Liran Alon851c1a182017-12-24 18:12:56 +02009843 if (is_guest_mode(vcpu) && max_irr_updated) {
9844 if (nested_exit_on_intr(vcpu))
9845 kvm_vcpu_exiting_guest_mode(vcpu);
9846 else
9847 kvm_make_request(KVM_REQ_EVENT, vcpu);
9848 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009849 } else {
9850 max_irr = kvm_lapic_find_highest_irr(vcpu);
9851 }
9852 vmx_hwapic_irr_update(vcpu, max_irr);
9853 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009854}
9855
Andrey Smetanin63086302015-11-10 15:36:32 +03009856static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009857{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009858 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009859 return;
9860
Yang Zhangc7c9c562013-01-25 10:18:51 +08009861 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9862 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9863 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9864 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9865}
9866
Paolo Bonzini967235d2016-12-19 14:03:45 +01009867static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9868{
9869 struct vcpu_vmx *vmx = to_vmx(vcpu);
9870
9871 pi_clear_on(&vmx->pi_desc);
9872 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9873}
9874
Avi Kivity51aa01d2010-07-20 14:31:20 +03009875static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009876{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009877 u32 exit_intr_info = 0;
9878 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009879
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009880 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9881 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009882 return;
9883
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009884 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9885 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9886 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009887
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009888 /* if exit due to PF check for async PF */
9889 if (is_page_fault(exit_intr_info))
9890 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9891
Andi Kleena0861c02009-06-08 17:37:09 +08009892 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009893 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9894 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009895 kvm_machine_check();
9896
Gleb Natapov20f65982009-05-11 13:35:55 +03009897 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009898 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009899 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009900 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009901 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009902 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009903}
Gleb Natapov20f65982009-05-11 13:35:55 +03009904
Yang Zhanga547c6d2013-04-11 19:25:10 +08009905static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9906{
9907 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9908
Yang Zhanga547c6d2013-04-11 19:25:10 +08009909 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9910 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9911 unsigned int vector;
9912 unsigned long entry;
9913 gate_desc *desc;
9914 struct vcpu_vmx *vmx = to_vmx(vcpu);
9915#ifdef CONFIG_X86_64
9916 unsigned long tmp;
9917#endif
9918
9919 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9920 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009921 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009922 asm volatile(
9923#ifdef CONFIG_X86_64
9924 "mov %%" _ASM_SP ", %[sp]\n\t"
9925 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9926 "push $%c[ss]\n\t"
9927 "push %[sp]\n\t"
9928#endif
9929 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009930 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009931 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009932 :
9933#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009934 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009935#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009936 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009937 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009938 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009939 [ss]"i"(__KERNEL_DS),
9940 [cs]"i"(__KERNEL_CS)
9941 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009942 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009943 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009944}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009945STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009946
Tom Lendackybc226f02018-05-10 22:06:39 +02009947static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009948{
Tom Lendackybc226f02018-05-10 22:06:39 +02009949 switch (index) {
9950 case MSR_IA32_SMBASE:
9951 /*
9952 * We cannot do SMM unless we can run the guest in big
9953 * real mode.
9954 */
9955 return enable_unrestricted_guest || emulate_invalid_guest_state;
9956 case MSR_AMD64_VIRT_SPEC_CTRL:
9957 /* This is AMD only. */
9958 return false;
9959 default:
9960 return true;
9961 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009962}
9963
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009964static bool vmx_mpx_supported(void)
9965{
9966 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9967 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9968}
9969
Wanpeng Li55412b22014-12-02 19:21:30 +08009970static bool vmx_xsaves_supported(void)
9971{
9972 return vmcs_config.cpu_based_2nd_exec_ctrl &
9973 SECONDARY_EXEC_XSAVES;
9974}
9975
Avi Kivity51aa01d2010-07-20 14:31:20 +03009976static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9977{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009978 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009979 bool unblock_nmi;
9980 u8 vector;
9981 bool idtv_info_valid;
9982
9983 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009984
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009985 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009986 if (vmx->loaded_vmcs->nmi_known_unmasked)
9987 return;
9988 /*
9989 * Can't use vmx->exit_intr_info since we're not sure what
9990 * the exit reason is.
9991 */
9992 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9993 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9994 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9995 /*
9996 * SDM 3: 27.7.1.2 (September 2008)
9997 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9998 * a guest IRET fault.
9999 * SDM 3: 23.2.2 (September 2008)
10000 * Bit 12 is undefined in any of the following cases:
10001 * If the VM exit sets the valid bit in the IDT-vectoring
10002 * information field.
10003 * If the VM exit is due to a double fault.
10004 */
10005 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10006 vector != DF_VECTOR && !idtv_info_valid)
10007 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10008 GUEST_INTR_STATE_NMI);
10009 else
10010 vmx->loaded_vmcs->nmi_known_unmasked =
10011 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10012 & GUEST_INTR_STATE_NMI);
10013 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10014 vmx->loaded_vmcs->vnmi_blocked_time +=
10015 ktime_to_ns(ktime_sub(ktime_get(),
10016 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010017}
10018
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010019static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010020 u32 idt_vectoring_info,
10021 int instr_len_field,
10022 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010023{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010024 u8 vector;
10025 int type;
10026 bool idtv_info_valid;
10027
10028 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010029
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010030 vcpu->arch.nmi_injected = false;
10031 kvm_clear_exception_queue(vcpu);
10032 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010033
10034 if (!idtv_info_valid)
10035 return;
10036
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010037 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010038
Avi Kivity668f6122008-07-02 09:28:55 +030010039 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10040 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010041
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010042 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010043 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010044 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010045 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010046 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010047 * Clear bit "block by NMI" before VM entry if a NMI
10048 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010049 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010050 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010051 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010052 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010053 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010054 /* fall through */
10055 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010056 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010057 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010058 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010059 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010060 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010061 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010062 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010063 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010064 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010065 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010066 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010067 break;
10068 default:
10069 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010070 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010071}
10072
Avi Kivity83422e12010-07-20 14:43:23 +030010073static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10074{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010075 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010076 VM_EXIT_INSTRUCTION_LEN,
10077 IDT_VECTORING_ERROR_CODE);
10078}
10079
Avi Kivityb463a6f2010-07-20 15:06:17 +030010080static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10081{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010082 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010083 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10084 VM_ENTRY_INSTRUCTION_LEN,
10085 VM_ENTRY_EXCEPTION_ERROR_CODE);
10086
10087 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10088}
10089
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010090static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10091{
10092 int i, nr_msrs;
10093 struct perf_guest_switch_msr *msrs;
10094
10095 msrs = perf_guest_get_msrs(&nr_msrs);
10096
10097 if (!msrs)
10098 return;
10099
10100 for (i = 0; i < nr_msrs; i++)
10101 if (msrs[i].host == msrs[i].guest)
10102 clear_atomic_switch_msr(vmx, msrs[i].msr);
10103 else
10104 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010105 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010106}
10107
Jiang Biao33365e72016-11-03 15:03:37 +080010108static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010109{
10110 struct vcpu_vmx *vmx = to_vmx(vcpu);
10111 u64 tscl;
10112 u32 delta_tsc;
10113
10114 if (vmx->hv_deadline_tsc == -1)
10115 return;
10116
10117 tscl = rdtsc();
10118 if (vmx->hv_deadline_tsc > tscl)
10119 /* sure to be 32 bit only because checked on set_hv_timer */
10120 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10121 cpu_preemption_timer_multi);
10122 else
10123 delta_tsc = 0;
10124
10125 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10126}
10127
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010128static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010129{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010130 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010131 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010132
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010133 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010134 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010135 vmx->loaded_vmcs->soft_vnmi_blocked))
10136 vmx->loaded_vmcs->entry_time = ktime_get();
10137
Avi Kivity104f2262010-11-18 13:12:52 +020010138 /* Don't enter VMX if guest state is invalid, let the exit handler
10139 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010140 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010141 return;
10142
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010143 if (vmx->ple_window_dirty) {
10144 vmx->ple_window_dirty = false;
10145 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10146 }
10147
Abel Gordon012f83c2013-04-18 14:39:25 +030010148 if (vmx->nested.sync_shadow_vmcs) {
10149 copy_vmcs12_to_shadow(vmx);
10150 vmx->nested.sync_shadow_vmcs = false;
10151 }
10152
Avi Kivity104f2262010-11-18 13:12:52 +020010153 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10154 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10155 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10156 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10157
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010158 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010159 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010160 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010161 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010162 }
10163
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010164 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010165 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010166 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010167 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010168 }
10169
Avi Kivity104f2262010-11-18 13:12:52 +020010170 /* When single-stepping over STI and MOV SS, we must clear the
10171 * corresponding interruptibility bits in the guest state. Otherwise
10172 * vmentry fails as it then expects bit 14 (BS) in pending debug
10173 * exceptions being set, but that's not correct for the guest debugging
10174 * case. */
10175 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10176 vmx_set_interrupt_shadow(vcpu, 0);
10177
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010178 if (static_cpu_has(X86_FEATURE_PKU) &&
10179 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10180 vcpu->arch.pkru != vmx->host_pkru)
10181 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010182
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010183 atomic_switch_perf_msrs(vmx);
10184
Yunhong Jiang64672c92016-06-13 14:19:59 -070010185 vmx_arm_hv_timer(vcpu);
10186
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010187 /*
10188 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10189 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10190 * is no need to worry about the conditional branch over the wrmsr
10191 * being speculatively taken.
10192 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010193 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010194
Nadav Har'Eld462b812011-05-24 15:26:10 +030010195 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010196
10197 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10198 (unsigned long)&current_evmcs->host_rsp : 0;
10199
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010200 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10201 if (vcpu->arch.l1tf_flush_l1d)
10202 vmx_l1d_flush(vcpu);
10203 }
10204
Avi Kivity104f2262010-11-18 13:12:52 +020010205 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010206 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010207 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10208 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10209 "push %%" _ASM_CX " \n\t"
10210 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010211 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010212 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010213 /* Avoid VMWRITE when Enlightened VMCS is in use */
10214 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10215 "jz 2f \n\t"
10216 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10217 "jmp 1f \n\t"
10218 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010219 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010220 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010221 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010222 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10223 "mov %%cr2, %%" _ASM_DX " \n\t"
10224 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010225 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010226 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010227 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010228 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010229 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010230 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010231 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10232 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10233 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10234 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10235 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10236 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010237#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010238 "mov %c[r8](%0), %%r8 \n\t"
10239 "mov %c[r9](%0), %%r9 \n\t"
10240 "mov %c[r10](%0), %%r10 \n\t"
10241 "mov %c[r11](%0), %%r11 \n\t"
10242 "mov %c[r12](%0), %%r12 \n\t"
10243 "mov %c[r13](%0), %%r13 \n\t"
10244 "mov %c[r14](%0), %%r14 \n\t"
10245 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010246#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010247 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010248
Avi Kivity6aa8b732006-12-10 02:21:36 -080010249 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010250 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010251 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010252 "jmp 2f \n\t"
10253 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10254 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010255 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010256 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010257 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010258 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010259 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10260 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10261 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10262 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10263 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10264 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10265 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010266#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010267 "mov %%r8, %c[r8](%0) \n\t"
10268 "mov %%r9, %c[r9](%0) \n\t"
10269 "mov %%r10, %c[r10](%0) \n\t"
10270 "mov %%r11, %c[r11](%0) \n\t"
10271 "mov %%r12, %c[r12](%0) \n\t"
10272 "mov %%r13, %c[r13](%0) \n\t"
10273 "mov %%r14, %c[r14](%0) \n\t"
10274 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010275 "xor %%r8d, %%r8d \n\t"
10276 "xor %%r9d, %%r9d \n\t"
10277 "xor %%r10d, %%r10d \n\t"
10278 "xor %%r11d, %%r11d \n\t"
10279 "xor %%r12d, %%r12d \n\t"
10280 "xor %%r13d, %%r13d \n\t"
10281 "xor %%r14d, %%r14d \n\t"
10282 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010283#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010284 "mov %%cr2, %%" _ASM_AX " \n\t"
10285 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010286
Jim Mattson0cb5b302018-01-03 14:31:38 -080010287 "xor %%eax, %%eax \n\t"
10288 "xor %%ebx, %%ebx \n\t"
10289 "xor %%esi, %%esi \n\t"
10290 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010291 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010292 ".pushsection .rodata \n\t"
10293 ".global vmx_return \n\t"
10294 "vmx_return: " _ASM_PTR " 2b \n\t"
10295 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010296 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010297 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010298 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010299 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010300 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10301 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10302 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10303 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10304 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10305 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10306 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010307#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010308 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10309 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10310 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10311 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10312 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10313 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10314 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10315 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010316#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010317 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10318 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010319 : "cc", "memory"
10320#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010321 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010322 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010323#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010324 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010325#endif
10326 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010327
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010328 /*
10329 * We do not use IBRS in the kernel. If this vCPU has used the
10330 * SPEC_CTRL MSR it may have left it on; save the value and
10331 * turn it off. This is much more efficient than blindly adding
10332 * it to the atomic save/restore list. Especially as the former
10333 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10334 *
10335 * For non-nested case:
10336 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10337 * save it.
10338 *
10339 * For nested case:
10340 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10341 * save it.
10342 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010343 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010344 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010345
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010346 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010347
David Woodhouse117cc7a2018-01-12 11:11:27 +000010348 /* Eliminate branch target predictions from guest mode */
10349 vmexit_fill_RSB();
10350
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010351 /* All fields are clean at this point */
10352 if (static_branch_unlikely(&enable_evmcs))
10353 current_evmcs->hv_clean_fields |=
10354 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10355
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010356 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010357 if (vmx->host_debugctlmsr)
10358 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010359
Avi Kivityaa67f602012-08-01 16:48:03 +030010360#ifndef CONFIG_X86_64
10361 /*
10362 * The sysexit path does not restore ds/es, so we must set them to
10363 * a reasonable value ourselves.
10364 *
10365 * We can't defer this to vmx_load_host_state() since that function
10366 * may be executed in interrupt context, which saves and restore segments
10367 * around it, nullifying its effect.
10368 */
10369 loadsegment(ds, __USER_DS);
10370 loadsegment(es, __USER_DS);
10371#endif
10372
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010373 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010374 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010375 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010376 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010377 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010378 vcpu->arch.regs_dirty = 0;
10379
Gleb Natapove0b890d2013-09-25 12:51:33 +030010380 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010381 * eager fpu is enabled if PKEY is supported and CR4 is switched
10382 * back on host, so it is safe to read guest PKRU from current
10383 * XSAVE.
10384 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010385 if (static_cpu_has(X86_FEATURE_PKU) &&
10386 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10387 vcpu->arch.pkru = __read_pkru();
10388 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010389 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010390 }
10391
Gleb Natapove0b890d2013-09-25 12:51:33 +030010392 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010393 vmx->idt_vectoring_info = 0;
10394
10395 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10396 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10397 return;
10398
10399 vmx->loaded_vmcs->launched = 1;
10400 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010401
Avi Kivity51aa01d2010-07-20 14:31:20 +030010402 vmx_complete_atomic_exit(vmx);
10403 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010404 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010405}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010406STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010407
Sean Christopherson434a1e92018-03-20 12:17:18 -070010408static struct kvm *vmx_vm_alloc(void)
10409{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010410 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010411 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010412}
10413
10414static void vmx_vm_free(struct kvm *kvm)
10415{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010416 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010417}
10418
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010419static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010420{
10421 struct vcpu_vmx *vmx = to_vmx(vcpu);
10422 int cpu;
10423
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010424 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010425 return;
10426
10427 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010428 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010429 vmx_vcpu_put(vcpu);
10430 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010431 put_cpu();
10432}
10433
Jim Mattson2f1fe812016-07-08 15:36:06 -070010434/*
10435 * Ensure that the current vmcs of the logical processor is the
10436 * vmcs01 of the vcpu before calling free_nested().
10437 */
10438static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10439{
10440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010441
Christoffer Dallec7660c2017-12-04 21:35:23 +010010442 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010443 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010444 free_nested(vmx);
10445 vcpu_put(vcpu);
10446}
10447
Avi Kivity6aa8b732006-12-10 02:21:36 -080010448static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10449{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010450 struct vcpu_vmx *vmx = to_vmx(vcpu);
10451
Kai Huang843e4332015-01-28 10:54:28 +080010452 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010453 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010454 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010455 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010456 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010457 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010458 kfree(vmx->guest_msrs);
10459 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010460 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010461}
10462
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010463static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010464{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010465 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010466 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010467 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010468 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010469
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010470 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010471 return ERR_PTR(-ENOMEM);
10472
Wanpeng Li991e7a02015-09-16 17:30:05 +080010473 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010474
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010475 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10476 if (err)
10477 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010478
Peter Feiner4e595162016-07-07 14:49:58 -070010479 err = -ENOMEM;
10480
10481 /*
10482 * If PML is turned on, failure on enabling PML just results in failure
10483 * of creating the vcpu, therefore we can simplify PML logic (by
10484 * avoiding dealing with cases, such as enabling PML partially on vcpus
10485 * for the guest, etc.
10486 */
10487 if (enable_pml) {
10488 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10489 if (!vmx->pml_pg)
10490 goto uninit_vcpu;
10491 }
10492
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010493 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010494 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10495 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010496
Peter Feiner4e595162016-07-07 14:49:58 -070010497 if (!vmx->guest_msrs)
10498 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010499
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010500 err = alloc_loaded_vmcs(&vmx->vmcs01);
10501 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010502 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010503
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010504 msr_bitmap = vmx->vmcs01.msr_bitmap;
10505 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10506 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10507 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10508 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10509 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10510 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10511 vmx->msr_bitmap_mode = 0;
10512
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010513 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010514 cpu = get_cpu();
10515 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010516 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010517 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010518 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010519 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010520 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010521 err = alloc_apic_access_page(kvm);
10522 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010523 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010524 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010525
Sean Christophersone90008d2018-03-05 12:04:37 -080010526 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010527 err = init_rmode_identity_map(kvm);
10528 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010529 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010530 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010531
Wanpeng Li5c614b32015-10-13 09:18:36 -070010532 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010533 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10534 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010535 vmx->nested.vpid02 = allocate_vpid();
10536 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010537
Wincy Van705699a2015-02-03 23:58:17 +080010538 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010539 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010540
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010541 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10542
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010543 /*
10544 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10545 * or POSTED_INTR_WAKEUP_VECTOR.
10546 */
10547 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10548 vmx->pi_desc.sn = 1;
10549
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010550 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010551
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010552free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010553 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010554 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010555free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010556 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010557free_pml:
10558 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010559uninit_vcpu:
10560 kvm_vcpu_uninit(&vmx->vcpu);
10561free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010562 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010563 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010564 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010565}
10566
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010567#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10568
Wanpeng Lib31c1142018-03-12 04:53:04 -070010569static int vmx_vm_init(struct kvm *kvm)
10570{
10571 if (!ple_gap)
10572 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010573
10574 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10575 if (nosmt) {
10576 pr_err(L1TF_MSG);
10577 return -EOPNOTSUPP;
10578 }
10579 pr_warn(L1TF_MSG);
10580 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010581 return 0;
10582}
10583
Yang, Sheng002c7f72007-07-31 14:23:01 +030010584static void __init vmx_check_processor_compat(void *rtn)
10585{
10586 struct vmcs_config vmcs_conf;
10587
10588 *(int *)rtn = 0;
10589 if (setup_vmcs_config(&vmcs_conf) < 0)
10590 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010591 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010592 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10593 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10594 smp_processor_id());
10595 *(int *)rtn = -EIO;
10596 }
10597}
10598
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010599static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010600{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010601 u8 cache;
10602 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010603
Sheng Yang522c68c2009-04-27 20:35:43 +080010604 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010605 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010606 * 2. EPT with VT-d:
10607 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010608 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010609 * b. VT-d with snooping control feature: snooping control feature of
10610 * VT-d engine can guarantee the cache correctness. Just set it
10611 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010612 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010613 * consistent with host MTRR
10614 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010615 if (is_mmio) {
10616 cache = MTRR_TYPE_UNCACHABLE;
10617 goto exit;
10618 }
10619
10620 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010621 ipat = VMX_EPT_IPAT_BIT;
10622 cache = MTRR_TYPE_WRBACK;
10623 goto exit;
10624 }
10625
10626 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10627 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010628 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010629 cache = MTRR_TYPE_WRBACK;
10630 else
10631 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010632 goto exit;
10633 }
10634
Xiao Guangrongff536042015-06-15 16:55:22 +080010635 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010636
10637exit:
10638 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010639}
10640
Sheng Yang17cc3932010-01-05 19:02:27 +080010641static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010642{
Sheng Yang878403b2010-01-05 19:02:29 +080010643 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10644 return PT_DIRECTORY_LEVEL;
10645 else
10646 /* For shadow and EPT supported 1GB page */
10647 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010648}
10649
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010650static void vmcs_set_secondary_exec_control(u32 new_ctl)
10651{
10652 /*
10653 * These bits in the secondary execution controls field
10654 * are dynamic, the others are mostly based on the hypervisor
10655 * architecture and the guest's CPUID. Do not touch the
10656 * dynamic bits.
10657 */
10658 u32 mask =
10659 SECONDARY_EXEC_SHADOW_VMCS |
10660 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010661 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10662 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010663
10664 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10665
10666 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10667 (new_ctl & ~mask) | (cur_ctl & mask));
10668}
10669
David Matlack8322ebb2016-11-29 18:14:09 -080010670/*
10671 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10672 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10673 */
10674static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10675{
10676 struct vcpu_vmx *vmx = to_vmx(vcpu);
10677 struct kvm_cpuid_entry2 *entry;
10678
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010679 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10680 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010681
10682#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10683 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010684 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010685} while (0)
10686
10687 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10688 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10689 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10690 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10691 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10692 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10693 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10694 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10695 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10696 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10697 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10698 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10699 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10700 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10701 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10702
10703 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10704 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10705 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10706 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10707 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010708 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010709
10710#undef cr4_fixed1_update
10711}
10712
Sheng Yang0e851882009-12-18 16:48:46 +080010713static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10714{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010715 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010716
Paolo Bonzini80154d72017-08-24 13:55:35 +020010717 if (cpu_has_secondary_exec_ctrls()) {
10718 vmx_compute_secondary_exec_control(vmx);
10719 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010720 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010721
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010722 if (nested_vmx_allowed(vcpu))
10723 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10724 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10725 else
10726 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10727 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010728
10729 if (nested_vmx_allowed(vcpu))
10730 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010731}
10732
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010733static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10734{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010735 if (func == 1 && nested)
10736 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010737}
10738
Yang Zhang25d92082013-08-06 12:00:32 +030010739static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10740 struct x86_exception *fault)
10741{
Jan Kiszka533558b2014-01-04 18:47:20 +010010742 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010743 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010744 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010745 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010746
Bandan Dasc5f983f2017-05-05 15:25:14 -040010747 if (vmx->nested.pml_full) {
10748 exit_reason = EXIT_REASON_PML_FULL;
10749 vmx->nested.pml_full = false;
10750 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10751 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010752 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010753 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010754 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010755
10756 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010757 vmcs12->guest_physical_address = fault->address;
10758}
10759
Peter Feiner995f00a2017-06-30 17:26:32 -070010760static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10761{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010762 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010763}
10764
Nadav Har'El155a97a2013-08-05 11:07:16 +030010765/* Callbacks for nested_ept_init_mmu_context: */
10766
10767static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10768{
10769 /* return the page table to be shadowed - in our case, EPT12 */
10770 return get_vmcs12(vcpu)->ept_pointer;
10771}
10772
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010773static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010774{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010775 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010776 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010777 return 1;
10778
10779 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010780 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010781 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010782 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010783 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010784 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10785 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10786 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10787
10788 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010789 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010790}
10791
10792static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10793{
10794 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10795}
10796
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010797static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10798 u16 error_code)
10799{
10800 bool inequality, bit;
10801
10802 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10803 inequality =
10804 (error_code & vmcs12->page_fault_error_code_mask) !=
10805 vmcs12->page_fault_error_code_match;
10806 return inequality ^ bit;
10807}
10808
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010809static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10810 struct x86_exception *fault)
10811{
10812 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10813
10814 WARN_ON(!is_guest_mode(vcpu));
10815
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010816 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10817 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010818 vmcs12->vm_exit_intr_error_code = fault->error_code;
10819 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10820 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10821 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10822 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010823 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010824 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010825 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010826}
10827
Paolo Bonzinic9923842017-12-13 14:16:30 +010010828static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10829 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010830
10831static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010832 struct vmcs12 *vmcs12)
10833{
10834 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010835 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010836 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010837
10838 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010839 /*
10840 * Translate L1 physical address to host physical
10841 * address for vmcs02. Keep the page pinned, so this
10842 * physical address remains valid. We keep a reference
10843 * to it so we can release it later.
10844 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010845 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010846 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010847 vmx->nested.apic_access_page = NULL;
10848 }
10849 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010850 /*
10851 * If translation failed, no matter: This feature asks
10852 * to exit when accessing the given address, and if it
10853 * can never be accessed, this feature won't do
10854 * anything anyway.
10855 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010856 if (!is_error_page(page)) {
10857 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010858 hpa = page_to_phys(vmx->nested.apic_access_page);
10859 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10860 } else {
10861 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10862 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10863 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010864 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010865
10866 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010867 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010868 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010869 vmx->nested.virtual_apic_page = NULL;
10870 }
10871 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010872
10873 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010874 * If translation failed, VM entry will fail because
10875 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10876 * Failing the vm entry is _not_ what the processor
10877 * does but it's basically the only possibility we
10878 * have. We could still enter the guest if CR8 load
10879 * exits are enabled, CR8 store exits are enabled, and
10880 * virtualize APIC access is disabled; in this case
10881 * the processor would never use the TPR shadow and we
10882 * could simply clear the bit from the execution
10883 * control. But such a configuration is useless, so
10884 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010885 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010886 if (!is_error_page(page)) {
10887 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010888 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10889 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10890 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010891 }
10892
Wincy Van705699a2015-02-03 23:58:17 +080010893 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010894 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10895 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010896 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010897 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010898 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010899 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10900 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010901 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010902 vmx->nested.pi_desc_page = page;
10903 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010904 vmx->nested.pi_desc =
10905 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10906 (unsigned long)(vmcs12->posted_intr_desc_addr &
10907 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010908 vmcs_write64(POSTED_INTR_DESC_ADDR,
10909 page_to_phys(vmx->nested.pi_desc_page) +
10910 (unsigned long)(vmcs12->posted_intr_desc_addr &
10911 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010912 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010913 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010914 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10915 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010916 else
10917 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10918 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010919}
10920
Jan Kiszkaf4124502014-03-07 20:03:13 +010010921static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10922{
10923 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10924 struct vcpu_vmx *vmx = to_vmx(vcpu);
10925
10926 if (vcpu->arch.virtual_tsc_khz == 0)
10927 return;
10928
10929 /* Make sure short timeouts reliably trigger an immediate vmexit.
10930 * hrtimer_start does not guarantee this. */
10931 if (preemption_timeout <= 1) {
10932 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10933 return;
10934 }
10935
10936 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10937 preemption_timeout *= 1000000;
10938 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10939 hrtimer_start(&vmx->nested.preemption_timer,
10940 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10941}
10942
Jim Mattson56a20512017-07-06 16:33:06 -070010943static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10944 struct vmcs12 *vmcs12)
10945{
10946 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10947 return 0;
10948
10949 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10950 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10951 return -EINVAL;
10952
10953 return 0;
10954}
10955
Wincy Van3af18d92015-02-03 23:49:31 +080010956static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10957 struct vmcs12 *vmcs12)
10958{
Wincy Van3af18d92015-02-03 23:49:31 +080010959 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10960 return 0;
10961
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010962 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010963 return -EINVAL;
10964
10965 return 0;
10966}
10967
Jim Mattson712b12d2017-08-24 13:24:47 -070010968static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10969 struct vmcs12 *vmcs12)
10970{
10971 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10972 return 0;
10973
10974 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10975 return -EINVAL;
10976
10977 return 0;
10978}
10979
Wincy Van3af18d92015-02-03 23:49:31 +080010980/*
10981 * Merge L0's and L1's MSR bitmap, return false to indicate that
10982 * we do not use the hardware.
10983 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010984static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10985 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010986{
Wincy Van82f0dd42015-02-03 23:57:18 +080010987 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010988 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010989 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010990 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010991 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010992 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010993 *
10994 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10995 * ensures that we do not accidentally generate an L02 MSR bitmap
10996 * from the L12 MSR bitmap that is too permissive.
10997 * 2. That L1 or L2s have actually used the MSR. This avoids
10998 * unnecessarily merging of the bitmap if the MSR is unused. This
10999 * works properly because we only update the L01 MSR bitmap lazily.
11000 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11001 * updated to reflect this when L1 (or its L2s) actually write to
11002 * the MSR.
11003 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011004 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11005 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011006
Paolo Bonzinic9923842017-12-13 14:16:30 +010011007 /* Nothing to do if the MSR bitmap is not in use. */
11008 if (!cpu_has_vmx_msr_bitmap() ||
11009 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11010 return false;
11011
Ashok Raj15d45072018-02-01 22:59:43 +010011012 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011013 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011014 return false;
11015
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011016 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11017 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011018 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011019
Radim Krčmářd048c092016-08-08 20:16:22 +020011020 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011021 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11022 /*
11023 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11024 * just lets the processor take the value from the virtual-APIC page;
11025 * take those 256 bits directly from the L1 bitmap.
11026 */
11027 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11028 unsigned word = msr / BITS_PER_LONG;
11029 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11030 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011031 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011032 } else {
11033 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11034 unsigned word = msr / BITS_PER_LONG;
11035 msr_bitmap_l0[word] = ~0;
11036 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11037 }
11038 }
11039
11040 nested_vmx_disable_intercept_for_msr(
11041 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011042 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011043 MSR_TYPE_W);
11044
11045 if (nested_cpu_has_vid(vmcs12)) {
11046 nested_vmx_disable_intercept_for_msr(
11047 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011048 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011049 MSR_TYPE_W);
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_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011053 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011054 }
Ashok Raj15d45072018-02-01 22:59:43 +010011055
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011056 if (spec_ctrl)
11057 nested_vmx_disable_intercept_for_msr(
11058 msr_bitmap_l1, msr_bitmap_l0,
11059 MSR_IA32_SPEC_CTRL,
11060 MSR_TYPE_R | MSR_TYPE_W);
11061
Ashok Raj15d45072018-02-01 22:59:43 +010011062 if (pred_cmd)
11063 nested_vmx_disable_intercept_for_msr(
11064 msr_bitmap_l1, msr_bitmap_l0,
11065 MSR_IA32_PRED_CMD,
11066 MSR_TYPE_W);
11067
Wincy Vanf2b93282015-02-03 23:56:03 +080011068 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011069 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011070
11071 return true;
11072}
11073
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011074static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11075 struct vmcs12 *vmcs12)
11076{
11077 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11078 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11079 return -EINVAL;
11080 else
11081 return 0;
11082}
11083
Wincy Vanf2b93282015-02-03 23:56:03 +080011084static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11085 struct vmcs12 *vmcs12)
11086{
Wincy Van82f0dd42015-02-03 23:57:18 +080011087 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011088 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011089 !nested_cpu_has_vid(vmcs12) &&
11090 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011091 return 0;
11092
11093 /*
11094 * If virtualize x2apic mode is enabled,
11095 * virtualize apic access must be disabled.
11096 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011097 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11098 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011099 return -EINVAL;
11100
Wincy Van608406e2015-02-03 23:57:51 +080011101 /*
11102 * If virtual interrupt delivery is enabled,
11103 * we must exit on external interrupts.
11104 */
11105 if (nested_cpu_has_vid(vmcs12) &&
11106 !nested_exit_on_intr(vcpu))
11107 return -EINVAL;
11108
Wincy Van705699a2015-02-03 23:58:17 +080011109 /*
11110 * bits 15:8 should be zero in posted_intr_nv,
11111 * the descriptor address has been already checked
11112 * in nested_get_vmcs12_pages.
11113 */
11114 if (nested_cpu_has_posted_intr(vmcs12) &&
11115 (!nested_cpu_has_vid(vmcs12) ||
11116 !nested_exit_intr_ack_set(vcpu) ||
11117 vmcs12->posted_intr_nv & 0xff00))
11118 return -EINVAL;
11119
Wincy Vanf2b93282015-02-03 23:56:03 +080011120 /* tpr shadow is needed by all apicv features. */
11121 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11122 return -EINVAL;
11123
11124 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011125}
11126
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011127static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11128 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011129 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011130{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011131 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011132 u64 count, addr;
11133
11134 if (vmcs12_read_any(vcpu, count_field, &count) ||
11135 vmcs12_read_any(vcpu, addr_field, &addr)) {
11136 WARN_ON(1);
11137 return -EINVAL;
11138 }
11139 if (count == 0)
11140 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011141 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011142 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11143 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011144 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011145 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11146 addr_field, maxphyaddr, count, addr);
11147 return -EINVAL;
11148 }
11149 return 0;
11150}
11151
11152static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11153 struct vmcs12 *vmcs12)
11154{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011155 if (vmcs12->vm_exit_msr_load_count == 0 &&
11156 vmcs12->vm_exit_msr_store_count == 0 &&
11157 vmcs12->vm_entry_msr_load_count == 0)
11158 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011159 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011160 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011161 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011162 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011163 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011164 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011165 return -EINVAL;
11166 return 0;
11167}
11168
Bandan Dasc5f983f2017-05-05 15:25:14 -040011169static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11170 struct vmcs12 *vmcs12)
11171{
11172 u64 address = vmcs12->pml_address;
11173 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11174
11175 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11176 if (!nested_cpu_has_ept(vmcs12) ||
11177 !IS_ALIGNED(address, 4096) ||
11178 address >> maxphyaddr)
11179 return -EINVAL;
11180 }
11181
11182 return 0;
11183}
11184
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011185static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11186 struct vmx_msr_entry *e)
11187{
11188 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011189 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011190 return -EINVAL;
11191 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11192 e->index == MSR_IA32_UCODE_REV)
11193 return -EINVAL;
11194 if (e->reserved != 0)
11195 return -EINVAL;
11196 return 0;
11197}
11198
11199static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11200 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011201{
11202 if (e->index == MSR_FS_BASE ||
11203 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011204 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11205 nested_vmx_msr_check_common(vcpu, e))
11206 return -EINVAL;
11207 return 0;
11208}
11209
11210static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11211 struct vmx_msr_entry *e)
11212{
11213 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11214 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011215 return -EINVAL;
11216 return 0;
11217}
11218
11219/*
11220 * Load guest's/host's msr at nested entry/exit.
11221 * return 0 for success, entry index for failure.
11222 */
11223static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11224{
11225 u32 i;
11226 struct vmx_msr_entry e;
11227 struct msr_data msr;
11228
11229 msr.host_initiated = false;
11230 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011231 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11232 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011233 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011234 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11235 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011236 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011237 }
11238 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011239 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011240 "%s check failed (%u, 0x%x, 0x%x)\n",
11241 __func__, i, e.index, e.reserved);
11242 goto fail;
11243 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011244 msr.index = e.index;
11245 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011246 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011247 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011248 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11249 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011250 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011251 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011252 }
11253 return 0;
11254fail:
11255 return i + 1;
11256}
11257
11258static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11259{
11260 u32 i;
11261 struct vmx_msr_entry e;
11262
11263 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011264 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011265 if (kvm_vcpu_read_guest(vcpu,
11266 gpa + i * sizeof(e),
11267 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011268 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011269 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11270 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011271 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011272 }
11273 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011274 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011275 "%s check failed (%u, 0x%x, 0x%x)\n",
11276 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011277 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011278 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011279 msr_info.host_initiated = false;
11280 msr_info.index = e.index;
11281 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011282 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011283 "%s cannot read MSR (%u, 0x%x)\n",
11284 __func__, i, e.index);
11285 return -EINVAL;
11286 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011287 if (kvm_vcpu_write_guest(vcpu,
11288 gpa + i * sizeof(e) +
11289 offsetof(struct vmx_msr_entry, value),
11290 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011291 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011292 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011293 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011294 return -EINVAL;
11295 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011296 }
11297 return 0;
11298}
11299
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011300static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11301{
11302 unsigned long invalid_mask;
11303
11304 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11305 return (val & invalid_mask) == 0;
11306}
11307
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011308/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011309 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11310 * emulating VM entry into a guest with EPT enabled.
11311 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11312 * is assigned to entry_failure_code on failure.
11313 */
11314static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011315 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011316{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011317 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011318 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011319 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11320 return 1;
11321 }
11322
11323 /*
11324 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11325 * must not be dereferenced.
11326 */
11327 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11328 !nested_ept) {
11329 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11330 *entry_failure_code = ENTRY_FAIL_PDPTE;
11331 return 1;
11332 }
11333 }
11334
11335 vcpu->arch.cr3 = cr3;
11336 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11337 }
11338
11339 kvm_mmu_reset_context(vcpu);
11340 return 0;
11341}
11342
Jim Mattson6514dc32018-04-26 16:09:12 -070011343static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011344{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011345 struct vcpu_vmx *vmx = to_vmx(vcpu);
11346
11347 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11348 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11349 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11350 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11351 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11352 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11353 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11354 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11355 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11356 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11357 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11358 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11359 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11360 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11361 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11362 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11363 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11364 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11365 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11366 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11367 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11368 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11369 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11370 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11371 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11372 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11373 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11374 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11375 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11376 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11377 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011378
11379 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11380 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11381 vmcs12->guest_pending_dbg_exceptions);
11382 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11383 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11384
11385 if (nested_cpu_has_xsaves(vmcs12))
11386 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11387 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11388
11389 if (cpu_has_vmx_posted_intr())
11390 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11391
11392 /*
11393 * Whether page-faults are trapped is determined by a combination of
11394 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11395 * If enable_ept, L0 doesn't care about page faults and we should
11396 * set all of these to L1's desires. However, if !enable_ept, L0 does
11397 * care about (at least some) page faults, and because it is not easy
11398 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11399 * to exit on each and every L2 page fault. This is done by setting
11400 * MASK=MATCH=0 and (see below) EB.PF=1.
11401 * Note that below we don't need special code to set EB.PF beyond the
11402 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11403 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11404 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11405 */
11406 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11407 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11408 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11409 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11410
11411 /* All VMFUNCs are currently emulated through L0 vmexits. */
11412 if (cpu_has_vmx_vmfunc())
11413 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11414
11415 if (cpu_has_vmx_apicv()) {
11416 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11417 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11418 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11419 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11420 }
11421
11422 /*
11423 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11424 * Some constant fields are set here by vmx_set_constant_host_state().
11425 * Other fields are different per CPU, and will be set later when
11426 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11427 */
11428 vmx_set_constant_host_state(vmx);
11429
11430 /*
11431 * Set the MSR load/store lists to match L0's settings.
11432 */
11433 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011434 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11435 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11436 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11437 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011438
11439 set_cr4_guest_host_mask(vmx);
11440
11441 if (vmx_mpx_supported())
11442 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11443
11444 if (enable_vpid) {
11445 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11446 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11447 else
11448 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11449 }
11450
11451 /*
11452 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11453 */
11454 if (enable_ept) {
11455 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11456 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11457 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11458 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11459 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011460
11461 if (cpu_has_vmx_msr_bitmap())
11462 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011463}
11464
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011465/*
11466 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11467 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011468 * 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 +030011469 * guest in a way that will both be appropriate to L1's requests, and our
11470 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11471 * function also has additional necessary side-effects, like setting various
11472 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011473 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11474 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011475 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011476static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011477 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011478{
11479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011480 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011481
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011482 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011483 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011484 vmx->nested.dirty_vmcs12 = false;
11485 }
11486
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011487 /*
11488 * First, the fields that are shadowed. This must be kept in sync
11489 * with vmx_shadow_fields.h.
11490 */
11491
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011492 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011493 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011494 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011495 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11496 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011497
11498 /*
11499 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11500 * HOST_FS_BASE, HOST_GS_BASE.
11501 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011502
Jim Mattson6514dc32018-04-26 16:09:12 -070011503 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011504 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011505 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11506 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11507 } else {
11508 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11509 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11510 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011511 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011512 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11513 vmcs12->vm_entry_intr_info_field);
11514 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11515 vmcs12->vm_entry_exception_error_code);
11516 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11517 vmcs12->vm_entry_instruction_len);
11518 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11519 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011520 vmx->loaded_vmcs->nmi_known_unmasked =
11521 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011522 } else {
11523 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11524 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011525 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011526
Jan Kiszkaf4124502014-03-07 20:03:13 +010011527 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011528
Paolo Bonzini93140062016-07-06 13:23:51 +020011529 /* Preemption timer setting is only taken from vmcs01. */
11530 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11531 exec_control |= vmcs_config.pin_based_exec_ctrl;
11532 if (vmx->hv_deadline_tsc == -1)
11533 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11534
11535 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011536 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011537 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11538 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011539 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011540 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011541 }
Wincy Van705699a2015-02-03 23:58:17 +080011542
Jan Kiszkaf4124502014-03-07 20:03:13 +010011543 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011544
Jan Kiszkaf4124502014-03-07 20:03:13 +010011545 vmx->nested.preemption_timer_expired = false;
11546 if (nested_cpu_has_preemption_timer(vmcs12))
11547 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011548
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011549 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011550 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011551
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011552 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011553 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011554 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011555 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011556 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011557 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011558 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11559 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011560 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011561 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11562 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11563 ~SECONDARY_EXEC_ENABLE_PML;
11564 exec_control |= vmcs12_exec_ctrl;
11565 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011566
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011567 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011568 vmcs_write16(GUEST_INTR_STATUS,
11569 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011570
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011571 /*
11572 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11573 * nested_get_vmcs12_pages will either fix it up or
11574 * remove the VM execution control.
11575 */
11576 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11577 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11578
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011579 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11580 }
11581
Jim Mattson83bafef2016-10-04 10:48:38 -070011582 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011583 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11584 * entry, but only if the current (host) sp changed from the value
11585 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11586 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11587 * here we just force the write to happen on entry.
11588 */
11589 vmx->host_rsp = 0;
11590
11591 exec_control = vmx_exec_control(vmx); /* L0's desires */
11592 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11593 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11594 exec_control &= ~CPU_BASED_TPR_SHADOW;
11595 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011596
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011597 /*
11598 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11599 * nested_get_vmcs12_pages can't fix it up, the illegal value
11600 * will result in a VM entry failure.
11601 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011602 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011603 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011604 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011605 } else {
11606#ifdef CONFIG_X86_64
11607 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11608 CPU_BASED_CR8_STORE_EXITING;
11609#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011610 }
11611
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011612 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011613 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11614 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011615 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011616 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11617 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11618
11619 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11620
11621 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11622 * bitwise-or of what L1 wants to trap for L2, and what we want to
11623 * trap. Note that CR0.TS also needs updating - we do this later.
11624 */
11625 update_exception_bitmap(vcpu);
11626 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11627 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11628
Nadav Har'El8049d652013-08-05 11:07:06 +030011629 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11630 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11631 * bits are further modified by vmx_set_efer() below.
11632 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011633 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011634
11635 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11636 * emulated by vmx_set_efer(), below.
11637 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011638 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011639 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11640 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011641 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11642
Jim Mattson6514dc32018-04-26 16:09:12 -070011643 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011644 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011645 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011646 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011647 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011648 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011649 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011650
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011651 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11652
Peter Feinerc95ba922016-08-17 09:36:47 -070011653 if (kvm_has_tsc_control)
11654 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011655
11656 if (enable_vpid) {
11657 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011658 * There is no direct mapping between vpid02 and vpid12, the
11659 * vpid02 is per-vCPU for L0 and reused while the value of
11660 * vpid12 is changed w/ one invvpid during nested vmentry.
11661 * The vpid12 is allocated by L1 for L2, so it will not
11662 * influence global bitmap(for vpid01 and vpid02 allocation)
11663 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011664 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011665 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011666 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11667 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011668 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011669 }
11670 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011671 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011672 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011673 }
11674
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011675 if (enable_pml) {
11676 /*
11677 * Conceptually we want to copy the PML address and index from
11678 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11679 * since we always flush the log on each vmexit, this happens
11680 * to be equivalent to simply resetting the fields in vmcs02.
11681 */
11682 ASSERT(vmx->pml_pg);
11683 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11684 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11685 }
11686
Nadav Har'El155a97a2013-08-05 11:07:16 +030011687 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011688 if (nested_ept_init_mmu_context(vcpu)) {
11689 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11690 return 1;
11691 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011692 } else if (nested_cpu_has2(vmcs12,
11693 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011694 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011695 }
11696
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011697 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011698 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11699 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011700 * The CR0_READ_SHADOW is what L2 should have expected to read given
11701 * the specifications by L1; It's not enough to take
11702 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11703 * have more bits than L1 expected.
11704 */
11705 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11706 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11707
11708 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11709 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11710
Jim Mattson6514dc32018-04-26 16:09:12 -070011711 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011712 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011713 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11714 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11715 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11716 else
11717 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11718 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11719 vmx_set_efer(vcpu, vcpu->arch.efer);
11720
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011721 /*
11722 * Guest state is invalid and unrestricted guest is disabled,
11723 * which means L1 attempted VMEntry to L2 with invalid state.
11724 * Fail the VMEntry.
11725 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011726 if (vmx->emulation_required) {
11727 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011728 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011729 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011730
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011731 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011732 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011733 entry_failure_code))
11734 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011735
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011736 if (!enable_ept)
11737 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11738
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011739 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11740 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011741 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011742}
11743
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011744static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11745{
11746 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11747 nested_cpu_has_virtual_nmis(vmcs12))
11748 return -EINVAL;
11749
11750 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11751 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11752 return -EINVAL;
11753
11754 return 0;
11755}
11756
Jim Mattsonca0bde22016-11-30 12:03:46 -080011757static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11758{
11759 struct vcpu_vmx *vmx = to_vmx(vcpu);
11760
11761 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11762 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11763 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11764
Jim Mattson56a20512017-07-06 16:33:06 -070011765 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11766 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11767
Jim Mattsonca0bde22016-11-30 12:03:46 -080011768 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11769 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11770
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011771 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11772 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11773
Jim Mattson712b12d2017-08-24 13:24:47 -070011774 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11775 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11776
Jim Mattsonca0bde22016-11-30 12:03:46 -080011777 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11778 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11779
11780 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11781 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11782
Bandan Dasc5f983f2017-05-05 15:25:14 -040011783 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11784 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11785
Jim Mattsonca0bde22016-11-30 12:03:46 -080011786 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011787 vmx->nested.msrs.procbased_ctls_low,
11788 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011789 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11790 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011791 vmx->nested.msrs.secondary_ctls_low,
11792 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011793 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011794 vmx->nested.msrs.pinbased_ctls_low,
11795 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011796 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011797 vmx->nested.msrs.exit_ctls_low,
11798 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011799 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011800 vmx->nested.msrs.entry_ctls_low,
11801 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011802 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11803
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011804 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011805 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11806
Bandan Das41ab9372017-08-03 15:54:43 -040011807 if (nested_cpu_has_vmfunc(vmcs12)) {
11808 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011809 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011810 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11811
11812 if (nested_cpu_has_eptp_switching(vmcs12)) {
11813 if (!nested_cpu_has_ept(vmcs12) ||
11814 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11815 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11816 }
11817 }
Bandan Das27c42a12017-08-03 15:54:42 -040011818
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011819 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11820 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11821
Jim Mattsonca0bde22016-11-30 12:03:46 -080011822 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11823 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11824 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11825 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11826
11827 return 0;
11828}
11829
11830static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11831 u32 *exit_qual)
11832{
11833 bool ia32e;
11834
11835 *exit_qual = ENTRY_FAIL_DEFAULT;
11836
11837 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11838 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11839 return 1;
11840
11841 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11842 vmcs12->vmcs_link_pointer != -1ull) {
11843 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11844 return 1;
11845 }
11846
11847 /*
11848 * If the load IA32_EFER VM-entry control is 1, the following checks
11849 * are performed on the field for the IA32_EFER MSR:
11850 * - Bits reserved in the IA32_EFER MSR must be 0.
11851 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11852 * the IA-32e mode guest VM-exit control. It must also be identical
11853 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11854 * CR0.PG) is 1.
11855 */
11856 if (to_vmx(vcpu)->nested.nested_run_pending &&
11857 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11858 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11859 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11860 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11861 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11862 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11863 return 1;
11864 }
11865
11866 /*
11867 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11868 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11869 * the values of the LMA and LME bits in the field must each be that of
11870 * the host address-space size VM-exit control.
11871 */
11872 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11873 ia32e = (vmcs12->vm_exit_controls &
11874 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11875 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11876 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11877 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11878 return 1;
11879 }
11880
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011881 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11882 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11883 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11884 return 1;
11885
Jim Mattsonca0bde22016-11-30 12:03:46 -080011886 return 0;
11887}
11888
Jim Mattson6514dc32018-04-26 16:09:12 -070011889static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011890{
11891 struct vcpu_vmx *vmx = to_vmx(vcpu);
11892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011893 u32 msr_entry_idx;
11894 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011895 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011896
Jim Mattson858e25c2016-11-30 12:03:47 -080011897 enter_guest_mode(vcpu);
11898
11899 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11900 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11901
Jim Mattsonde3a0022017-11-27 17:22:25 -060011902 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011903 vmx_segment_cache_clear(vmx);
11904
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011905 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11906 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11907
11908 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011909 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011910 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011911
11912 nested_get_vmcs12_pages(vcpu, vmcs12);
11913
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011914 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011915 msr_entry_idx = nested_vmx_load_msr(vcpu,
11916 vmcs12->vm_entry_msr_load_addr,
11917 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011918 if (msr_entry_idx)
11919 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011920
Jim Mattson858e25c2016-11-30 12:03:47 -080011921 /*
11922 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11923 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11924 * returned as far as L1 is concerned. It will only return (and set
11925 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11926 */
11927 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011928
11929fail:
11930 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11931 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11932 leave_guest_mode(vcpu);
11933 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11934 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11935 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011936}
11937
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011938/*
11939 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11940 * for running an L2 nested guest.
11941 */
11942static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11943{
11944 struct vmcs12 *vmcs12;
11945 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011946 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011947 u32 exit_qual;
11948 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011949
Kyle Hueyeb277562016-11-29 12:40:39 -080011950 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011951 return 1;
11952
Kyle Hueyeb277562016-11-29 12:40:39 -080011953 if (!nested_vmx_check_vmcs12(vcpu))
11954 goto out;
11955
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011956 vmcs12 = get_vmcs12(vcpu);
11957
Abel Gordon012f83c2013-04-18 14:39:25 +030011958 if (enable_shadow_vmcs)
11959 copy_shadow_to_vmcs12(vmx);
11960
Nadav Har'El7c177932011-05-25 23:12:04 +030011961 /*
11962 * The nested entry process starts with enforcing various prerequisites
11963 * on vmcs12 as required by the Intel SDM, and act appropriately when
11964 * they fail: As the SDM explains, some conditions should cause the
11965 * instruction to fail, while others will cause the instruction to seem
11966 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11967 * To speed up the normal (success) code path, we should avoid checking
11968 * for misconfigurations which will anyway be caught by the processor
11969 * when using the merged vmcs02.
11970 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011971 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11972 nested_vmx_failValid(vcpu,
11973 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11974 goto out;
11975 }
11976
Nadav Har'El7c177932011-05-25 23:12:04 +030011977 if (vmcs12->launch_state == launch) {
11978 nested_vmx_failValid(vcpu,
11979 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11980 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011981 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011982 }
11983
Jim Mattsonca0bde22016-11-30 12:03:46 -080011984 ret = check_vmentry_prereqs(vcpu, vmcs12);
11985 if (ret) {
11986 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011987 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011988 }
11989
Nadav Har'El7c177932011-05-25 23:12:04 +030011990 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011991 * After this point, the trap flag no longer triggers a singlestep trap
11992 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11993 * This is not 100% correct; for performance reasons, we delegate most
11994 * of the checks on host state to the processor. If those fail,
11995 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011996 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011997 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011998
Jim Mattsonca0bde22016-11-30 12:03:46 -080011999 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12000 if (ret) {
12001 nested_vmx_entry_failure(vcpu, vmcs12,
12002 EXIT_REASON_INVALID_STATE, exit_qual);
12003 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012004 }
12005
12006 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012007 * We're finally done with prerequisite checking, and can start with
12008 * the nested entry.
12009 */
12010
Jim Mattson6514dc32018-04-26 16:09:12 -070012011 vmx->nested.nested_run_pending = 1;
12012 ret = enter_vmx_non_root_mode(vcpu);
12013 if (ret) {
12014 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012015 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012016 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012017
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012018 /* Hide L1D cache contents from the nested guest. */
12019 vmx->vcpu.arch.l1tf_flush_l1d = true;
12020
Chao Gao135a06c2018-02-11 10:06:30 +080012021 /*
12022 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12023 * by event injection, halt vcpu.
12024 */
12025 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012026 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12027 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012028 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012029 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012030 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012031
12032out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012033 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012034}
12035
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012036/*
12037 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12038 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12039 * This function returns the new value we should put in vmcs12.guest_cr0.
12040 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12041 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12042 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12043 * didn't trap the bit, because if L1 did, so would L0).
12044 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12045 * been modified by L2, and L1 knows it. So just leave the old value of
12046 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12047 * isn't relevant, because if L0 traps this bit it can set it to anything.
12048 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12049 * changed these bits, and therefore they need to be updated, but L0
12050 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12051 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12052 */
12053static inline unsigned long
12054vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12055{
12056 return
12057 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12058 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12059 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12060 vcpu->arch.cr0_guest_owned_bits));
12061}
12062
12063static inline unsigned long
12064vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12065{
12066 return
12067 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12068 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12069 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12070 vcpu->arch.cr4_guest_owned_bits));
12071}
12072
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012073static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12074 struct vmcs12 *vmcs12)
12075{
12076 u32 idt_vectoring;
12077 unsigned int nr;
12078
Wanpeng Li664f8e22017-08-24 03:35:09 -070012079 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012080 nr = vcpu->arch.exception.nr;
12081 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12082
12083 if (kvm_exception_is_soft(nr)) {
12084 vmcs12->vm_exit_instruction_len =
12085 vcpu->arch.event_exit_inst_len;
12086 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12087 } else
12088 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12089
12090 if (vcpu->arch.exception.has_error_code) {
12091 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12092 vmcs12->idt_vectoring_error_code =
12093 vcpu->arch.exception.error_code;
12094 }
12095
12096 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012097 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012098 vmcs12->idt_vectoring_info_field =
12099 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012100 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012101 nr = vcpu->arch.interrupt.nr;
12102 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12103
12104 if (vcpu->arch.interrupt.soft) {
12105 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12106 vmcs12->vm_entry_instruction_len =
12107 vcpu->arch.event_exit_inst_len;
12108 } else
12109 idt_vectoring |= INTR_TYPE_EXT_INTR;
12110
12111 vmcs12->idt_vectoring_info_field = idt_vectoring;
12112 }
12113}
12114
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012115static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12116{
12117 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012118 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012119 bool block_nested_events =
12120 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012121
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012122 if (vcpu->arch.exception.pending &&
12123 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012124 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012125 return -EBUSY;
12126 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012127 return 0;
12128 }
12129
Jan Kiszkaf4124502014-03-07 20:03:13 +010012130 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12131 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012132 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012133 return -EBUSY;
12134 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12135 return 0;
12136 }
12137
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012138 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012139 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012140 return -EBUSY;
12141 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12142 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12143 INTR_INFO_VALID_MASK, 0);
12144 /*
12145 * The NMI-triggered VM exit counts as injection:
12146 * clear this one and block further NMIs.
12147 */
12148 vcpu->arch.nmi_pending = 0;
12149 vmx_set_nmi_mask(vcpu, true);
12150 return 0;
12151 }
12152
12153 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12154 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012155 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012156 return -EBUSY;
12157 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012158 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012159 }
12160
David Hildenbrand6342c502017-01-25 11:58:58 +010012161 vmx_complete_nested_posted_interrupt(vcpu);
12162 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012163}
12164
Jan Kiszkaf4124502014-03-07 20:03:13 +010012165static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12166{
12167 ktime_t remaining =
12168 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12169 u64 value;
12170
12171 if (ktime_to_ns(remaining) <= 0)
12172 return 0;
12173
12174 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12175 do_div(value, 1000000);
12176 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12177}
12178
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012179/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012180 * Update the guest state fields of vmcs12 to reflect changes that
12181 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12182 * VM-entry controls is also updated, since this is really a guest
12183 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012184 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012185static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012186{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012187 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12188 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12189
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012190 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12191 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12192 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12193
12194 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12195 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12196 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12197 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12198 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12199 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12200 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12201 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12202 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12203 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12204 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12205 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12206 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12207 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12208 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12209 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12210 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12211 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12212 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12213 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12214 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12215 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12216 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12217 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12218 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12219 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12220 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12221 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12222 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12223 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12224 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12225 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12226 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12227 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12228 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12229 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12230
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012231 vmcs12->guest_interruptibility_info =
12232 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12233 vmcs12->guest_pending_dbg_exceptions =
12234 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012235 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12236 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12237 else
12238 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012239
Jan Kiszkaf4124502014-03-07 20:03:13 +010012240 if (nested_cpu_has_preemption_timer(vmcs12)) {
12241 if (vmcs12->vm_exit_controls &
12242 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12243 vmcs12->vmx_preemption_timer_value =
12244 vmx_get_preemption_timer_value(vcpu);
12245 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12246 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012247
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012248 /*
12249 * In some cases (usually, nested EPT), L2 is allowed to change its
12250 * own CR3 without exiting. If it has changed it, we must keep it.
12251 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12252 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12253 *
12254 * Additionally, restore L2's PDPTR to vmcs12.
12255 */
12256 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012257 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012258 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12259 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12260 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12261 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12262 }
12263
Jim Mattsond281e132017-06-01 12:44:46 -070012264 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012265
Wincy Van608406e2015-02-03 23:57:51 +080012266 if (nested_cpu_has_vid(vmcs12))
12267 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12268
Jan Kiszkac18911a2013-03-13 16:06:41 +010012269 vmcs12->vm_entry_controls =
12270 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012271 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012272
Jan Kiszka2996fca2014-06-16 13:59:43 +020012273 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12274 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12275 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12276 }
12277
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012278 /* TODO: These cannot have changed unless we have MSR bitmaps and
12279 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012280 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012281 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012282 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12283 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012284 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12285 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12286 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012287 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012288 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012289}
12290
12291/*
12292 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12293 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12294 * and this function updates it to reflect the changes to the guest state while
12295 * L2 was running (and perhaps made some exits which were handled directly by L0
12296 * without going back to L1), and to reflect the exit reason.
12297 * Note that we do not have to copy here all VMCS fields, just those that
12298 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12299 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12300 * which already writes to vmcs12 directly.
12301 */
12302static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12303 u32 exit_reason, u32 exit_intr_info,
12304 unsigned long exit_qualification)
12305{
12306 /* update guest state fields: */
12307 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012308
12309 /* update exit information fields: */
12310
Jan Kiszka533558b2014-01-04 18:47:20 +010012311 vmcs12->vm_exit_reason = exit_reason;
12312 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012313 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012314
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012315 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012316 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12317 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12318
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012319 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012320 vmcs12->launch_state = 1;
12321
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012322 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12323 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012324 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012325
12326 /*
12327 * Transfer the event that L0 or L1 may wanted to inject into
12328 * L2 to IDT_VECTORING_INFO_FIELD.
12329 */
12330 vmcs12_save_pending_event(vcpu, vmcs12);
12331 }
12332
12333 /*
12334 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12335 * preserved above and would only end up incorrectly in L1.
12336 */
12337 vcpu->arch.nmi_injected = false;
12338 kvm_clear_exception_queue(vcpu);
12339 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012340}
12341
Wanpeng Li5af41572017-11-05 16:54:49 -080012342static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12343 struct vmcs12 *vmcs12)
12344{
12345 u32 entry_failure_code;
12346
12347 nested_ept_uninit_mmu_context(vcpu);
12348
12349 /*
12350 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12351 * couldn't have changed.
12352 */
12353 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12354 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12355
12356 if (!enable_ept)
12357 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12358}
12359
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012360/*
12361 * A part of what we need to when the nested L2 guest exits and we want to
12362 * run its L1 parent, is to reset L1's guest state to the host state specified
12363 * in vmcs12.
12364 * This function is to be called not only on normal nested exit, but also on
12365 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12366 * Failures During or After Loading Guest State").
12367 * This function should be called when the active VMCS is L1's (vmcs01).
12368 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012369static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12370 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012371{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012372 struct kvm_segment seg;
12373
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012374 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12375 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012376 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012377 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12378 else
12379 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12380 vmx_set_efer(vcpu, vcpu->arch.efer);
12381
12382 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12383 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012384 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012385 /*
12386 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012387 * actually changed, because vmx_set_cr0 refers to efer set above.
12388 *
12389 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12390 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012391 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012392 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012393 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012394
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012395 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012396 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012397 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012398
Wanpeng Li5af41572017-11-05 16:54:49 -080012399 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012400
Liran Alon6f1e03b2018-05-22 17:16:14 +030012401 /*
12402 * If vmcs01 don't use VPID, CPU flushes TLB on every
12403 * VMEntry/VMExit. Thus, no need to flush TLB.
12404 *
12405 * If vmcs12 uses VPID, TLB entries populated by L2 are
12406 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12407 * with vmx->vpid. Thus, no need to flush TLB.
12408 *
12409 * Therefore, flush TLB only in case vmcs01 uses VPID and
12410 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12411 * are both tagged with vmx->vpid.
12412 */
12413 if (enable_vpid &&
12414 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012415 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012416 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012417
12418 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12419 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12420 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12421 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12422 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012423 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12424 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012425
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012426 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12427 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12428 vmcs_write64(GUEST_BNDCFGS, 0);
12429
Jan Kiszka44811c02013-08-04 17:17:27 +020012430 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012431 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012432 vcpu->arch.pat = vmcs12->host_ia32_pat;
12433 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012434 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12435 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12436 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012437
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012438 /* Set L1 segment info according to Intel SDM
12439 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12440 seg = (struct kvm_segment) {
12441 .base = 0,
12442 .limit = 0xFFFFFFFF,
12443 .selector = vmcs12->host_cs_selector,
12444 .type = 11,
12445 .present = 1,
12446 .s = 1,
12447 .g = 1
12448 };
12449 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12450 seg.l = 1;
12451 else
12452 seg.db = 1;
12453 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12454 seg = (struct kvm_segment) {
12455 .base = 0,
12456 .limit = 0xFFFFFFFF,
12457 .type = 3,
12458 .present = 1,
12459 .s = 1,
12460 .db = 1,
12461 .g = 1
12462 };
12463 seg.selector = vmcs12->host_ds_selector;
12464 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12465 seg.selector = vmcs12->host_es_selector;
12466 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12467 seg.selector = vmcs12->host_ss_selector;
12468 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12469 seg.selector = vmcs12->host_fs_selector;
12470 seg.base = vmcs12->host_fs_base;
12471 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12472 seg.selector = vmcs12->host_gs_selector;
12473 seg.base = vmcs12->host_gs_base;
12474 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12475 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012476 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012477 .limit = 0x67,
12478 .selector = vmcs12->host_tr_selector,
12479 .type = 11,
12480 .present = 1
12481 };
12482 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12483
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012484 kvm_set_dr(vcpu, 7, 0x400);
12485 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012486
Wincy Van3af18d92015-02-03 23:49:31 +080012487 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012488 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012489
Wincy Vanff651cb2014-12-11 08:52:58 +030012490 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12491 vmcs12->vm_exit_msr_load_count))
12492 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012493}
12494
12495/*
12496 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12497 * and modify vmcs12 to make it see what it would expect to see there if
12498 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12499 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012500static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12501 u32 exit_intr_info,
12502 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012503{
12504 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012505 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12506
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012507 /* trying to cancel vmlaunch/vmresume is a bug */
12508 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12509
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012510 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012511 * The only expected VM-instruction error is "VM entry with
12512 * invalid control field(s)." Anything else indicates a
12513 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012514 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012515 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12516 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12517
12518 leave_guest_mode(vcpu);
12519
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012520 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12521 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12522
Jim Mattson4f350c62017-09-14 16:31:44 -070012523 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012524 if (exit_reason == -1)
12525 sync_vmcs12(vcpu, vmcs12);
12526 else
12527 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12528 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012529
12530 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12531 vmcs12->vm_exit_msr_store_count))
12532 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012533 }
12534
Jim Mattson4f350c62017-09-14 16:31:44 -070012535 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012536 vm_entry_controls_reset_shadow(vmx);
12537 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012538 vmx_segment_cache_clear(vmx);
12539
Paolo Bonzini93140062016-07-06 13:23:51 +020012540 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012541 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12542 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012543 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012544 if (vmx->hv_deadline_tsc == -1)
12545 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12546 PIN_BASED_VMX_PREEMPTION_TIMER);
12547 else
12548 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12549 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012550 if (kvm_has_tsc_control)
12551 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012552
Jim Mattson8d860bb2018-05-09 16:56:05 -040012553 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12554 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12555 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012556 } else if (!nested_cpu_has_ept(vmcs12) &&
12557 nested_cpu_has2(vmcs12,
12558 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012559 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012560 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012561
12562 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12563 vmx->host_rsp = 0;
12564
12565 /* Unpin physical memory we referred to in vmcs02 */
12566 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012567 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012568 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012569 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012570 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012571 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012572 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012573 }
Wincy Van705699a2015-02-03 23:58:17 +080012574 if (vmx->nested.pi_desc_page) {
12575 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012576 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012577 vmx->nested.pi_desc_page = NULL;
12578 vmx->nested.pi_desc = NULL;
12579 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012580
12581 /*
Tang Chen38b99172014-09-24 15:57:54 +080012582 * We are now running in L2, mmu_notifier will force to reload the
12583 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12584 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012585 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012586
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012587 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012588 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012589
12590 /* in case we halted in L2 */
12591 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012592
12593 if (likely(!vmx->fail)) {
12594 /*
12595 * TODO: SDM says that with acknowledge interrupt on
12596 * exit, bit 31 of the VM-exit interrupt information
12597 * (valid interrupt) is always set to 1 on
12598 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12599 * need kvm_cpu_has_interrupt(). See the commit
12600 * message for details.
12601 */
12602 if (nested_exit_intr_ack_set(vcpu) &&
12603 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12604 kvm_cpu_has_interrupt(vcpu)) {
12605 int irq = kvm_cpu_get_interrupt(vcpu);
12606 WARN_ON(irq < 0);
12607 vmcs12->vm_exit_intr_info = irq |
12608 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12609 }
12610
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012611 if (exit_reason != -1)
12612 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12613 vmcs12->exit_qualification,
12614 vmcs12->idt_vectoring_info_field,
12615 vmcs12->vm_exit_intr_info,
12616 vmcs12->vm_exit_intr_error_code,
12617 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012618
12619 load_vmcs12_host_state(vcpu, vmcs12);
12620
12621 return;
12622 }
12623
12624 /*
12625 * After an early L2 VM-entry failure, we're now back
12626 * in L1 which thinks it just finished a VMLAUNCH or
12627 * VMRESUME instruction, so we need to set the failure
12628 * flag and the VM-instruction error field of the VMCS
12629 * accordingly.
12630 */
12631 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012632
12633 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12634
Jim Mattson4f350c62017-09-14 16:31:44 -070012635 /*
12636 * The emulated instruction was already skipped in
12637 * nested_vmx_run, but the updated RIP was never
12638 * written back to the vmcs01.
12639 */
12640 skip_emulated_instruction(vcpu);
12641 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012642}
12643
Nadav Har'El7c177932011-05-25 23:12:04 +030012644/*
Jan Kiszka42124922014-01-04 18:47:19 +010012645 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12646 */
12647static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12648{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012649 if (is_guest_mode(vcpu)) {
12650 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012651 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012652 }
Jan Kiszka42124922014-01-04 18:47:19 +010012653 free_nested(to_vmx(vcpu));
12654}
12655
12656/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012657 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12658 * 23.7 "VM-entry failures during or after loading guest state" (this also
12659 * lists the acceptable exit-reason and exit-qualification parameters).
12660 * It should only be called before L2 actually succeeded to run, and when
12661 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12662 */
12663static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12664 struct vmcs12 *vmcs12,
12665 u32 reason, unsigned long qualification)
12666{
12667 load_vmcs12_host_state(vcpu, vmcs12);
12668 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12669 vmcs12->exit_qualification = qualification;
12670 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012671 if (enable_shadow_vmcs)
12672 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012673}
12674
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012675static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12676 struct x86_instruction_info *info,
12677 enum x86_intercept_stage stage)
12678{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012679 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12680 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12681
12682 /*
12683 * RDPID causes #UD if disabled through secondary execution controls.
12684 * Because it is marked as EmulateOnUD, we need to intercept it here.
12685 */
12686 if (info->intercept == x86_intercept_rdtscp &&
12687 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12688 ctxt->exception.vector = UD_VECTOR;
12689 ctxt->exception.error_code_valid = false;
12690 return X86EMUL_PROPAGATE_FAULT;
12691 }
12692
12693 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012694 return X86EMUL_CONTINUE;
12695}
12696
Yunhong Jiang64672c92016-06-13 14:19:59 -070012697#ifdef CONFIG_X86_64
12698/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12699static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12700 u64 divisor, u64 *result)
12701{
12702 u64 low = a << shift, high = a >> (64 - shift);
12703
12704 /* To avoid the overflow on divq */
12705 if (high >= divisor)
12706 return 1;
12707
12708 /* Low hold the result, high hold rem which is discarded */
12709 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12710 "rm" (divisor), "0" (low), "1" (high));
12711 *result = low;
12712
12713 return 0;
12714}
12715
12716static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12717{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012718 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012719 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012720
12721 if (kvm_mwait_in_guest(vcpu->kvm))
12722 return -EOPNOTSUPP;
12723
12724 vmx = to_vmx(vcpu);
12725 tscl = rdtsc();
12726 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12727 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012728 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12729
12730 if (delta_tsc > lapic_timer_advance_cycles)
12731 delta_tsc -= lapic_timer_advance_cycles;
12732 else
12733 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012734
12735 /* Convert to host delta tsc if tsc scaling is enabled */
12736 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12737 u64_shl_div_u64(delta_tsc,
12738 kvm_tsc_scaling_ratio_frac_bits,
12739 vcpu->arch.tsc_scaling_ratio,
12740 &delta_tsc))
12741 return -ERANGE;
12742
12743 /*
12744 * If the delta tsc can't fit in the 32 bit after the multi shift,
12745 * we can't use the preemption timer.
12746 * It's possible that it fits on later vmentries, but checking
12747 * on every vmentry is costly so we just use an hrtimer.
12748 */
12749 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12750 return -ERANGE;
12751
12752 vmx->hv_deadline_tsc = tscl + delta_tsc;
12753 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12754 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012755
12756 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012757}
12758
12759static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12760{
12761 struct vcpu_vmx *vmx = to_vmx(vcpu);
12762 vmx->hv_deadline_tsc = -1;
12763 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12764 PIN_BASED_VMX_PREEMPTION_TIMER);
12765}
12766#endif
12767
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012768static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012769{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012770 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012771 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012772}
12773
Kai Huang843e4332015-01-28 10:54:28 +080012774static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12775 struct kvm_memory_slot *slot)
12776{
12777 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12778 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12779}
12780
12781static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12782 struct kvm_memory_slot *slot)
12783{
12784 kvm_mmu_slot_set_dirty(kvm, slot);
12785}
12786
12787static void vmx_flush_log_dirty(struct kvm *kvm)
12788{
12789 kvm_flush_pml_buffers(kvm);
12790}
12791
Bandan Dasc5f983f2017-05-05 15:25:14 -040012792static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12793{
12794 struct vmcs12 *vmcs12;
12795 struct vcpu_vmx *vmx = to_vmx(vcpu);
12796 gpa_t gpa;
12797 struct page *page = NULL;
12798 u64 *pml_address;
12799
12800 if (is_guest_mode(vcpu)) {
12801 WARN_ON_ONCE(vmx->nested.pml_full);
12802
12803 /*
12804 * Check if PML is enabled for the nested guest.
12805 * Whether eptp bit 6 is set is already checked
12806 * as part of A/D emulation.
12807 */
12808 vmcs12 = get_vmcs12(vcpu);
12809 if (!nested_cpu_has_pml(vmcs12))
12810 return 0;
12811
Dan Carpenter47698862017-05-10 22:43:17 +030012812 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012813 vmx->nested.pml_full = true;
12814 return 1;
12815 }
12816
12817 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12818
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012819 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12820 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012821 return 0;
12822
12823 pml_address = kmap(page);
12824 pml_address[vmcs12->guest_pml_index--] = gpa;
12825 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012826 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012827 }
12828
12829 return 0;
12830}
12831
Kai Huang843e4332015-01-28 10:54:28 +080012832static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12833 struct kvm_memory_slot *memslot,
12834 gfn_t offset, unsigned long mask)
12835{
12836 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12837}
12838
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012839static void __pi_post_block(struct kvm_vcpu *vcpu)
12840{
12841 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12842 struct pi_desc old, new;
12843 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012844
12845 do {
12846 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012847 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12848 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012849
12850 dest = cpu_physical_id(vcpu->cpu);
12851
12852 if (x2apic_enabled())
12853 new.ndst = dest;
12854 else
12855 new.ndst = (dest << 8) & 0xFF00;
12856
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012857 /* set 'NV' to 'notification vector' */
12858 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012859 } while (cmpxchg64(&pi_desc->control, old.control,
12860 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012861
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012862 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12863 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012864 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012865 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012866 vcpu->pre_pcpu = -1;
12867 }
12868}
12869
Feng Wuefc64402015-09-18 22:29:51 +080012870/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012871 * This routine does the following things for vCPU which is going
12872 * to be blocked if VT-d PI is enabled.
12873 * - Store the vCPU to the wakeup list, so when interrupts happen
12874 * we can find the right vCPU to wake up.
12875 * - Change the Posted-interrupt descriptor as below:
12876 * 'NDST' <-- vcpu->pre_pcpu
12877 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12878 * - If 'ON' is set during this process, which means at least one
12879 * interrupt is posted for this vCPU, we cannot block it, in
12880 * this case, return 1, otherwise, return 0.
12881 *
12882 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012883static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012884{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012885 unsigned int dest;
12886 struct pi_desc old, new;
12887 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12888
12889 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012890 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12891 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012892 return 0;
12893
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012894 WARN_ON(irqs_disabled());
12895 local_irq_disable();
12896 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12897 vcpu->pre_pcpu = vcpu->cpu;
12898 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12899 list_add_tail(&vcpu->blocked_vcpu_list,
12900 &per_cpu(blocked_vcpu_on_cpu,
12901 vcpu->pre_pcpu));
12902 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12903 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012904
12905 do {
12906 old.control = new.control = pi_desc->control;
12907
Feng Wubf9f6ac2015-09-18 22:29:55 +080012908 WARN((pi_desc->sn == 1),
12909 "Warning: SN field of posted-interrupts "
12910 "is set before blocking\n");
12911
12912 /*
12913 * Since vCPU can be preempted during this process,
12914 * vcpu->cpu could be different with pre_pcpu, we
12915 * need to set pre_pcpu as the destination of wakeup
12916 * notification event, then we can find the right vCPU
12917 * to wakeup in wakeup handler if interrupts happen
12918 * when the vCPU is in blocked state.
12919 */
12920 dest = cpu_physical_id(vcpu->pre_pcpu);
12921
12922 if (x2apic_enabled())
12923 new.ndst = dest;
12924 else
12925 new.ndst = (dest << 8) & 0xFF00;
12926
12927 /* set 'NV' to 'wakeup vector' */
12928 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012929 } while (cmpxchg64(&pi_desc->control, old.control,
12930 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012931
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012932 /* We should not block the vCPU if an interrupt is posted for it. */
12933 if (pi_test_on(pi_desc) == 1)
12934 __pi_post_block(vcpu);
12935
12936 local_irq_enable();
12937 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012938}
12939
Yunhong Jiangbc225122016-06-13 14:19:58 -070012940static int vmx_pre_block(struct kvm_vcpu *vcpu)
12941{
12942 if (pi_pre_block(vcpu))
12943 return 1;
12944
Yunhong Jiang64672c92016-06-13 14:19:59 -070012945 if (kvm_lapic_hv_timer_in_use(vcpu))
12946 kvm_lapic_switch_to_sw_timer(vcpu);
12947
Yunhong Jiangbc225122016-06-13 14:19:58 -070012948 return 0;
12949}
12950
12951static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012952{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012953 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012954 return;
12955
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012956 WARN_ON(irqs_disabled());
12957 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012958 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012959 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012960}
12961
Yunhong Jiangbc225122016-06-13 14:19:58 -070012962static void vmx_post_block(struct kvm_vcpu *vcpu)
12963{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012964 if (kvm_x86_ops->set_hv_timer)
12965 kvm_lapic_switch_to_hv_timer(vcpu);
12966
Yunhong Jiangbc225122016-06-13 14:19:58 -070012967 pi_post_block(vcpu);
12968}
12969
Feng Wubf9f6ac2015-09-18 22:29:55 +080012970/*
Feng Wuefc64402015-09-18 22:29:51 +080012971 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12972 *
12973 * @kvm: kvm
12974 * @host_irq: host irq of the interrupt
12975 * @guest_irq: gsi of the interrupt
12976 * @set: set or unset PI
12977 * returns 0 on success, < 0 on failure
12978 */
12979static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12980 uint32_t guest_irq, bool set)
12981{
12982 struct kvm_kernel_irq_routing_entry *e;
12983 struct kvm_irq_routing_table *irq_rt;
12984 struct kvm_lapic_irq irq;
12985 struct kvm_vcpu *vcpu;
12986 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012987 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012988
12989 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012990 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12991 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012992 return 0;
12993
12994 idx = srcu_read_lock(&kvm->irq_srcu);
12995 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012996 if (guest_irq >= irq_rt->nr_rt_entries ||
12997 hlist_empty(&irq_rt->map[guest_irq])) {
12998 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12999 guest_irq, irq_rt->nr_rt_entries);
13000 goto out;
13001 }
Feng Wuefc64402015-09-18 22:29:51 +080013002
13003 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13004 if (e->type != KVM_IRQ_ROUTING_MSI)
13005 continue;
13006 /*
13007 * VT-d PI cannot support posting multicast/broadcast
13008 * interrupts to a vCPU, we still use interrupt remapping
13009 * for these kind of interrupts.
13010 *
13011 * For lowest-priority interrupts, we only support
13012 * those with single CPU as the destination, e.g. user
13013 * configures the interrupts via /proc/irq or uses
13014 * irqbalance to make the interrupts single-CPU.
13015 *
13016 * We will support full lowest-priority interrupt later.
13017 */
13018
Radim Krčmář371313132016-07-12 22:09:27 +020013019 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013020 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13021 /*
13022 * Make sure the IRTE is in remapped mode if
13023 * we don't handle it in posted mode.
13024 */
13025 ret = irq_set_vcpu_affinity(host_irq, NULL);
13026 if (ret < 0) {
13027 printk(KERN_INFO
13028 "failed to back to remapped mode, irq: %u\n",
13029 host_irq);
13030 goto out;
13031 }
13032
Feng Wuefc64402015-09-18 22:29:51 +080013033 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013034 }
Feng Wuefc64402015-09-18 22:29:51 +080013035
13036 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13037 vcpu_info.vector = irq.vector;
13038
hu huajun2698d822018-04-11 15:16:40 +080013039 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013040 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13041
13042 if (set)
13043 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013044 else
Feng Wuefc64402015-09-18 22:29:51 +080013045 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013046
13047 if (ret < 0) {
13048 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13049 __func__);
13050 goto out;
13051 }
13052 }
13053
13054 ret = 0;
13055out:
13056 srcu_read_unlock(&kvm->irq_srcu, idx);
13057 return ret;
13058}
13059
Ashok Rajc45dcc72016-06-22 14:59:56 +080013060static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13061{
13062 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13063 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13064 FEATURE_CONTROL_LMCE;
13065 else
13066 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13067 ~FEATURE_CONTROL_LMCE;
13068}
13069
Ladi Prosek72d7b372017-10-11 16:54:41 +020013070static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13071{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013072 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13073 if (to_vmx(vcpu)->nested.nested_run_pending)
13074 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013075 return 1;
13076}
13077
Ladi Prosek0234bf82017-10-11 16:54:40 +020013078static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13079{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013080 struct vcpu_vmx *vmx = to_vmx(vcpu);
13081
13082 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13083 if (vmx->nested.smm.guest_mode)
13084 nested_vmx_vmexit(vcpu, -1, 0, 0);
13085
13086 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13087 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013088 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013089 return 0;
13090}
13091
13092static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13093{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013094 struct vcpu_vmx *vmx = to_vmx(vcpu);
13095 int ret;
13096
13097 if (vmx->nested.smm.vmxon) {
13098 vmx->nested.vmxon = true;
13099 vmx->nested.smm.vmxon = false;
13100 }
13101
13102 if (vmx->nested.smm.guest_mode) {
13103 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013104 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013105 vcpu->arch.hflags |= HF_SMM_MASK;
13106 if (ret)
13107 return ret;
13108
13109 vmx->nested.smm.guest_mode = false;
13110 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013111 return 0;
13112}
13113
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013114static int enable_smi_window(struct kvm_vcpu *vcpu)
13115{
13116 return 0;
13117}
13118
Kees Cook404f6aa2016-08-08 16:29:06 -070013119static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013120 .cpu_has_kvm_support = cpu_has_kvm_support,
13121 .disabled_by_bios = vmx_disabled_by_bios,
13122 .hardware_setup = hardware_setup,
13123 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013124 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013125 .hardware_enable = hardware_enable,
13126 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013127 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013128 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013129
Wanpeng Lib31c1142018-03-12 04:53:04 -070013130 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013131 .vm_alloc = vmx_vm_alloc,
13132 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013133
Avi Kivity6aa8b732006-12-10 02:21:36 -080013134 .vcpu_create = vmx_create_vcpu,
13135 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013136 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013137
Avi Kivity04d2cc72007-09-10 18:10:54 +030013138 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013139 .vcpu_load = vmx_vcpu_load,
13140 .vcpu_put = vmx_vcpu_put,
13141
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013142 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013143 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013144 .get_msr = vmx_get_msr,
13145 .set_msr = vmx_set_msr,
13146 .get_segment_base = vmx_get_segment_base,
13147 .get_segment = vmx_get_segment,
13148 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013149 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013150 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013151 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013152 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013153 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013154 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013155 .set_cr3 = vmx_set_cr3,
13156 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013157 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013158 .get_idt = vmx_get_idt,
13159 .set_idt = vmx_set_idt,
13160 .get_gdt = vmx_get_gdt,
13161 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013162 .get_dr6 = vmx_get_dr6,
13163 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013164 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013165 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013166 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013167 .get_rflags = vmx_get_rflags,
13168 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013169
Avi Kivity6aa8b732006-12-10 02:21:36 -080013170 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013171
Avi Kivity6aa8b732006-12-10 02:21:36 -080013172 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013173 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013174 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013175 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13176 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013177 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013178 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013179 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013180 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013181 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013182 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013183 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013184 .get_nmi_mask = vmx_get_nmi_mask,
13185 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013186 .enable_nmi_window = enable_nmi_window,
13187 .enable_irq_window = enable_irq_window,
13188 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013189 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013190 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013191 .get_enable_apicv = vmx_get_enable_apicv,
13192 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013193 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013194 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013195 .hwapic_irr_update = vmx_hwapic_irr_update,
13196 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013197 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13198 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013199
Izik Eiduscbc94022007-10-25 00:29:55 +020013200 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013201 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013202 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013203 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013204
Avi Kivity586f9602010-11-18 13:09:54 +020013205 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013206
Sheng Yang17cc3932010-01-05 19:02:27 +080013207 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013208
13209 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013210
13211 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013212 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013213
13214 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013215
13216 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013217
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013218 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013219 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013220
13221 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013222
13223 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013224 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013225 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013226 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013227 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013228
13229 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013230
13231 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013232
13233 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13234 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13235 .flush_log_dirty = vmx_flush_log_dirty,
13236 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013237 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013238
Feng Wubf9f6ac2015-09-18 22:29:55 +080013239 .pre_block = vmx_pre_block,
13240 .post_block = vmx_post_block,
13241
Wei Huang25462f72015-06-19 15:45:05 +020013242 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013243
13244 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013245
13246#ifdef CONFIG_X86_64
13247 .set_hv_timer = vmx_set_hv_timer,
13248 .cancel_hv_timer = vmx_cancel_hv_timer,
13249#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013250
13251 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013252
Ladi Prosek72d7b372017-10-11 16:54:41 +020013253 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013254 .pre_enter_smm = vmx_pre_enter_smm,
13255 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013256 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013257};
13258
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013259static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013260{
13261 if (vmx_l1d_flush_pages) {
13262 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13263 vmx_l1d_flush_pages = NULL;
13264 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013265 /* Restore state so sysfs ignores VMX */
13266 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013267}
13268
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013269static void vmx_exit(void)
13270{
13271#ifdef CONFIG_KEXEC_CORE
13272 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13273 synchronize_rcu();
13274#endif
13275
13276 kvm_exit();
13277
13278#if IS_ENABLED(CONFIG_HYPERV)
13279 if (static_branch_unlikely(&enable_evmcs)) {
13280 int cpu;
13281 struct hv_vp_assist_page *vp_ap;
13282 /*
13283 * Reset everything to support using non-enlightened VMCS
13284 * access later (e.g. when we reload the module with
13285 * enlightened_vmcs=0)
13286 */
13287 for_each_online_cpu(cpu) {
13288 vp_ap = hv_get_vp_assist_page(cpu);
13289
13290 if (!vp_ap)
13291 continue;
13292
13293 vp_ap->current_nested_vmcs = 0;
13294 vp_ap->enlighten_vmentry = 0;
13295 }
13296
13297 static_branch_disable(&enable_evmcs);
13298 }
13299#endif
13300 vmx_cleanup_l1d_flush();
13301}
13302module_exit(vmx_exit);
13303
Avi Kivity6aa8b732006-12-10 02:21:36 -080013304static int __init vmx_init(void)
13305{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013306 int r;
13307
13308#if IS_ENABLED(CONFIG_HYPERV)
13309 /*
13310 * Enlightened VMCS usage should be recommended and the host needs
13311 * to support eVMCS v1 or above. We can also disable eVMCS support
13312 * with module parameter.
13313 */
13314 if (enlightened_vmcs &&
13315 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13316 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13317 KVM_EVMCS_VERSION) {
13318 int cpu;
13319
13320 /* Check that we have assist pages on all online CPUs */
13321 for_each_online_cpu(cpu) {
13322 if (!hv_get_vp_assist_page(cpu)) {
13323 enlightened_vmcs = false;
13324 break;
13325 }
13326 }
13327
13328 if (enlightened_vmcs) {
13329 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13330 static_branch_enable(&enable_evmcs);
13331 }
13332 } else {
13333 enlightened_vmcs = false;
13334 }
13335#endif
13336
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013337 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13338 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013339 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013340 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013341
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013342 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013343 * Must be called after kvm_init() so enable_ept is properly set
13344 * up. Hand the parameter mitigation value in which was stored in
13345 * the pre module init parser. If no parameter was given, it will
13346 * contain 'auto' which will be turned into the default 'cond'
13347 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013348 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013349 if (boot_cpu_has(X86_BUG_L1TF)) {
13350 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13351 if (r) {
13352 vmx_exit();
13353 return r;
13354 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013355 }
13356
Dave Young2965faa2015-09-09 15:38:55 -070013357#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013358 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13359 crash_vmclear_local_loaded_vmcss);
13360#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013361 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013362
He, Qingfdef3ad2007-04-30 09:45:24 +030013363 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013364}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013365module_init(vmx_init);