blob: eb7c207a3bc3e505a65e50fc12eb7120d7d5abbd [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);
195
196/* These MUST be in sync with vmentry_l1d_param order. */
197enum vmx_l1d_flush_state {
198 VMENTER_L1D_FLUSH_NEVER,
199 VMENTER_L1D_FLUSH_COND,
200 VMENTER_L1D_FLUSH_ALWAYS,
201};
202
203static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND;
204
205static const struct {
206 const char *option;
207 enum vmx_l1d_flush_state cmd;
208} vmentry_l1d_param[] = {
209 {"never", VMENTER_L1D_FLUSH_NEVER},
210 {"cond", VMENTER_L1D_FLUSH_COND},
211 {"always", VMENTER_L1D_FLUSH_ALWAYS},
212};
213
214static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
215{
216 unsigned int i;
217
218 if (!s)
219 return -EINVAL;
220
221 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
222 if (!strcmp(s, vmentry_l1d_param[i].option)) {
223 vmentry_l1d_flush = vmentry_l1d_param[i].cmd;
224 return 0;
225 }
226 }
227
228 return -EINVAL;
229}
230
231static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
232{
233 return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option);
234}
235
236static const struct kernel_param_ops vmentry_l1d_flush_ops = {
237 .set = vmentry_l1d_flush_set,
238 .get = vmentry_l1d_flush_get,
239};
240module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO);
241
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700242struct kvm_vmx {
243 struct kvm kvm;
244
245 unsigned int tss_addr;
246 bool ept_identity_pagetable_done;
247 gpa_t ept_identity_map_addr;
248};
249
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200250#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300251
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400252struct vmcs {
253 u32 revision_id;
254 u32 abort;
255 char data[0];
256};
257
Nadav Har'Eld462b812011-05-24 15:26:10 +0300258/*
259 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
260 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
261 * loaded on this CPU (so we can clear them if the CPU goes down).
262 */
263struct loaded_vmcs {
264 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700265 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300266 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200267 bool launched;
268 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200269 unsigned long vmcs_host_cr3; /* May not match real cr3 */
270 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100271 /* Support for vnmi-less CPUs */
272 int soft_vnmi_blocked;
273 ktime_t entry_time;
274 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100275 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300276 struct list_head loaded_vmcss_on_cpu_link;
277};
278
Avi Kivity26bb0982009-09-07 11:14:12 +0300279struct shared_msr_entry {
280 unsigned index;
281 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200282 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300283};
284
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300285/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300286 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
287 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
288 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
289 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
290 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
291 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600292 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300293 * underlying hardware which will be used to run L2.
294 * This structure is packed to ensure that its layout is identical across
295 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700296 *
297 * IMPORTANT: Changing the layout of existing fields in this structure
298 * will break save/restore compatibility with older kvm releases. When
299 * adding new fields, either use space in the reserved padding* arrays
300 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300301 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300302typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300303struct __packed vmcs12 {
304 /* According to the Intel spec, a VMCS region must start with the
305 * following two fields. Then follow implementation-specific data.
306 */
307 u32 revision_id;
308 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300309
Nadav Har'El27d6c862011-05-25 23:06:59 +0300310 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
311 u32 padding[7]; /* room for future expansion */
312
Nadav Har'El22bd0352011-05-25 23:05:57 +0300313 u64 io_bitmap_a;
314 u64 io_bitmap_b;
315 u64 msr_bitmap;
316 u64 vm_exit_msr_store_addr;
317 u64 vm_exit_msr_load_addr;
318 u64 vm_entry_msr_load_addr;
319 u64 tsc_offset;
320 u64 virtual_apic_page_addr;
321 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800322 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300323 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800324 u64 eoi_exit_bitmap0;
325 u64 eoi_exit_bitmap1;
326 u64 eoi_exit_bitmap2;
327 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800328 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300329 u64 guest_physical_address;
330 u64 vmcs_link_pointer;
331 u64 guest_ia32_debugctl;
332 u64 guest_ia32_pat;
333 u64 guest_ia32_efer;
334 u64 guest_ia32_perf_global_ctrl;
335 u64 guest_pdptr0;
336 u64 guest_pdptr1;
337 u64 guest_pdptr2;
338 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100339 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300340 u64 host_ia32_pat;
341 u64 host_ia32_efer;
342 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700343 u64 vmread_bitmap;
344 u64 vmwrite_bitmap;
345 u64 vm_function_control;
346 u64 eptp_list_address;
347 u64 pml_address;
348 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300349 /*
350 * To allow migration of L1 (complete with its L2 guests) between
351 * machines of different natural widths (32 or 64 bit), we cannot have
352 * unsigned long fields with no explict size. We use u64 (aliased
353 * natural_width) instead. Luckily, x86 is little-endian.
354 */
355 natural_width cr0_guest_host_mask;
356 natural_width cr4_guest_host_mask;
357 natural_width cr0_read_shadow;
358 natural_width cr4_read_shadow;
359 natural_width cr3_target_value0;
360 natural_width cr3_target_value1;
361 natural_width cr3_target_value2;
362 natural_width cr3_target_value3;
363 natural_width exit_qualification;
364 natural_width guest_linear_address;
365 natural_width guest_cr0;
366 natural_width guest_cr3;
367 natural_width guest_cr4;
368 natural_width guest_es_base;
369 natural_width guest_cs_base;
370 natural_width guest_ss_base;
371 natural_width guest_ds_base;
372 natural_width guest_fs_base;
373 natural_width guest_gs_base;
374 natural_width guest_ldtr_base;
375 natural_width guest_tr_base;
376 natural_width guest_gdtr_base;
377 natural_width guest_idtr_base;
378 natural_width guest_dr7;
379 natural_width guest_rsp;
380 natural_width guest_rip;
381 natural_width guest_rflags;
382 natural_width guest_pending_dbg_exceptions;
383 natural_width guest_sysenter_esp;
384 natural_width guest_sysenter_eip;
385 natural_width host_cr0;
386 natural_width host_cr3;
387 natural_width host_cr4;
388 natural_width host_fs_base;
389 natural_width host_gs_base;
390 natural_width host_tr_base;
391 natural_width host_gdtr_base;
392 natural_width host_idtr_base;
393 natural_width host_ia32_sysenter_esp;
394 natural_width host_ia32_sysenter_eip;
395 natural_width host_rsp;
396 natural_width host_rip;
397 natural_width paddingl[8]; /* room for future expansion */
398 u32 pin_based_vm_exec_control;
399 u32 cpu_based_vm_exec_control;
400 u32 exception_bitmap;
401 u32 page_fault_error_code_mask;
402 u32 page_fault_error_code_match;
403 u32 cr3_target_count;
404 u32 vm_exit_controls;
405 u32 vm_exit_msr_store_count;
406 u32 vm_exit_msr_load_count;
407 u32 vm_entry_controls;
408 u32 vm_entry_msr_load_count;
409 u32 vm_entry_intr_info_field;
410 u32 vm_entry_exception_error_code;
411 u32 vm_entry_instruction_len;
412 u32 tpr_threshold;
413 u32 secondary_vm_exec_control;
414 u32 vm_instruction_error;
415 u32 vm_exit_reason;
416 u32 vm_exit_intr_info;
417 u32 vm_exit_intr_error_code;
418 u32 idt_vectoring_info_field;
419 u32 idt_vectoring_error_code;
420 u32 vm_exit_instruction_len;
421 u32 vmx_instruction_info;
422 u32 guest_es_limit;
423 u32 guest_cs_limit;
424 u32 guest_ss_limit;
425 u32 guest_ds_limit;
426 u32 guest_fs_limit;
427 u32 guest_gs_limit;
428 u32 guest_ldtr_limit;
429 u32 guest_tr_limit;
430 u32 guest_gdtr_limit;
431 u32 guest_idtr_limit;
432 u32 guest_es_ar_bytes;
433 u32 guest_cs_ar_bytes;
434 u32 guest_ss_ar_bytes;
435 u32 guest_ds_ar_bytes;
436 u32 guest_fs_ar_bytes;
437 u32 guest_gs_ar_bytes;
438 u32 guest_ldtr_ar_bytes;
439 u32 guest_tr_ar_bytes;
440 u32 guest_interruptibility_info;
441 u32 guest_activity_state;
442 u32 guest_sysenter_cs;
443 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100444 u32 vmx_preemption_timer_value;
445 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300446 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800447 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300448 u16 guest_es_selector;
449 u16 guest_cs_selector;
450 u16 guest_ss_selector;
451 u16 guest_ds_selector;
452 u16 guest_fs_selector;
453 u16 guest_gs_selector;
454 u16 guest_ldtr_selector;
455 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800456 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300457 u16 host_es_selector;
458 u16 host_cs_selector;
459 u16 host_ss_selector;
460 u16 host_ds_selector;
461 u16 host_fs_selector;
462 u16 host_gs_selector;
463 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700464 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300465};
466
467/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700468 * For save/restore compatibility, the vmcs12 field offsets must not change.
469 */
470#define CHECK_OFFSET(field, loc) \
471 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
472 "Offset of " #field " in struct vmcs12 has changed.")
473
474static inline void vmx_check_vmcs12_offsets(void) {
475 CHECK_OFFSET(revision_id, 0);
476 CHECK_OFFSET(abort, 4);
477 CHECK_OFFSET(launch_state, 8);
478 CHECK_OFFSET(io_bitmap_a, 40);
479 CHECK_OFFSET(io_bitmap_b, 48);
480 CHECK_OFFSET(msr_bitmap, 56);
481 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
482 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
483 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
484 CHECK_OFFSET(tsc_offset, 88);
485 CHECK_OFFSET(virtual_apic_page_addr, 96);
486 CHECK_OFFSET(apic_access_addr, 104);
487 CHECK_OFFSET(posted_intr_desc_addr, 112);
488 CHECK_OFFSET(ept_pointer, 120);
489 CHECK_OFFSET(eoi_exit_bitmap0, 128);
490 CHECK_OFFSET(eoi_exit_bitmap1, 136);
491 CHECK_OFFSET(eoi_exit_bitmap2, 144);
492 CHECK_OFFSET(eoi_exit_bitmap3, 152);
493 CHECK_OFFSET(xss_exit_bitmap, 160);
494 CHECK_OFFSET(guest_physical_address, 168);
495 CHECK_OFFSET(vmcs_link_pointer, 176);
496 CHECK_OFFSET(guest_ia32_debugctl, 184);
497 CHECK_OFFSET(guest_ia32_pat, 192);
498 CHECK_OFFSET(guest_ia32_efer, 200);
499 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
500 CHECK_OFFSET(guest_pdptr0, 216);
501 CHECK_OFFSET(guest_pdptr1, 224);
502 CHECK_OFFSET(guest_pdptr2, 232);
503 CHECK_OFFSET(guest_pdptr3, 240);
504 CHECK_OFFSET(guest_bndcfgs, 248);
505 CHECK_OFFSET(host_ia32_pat, 256);
506 CHECK_OFFSET(host_ia32_efer, 264);
507 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
508 CHECK_OFFSET(vmread_bitmap, 280);
509 CHECK_OFFSET(vmwrite_bitmap, 288);
510 CHECK_OFFSET(vm_function_control, 296);
511 CHECK_OFFSET(eptp_list_address, 304);
512 CHECK_OFFSET(pml_address, 312);
513 CHECK_OFFSET(cr0_guest_host_mask, 344);
514 CHECK_OFFSET(cr4_guest_host_mask, 352);
515 CHECK_OFFSET(cr0_read_shadow, 360);
516 CHECK_OFFSET(cr4_read_shadow, 368);
517 CHECK_OFFSET(cr3_target_value0, 376);
518 CHECK_OFFSET(cr3_target_value1, 384);
519 CHECK_OFFSET(cr3_target_value2, 392);
520 CHECK_OFFSET(cr3_target_value3, 400);
521 CHECK_OFFSET(exit_qualification, 408);
522 CHECK_OFFSET(guest_linear_address, 416);
523 CHECK_OFFSET(guest_cr0, 424);
524 CHECK_OFFSET(guest_cr3, 432);
525 CHECK_OFFSET(guest_cr4, 440);
526 CHECK_OFFSET(guest_es_base, 448);
527 CHECK_OFFSET(guest_cs_base, 456);
528 CHECK_OFFSET(guest_ss_base, 464);
529 CHECK_OFFSET(guest_ds_base, 472);
530 CHECK_OFFSET(guest_fs_base, 480);
531 CHECK_OFFSET(guest_gs_base, 488);
532 CHECK_OFFSET(guest_ldtr_base, 496);
533 CHECK_OFFSET(guest_tr_base, 504);
534 CHECK_OFFSET(guest_gdtr_base, 512);
535 CHECK_OFFSET(guest_idtr_base, 520);
536 CHECK_OFFSET(guest_dr7, 528);
537 CHECK_OFFSET(guest_rsp, 536);
538 CHECK_OFFSET(guest_rip, 544);
539 CHECK_OFFSET(guest_rflags, 552);
540 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
541 CHECK_OFFSET(guest_sysenter_esp, 568);
542 CHECK_OFFSET(guest_sysenter_eip, 576);
543 CHECK_OFFSET(host_cr0, 584);
544 CHECK_OFFSET(host_cr3, 592);
545 CHECK_OFFSET(host_cr4, 600);
546 CHECK_OFFSET(host_fs_base, 608);
547 CHECK_OFFSET(host_gs_base, 616);
548 CHECK_OFFSET(host_tr_base, 624);
549 CHECK_OFFSET(host_gdtr_base, 632);
550 CHECK_OFFSET(host_idtr_base, 640);
551 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
552 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
553 CHECK_OFFSET(host_rsp, 664);
554 CHECK_OFFSET(host_rip, 672);
555 CHECK_OFFSET(pin_based_vm_exec_control, 744);
556 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
557 CHECK_OFFSET(exception_bitmap, 752);
558 CHECK_OFFSET(page_fault_error_code_mask, 756);
559 CHECK_OFFSET(page_fault_error_code_match, 760);
560 CHECK_OFFSET(cr3_target_count, 764);
561 CHECK_OFFSET(vm_exit_controls, 768);
562 CHECK_OFFSET(vm_exit_msr_store_count, 772);
563 CHECK_OFFSET(vm_exit_msr_load_count, 776);
564 CHECK_OFFSET(vm_entry_controls, 780);
565 CHECK_OFFSET(vm_entry_msr_load_count, 784);
566 CHECK_OFFSET(vm_entry_intr_info_field, 788);
567 CHECK_OFFSET(vm_entry_exception_error_code, 792);
568 CHECK_OFFSET(vm_entry_instruction_len, 796);
569 CHECK_OFFSET(tpr_threshold, 800);
570 CHECK_OFFSET(secondary_vm_exec_control, 804);
571 CHECK_OFFSET(vm_instruction_error, 808);
572 CHECK_OFFSET(vm_exit_reason, 812);
573 CHECK_OFFSET(vm_exit_intr_info, 816);
574 CHECK_OFFSET(vm_exit_intr_error_code, 820);
575 CHECK_OFFSET(idt_vectoring_info_field, 824);
576 CHECK_OFFSET(idt_vectoring_error_code, 828);
577 CHECK_OFFSET(vm_exit_instruction_len, 832);
578 CHECK_OFFSET(vmx_instruction_info, 836);
579 CHECK_OFFSET(guest_es_limit, 840);
580 CHECK_OFFSET(guest_cs_limit, 844);
581 CHECK_OFFSET(guest_ss_limit, 848);
582 CHECK_OFFSET(guest_ds_limit, 852);
583 CHECK_OFFSET(guest_fs_limit, 856);
584 CHECK_OFFSET(guest_gs_limit, 860);
585 CHECK_OFFSET(guest_ldtr_limit, 864);
586 CHECK_OFFSET(guest_tr_limit, 868);
587 CHECK_OFFSET(guest_gdtr_limit, 872);
588 CHECK_OFFSET(guest_idtr_limit, 876);
589 CHECK_OFFSET(guest_es_ar_bytes, 880);
590 CHECK_OFFSET(guest_cs_ar_bytes, 884);
591 CHECK_OFFSET(guest_ss_ar_bytes, 888);
592 CHECK_OFFSET(guest_ds_ar_bytes, 892);
593 CHECK_OFFSET(guest_fs_ar_bytes, 896);
594 CHECK_OFFSET(guest_gs_ar_bytes, 900);
595 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
596 CHECK_OFFSET(guest_tr_ar_bytes, 908);
597 CHECK_OFFSET(guest_interruptibility_info, 912);
598 CHECK_OFFSET(guest_activity_state, 916);
599 CHECK_OFFSET(guest_sysenter_cs, 920);
600 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
601 CHECK_OFFSET(vmx_preemption_timer_value, 928);
602 CHECK_OFFSET(virtual_processor_id, 960);
603 CHECK_OFFSET(posted_intr_nv, 962);
604 CHECK_OFFSET(guest_es_selector, 964);
605 CHECK_OFFSET(guest_cs_selector, 966);
606 CHECK_OFFSET(guest_ss_selector, 968);
607 CHECK_OFFSET(guest_ds_selector, 970);
608 CHECK_OFFSET(guest_fs_selector, 972);
609 CHECK_OFFSET(guest_gs_selector, 974);
610 CHECK_OFFSET(guest_ldtr_selector, 976);
611 CHECK_OFFSET(guest_tr_selector, 978);
612 CHECK_OFFSET(guest_intr_status, 980);
613 CHECK_OFFSET(host_es_selector, 982);
614 CHECK_OFFSET(host_cs_selector, 984);
615 CHECK_OFFSET(host_ss_selector, 986);
616 CHECK_OFFSET(host_ds_selector, 988);
617 CHECK_OFFSET(host_fs_selector, 990);
618 CHECK_OFFSET(host_gs_selector, 992);
619 CHECK_OFFSET(host_tr_selector, 994);
620 CHECK_OFFSET(guest_pml_index, 996);
621}
622
623/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300624 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
625 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
626 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700627 *
628 * IMPORTANT: Changing this value will break save/restore compatibility with
629 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300630 */
631#define VMCS12_REVISION 0x11e57ed0
632
633/*
634 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
635 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
636 * current implementation, 4K are reserved to avoid future complications.
637 */
638#define VMCS12_SIZE 0x1000
639
640/*
Jim Mattson5b157062017-12-22 12:11:12 -0800641 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
642 * supported VMCS12 field encoding.
643 */
644#define VMCS12_MAX_FIELD_INDEX 0x17
645
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100646struct nested_vmx_msrs {
647 /*
648 * We only store the "true" versions of the VMX capability MSRs. We
649 * generate the "non-true" versions by setting the must-be-1 bits
650 * according to the SDM.
651 */
652 u32 procbased_ctls_low;
653 u32 procbased_ctls_high;
654 u32 secondary_ctls_low;
655 u32 secondary_ctls_high;
656 u32 pinbased_ctls_low;
657 u32 pinbased_ctls_high;
658 u32 exit_ctls_low;
659 u32 exit_ctls_high;
660 u32 entry_ctls_low;
661 u32 entry_ctls_high;
662 u32 misc_low;
663 u32 misc_high;
664 u32 ept_caps;
665 u32 vpid_caps;
666 u64 basic;
667 u64 cr0_fixed0;
668 u64 cr0_fixed1;
669 u64 cr4_fixed0;
670 u64 cr4_fixed1;
671 u64 vmcs_enum;
672 u64 vmfunc_controls;
673};
674
Jim Mattson5b157062017-12-22 12:11:12 -0800675/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300676 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
677 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
678 */
679struct nested_vmx {
680 /* Has the level1 guest done vmxon? */
681 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400682 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400683 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300684
685 /* The guest-physical address of the current VMCS L1 keeps for L2 */
686 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700687 /*
688 * Cache of the guest's VMCS, existing outside of guest memory.
689 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700690 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700691 */
692 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300693 /*
694 * Indicates if the shadow vmcs must be updated with the
695 * data hold by vmcs12
696 */
697 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100698 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300699
Jim Mattson8d860bb2018-05-09 16:56:05 -0400700 bool change_vmcs01_virtual_apic_mode;
701
Nadav Har'El644d7112011-05-25 23:12:35 +0300702 /* L2 must run next, and mustn't decide to exit to L1. */
703 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600704
705 struct loaded_vmcs vmcs02;
706
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300707 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600708 * Guest pages referred to in the vmcs02 with host-physical
709 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300710 */
711 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800712 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800713 struct page *pi_desc_page;
714 struct pi_desc *pi_desc;
715 bool pi_pending;
716 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100717
718 struct hrtimer preemption_timer;
719 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200720
721 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
722 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800723
Wanpeng Li5c614b32015-10-13 09:18:36 -0700724 u16 vpid02;
725 u16 last_vpid;
726
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100727 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200728
729 /* SMM related state */
730 struct {
731 /* in VMX operation on SMM entry? */
732 bool vmxon;
733 /* in guest mode on SMM entry? */
734 bool guest_mode;
735 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300736};
737
Yang Zhang01e439b2013-04-11 19:25:12 +0800738#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800739#define POSTED_INTR_SN 1
740
Yang Zhang01e439b2013-04-11 19:25:12 +0800741/* Posted-Interrupt Descriptor */
742struct pi_desc {
743 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800744 union {
745 struct {
746 /* bit 256 - Outstanding Notification */
747 u16 on : 1,
748 /* bit 257 - Suppress Notification */
749 sn : 1,
750 /* bit 271:258 - Reserved */
751 rsvd_1 : 14;
752 /* bit 279:272 - Notification Vector */
753 u8 nv;
754 /* bit 287:280 - Reserved */
755 u8 rsvd_2;
756 /* bit 319:288 - Notification Destination */
757 u32 ndst;
758 };
759 u64 control;
760 };
761 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800762} __aligned(64);
763
Yang Zhanga20ed542013-04-11 19:25:15 +0800764static bool pi_test_and_set_on(struct pi_desc *pi_desc)
765{
766 return test_and_set_bit(POSTED_INTR_ON,
767 (unsigned long *)&pi_desc->control);
768}
769
770static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
771{
772 return test_and_clear_bit(POSTED_INTR_ON,
773 (unsigned long *)&pi_desc->control);
774}
775
776static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
777{
778 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
779}
780
Feng Wuebbfc762015-09-18 22:29:46 +0800781static inline void pi_clear_sn(struct pi_desc *pi_desc)
782{
783 return clear_bit(POSTED_INTR_SN,
784 (unsigned long *)&pi_desc->control);
785}
786
787static inline void pi_set_sn(struct pi_desc *pi_desc)
788{
789 return set_bit(POSTED_INTR_SN,
790 (unsigned long *)&pi_desc->control);
791}
792
Paolo Bonziniad361092016-09-20 16:15:05 +0200793static inline void pi_clear_on(struct pi_desc *pi_desc)
794{
795 clear_bit(POSTED_INTR_ON,
796 (unsigned long *)&pi_desc->control);
797}
798
Feng Wuebbfc762015-09-18 22:29:46 +0800799static inline int pi_test_on(struct pi_desc *pi_desc)
800{
801 return test_bit(POSTED_INTR_ON,
802 (unsigned long *)&pi_desc->control);
803}
804
805static inline int pi_test_sn(struct pi_desc *pi_desc)
806{
807 return test_bit(POSTED_INTR_SN,
808 (unsigned long *)&pi_desc->control);
809}
810
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400811struct vmx_msrs {
812 unsigned int nr;
813 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
814};
815
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400816struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000817 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300818 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300819 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100820 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300821 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200822 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200823 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300824 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400825 int nmsrs;
826 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800827 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400828#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300829 u64 msr_host_kernel_gs_base;
830 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400831#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100832
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100833 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100834 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100835
Gleb Natapov2961e8762013-11-25 15:37:13 +0200836 u32 vm_entry_controls_shadow;
837 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200838 u32 secondary_exec_control;
839
Nadav Har'Eld462b812011-05-24 15:26:10 +0300840 /*
841 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
842 * non-nested (L1) guest, it always points to vmcs01. For a nested
843 * guest (L2), it points to a different VMCS.
844 */
845 struct loaded_vmcs vmcs01;
846 struct loaded_vmcs *loaded_vmcs;
847 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300848 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400849 struct vmx_msrs guest;
850 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300851 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400852 struct {
853 int loaded;
854 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300855#ifdef CONFIG_X86_64
856 u16 ds_sel, es_sel;
857#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200858 int gs_ldt_reload_needed;
859 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000860 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400861 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200862 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300863 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300864 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300865 struct kvm_segment segs[8];
866 } rmode;
867 struct {
868 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300869 struct kvm_save_segment {
870 u16 selector;
871 unsigned long base;
872 u32 limit;
873 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300874 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300875 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800876 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300877 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200878
Andi Kleena0861c02009-06-08 17:37:09 +0800879 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800880
Yang Zhang01e439b2013-04-11 19:25:12 +0800881 /* Posted interrupt descriptor */
882 struct pi_desc pi_desc;
883
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300884 /* Support for a guest hypervisor (nested VMX) */
885 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200886
887 /* Dynamic PLE window. */
888 int ple_window;
889 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800890
891 /* Support for PML */
892#define PML_ENTITY_NUM 512
893 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800894
Yunhong Jiang64672c92016-06-13 14:19:59 -0700895 /* apic deadline value in host tsc */
896 u64 hv_deadline_tsc;
897
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800898 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800899
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800900 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800901
Wanpeng Li74c55932017-11-29 01:31:20 -0800902 unsigned long host_debugctlmsr;
903
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800904 /*
905 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
906 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
907 * in msr_ia32_feature_control_valid_bits.
908 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800909 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800910 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400911};
912
Avi Kivity2fb92db2011-04-27 19:42:18 +0300913enum segment_cache_field {
914 SEG_FIELD_SEL = 0,
915 SEG_FIELD_BASE = 1,
916 SEG_FIELD_LIMIT = 2,
917 SEG_FIELD_AR = 3,
918
919 SEG_FIELD_NR = 4
920};
921
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700922static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
923{
924 return container_of(kvm, struct kvm_vmx, kvm);
925}
926
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400927static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
928{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000929 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400930}
931
Feng Wuefc64402015-09-18 22:29:51 +0800932static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
933{
934 return &(to_vmx(vcpu)->pi_desc);
935}
936
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800937#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300938#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800939#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
940#define FIELD64(number, name) \
941 FIELD(number, name), \
942 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300943
Abel Gordon4607c2d2013-04-18 14:35:55 +0300944
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100945static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100946#define SHADOW_FIELD_RO(x) x,
947#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300948};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400949static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300950 ARRAY_SIZE(shadow_read_only_fields);
951
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100952static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100953#define SHADOW_FIELD_RW(x) x,
954#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300955};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400956static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300957 ARRAY_SIZE(shadow_read_write_fields);
958
Mathias Krause772e0312012-08-30 01:30:19 +0200959static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300960 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800961 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300962 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
963 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
964 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
965 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
966 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
967 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
968 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
969 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800970 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400971 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300972 FIELD(HOST_ES_SELECTOR, host_es_selector),
973 FIELD(HOST_CS_SELECTOR, host_cs_selector),
974 FIELD(HOST_SS_SELECTOR, host_ss_selector),
975 FIELD(HOST_DS_SELECTOR, host_ds_selector),
976 FIELD(HOST_FS_SELECTOR, host_fs_selector),
977 FIELD(HOST_GS_SELECTOR, host_gs_selector),
978 FIELD(HOST_TR_SELECTOR, host_tr_selector),
979 FIELD64(IO_BITMAP_A, io_bitmap_a),
980 FIELD64(IO_BITMAP_B, io_bitmap_b),
981 FIELD64(MSR_BITMAP, msr_bitmap),
982 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
983 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
984 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700985 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300986 FIELD64(TSC_OFFSET, tsc_offset),
987 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
988 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800989 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400990 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300991 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800992 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
993 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
994 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
995 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400996 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700997 FIELD64(VMREAD_BITMAP, vmread_bitmap),
998 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800999 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001000 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1001 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1002 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1003 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1004 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1005 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1006 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1007 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1008 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1009 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001010 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001011 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1012 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1013 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1014 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1015 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1016 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1017 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1018 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1019 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1020 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1021 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1022 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1023 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1024 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1025 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1026 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1027 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1028 FIELD(TPR_THRESHOLD, tpr_threshold),
1029 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1030 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1031 FIELD(VM_EXIT_REASON, vm_exit_reason),
1032 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1033 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1034 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1035 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1036 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1037 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1038 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1039 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1040 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1041 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1042 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1043 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1044 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1045 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1046 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1047 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1048 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1049 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1050 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1051 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1052 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1053 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1054 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1055 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1056 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1057 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1058 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1059 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001060 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001061 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1062 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1063 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1064 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1065 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1066 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1067 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1068 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1069 FIELD(EXIT_QUALIFICATION, exit_qualification),
1070 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1071 FIELD(GUEST_CR0, guest_cr0),
1072 FIELD(GUEST_CR3, guest_cr3),
1073 FIELD(GUEST_CR4, guest_cr4),
1074 FIELD(GUEST_ES_BASE, guest_es_base),
1075 FIELD(GUEST_CS_BASE, guest_cs_base),
1076 FIELD(GUEST_SS_BASE, guest_ss_base),
1077 FIELD(GUEST_DS_BASE, guest_ds_base),
1078 FIELD(GUEST_FS_BASE, guest_fs_base),
1079 FIELD(GUEST_GS_BASE, guest_gs_base),
1080 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1081 FIELD(GUEST_TR_BASE, guest_tr_base),
1082 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1083 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1084 FIELD(GUEST_DR7, guest_dr7),
1085 FIELD(GUEST_RSP, guest_rsp),
1086 FIELD(GUEST_RIP, guest_rip),
1087 FIELD(GUEST_RFLAGS, guest_rflags),
1088 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1089 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1090 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1091 FIELD(HOST_CR0, host_cr0),
1092 FIELD(HOST_CR3, host_cr3),
1093 FIELD(HOST_CR4, host_cr4),
1094 FIELD(HOST_FS_BASE, host_fs_base),
1095 FIELD(HOST_GS_BASE, host_gs_base),
1096 FIELD(HOST_TR_BASE, host_tr_base),
1097 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1098 FIELD(HOST_IDTR_BASE, host_idtr_base),
1099 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1100 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1101 FIELD(HOST_RSP, host_rsp),
1102 FIELD(HOST_RIP, host_rip),
1103};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001104
1105static inline short vmcs_field_to_offset(unsigned long field)
1106{
Dan Williams085331d2018-01-31 17:47:03 -08001107 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1108 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001109 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001110
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001111 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001112 return -ENOENT;
1113
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001114 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001115 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001116 return -ENOENT;
1117
Linus Torvalds15303ba2018-02-10 13:16:35 -08001118 index = array_index_nospec(index, size);
1119 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001120 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001121 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001122 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001123}
1124
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001125static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1126{
David Matlack4f2777b2016-07-13 17:16:37 -07001127 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001128}
1129
Peter Feiner995f00a2017-06-30 17:26:32 -07001130static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001131static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001132static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001133static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001134static void vmx_set_segment(struct kvm_vcpu *vcpu,
1135 struct kvm_segment *var, int seg);
1136static void vmx_get_segment(struct kvm_vcpu *vcpu,
1137 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001138static bool guest_state_valid(struct kvm_vcpu *vcpu);
1139static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001140static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001141static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1142static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1143static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1144 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001145static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001146static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1147 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001148
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1150static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001151/*
1152 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1153 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1154 */
1155static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
Feng Wubf9f6ac2015-09-18 22:29:55 +08001157/*
1158 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1159 * can find which vCPU should be waken up.
1160 */
1161static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1162static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1163
Radim Krčmář23611332016-09-29 22:41:33 +02001164enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001165 VMX_VMREAD_BITMAP,
1166 VMX_VMWRITE_BITMAP,
1167 VMX_BITMAP_NR
1168};
1169
1170static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1171
Radim Krčmář23611332016-09-29 22:41:33 +02001172#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1173#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001174
Avi Kivity110312c2010-12-21 12:54:20 +02001175static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001176static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001177
Sheng Yang2384d2b2008-01-17 15:14:33 +08001178static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1179static DEFINE_SPINLOCK(vmx_vpid_lock);
1180
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001181static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 int size;
1183 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001184 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186 u32 pin_based_exec_ctrl;
1187 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001188 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001189 u32 vmexit_ctrl;
1190 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001191 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001192} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193
Hannes Ederefff9e52008-11-28 17:02:06 +01001194static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001195 u32 ept;
1196 u32 vpid;
1197} vmx_capability;
1198
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199#define VMX_SEGMENT_FIELD(seg) \
1200 [VCPU_SREG_##seg] = { \
1201 .selector = GUEST_##seg##_SELECTOR, \
1202 .base = GUEST_##seg##_BASE, \
1203 .limit = GUEST_##seg##_LIMIT, \
1204 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1205 }
1206
Mathias Krause772e0312012-08-30 01:30:19 +02001207static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 unsigned selector;
1209 unsigned base;
1210 unsigned limit;
1211 unsigned ar_bytes;
1212} kvm_vmx_segment_fields[] = {
1213 VMX_SEGMENT_FIELD(CS),
1214 VMX_SEGMENT_FIELD(DS),
1215 VMX_SEGMENT_FIELD(ES),
1216 VMX_SEGMENT_FIELD(FS),
1217 VMX_SEGMENT_FIELD(GS),
1218 VMX_SEGMENT_FIELD(SS),
1219 VMX_SEGMENT_FIELD(TR),
1220 VMX_SEGMENT_FIELD(LDTR),
1221};
1222
Avi Kivity26bb0982009-09-07 11:14:12 +03001223static u64 host_efer;
1224
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001225static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1226
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001227/*
Brian Gerst8c065852010-07-17 09:03:26 -04001228 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001229 * away by decrementing the array size.
1230 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001232#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001233 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001235 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001238DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1239
1240#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1241
1242#define KVM_EVMCS_VERSION 1
1243
1244#if IS_ENABLED(CONFIG_HYPERV)
1245static bool __read_mostly enlightened_vmcs = true;
1246module_param(enlightened_vmcs, bool, 0444);
1247
1248static inline void evmcs_write64(unsigned long field, u64 value)
1249{
1250 u16 clean_field;
1251 int offset = get_evmcs_offset(field, &clean_field);
1252
1253 if (offset < 0)
1254 return;
1255
1256 *(u64 *)((char *)current_evmcs + offset) = value;
1257
1258 current_evmcs->hv_clean_fields &= ~clean_field;
1259}
1260
1261static inline void evmcs_write32(unsigned long field, u32 value)
1262{
1263 u16 clean_field;
1264 int offset = get_evmcs_offset(field, &clean_field);
1265
1266 if (offset < 0)
1267 return;
1268
1269 *(u32 *)((char *)current_evmcs + offset) = value;
1270 current_evmcs->hv_clean_fields &= ~clean_field;
1271}
1272
1273static inline void evmcs_write16(unsigned long field, u16 value)
1274{
1275 u16 clean_field;
1276 int offset = get_evmcs_offset(field, &clean_field);
1277
1278 if (offset < 0)
1279 return;
1280
1281 *(u16 *)((char *)current_evmcs + offset) = value;
1282 current_evmcs->hv_clean_fields &= ~clean_field;
1283}
1284
1285static inline u64 evmcs_read64(unsigned long field)
1286{
1287 int offset = get_evmcs_offset(field, NULL);
1288
1289 if (offset < 0)
1290 return 0;
1291
1292 return *(u64 *)((char *)current_evmcs + offset);
1293}
1294
1295static inline u32 evmcs_read32(unsigned long field)
1296{
1297 int offset = get_evmcs_offset(field, NULL);
1298
1299 if (offset < 0)
1300 return 0;
1301
1302 return *(u32 *)((char *)current_evmcs + offset);
1303}
1304
1305static inline u16 evmcs_read16(unsigned long field)
1306{
1307 int offset = get_evmcs_offset(field, NULL);
1308
1309 if (offset < 0)
1310 return 0;
1311
1312 return *(u16 *)((char *)current_evmcs + offset);
1313}
1314
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001315static inline void evmcs_touch_msr_bitmap(void)
1316{
1317 if (unlikely(!current_evmcs))
1318 return;
1319
1320 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1321 current_evmcs->hv_clean_fields &=
1322 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1323}
1324
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001325static void evmcs_load(u64 phys_addr)
1326{
1327 struct hv_vp_assist_page *vp_ap =
1328 hv_get_vp_assist_page(smp_processor_id());
1329
1330 vp_ap->current_nested_vmcs = phys_addr;
1331 vp_ap->enlighten_vmentry = 1;
1332}
1333
1334static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1335{
1336 /*
1337 * Enlightened VMCSv1 doesn't support these:
1338 *
1339 * POSTED_INTR_NV = 0x00000002,
1340 * GUEST_INTR_STATUS = 0x00000810,
1341 * APIC_ACCESS_ADDR = 0x00002014,
1342 * POSTED_INTR_DESC_ADDR = 0x00002016,
1343 * EOI_EXIT_BITMAP0 = 0x0000201c,
1344 * EOI_EXIT_BITMAP1 = 0x0000201e,
1345 * EOI_EXIT_BITMAP2 = 0x00002020,
1346 * EOI_EXIT_BITMAP3 = 0x00002022,
1347 */
1348 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1349 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1350 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1351 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1352 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1353 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1354 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1355
1356 /*
1357 * GUEST_PML_INDEX = 0x00000812,
1358 * PML_ADDRESS = 0x0000200e,
1359 */
1360 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1361
1362 /* VM_FUNCTION_CONTROL = 0x00002018, */
1363 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1364
1365 /*
1366 * EPTP_LIST_ADDRESS = 0x00002024,
1367 * VMREAD_BITMAP = 0x00002026,
1368 * VMWRITE_BITMAP = 0x00002028,
1369 */
1370 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1371
1372 /*
1373 * TSC_MULTIPLIER = 0x00002032,
1374 */
1375 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1376
1377 /*
1378 * PLE_GAP = 0x00004020,
1379 * PLE_WINDOW = 0x00004022,
1380 */
1381 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1382
1383 /*
1384 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1385 */
1386 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1387
1388 /*
1389 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1390 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1391 */
1392 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1393 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1394
1395 /*
1396 * Currently unsupported in KVM:
1397 * GUEST_IA32_RTIT_CTL = 0x00002814,
1398 */
1399}
1400#else /* !IS_ENABLED(CONFIG_HYPERV) */
1401static inline void evmcs_write64(unsigned long field, u64 value) {}
1402static inline void evmcs_write32(unsigned long field, u32 value) {}
1403static inline void evmcs_write16(unsigned long field, u16 value) {}
1404static inline u64 evmcs_read64(unsigned long field) { return 0; }
1405static inline u32 evmcs_read32(unsigned long field) { return 0; }
1406static inline u16 evmcs_read16(unsigned long field) { return 0; }
1407static inline void evmcs_load(u64 phys_addr) {}
1408static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001409static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001410#endif /* IS_ENABLED(CONFIG_HYPERV) */
1411
Jan Kiszka5bb16012016-02-09 20:14:21 +01001412static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413{
1414 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1415 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001416 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1417}
1418
Jan Kiszka6f054852016-02-09 20:15:18 +01001419static inline bool is_debug(u32 intr_info)
1420{
1421 return is_exception_n(intr_info, DB_VECTOR);
1422}
1423
1424static inline bool is_breakpoint(u32 intr_info)
1425{
1426 return is_exception_n(intr_info, BP_VECTOR);
1427}
1428
Jan Kiszka5bb16012016-02-09 20:14:21 +01001429static inline bool is_page_fault(u32 intr_info)
1430{
1431 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432}
1433
Gui Jianfeng31299942010-03-15 17:29:09 +08001434static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001435{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001436 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001437}
1438
Gui Jianfeng31299942010-03-15 17:29:09 +08001439static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001440{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001441 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001442}
1443
Liran Alon9e869482018-03-12 13:12:51 +02001444static inline bool is_gp_fault(u32 intr_info)
1445{
1446 return is_exception_n(intr_info, GP_VECTOR);
1447}
1448
Gui Jianfeng31299942010-03-15 17:29:09 +08001449static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450{
1451 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1452 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1453}
1454
Gui Jianfeng31299942010-03-15 17:29:09 +08001455static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001456{
1457 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1458 INTR_INFO_VALID_MASK)) ==
1459 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1460}
1461
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001462/* Undocumented: icebp/int1 */
1463static inline bool is_icebp(u32 intr_info)
1464{
1465 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1466 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1467}
1468
Gui Jianfeng31299942010-03-15 17:29:09 +08001469static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001470{
Sheng Yang04547152009-04-01 15:52:31 +08001471 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001472}
1473
Gui Jianfeng31299942010-03-15 17:29:09 +08001474static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001475{
Sheng Yang04547152009-04-01 15:52:31 +08001476 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001477}
1478
Paolo Bonzini35754c92015-07-29 12:05:37 +02001479static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001480{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001481 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001482}
1483
Gui Jianfeng31299942010-03-15 17:29:09 +08001484static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001485{
Sheng Yang04547152009-04-01 15:52:31 +08001486 return vmcs_config.cpu_based_exec_ctrl &
1487 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001488}
1489
Avi Kivity774ead32007-12-26 13:57:04 +02001490static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001491{
Sheng Yang04547152009-04-01 15:52:31 +08001492 return vmcs_config.cpu_based_2nd_exec_ctrl &
1493 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1494}
1495
Yang Zhang8d146952013-01-25 10:18:50 +08001496static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1497{
1498 return vmcs_config.cpu_based_2nd_exec_ctrl &
1499 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1500}
1501
Yang Zhang83d4c282013-01-25 10:18:49 +08001502static inline bool cpu_has_vmx_apic_register_virt(void)
1503{
1504 return vmcs_config.cpu_based_2nd_exec_ctrl &
1505 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1506}
1507
Yang Zhangc7c9c562013-01-25 10:18:51 +08001508static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1509{
1510 return vmcs_config.cpu_based_2nd_exec_ctrl &
1511 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1512}
1513
Yunhong Jiang64672c92016-06-13 14:19:59 -07001514/*
1515 * Comment's format: document - errata name - stepping - processor name.
1516 * Refer from
1517 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1518 */
1519static u32 vmx_preemption_cpu_tfms[] = {
1520/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15210x000206E6,
1522/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1523/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1524/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15250x00020652,
1526/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15270x00020655,
1528/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1529/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1530/*
1531 * 320767.pdf - AAP86 - B1 -
1532 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1533 */
15340x000106E5,
1535/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15360x000106A0,
1537/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15380x000106A1,
1539/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15400x000106A4,
1541 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1542 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1543 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15440x000106A5,
1545};
1546
1547static inline bool cpu_has_broken_vmx_preemption_timer(void)
1548{
1549 u32 eax = cpuid_eax(0x00000001), i;
1550
1551 /* Clear the reserved bits */
1552 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001553 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001554 if (eax == vmx_preemption_cpu_tfms[i])
1555 return true;
1556
1557 return false;
1558}
1559
1560static inline bool cpu_has_vmx_preemption_timer(void)
1561{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001562 return vmcs_config.pin_based_exec_ctrl &
1563 PIN_BASED_VMX_PREEMPTION_TIMER;
1564}
1565
Yang Zhang01e439b2013-04-11 19:25:12 +08001566static inline bool cpu_has_vmx_posted_intr(void)
1567{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001568 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1569 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001570}
1571
1572static inline bool cpu_has_vmx_apicv(void)
1573{
1574 return cpu_has_vmx_apic_register_virt() &&
1575 cpu_has_vmx_virtual_intr_delivery() &&
1576 cpu_has_vmx_posted_intr();
1577}
1578
Sheng Yang04547152009-04-01 15:52:31 +08001579static inline bool cpu_has_vmx_flexpriority(void)
1580{
1581 return cpu_has_vmx_tpr_shadow() &&
1582 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001583}
1584
Marcelo Tosattie7997942009-06-11 12:07:40 -03001585static inline bool cpu_has_vmx_ept_execute_only(void)
1586{
Gui Jianfeng31299942010-03-15 17:29:09 +08001587 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001588}
1589
Marcelo Tosattie7997942009-06-11 12:07:40 -03001590static inline bool cpu_has_vmx_ept_2m_page(void)
1591{
Gui Jianfeng31299942010-03-15 17:29:09 +08001592 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001593}
1594
Sheng Yang878403b2010-01-05 19:02:29 +08001595static inline bool cpu_has_vmx_ept_1g_page(void)
1596{
Gui Jianfeng31299942010-03-15 17:29:09 +08001597 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001598}
1599
Sheng Yang4bc9b982010-06-02 14:05:24 +08001600static inline bool cpu_has_vmx_ept_4levels(void)
1601{
1602 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1603}
1604
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001605static inline bool cpu_has_vmx_ept_mt_wb(void)
1606{
1607 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1608}
1609
Yu Zhang855feb62017-08-24 20:27:55 +08001610static inline bool cpu_has_vmx_ept_5levels(void)
1611{
1612 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1613}
1614
Xudong Hao83c3a332012-05-28 19:33:35 +08001615static inline bool cpu_has_vmx_ept_ad_bits(void)
1616{
1617 return vmx_capability.ept & VMX_EPT_AD_BIT;
1618}
1619
Gui Jianfeng31299942010-03-15 17:29:09 +08001620static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001621{
Gui Jianfeng31299942010-03-15 17:29:09 +08001622 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001623}
1624
Gui Jianfeng31299942010-03-15 17:29:09 +08001625static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001626{
Gui Jianfeng31299942010-03-15 17:29:09 +08001627 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001628}
1629
Liran Aloncd9a4912018-05-22 17:16:15 +03001630static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1631{
1632 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1633}
1634
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001635static inline bool cpu_has_vmx_invvpid_single(void)
1636{
1637 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1638}
1639
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001640static inline bool cpu_has_vmx_invvpid_global(void)
1641{
1642 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1643}
1644
Wanpeng Li08d839c2017-03-23 05:30:08 -07001645static inline bool cpu_has_vmx_invvpid(void)
1646{
1647 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1648}
1649
Gui Jianfeng31299942010-03-15 17:29:09 +08001650static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001651{
Sheng Yang04547152009-04-01 15:52:31 +08001652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001657{
1658 return vmcs_config.cpu_based_2nd_exec_ctrl &
1659 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1660}
1661
Gui Jianfeng31299942010-03-15 17:29:09 +08001662static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001663{
1664 return vmcs_config.cpu_based_2nd_exec_ctrl &
1665 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1666}
1667
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001668static inline bool cpu_has_vmx_basic_inout(void)
1669{
1670 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1671}
1672
Paolo Bonzini35754c92015-07-29 12:05:37 +02001673static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001674{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001675 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001676}
1677
Gui Jianfeng31299942010-03-15 17:29:09 +08001678static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001679{
Sheng Yang04547152009-04-01 15:52:31 +08001680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001682}
1683
Gui Jianfeng31299942010-03-15 17:29:09 +08001684static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_RDTSCP;
1688}
1689
Mao, Junjiead756a12012-07-02 01:18:48 +00001690static inline bool cpu_has_vmx_invpcid(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_INVPCID;
1694}
1695
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001696static inline bool cpu_has_virtual_nmis(void)
1697{
1698 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1699}
1700
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001701static inline bool cpu_has_vmx_wbinvd_exit(void)
1702{
1703 return vmcs_config.cpu_based_2nd_exec_ctrl &
1704 SECONDARY_EXEC_WBINVD_EXITING;
1705}
1706
Abel Gordonabc4fc52013-04-18 14:35:25 +03001707static inline bool cpu_has_vmx_shadow_vmcs(void)
1708{
1709 u64 vmx_msr;
1710 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1711 /* check if the cpu supports writing r/o exit information fields */
1712 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1713 return false;
1714
1715 return vmcs_config.cpu_based_2nd_exec_ctrl &
1716 SECONDARY_EXEC_SHADOW_VMCS;
1717}
1718
Kai Huang843e4332015-01-28 10:54:28 +08001719static inline bool cpu_has_vmx_pml(void)
1720{
1721 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1722}
1723
Haozhong Zhang64903d62015-10-20 15:39:09 +08001724static inline bool cpu_has_vmx_tsc_scaling(void)
1725{
1726 return vmcs_config.cpu_based_2nd_exec_ctrl &
1727 SECONDARY_EXEC_TSC_SCALING;
1728}
1729
Bandan Das2a499e42017-08-03 15:54:41 -04001730static inline bool cpu_has_vmx_vmfunc(void)
1731{
1732 return vmcs_config.cpu_based_2nd_exec_ctrl &
1733 SECONDARY_EXEC_ENABLE_VMFUNC;
1734}
1735
Sean Christopherson64f7a112018-04-30 10:01:06 -07001736static bool vmx_umip_emulated(void)
1737{
1738 return vmcs_config.cpu_based_2nd_exec_ctrl &
1739 SECONDARY_EXEC_DESC;
1740}
1741
Sheng Yang04547152009-04-01 15:52:31 +08001742static inline bool report_flexpriority(void)
1743{
1744 return flexpriority_enabled;
1745}
1746
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001747static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1748{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001749 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001750}
1751
Jim Mattsonf4160e42018-05-29 09:11:33 -07001752/*
1753 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1754 * to modify any valid field of the VMCS, or are the VM-exit
1755 * information fields read-only?
1756 */
1757static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1758{
1759 return to_vmx(vcpu)->nested.msrs.misc_low &
1760 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1761}
1762
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001763static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1764{
1765 return vmcs12->cpu_based_vm_exec_control & bit;
1766}
1767
1768static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1769{
1770 return (vmcs12->cpu_based_vm_exec_control &
1771 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1772 (vmcs12->secondary_vm_exec_control & bit);
1773}
1774
Jan Kiszkaf4124502014-03-07 20:03:13 +01001775static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1776{
1777 return vmcs12->pin_based_vm_exec_control &
1778 PIN_BASED_VMX_PREEMPTION_TIMER;
1779}
1780
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001781static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1782{
1783 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1784}
1785
1786static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1787{
1788 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1789}
1790
Nadav Har'El155a97a2013-08-05 11:07:16 +03001791static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1792{
1793 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1794}
1795
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001796static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1797{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001798 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001799}
1800
Bandan Dasc5f983f2017-05-05 15:25:14 -04001801static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1802{
1803 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1804}
1805
Wincy Vanf2b93282015-02-03 23:56:03 +08001806static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1807{
1808 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1809}
1810
Wanpeng Li5c614b32015-10-13 09:18:36 -07001811static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1812{
1813 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1814}
1815
Wincy Van82f0dd42015-02-03 23:57:18 +08001816static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1817{
1818 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1819}
1820
Wincy Van608406e2015-02-03 23:57:51 +08001821static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1822{
1823 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1824}
1825
Wincy Van705699a2015-02-03 23:58:17 +08001826static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1827{
1828 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1829}
1830
Bandan Das27c42a12017-08-03 15:54:42 -04001831static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1832{
1833 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1834}
1835
Bandan Das41ab9372017-08-03 15:54:43 -04001836static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1837{
1838 return nested_cpu_has_vmfunc(vmcs12) &&
1839 (vmcs12->vm_function_control &
1840 VMX_VMFUNC_EPTP_SWITCHING);
1841}
1842
Jim Mattsonef85b672016-12-12 11:01:37 -08001843static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001844{
1845 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001846 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001847}
1848
Jan Kiszka533558b2014-01-04 18:47:20 +01001849static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1850 u32 exit_intr_info,
1851 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001852static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1853 struct vmcs12 *vmcs12,
1854 u32 reason, unsigned long qualification);
1855
Rusty Russell8b9cf982007-07-30 16:31:43 +10001856static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001857{
1858 int i;
1859
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001860 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001861 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001862 return i;
1863 return -1;
1864}
1865
Sheng Yang2384d2b2008-01-17 15:14:33 +08001866static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1867{
1868 struct {
1869 u64 vpid : 16;
1870 u64 rsvd : 48;
1871 u64 gva;
1872 } operand = { vpid, 0, gva };
1873
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001874 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001875 /* CF==1 or ZF==1 --> rc = -1 */
1876 "; ja 1f ; ud2 ; 1:"
1877 : : "a"(&operand), "c"(ext) : "cc", "memory");
1878}
1879
Sheng Yang14394422008-04-28 12:24:45 +08001880static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1881{
1882 struct {
1883 u64 eptp, gpa;
1884 } operand = {eptp, gpa};
1885
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001886 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001887 /* CF==1 or ZF==1 --> rc = -1 */
1888 "; ja 1f ; ud2 ; 1:\n"
1889 : : "a" (&operand), "c" (ext) : "cc", "memory");
1890}
1891
Avi Kivity26bb0982009-09-07 11:14:12 +03001892static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001893{
1894 int i;
1895
Rusty Russell8b9cf982007-07-30 16:31:43 +10001896 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001897 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001898 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001899 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001900}
1901
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902static void vmcs_clear(struct vmcs *vmcs)
1903{
1904 u64 phys_addr = __pa(vmcs);
1905 u8 error;
1906
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001907 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001908 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 : "cc", "memory");
1910 if (error)
1911 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1912 vmcs, phys_addr);
1913}
1914
Nadav Har'Eld462b812011-05-24 15:26:10 +03001915static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1916{
1917 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001918 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1919 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001920 loaded_vmcs->cpu = -1;
1921 loaded_vmcs->launched = 0;
1922}
1923
Dongxiao Xu7725b892010-05-11 18:29:38 +08001924static void vmcs_load(struct vmcs *vmcs)
1925{
1926 u64 phys_addr = __pa(vmcs);
1927 u8 error;
1928
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001929 if (static_branch_unlikely(&enable_evmcs))
1930 return evmcs_load(phys_addr);
1931
Dongxiao Xu7725b892010-05-11 18:29:38 +08001932 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001933 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001934 : "cc", "memory");
1935 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001936 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001937 vmcs, phys_addr);
1938}
1939
Dave Young2965faa2015-09-09 15:38:55 -07001940#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001941/*
1942 * This bitmap is used to indicate whether the vmclear
1943 * operation is enabled on all cpus. All disabled by
1944 * default.
1945 */
1946static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1947
1948static inline void crash_enable_local_vmclear(int cpu)
1949{
1950 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1951}
1952
1953static inline void crash_disable_local_vmclear(int cpu)
1954{
1955 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1956}
1957
1958static inline int crash_local_vmclear_enabled(int cpu)
1959{
1960 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1961}
1962
1963static void crash_vmclear_local_loaded_vmcss(void)
1964{
1965 int cpu = raw_smp_processor_id();
1966 struct loaded_vmcs *v;
1967
1968 if (!crash_local_vmclear_enabled(cpu))
1969 return;
1970
1971 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1972 loaded_vmcss_on_cpu_link)
1973 vmcs_clear(v->vmcs);
1974}
1975#else
1976static inline void crash_enable_local_vmclear(int cpu) { }
1977static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001978#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001979
Nadav Har'Eld462b812011-05-24 15:26:10 +03001980static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001982 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001983 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984
Nadav Har'Eld462b812011-05-24 15:26:10 +03001985 if (loaded_vmcs->cpu != cpu)
1986 return; /* vcpu migration can race with cpu offline */
1987 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001989 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001990 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001991
1992 /*
1993 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1994 * is before setting loaded_vmcs->vcpu to -1 which is done in
1995 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1996 * then adds the vmcs into percpu list before it is deleted.
1997 */
1998 smp_wmb();
1999
Nadav Har'Eld462b812011-05-24 15:26:10 +03002000 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002001 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002}
2003
Nadav Har'Eld462b812011-05-24 15:26:10 +03002004static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002005{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002006 int cpu = loaded_vmcs->cpu;
2007
2008 if (cpu != -1)
2009 smp_call_function_single(cpu,
2010 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002011}
2012
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002013static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002014{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002015 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002016 return;
2017
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002018 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002019 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002020}
2021
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002022static inline void vpid_sync_vcpu_global(void)
2023{
2024 if (cpu_has_vmx_invvpid_global())
2025 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2026}
2027
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002028static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002029{
2030 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002031 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002032 else
2033 vpid_sync_vcpu_global();
2034}
2035
Sheng Yang14394422008-04-28 12:24:45 +08002036static inline void ept_sync_global(void)
2037{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002038 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002039}
2040
2041static inline void ept_sync_context(u64 eptp)
2042{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002043 if (cpu_has_vmx_invept_context())
2044 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2045 else
2046 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002047}
2048
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002049static __always_inline void vmcs_check16(unsigned long field)
2050{
2051 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2052 "16-bit accessor invalid for 64-bit field");
2053 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2054 "16-bit accessor invalid for 64-bit high field");
2055 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2056 "16-bit accessor invalid for 32-bit high field");
2057 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2058 "16-bit accessor invalid for natural width field");
2059}
2060
2061static __always_inline void vmcs_check32(unsigned long field)
2062{
2063 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2064 "32-bit accessor invalid for 16-bit field");
2065 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2066 "32-bit accessor invalid for natural width field");
2067}
2068
2069static __always_inline void vmcs_check64(unsigned long field)
2070{
2071 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2072 "64-bit accessor invalid for 16-bit field");
2073 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2074 "64-bit accessor invalid for 64-bit high field");
2075 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2076 "64-bit accessor invalid for 32-bit field");
2077 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2078 "64-bit accessor invalid for natural width field");
2079}
2080
2081static __always_inline void vmcs_checkl(unsigned long field)
2082{
2083 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2084 "Natural width accessor invalid for 16-bit field");
2085 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2086 "Natural width accessor invalid for 64-bit field");
2087 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2088 "Natural width accessor invalid for 64-bit high field");
2089 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2090 "Natural width accessor invalid for 32-bit field");
2091}
2092
2093static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
Avi Kivity5e520e62011-05-15 10:13:12 -04002095 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096
Avi Kivity5e520e62011-05-15 10:13:12 -04002097 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2098 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 return value;
2100}
2101
Avi Kivity96304212011-05-15 10:13:13 -04002102static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002104 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002105 if (static_branch_unlikely(&enable_evmcs))
2106 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002107 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108}
2109
Avi Kivity96304212011-05-15 10:13:13 -04002110static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002112 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002113 if (static_branch_unlikely(&enable_evmcs))
2114 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002115 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116}
2117
Avi Kivity96304212011-05-15 10:13:13 -04002118static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002120 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002121 if (static_branch_unlikely(&enable_evmcs))
2122 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002123#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002124 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002126 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127#endif
2128}
2129
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002130static __always_inline unsigned long vmcs_readl(unsigned long field)
2131{
2132 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002133 if (static_branch_unlikely(&enable_evmcs))
2134 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002135 return __vmcs_readl(field);
2136}
2137
Avi Kivitye52de1b2007-01-05 16:36:56 -08002138static noinline void vmwrite_error(unsigned long field, unsigned long value)
2139{
2140 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2141 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2142 dump_stack();
2143}
2144
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002145static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146{
2147 u8 error;
2148
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002149 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002150 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002151 if (unlikely(error))
2152 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002158 if (static_branch_unlikely(&enable_evmcs))
2159 return evmcs_write16(field, value);
2160
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002161 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162}
2163
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002166 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002167 if (static_branch_unlikely(&enable_evmcs))
2168 return evmcs_write32(field, value);
2169
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002170 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171}
2172
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002173static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002175 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002176 if (static_branch_unlikely(&enable_evmcs))
2177 return evmcs_write64(field, value);
2178
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002179 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002180#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183#endif
2184}
2185
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002186static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002187{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002188 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002189 if (static_branch_unlikely(&enable_evmcs))
2190 return evmcs_write64(field, value);
2191
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002193}
2194
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002195static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002196{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002197 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2198 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002199 if (static_branch_unlikely(&enable_evmcs))
2200 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2201
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002202 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2203}
2204
2205static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2206{
2207 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2208 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002209 if (static_branch_unlikely(&enable_evmcs))
2210 return evmcs_write32(field, evmcs_read32(field) | mask);
2211
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002212 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002213}
2214
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002215static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2216{
2217 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2218}
2219
Gleb Natapov2961e8762013-11-25 15:37:13 +02002220static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2221{
2222 vmcs_write32(VM_ENTRY_CONTROLS, val);
2223 vmx->vm_entry_controls_shadow = val;
2224}
2225
2226static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2227{
2228 if (vmx->vm_entry_controls_shadow != val)
2229 vm_entry_controls_init(vmx, val);
2230}
2231
2232static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2233{
2234 return vmx->vm_entry_controls_shadow;
2235}
2236
2237
2238static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2239{
2240 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2241}
2242
2243static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2244{
2245 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2246}
2247
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002248static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2249{
2250 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2251}
2252
Gleb Natapov2961e8762013-11-25 15:37:13 +02002253static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2254{
2255 vmcs_write32(VM_EXIT_CONTROLS, val);
2256 vmx->vm_exit_controls_shadow = val;
2257}
2258
2259static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2260{
2261 if (vmx->vm_exit_controls_shadow != val)
2262 vm_exit_controls_init(vmx, val);
2263}
2264
2265static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2266{
2267 return vmx->vm_exit_controls_shadow;
2268}
2269
2270
2271static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2272{
2273 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2274}
2275
2276static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2277{
2278 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2279}
2280
Avi Kivity2fb92db2011-04-27 19:42:18 +03002281static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2282{
2283 vmx->segment_cache.bitmask = 0;
2284}
2285
2286static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2287 unsigned field)
2288{
2289 bool ret;
2290 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2291
2292 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2293 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2294 vmx->segment_cache.bitmask = 0;
2295 }
2296 ret = vmx->segment_cache.bitmask & mask;
2297 vmx->segment_cache.bitmask |= mask;
2298 return ret;
2299}
2300
2301static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2302{
2303 u16 *p = &vmx->segment_cache.seg[seg].selector;
2304
2305 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2306 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2307 return *p;
2308}
2309
2310static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2311{
2312 ulong *p = &vmx->segment_cache.seg[seg].base;
2313
2314 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2315 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2316 return *p;
2317}
2318
2319static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2320{
2321 u32 *p = &vmx->segment_cache.seg[seg].limit;
2322
2323 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2324 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2325 return *p;
2326}
2327
2328static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2329{
2330 u32 *p = &vmx->segment_cache.seg[seg].ar;
2331
2332 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2333 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2334 return *p;
2335}
2336
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002337static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2338{
2339 u32 eb;
2340
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002341 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002342 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002343 /*
2344 * Guest access to VMware backdoor ports could legitimately
2345 * trigger #GP because of TSS I/O permission bitmap.
2346 * We intercept those #GP and allow access to them anyway
2347 * as VMware does.
2348 */
2349 if (enable_vmware_backdoor)
2350 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002351 if ((vcpu->guest_debug &
2352 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2353 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2354 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002355 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002356 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002357 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002358 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002359
2360 /* When we are running a nested L2 guest and L1 specified for it a
2361 * certain exception bitmap, we must trap the same exceptions and pass
2362 * them to L1. When running L2, we will only handle the exceptions
2363 * specified above if L1 did not want them.
2364 */
2365 if (is_guest_mode(vcpu))
2366 eb |= get_vmcs12(vcpu)->exception_bitmap;
2367
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002368 vmcs_write32(EXCEPTION_BITMAP, eb);
2369}
2370
Ashok Raj15d45072018-02-01 22:59:43 +01002371/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002372 * Check if MSR is intercepted for currently loaded MSR bitmap.
2373 */
2374static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2375{
2376 unsigned long *msr_bitmap;
2377 int f = sizeof(unsigned long);
2378
2379 if (!cpu_has_vmx_msr_bitmap())
2380 return true;
2381
2382 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2383
2384 if (msr <= 0x1fff) {
2385 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2386 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2387 msr &= 0x1fff;
2388 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2389 }
2390
2391 return true;
2392}
2393
2394/*
Ashok Raj15d45072018-02-01 22:59:43 +01002395 * Check if MSR is intercepted for L01 MSR bitmap.
2396 */
2397static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2398{
2399 unsigned long *msr_bitmap;
2400 int f = sizeof(unsigned long);
2401
2402 if (!cpu_has_vmx_msr_bitmap())
2403 return true;
2404
2405 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2406
2407 if (msr <= 0x1fff) {
2408 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2409 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2410 msr &= 0x1fff;
2411 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2412 }
2413
2414 return true;
2415}
2416
Gleb Natapov2961e8762013-11-25 15:37:13 +02002417static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2418 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002419{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002420 vm_entry_controls_clearbit(vmx, entry);
2421 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002422}
2423
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002424static int find_msr(struct vmx_msrs *m, unsigned int msr)
2425{
2426 unsigned int i;
2427
2428 for (i = 0; i < m->nr; ++i) {
2429 if (m->val[i].index == msr)
2430 return i;
2431 }
2432 return -ENOENT;
2433}
2434
Avi Kivity61d2ef22010-04-28 16:40:38 +03002435static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2436{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002437 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002438 struct msr_autoload *m = &vmx->msr_autoload;
2439
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002440 switch (msr) {
2441 case MSR_EFER:
2442 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002443 clear_atomic_switch_msr_special(vmx,
2444 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002445 VM_EXIT_LOAD_IA32_EFER);
2446 return;
2447 }
2448 break;
2449 case MSR_CORE_PERF_GLOBAL_CTRL:
2450 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002451 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002452 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2453 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2454 return;
2455 }
2456 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002457 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002458 i = find_msr(&m->guest, msr);
2459 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002460 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002461 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002462 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002463 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002464
2465skip_guest:
2466 i = find_msr(&m->host, msr);
2467 if (i < 0)
2468 return;
2469
2470 --m->host.nr;
2471 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002472 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002473}
2474
Gleb Natapov2961e8762013-11-25 15:37:13 +02002475static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2476 unsigned long entry, unsigned long exit,
2477 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2478 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002479{
2480 vmcs_write64(guest_val_vmcs, guest_val);
2481 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002482 vm_entry_controls_setbit(vmx, entry);
2483 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002484}
2485
Avi Kivity61d2ef22010-04-28 16:40:38 +03002486static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002487 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002488{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002489 int i, j = 0;
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 add_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 GUEST_IA32_EFER,
2499 HOST_IA32_EFER,
2500 guest_val, host_val);
2501 return;
2502 }
2503 break;
2504 case MSR_CORE_PERF_GLOBAL_CTRL:
2505 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002506 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002507 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2508 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2509 GUEST_IA32_PERF_GLOBAL_CTRL,
2510 HOST_IA32_PERF_GLOBAL_CTRL,
2511 guest_val, host_val);
2512 return;
2513 }
2514 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002515 case MSR_IA32_PEBS_ENABLE:
2516 /* PEBS needs a quiescent period after being disabled (to write
2517 * a record). Disabling PEBS through VMX MSR swapping doesn't
2518 * provide that period, so a CPU could write host's record into
2519 * guest's memory.
2520 */
2521 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002522 }
2523
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002524 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002525 if (!entry_only)
2526 j = find_msr(&m->host, msr);
2527
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002528 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002529 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002530 "Can't add msr %x\n", msr);
2531 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002532 }
2533 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002534 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002535 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002536 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002537 m->guest.val[i].index = msr;
2538 m->guest.val[i].value = guest_val;
2539
2540 if (entry_only)
2541 return;
2542
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002543 if (j < 0) {
2544 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002545 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002546 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002547 m->host.val[j].index = msr;
2548 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002549}
2550
Avi Kivity92c0d902009-10-29 11:00:16 +02002551static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002552{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002553 u64 guest_efer = vmx->vcpu.arch.efer;
2554 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002555
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002556 if (!enable_ept) {
2557 /*
2558 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2559 * host CPUID is more efficient than testing guest CPUID
2560 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2561 */
2562 if (boot_cpu_has(X86_FEATURE_SMEP))
2563 guest_efer |= EFER_NX;
2564 else if (!(guest_efer & EFER_NX))
2565 ignore_bits |= EFER_NX;
2566 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002567
Avi Kivity51c6cf62007-08-29 03:48:05 +03002568 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002569 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002570 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002571 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002572#ifdef CONFIG_X86_64
2573 ignore_bits |= EFER_LMA | EFER_LME;
2574 /* SCE is meaningful only in long mode on Intel */
2575 if (guest_efer & EFER_LMA)
2576 ignore_bits &= ~(u64)EFER_SCE;
2577#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002578
2579 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002580
2581 /*
2582 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2583 * On CPUs that support "load IA32_EFER", always switch EFER
2584 * atomically, since it's faster than switching it manually.
2585 */
2586 if (cpu_has_load_ia32_efer ||
2587 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002588 if (!(guest_efer & EFER_LMA))
2589 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002590 if (guest_efer != host_efer)
2591 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002592 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002593 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002594 } else {
2595 guest_efer &= ~ignore_bits;
2596 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002597
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002598 vmx->guest_msrs[efer_offset].data = guest_efer;
2599 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2600
2601 return true;
2602 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002603}
2604
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002605#ifdef CONFIG_X86_32
2606/*
2607 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2608 * VMCS rather than the segment table. KVM uses this helper to figure
2609 * out the current bases to poke them into the VMCS before entry.
2610 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002611static unsigned long segment_base(u16 selector)
2612{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002613 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002614 unsigned long v;
2615
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002616 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002617 return 0;
2618
Thomas Garnier45fc8752017-03-14 10:05:08 -07002619 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002620
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002621 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002622 u16 ldt_selector = kvm_read_ldt();
2623
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002624 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002625 return 0;
2626
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002627 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002628 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002629 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002630 return v;
2631}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002632#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002633
Avi Kivity04d2cc72007-09-10 18:10:54 +03002634static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002635{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002636 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002637#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002638 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002639#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002640 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002641
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002642 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002643 return;
2644
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002645 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002646 /*
2647 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2648 * allow segment selectors with cpl > 0 or ti == 1.
2649 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002650 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002651 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002652
2653#ifdef CONFIG_X86_64
2654 save_fsgs_for_kvm();
2655 vmx->host_state.fs_sel = current->thread.fsindex;
2656 vmx->host_state.gs_sel = current->thread.gsindex;
2657#else
Avi Kivity9581d442010-10-19 16:46:55 +02002658 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002659 savesegment(gs, vmx->host_state.gs_sel);
2660#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002661 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002662 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002663 vmx->host_state.fs_reload_needed = 0;
2664 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002665 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002666 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002667 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002668 if (!(vmx->host_state.gs_sel & 7))
2669 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002670 else {
2671 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002672 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002673 }
2674
2675#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002676 savesegment(ds, vmx->host_state.ds_sel);
2677 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002678
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002679 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002680 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002681
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002682 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002683 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002684 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002685#else
2686 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2687 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2688#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002689 if (boot_cpu_has(X86_FEATURE_MPX))
2690 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002691 for (i = 0; i < vmx->save_nmsrs; ++i)
2692 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002693 vmx->guest_msrs[i].data,
2694 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002695}
2696
Avi Kivitya9b21b62008-06-24 11:48:49 +03002697static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002698{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002699 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002700 return;
2701
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002702 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002703 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002704#ifdef CONFIG_X86_64
2705 if (is_long_mode(&vmx->vcpu))
2706 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2707#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002708 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002709 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002710#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002711 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002712#else
2713 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002714#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002715 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002716 if (vmx->host_state.fs_reload_needed)
2717 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002718#ifdef CONFIG_X86_64
2719 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2720 loadsegment(ds, vmx->host_state.ds_sel);
2721 loadsegment(es, vmx->host_state.es_sel);
2722 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002723#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002724 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002725#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002726 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002727#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002728 if (vmx->host_state.msr_host_bndcfgs)
2729 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002730 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002731}
2732
Avi Kivitya9b21b62008-06-24 11:48:49 +03002733static void vmx_load_host_state(struct vcpu_vmx *vmx)
2734{
2735 preempt_disable();
2736 __vmx_load_host_state(vmx);
2737 preempt_enable();
2738}
2739
Feng Wu28b835d2015-09-18 22:29:54 +08002740static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2741{
2742 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2743 struct pi_desc old, new;
2744 unsigned int dest;
2745
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002746 /*
2747 * In case of hot-plug or hot-unplug, we may have to undo
2748 * vmx_vcpu_pi_put even if there is no assigned device. And we
2749 * always keep PI.NDST up to date for simplicity: it makes the
2750 * code easier, and CPU migration is not a fast path.
2751 */
2752 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002753 return;
2754
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002755 /*
2756 * First handle the simple case where no cmpxchg is necessary; just
2757 * allow posting non-urgent interrupts.
2758 *
2759 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2760 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2761 * expects the VCPU to be on the blocked_vcpu_list that matches
2762 * PI.NDST.
2763 */
2764 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2765 vcpu->cpu == cpu) {
2766 pi_clear_sn(pi_desc);
2767 return;
2768 }
2769
2770 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002771 do {
2772 old.control = new.control = pi_desc->control;
2773
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002774 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002775
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002776 if (x2apic_enabled())
2777 new.ndst = dest;
2778 else
2779 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002780
Feng Wu28b835d2015-09-18 22:29:54 +08002781 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002782 } while (cmpxchg64(&pi_desc->control, old.control,
2783 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002784}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002785
Peter Feinerc95ba922016-08-17 09:36:47 -07002786static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2787{
2788 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2789 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2790}
2791
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792/*
2793 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2794 * vcpu mutex is already taken.
2795 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002796static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002798 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002799 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002801 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002802 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002803 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002804 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002805
2806 /*
2807 * Read loaded_vmcs->cpu should be before fetching
2808 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2809 * See the comments in __loaded_vmcs_clear().
2810 */
2811 smp_rmb();
2812
Nadav Har'Eld462b812011-05-24 15:26:10 +03002813 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2814 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002815 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002816 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002817 }
2818
2819 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2820 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2821 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002822 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002823 }
2824
2825 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002826 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002827 unsigned long sysenter_esp;
2828
2829 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002830
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 /*
2832 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002833 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002835 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002836 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002837 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002839 /*
2840 * VM exits change the host TR limit to 0x67 after a VM
2841 * exit. This is okay, since 0x67 covers everything except
2842 * the IO bitmap and have have code to handle the IO bitmap
2843 * being lost after a VM exit.
2844 */
2845 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2846
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2848 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002849
Nadav Har'Eld462b812011-05-24 15:26:10 +03002850 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 }
Feng Wu28b835d2015-09-18 22:29:54 +08002852
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002853 /* Setup TSC multiplier */
2854 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002855 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2856 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002857
Feng Wu28b835d2015-09-18 22:29:54 +08002858 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002859 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002860 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002861}
2862
2863static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2864{
2865 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2866
2867 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002868 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2869 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002870 return;
2871
2872 /* Set SN when the vCPU is preempted */
2873 if (vcpu->preempted)
2874 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875}
2876
2877static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2878{
Feng Wu28b835d2015-09-18 22:29:54 +08002879 vmx_vcpu_pi_put(vcpu);
2880
Avi Kivitya9b21b62008-06-24 11:48:49 +03002881 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Wanpeng Lif244dee2017-07-20 01:11:54 -07002884static bool emulation_required(struct kvm_vcpu *vcpu)
2885{
2886 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2887}
2888
Avi Kivityedcafe32009-12-30 18:07:40 +02002889static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2890
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002891/*
2892 * Return the cr0 value that a nested guest would read. This is a combination
2893 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2894 * its hypervisor (cr0_read_shadow).
2895 */
2896static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2897{
2898 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2899 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2900}
2901static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2902{
2903 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2904 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2905}
2906
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2908{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002909 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002910
Avi Kivity6de12732011-03-07 12:51:22 +02002911 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2912 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2913 rflags = vmcs_readl(GUEST_RFLAGS);
2914 if (to_vmx(vcpu)->rmode.vm86_active) {
2915 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2916 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2917 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2918 }
2919 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002920 }
Avi Kivity6de12732011-03-07 12:51:22 +02002921 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922}
2923
2924static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2925{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002926 unsigned long old_rflags = vmx_get_rflags(vcpu);
2927
Avi Kivity6de12732011-03-07 12:51:22 +02002928 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2929 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002930 if (to_vmx(vcpu)->rmode.vm86_active) {
2931 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002932 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002933 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002935
2936 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2937 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938}
2939
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002940static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002941{
2942 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2943 int ret = 0;
2944
2945 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002946 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002947 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002948 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002949
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002950 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002951}
2952
2953static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2954{
2955 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2956 u32 interruptibility = interruptibility_old;
2957
2958 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2959
Jan Kiszka48005f62010-02-19 19:38:07 +01002960 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002961 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002962 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002963 interruptibility |= GUEST_INTR_STATE_STI;
2964
2965 if ((interruptibility != interruptibility_old))
2966 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2967}
2968
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2970{
2971 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002973 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002975 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976
Glauber Costa2809f5d2009-05-12 16:21:05 -04002977 /* skipping an emulated instruction also counts */
2978 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979}
2980
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002981static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2982 unsigned long exit_qual)
2983{
2984 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2985 unsigned int nr = vcpu->arch.exception.nr;
2986 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2987
2988 if (vcpu->arch.exception.has_error_code) {
2989 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2990 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2991 }
2992
2993 if (kvm_exception_is_soft(nr))
2994 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2995 else
2996 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2997
2998 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2999 vmx_get_nmi_mask(vcpu))
3000 intr_info |= INTR_INFO_UNBLOCK_NMI;
3001
3002 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3003}
3004
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003005/*
3006 * KVM wants to inject page-faults which it got to the guest. This function
3007 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003008 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003009static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003010{
3011 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003012 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003013
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003014 if (nr == PF_VECTOR) {
3015 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003016 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003017 return 1;
3018 }
3019 /*
3020 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3021 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3022 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3023 * can be written only when inject_pending_event runs. This should be
3024 * conditional on a new capability---if the capability is disabled,
3025 * kvm_multiple_exception would write the ancillary information to
3026 * CR2 or DR6, for backwards ABI-compatibility.
3027 */
3028 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3029 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003030 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003031 return 1;
3032 }
3033 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003034 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003035 if (nr == DB_VECTOR)
3036 *exit_qual = vcpu->arch.dr6;
3037 else
3038 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003039 return 1;
3040 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003041 }
3042
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003043 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003044}
3045
Wanpeng Licaa057a2018-03-12 04:53:03 -07003046static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3047{
3048 /*
3049 * Ensure that we clear the HLT state in the VMCS. We don't need to
3050 * explicitly skip the instruction because if the HLT state is set,
3051 * then the instruction is already executing and RIP has already been
3052 * advanced.
3053 */
3054 if (kvm_hlt_in_guest(vcpu->kvm) &&
3055 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3056 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3057}
3058
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003059static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003060{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003062 unsigned nr = vcpu->arch.exception.nr;
3063 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003064 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003065 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003066
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003067 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003068 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003069 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3070 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003071
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003072 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003073 int inc_eip = 0;
3074 if (kvm_exception_is_soft(nr))
3075 inc_eip = vcpu->arch.event_exit_inst_len;
3076 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003077 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003078 return;
3079 }
3080
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003081 WARN_ON_ONCE(vmx->emulation_required);
3082
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003083 if (kvm_exception_is_soft(nr)) {
3084 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3085 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003086 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3087 } else
3088 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3089
3090 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003091
3092 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003093}
3094
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003095static bool vmx_rdtscp_supported(void)
3096{
3097 return cpu_has_vmx_rdtscp();
3098}
3099
Mao, Junjiead756a12012-07-02 01:18:48 +00003100static bool vmx_invpcid_supported(void)
3101{
3102 return cpu_has_vmx_invpcid() && enable_ept;
3103}
3104
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105/*
Eddie Donga75beee2007-05-17 18:55:15 +03003106 * Swap MSR entry in host/guest MSR entry array.
3107 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003108static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003109{
Avi Kivity26bb0982009-09-07 11:14:12 +03003110 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003111
3112 tmp = vmx->guest_msrs[to];
3113 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3114 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003115}
3116
3117/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003118 * Set up the vmcs to automatically save and restore system
3119 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3120 * mode, as fiddling with msrs is very expensive.
3121 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003122static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003123{
Avi Kivity26bb0982009-09-07 11:14:12 +03003124 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003125
Eddie Donga75beee2007-05-17 18:55:15 +03003126 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003127#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003128 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003129 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003130 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003131 move_msr_up(vmx, index, save_nmsrs++);
3132 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003133 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003134 move_msr_up(vmx, index, save_nmsrs++);
3135 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003136 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003137 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003138 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003139 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003140 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003141 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003142 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003143 * if efer.sce is enabled.
3144 */
Brian Gerst8c065852010-07-17 09:03:26 -04003145 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003146 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003147 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003148 }
Eddie Donga75beee2007-05-17 18:55:15 +03003149#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003150 index = __find_msr_index(vmx, MSR_EFER);
3151 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003152 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003153
Avi Kivity26bb0982009-09-07 11:14:12 +03003154 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003155
Yang Zhang8d146952013-01-25 10:18:50 +08003156 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003157 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003158}
3159
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003160static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003162 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003164 if (is_guest_mode(vcpu) &&
3165 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3166 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3167
3168 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169}
3170
3171/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003172 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003174static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003176 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003177 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003178 * We're here if L1 chose not to trap WRMSR to TSC. According
3179 * to the spec, this should set L1's TSC; The offset that L1
3180 * set for L2 remains unchanged, and still needs to be added
3181 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003182 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003183 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003184 /* recalculate vmcs02.TSC_OFFSET: */
3185 vmcs12 = get_vmcs12(vcpu);
3186 vmcs_write64(TSC_OFFSET, offset +
3187 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3188 vmcs12->tsc_offset : 0));
3189 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003190 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3191 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003192 vmcs_write64(TSC_OFFSET, offset);
3193 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194}
3195
Nadav Har'El801d3422011-05-25 23:02:23 +03003196/*
3197 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3198 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3199 * all guests if the "nested" module option is off, and can also be disabled
3200 * for a single guest by disabling its VMX cpuid bit.
3201 */
3202static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3203{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003204 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003205}
3206
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003208 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3209 * returned for the various VMX controls MSRs when nested VMX is enabled.
3210 * The same values should also be used to verify that vmcs12 control fields are
3211 * valid during nested entry from L1 to L2.
3212 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3213 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3214 * bit in the high half is on if the corresponding bit in the control field
3215 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003216 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003217static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003218{
Paolo Bonzini13893092018-02-26 13:40:09 +01003219 if (!nested) {
3220 memset(msrs, 0, sizeof(*msrs));
3221 return;
3222 }
3223
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003224 /*
3225 * Note that as a general rule, the high half of the MSRs (bits in
3226 * the control fields which may be 1) should be initialized by the
3227 * intersection of the underlying hardware's MSR (i.e., features which
3228 * can be supported) and the list of features we want to expose -
3229 * because they are known to be properly supported in our code.
3230 * Also, usually, the low half of the MSRs (bits which must be 1) can
3231 * be set to 0, meaning that L1 may turn off any of these bits. The
3232 * reason is that if one of these bits is necessary, it will appear
3233 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3234 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003235 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003236 * These rules have exceptions below.
3237 */
3238
3239 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003240 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->pinbased_ctls_low,
3242 msrs->pinbased_ctls_high);
3243 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003244 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003246 PIN_BASED_EXT_INTR_MASK |
3247 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003248 PIN_BASED_VIRTUAL_NMIS |
3249 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003250 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003251 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003252 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003253
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003254 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003255 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003256 msrs->exit_ctls_low,
3257 msrs->exit_ctls_high);
3258 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003259 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003260
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003261 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003262#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003263 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003264#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003265 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003267 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003268 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003269 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3270
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003271 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003272 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003273
Jan Kiszka2996fca2014-06-16 13:59:43 +02003274 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003275 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003276
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003277 /* entry controls */
3278 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 msrs->entry_ctls_low,
3280 msrs->entry_ctls_high);
3281 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003282 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003283 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003284#ifdef CONFIG_X86_64
3285 VM_ENTRY_IA32E_MODE |
3286#endif
3287 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003288 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003289 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003290 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003291 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003292
Jan Kiszka2996fca2014-06-16 13:59:43 +02003293 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003294 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003295
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003296 /* cpu-based controls */
3297 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003298 msrs->procbased_ctls_low,
3299 msrs->procbased_ctls_high);
3300 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003301 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003302 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003303 CPU_BASED_VIRTUAL_INTR_PENDING |
3304 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003305 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3306 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3307 CPU_BASED_CR3_STORE_EXITING |
3308#ifdef CONFIG_X86_64
3309 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3310#endif
3311 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003312 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3313 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3314 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3315 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003316 /*
3317 * We can allow some features even when not supported by the
3318 * hardware. For example, L1 can specify an MSR bitmap - and we
3319 * can use it to avoid exits to L1 - even when L0 runs L2
3320 * without MSR bitmaps.
3321 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003323 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003324 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003325
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003326 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003327 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003328 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3329
Paolo Bonzini80154d72017-08-24 13:55:35 +02003330 /*
3331 * secondary cpu-based controls. Do not include those that
3332 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3333 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003334 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->secondary_ctls_low,
3336 msrs->secondary_ctls_high);
3337 msrs->secondary_ctls_low = 0;
3338 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003339 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003340 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003341 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003342 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003343 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003344 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003345
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003346 if (enable_ept) {
3347 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003348 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003349 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003350 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003351 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003352 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003354 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003355 msrs->ept_caps &= vmx_capability.ept;
3356 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003357 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3358 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003359 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003360 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003361 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003362 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003363 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003364 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003365
Bandan Das27c42a12017-08-03 15:54:42 -04003366 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003367 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003368 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003369 /*
3370 * Advertise EPTP switching unconditionally
3371 * since we emulate it
3372 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003373 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003375 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003376 }
3377
Paolo Bonzinief697a72016-03-18 16:58:38 +01003378 /*
3379 * Old versions of KVM use the single-context version without
3380 * checking for support, so declare that it is supported even
3381 * though it is treated as global context. The alternative is
3382 * not failing the single-context invvpid, and it is worse.
3383 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003384 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003386 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003388 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003389 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003390
Radim Krčmář0790ec12015-03-17 14:02:32 +01003391 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003392 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003393 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3394
Jan Kiszkac18911a2013-03-13 16:06:41 +01003395 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003396 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->misc_low,
3398 msrs->misc_high);
3399 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3400 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003401 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003402 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003403 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003405
3406 /*
3407 * This MSR reports some information about VMX support. We
3408 * should return information about the VMX we emulate for the
3409 * guest, and the VMCS structure we give it - not about the
3410 * VMX support of the underlying hardware.
3411 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003412 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003413 VMCS12_REVISION |
3414 VMX_BASIC_TRUE_CTLS |
3415 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3416 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3417
3418 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003419 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003420
3421 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003422 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003423 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3424 * We picked the standard core2 setting.
3425 */
3426#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3427#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003428 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3429 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003430
3431 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3433 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003434
3435 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003436 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003437}
3438
David Matlack38991522016-11-29 18:14:08 -08003439/*
3440 * if fixed0[i] == 1: val[i] must be 1
3441 * if fixed1[i] == 0: val[i] must be 0
3442 */
3443static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3444{
3445 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003446}
3447
3448static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3449{
David Matlack38991522016-11-29 18:14:08 -08003450 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003451}
3452
3453static inline u64 vmx_control_msr(u32 low, u32 high)
3454{
3455 return low | ((u64)high << 32);
3456}
3457
David Matlack62cc6b9d2016-11-29 18:14:07 -08003458static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3459{
3460 superset &= mask;
3461 subset &= mask;
3462
3463 return (superset | subset) == superset;
3464}
3465
3466static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3467{
3468 const u64 feature_and_reserved =
3469 /* feature (except bit 48; see below) */
3470 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3471 /* reserved */
3472 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003474
3475 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3476 return -EINVAL;
3477
3478 /*
3479 * KVM does not emulate a version of VMX that constrains physical
3480 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3481 */
3482 if (data & BIT_ULL(48))
3483 return -EINVAL;
3484
3485 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3486 vmx_basic_vmcs_revision_id(data))
3487 return -EINVAL;
3488
3489 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3490 return -EINVAL;
3491
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003492 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003493 return 0;
3494}
3495
3496static int
3497vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3498{
3499 u64 supported;
3500 u32 *lowp, *highp;
3501
3502 switch (msr_index) {
3503 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003504 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3505 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003506 break;
3507 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003508 lowp = &vmx->nested.msrs.procbased_ctls_low;
3509 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003510 break;
3511 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003512 lowp = &vmx->nested.msrs.exit_ctls_low;
3513 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003514 break;
3515 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003516 lowp = &vmx->nested.msrs.entry_ctls_low;
3517 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003518 break;
3519 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 lowp = &vmx->nested.msrs.secondary_ctls_low;
3521 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003522 break;
3523 default:
3524 BUG();
3525 }
3526
3527 supported = vmx_control_msr(*lowp, *highp);
3528
3529 /* Check must-be-1 bits are still 1. */
3530 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3531 return -EINVAL;
3532
3533 /* Check must-be-0 bits are still 0. */
3534 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3535 return -EINVAL;
3536
3537 *lowp = data;
3538 *highp = data >> 32;
3539 return 0;
3540}
3541
3542static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3543{
3544 const u64 feature_and_reserved_bits =
3545 /* feature */
3546 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3547 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3548 /* reserved */
3549 GENMASK_ULL(13, 9) | BIT_ULL(31);
3550 u64 vmx_misc;
3551
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003552 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3553 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003554
3555 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3556 return -EINVAL;
3557
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3560 vmx_misc_preemption_timer_rate(data) !=
3561 vmx_misc_preemption_timer_rate(vmx_misc))
3562 return -EINVAL;
3563
3564 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3565 return -EINVAL;
3566
3567 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3568 return -EINVAL;
3569
3570 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3571 return -EINVAL;
3572
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003573 vmx->nested.msrs.misc_low = data;
3574 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003575
3576 /*
3577 * If L1 has read-only VM-exit information fields, use the
3578 * less permissive vmx_vmwrite_bitmap to specify write
3579 * permissions for the shadow VMCS.
3580 */
3581 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3582 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3583
David Matlack62cc6b9d2016-11-29 18:14:07 -08003584 return 0;
3585}
3586
3587static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3588{
3589 u64 vmx_ept_vpid_cap;
3590
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003591 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3592 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003593
3594 /* Every bit is either reserved or a feature bit. */
3595 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3596 return -EINVAL;
3597
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003598 vmx->nested.msrs.ept_caps = data;
3599 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003600 return 0;
3601}
3602
3603static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3604{
3605 u64 *msr;
3606
3607 switch (msr_index) {
3608 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003609 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003610 break;
3611 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003613 break;
3614 default:
3615 BUG();
3616 }
3617
3618 /*
3619 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3620 * must be 1 in the restored value.
3621 */
3622 if (!is_bitwise_subset(data, *msr, -1ULL))
3623 return -EINVAL;
3624
3625 *msr = data;
3626 return 0;
3627}
3628
3629/*
3630 * Called when userspace is restoring VMX MSRs.
3631 *
3632 * Returns 0 on success, non-0 otherwise.
3633 */
3634static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3635{
3636 struct vcpu_vmx *vmx = to_vmx(vcpu);
3637
Jim Mattsona943ac52018-05-29 09:11:32 -07003638 /*
3639 * Don't allow changes to the VMX capability MSRs while the vCPU
3640 * is in VMX operation.
3641 */
3642 if (vmx->nested.vmxon)
3643 return -EBUSY;
3644
David Matlack62cc6b9d2016-11-29 18:14:07 -08003645 switch (msr_index) {
3646 case MSR_IA32_VMX_BASIC:
3647 return vmx_restore_vmx_basic(vmx, data);
3648 case MSR_IA32_VMX_PINBASED_CTLS:
3649 case MSR_IA32_VMX_PROCBASED_CTLS:
3650 case MSR_IA32_VMX_EXIT_CTLS:
3651 case MSR_IA32_VMX_ENTRY_CTLS:
3652 /*
3653 * The "non-true" VMX capability MSRs are generated from the
3654 * "true" MSRs, so we do not support restoring them directly.
3655 *
3656 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3657 * should restore the "true" MSRs with the must-be-1 bits
3658 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3659 * DEFAULT SETTINGS".
3660 */
3661 return -EINVAL;
3662 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3663 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3664 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3665 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3666 case MSR_IA32_VMX_PROCBASED_CTLS2:
3667 return vmx_restore_control_msr(vmx, msr_index, data);
3668 case MSR_IA32_VMX_MISC:
3669 return vmx_restore_vmx_misc(vmx, data);
3670 case MSR_IA32_VMX_CR0_FIXED0:
3671 case MSR_IA32_VMX_CR4_FIXED0:
3672 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3673 case MSR_IA32_VMX_CR0_FIXED1:
3674 case MSR_IA32_VMX_CR4_FIXED1:
3675 /*
3676 * These MSRs are generated based on the vCPU's CPUID, so we
3677 * do not support restoring them directly.
3678 */
3679 return -EINVAL;
3680 case MSR_IA32_VMX_EPT_VPID_CAP:
3681 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3682 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003684 return 0;
3685 default:
3686 /*
3687 * The rest of the VMX capability MSRs do not support restore.
3688 */
3689 return -EINVAL;
3690 }
3691}
3692
Jan Kiszkacae50132014-01-04 18:47:22 +01003693/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003696 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003697 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003698 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003699 break;
3700 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3701 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003702 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003703 msrs->pinbased_ctls_low,
3704 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003705 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3706 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003707 break;
3708 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3709 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003710 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003711 msrs->procbased_ctls_low,
3712 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003713 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3714 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003715 break;
3716 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3717 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003718 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003719 msrs->exit_ctls_low,
3720 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003721 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3722 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003723 break;
3724 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3725 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003726 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003727 msrs->entry_ctls_low,
3728 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003729 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3730 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003731 break;
3732 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003733 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003734 msrs->misc_low,
3735 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003737 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003738 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003739 break;
3740 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003741 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003742 break;
3743 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003744 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003745 break;
3746 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003747 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003748 break;
3749 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003750 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003751 break;
3752 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003753 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003754 msrs->secondary_ctls_low,
3755 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003756 break;
3757 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 *pdata = msrs->ept_caps |
3759 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003760 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003761 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003762 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003763 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003764 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003765 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003766 }
3767
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003768 return 0;
3769}
3770
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003771static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3772 uint64_t val)
3773{
3774 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3775
3776 return !(val & ~valid_bits);
3777}
3778
Tom Lendacky801e4592018-02-21 13:39:51 -06003779static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3780{
Paolo Bonzini13893092018-02-26 13:40:09 +01003781 switch (msr->index) {
3782 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3783 if (!nested)
3784 return 1;
3785 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3786 default:
3787 return 1;
3788 }
3789
3790 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003791}
3792
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003793/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794 * Reads an msr value (of 'msr_index') into 'pdata'.
3795 * Returns 0 on success, non-0 otherwise.
3796 * Assumes vcpu_load() was already called.
3797 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003798static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003800 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003801 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003803 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003804#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003806 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807 break;
3808 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003809 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003811 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003812 vmx_load_host_state(vmx);
3813 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003814 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003815#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003817 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003818 case MSR_IA32_SPEC_CTRL:
3819 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003820 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3821 return 1;
3822
3823 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3824 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003825 case MSR_IA32_ARCH_CAPABILITIES:
3826 if (!msr_info->host_initiated &&
3827 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3828 return 1;
3829 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3830 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003832 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833 break;
3834 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003835 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836 break;
3837 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003838 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003840 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003841 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003842 (!msr_info->host_initiated &&
3843 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003844 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003845 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003846 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003847 case MSR_IA32_MCG_EXT_CTL:
3848 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003849 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003850 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003851 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003852 msr_info->data = vcpu->arch.mcg_ext_ctl;
3853 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003854 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003855 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003856 break;
3857 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3858 if (!nested_vmx_allowed(vcpu))
3859 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003860 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3861 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003862 case MSR_IA32_XSS:
3863 if (!vmx_xsaves_supported())
3864 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003865 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003866 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003867 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003868 if (!msr_info->host_initiated &&
3869 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003870 return 1;
3871 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003873 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003874 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003878 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 }
3880
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 return 0;
3882}
3883
Jan Kiszkacae50132014-01-04 18:47:22 +01003884static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3885
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886/*
3887 * Writes msr value into into the appropriate "register".
3888 * Returns 0 on success, non-0 otherwise.
3889 * Assumes vcpu_load() was already called.
3890 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003891static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003893 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003894 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003895 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003896 u32 msr_index = msr_info->index;
3897 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003898
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003900 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003901 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003902 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003903#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003904 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003905 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003906 vmcs_writel(GUEST_FS_BASE, data);
3907 break;
3908 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003909 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 vmcs_writel(GUEST_GS_BASE, data);
3911 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003912 case MSR_KERNEL_GS_BASE:
3913 vmx_load_host_state(vmx);
3914 vmx->msr_guest_kernel_gs_base = data;
3915 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916#endif
3917 case MSR_IA32_SYSENTER_CS:
3918 vmcs_write32(GUEST_SYSENTER_CS, data);
3919 break;
3920 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003921 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 break;
3923 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003924 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003926 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003927 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003928 (!msr_info->host_initiated &&
3929 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003930 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003931 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003932 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003934 vmcs_write64(GUEST_BNDCFGS, data);
3935 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003936 case MSR_IA32_SPEC_CTRL:
3937 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003938 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3939 return 1;
3940
3941 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003942 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003943 return 1;
3944
3945 vmx->spec_ctrl = data;
3946
3947 if (!data)
3948 break;
3949
3950 /*
3951 * For non-nested:
3952 * When it's written (to non-zero) for the first time, pass
3953 * it through.
3954 *
3955 * For nested:
3956 * The handling of the MSR bitmap for L2 guests is done in
3957 * nested_vmx_merge_msr_bitmap. We should not touch the
3958 * vmcs02.msr_bitmap here since it gets completely overwritten
3959 * in the merging. We update the vmcs01 here for L1 as well
3960 * since it will end up touching the MSR anyway now.
3961 */
3962 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3963 MSR_IA32_SPEC_CTRL,
3964 MSR_TYPE_RW);
3965 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003966 case MSR_IA32_PRED_CMD:
3967 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003968 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3969 return 1;
3970
3971 if (data & ~PRED_CMD_IBPB)
3972 return 1;
3973
3974 if (!data)
3975 break;
3976
3977 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3978
3979 /*
3980 * For non-nested:
3981 * When it's written (to non-zero) for the first time, pass
3982 * it through.
3983 *
3984 * For nested:
3985 * The handling of the MSR bitmap for L2 guests is done in
3986 * nested_vmx_merge_msr_bitmap. We should not touch the
3987 * vmcs02.msr_bitmap here since it gets completely overwritten
3988 * in the merging.
3989 */
3990 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3991 MSR_TYPE_W);
3992 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003993 case MSR_IA32_ARCH_CAPABILITIES:
3994 if (!msr_info->host_initiated)
3995 return 1;
3996 vmx->arch_capabilities = data;
3997 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003998 case MSR_IA32_CR_PAT:
3999 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004000 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4001 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004002 vmcs_write64(GUEST_IA32_PAT, data);
4003 vcpu->arch.pat = data;
4004 break;
4005 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004006 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004007 break;
Will Auldba904632012-11-29 12:42:50 -08004008 case MSR_IA32_TSC_ADJUST:
4009 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004010 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004011 case MSR_IA32_MCG_EXT_CTL:
4012 if ((!msr_info->host_initiated &&
4013 !(to_vmx(vcpu)->msr_ia32_feature_control &
4014 FEATURE_CONTROL_LMCE)) ||
4015 (data & ~MCG_EXT_CTL_LMCE_EN))
4016 return 1;
4017 vcpu->arch.mcg_ext_ctl = data;
4018 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004019 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004020 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004021 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004022 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4023 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004024 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004025 if (msr_info->host_initiated && data == 0)
4026 vmx_leave_nested(vcpu);
4027 break;
4028 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004029 if (!msr_info->host_initiated)
4030 return 1; /* they are read-only */
4031 if (!nested_vmx_allowed(vcpu))
4032 return 1;
4033 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004034 case MSR_IA32_XSS:
4035 if (!vmx_xsaves_supported())
4036 return 1;
4037 /*
4038 * The only supported bit as of Skylake is bit 8, but
4039 * it is not supported on KVM.
4040 */
4041 if (data != 0)
4042 return 1;
4043 vcpu->arch.ia32_xss = data;
4044 if (vcpu->arch.ia32_xss != host_xss)
4045 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004046 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004047 else
4048 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4049 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004050 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004051 if (!msr_info->host_initiated &&
4052 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004053 return 1;
4054 /* Check reserved bit, higher 32 bits should be zero */
4055 if ((data >> 32) != 0)
4056 return 1;
4057 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004059 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004060 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004061 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004062 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004063 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4064 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004065 ret = kvm_set_shared_msr(msr->index, msr->data,
4066 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004067 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004068 if (ret)
4069 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004070 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004073 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 }
4075
Eddie Dong2cc51562007-05-21 07:28:09 +03004076 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077}
4078
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004079static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004081 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4082 switch (reg) {
4083 case VCPU_REGS_RSP:
4084 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4085 break;
4086 case VCPU_REGS_RIP:
4087 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4088 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004089 case VCPU_EXREG_PDPTR:
4090 if (enable_ept)
4091 ept_save_pdptrs(vcpu);
4092 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004093 default:
4094 break;
4095 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096}
4097
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098static __init int cpu_has_kvm_support(void)
4099{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004100 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101}
4102
4103static __init int vmx_disabled_by_bios(void)
4104{
4105 u64 msr;
4106
4107 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004108 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004109 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004110 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4111 && tboot_enabled())
4112 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004113 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004114 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004115 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004116 && !tboot_enabled()) {
4117 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004118 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004119 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004120 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004121 /* launched w/o TXT and VMX disabled */
4122 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4123 && !tboot_enabled())
4124 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004125 }
4126
4127 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128}
4129
Dongxiao Xu7725b892010-05-11 18:29:38 +08004130static void kvm_cpu_vmxon(u64 addr)
4131{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004132 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004133 intel_pt_handle_vmx(1);
4134
Dongxiao Xu7725b892010-05-11 18:29:38 +08004135 asm volatile (ASM_VMX_VMXON_RAX
4136 : : "a"(&addr), "m"(addr)
4137 : "memory", "cc");
4138}
4139
Radim Krčmář13a34e02014-08-28 15:13:03 +02004140static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141{
4142 int cpu = raw_smp_processor_id();
4143 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004144 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004146 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004147 return -EBUSY;
4148
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004149 /*
4150 * This can happen if we hot-added a CPU but failed to allocate
4151 * VP assist page for it.
4152 */
4153 if (static_branch_unlikely(&enable_evmcs) &&
4154 !hv_get_vp_assist_page(cpu))
4155 return -EFAULT;
4156
Nadav Har'Eld462b812011-05-24 15:26:10 +03004157 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004158 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4159 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004160
4161 /*
4162 * Now we can enable the vmclear operation in kdump
4163 * since the loaded_vmcss_on_cpu list on this cpu
4164 * has been initialized.
4165 *
4166 * Though the cpu is not in VMX operation now, there
4167 * is no problem to enable the vmclear operation
4168 * for the loaded_vmcss_on_cpu list is empty!
4169 */
4170 crash_enable_local_vmclear(cpu);
4171
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004173
4174 test_bits = FEATURE_CONTROL_LOCKED;
4175 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4176 if (tboot_enabled())
4177 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4178
4179 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004181 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4182 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004183 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004184 if (enable_ept)
4185 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004186
4187 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188}
4189
Nadav Har'Eld462b812011-05-24 15:26:10 +03004190static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004191{
4192 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004193 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004194
Nadav Har'Eld462b812011-05-24 15:26:10 +03004195 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4196 loaded_vmcss_on_cpu_link)
4197 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004198}
4199
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004200
4201/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4202 * tricks.
4203 */
4204static void kvm_cpu_vmxoff(void)
4205{
4206 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004207
4208 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004209 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004210}
4211
Radim Krčmář13a34e02014-08-28 15:13:03 +02004212static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004214 vmclear_local_loaded_vmcss();
4215 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216}
4217
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004218static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004219 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220{
4221 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004222 u32 ctl = ctl_min | ctl_opt;
4223
4224 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4225
4226 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4227 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4228
4229 /* Ensure minimum (required) set of control bits are supported. */
4230 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004231 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004232
4233 *result = ctl;
4234 return 0;
4235}
4236
Avi Kivity110312c2010-12-21 12:54:20 +02004237static __init bool allow_1_setting(u32 msr, u32 ctl)
4238{
4239 u32 vmx_msr_low, vmx_msr_high;
4240
4241 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4242 return vmx_msr_high & ctl;
4243}
4244
Yang, Sheng002c7f72007-07-31 14:23:01 +03004245static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004246{
4247 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004248 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004249 u32 _pin_based_exec_control = 0;
4250 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004251 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004252 u32 _vmexit_control = 0;
4253 u32 _vmentry_control = 0;
4254
Paolo Bonzini13893092018-02-26 13:40:09 +01004255 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304256 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004257#ifdef CONFIG_X86_64
4258 CPU_BASED_CR8_LOAD_EXITING |
4259 CPU_BASED_CR8_STORE_EXITING |
4260#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004261 CPU_BASED_CR3_LOAD_EXITING |
4262 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004263 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004264 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004265 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004266 CPU_BASED_MWAIT_EXITING |
4267 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004268 CPU_BASED_INVLPG_EXITING |
4269 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004270
Sheng Yangf78e0e22007-10-29 09:40:42 +08004271 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004272 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004273 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004274 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4275 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004276 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004277#ifdef CONFIG_X86_64
4278 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4279 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4280 ~CPU_BASED_CR8_STORE_EXITING;
4281#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004282 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004283 min2 = 0;
4284 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004285 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004286 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004287 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004288 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004289 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004290 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004291 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004292 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004293 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004294 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004295 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004296 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004297 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004298 SECONDARY_EXEC_RDSEED_EXITING |
4299 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004300 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004301 SECONDARY_EXEC_TSC_SCALING |
4302 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004303 if (adjust_vmx_controls(min2, opt2,
4304 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004305 &_cpu_based_2nd_exec_control) < 0)
4306 return -EIO;
4307 }
4308#ifndef CONFIG_X86_64
4309 if (!(_cpu_based_2nd_exec_control &
4310 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4311 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4312#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004313
4314 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4315 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004316 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004317 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4318 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004319
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004320 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4321 &vmx_capability.ept, &vmx_capability.vpid);
4322
Sheng Yangd56f5462008-04-25 10:13:16 +08004323 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004324 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4325 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004326 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4327 CPU_BASED_CR3_STORE_EXITING |
4328 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004329 } else if (vmx_capability.ept) {
4330 vmx_capability.ept = 0;
4331 pr_warn_once("EPT CAP should not exist if not support "
4332 "1-setting enable EPT VM-execution control\n");
4333 }
4334 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4335 vmx_capability.vpid) {
4336 vmx_capability.vpid = 0;
4337 pr_warn_once("VPID CAP should not exist if not support "
4338 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004339 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004340
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004341 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004342#ifdef CONFIG_X86_64
4343 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4344#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004345 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004346 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004347 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4348 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004349 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004350
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004351 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4352 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4353 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004354 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4355 &_pin_based_exec_control) < 0)
4356 return -EIO;
4357
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004358 if (cpu_has_broken_vmx_preemption_timer())
4359 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004360 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004361 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004362 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4363
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004364 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004365 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004366 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4367 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004368 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004369
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004370 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004371
4372 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4373 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004374 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004375
4376#ifdef CONFIG_X86_64
4377 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4378 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004379 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004380#endif
4381
4382 /* Require Write-Back (WB) memory type for VMCS accesses. */
4383 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004384 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004385
Yang, Sheng002c7f72007-07-31 14:23:01 +03004386 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004387 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004388 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004389
4390 /* KVM supports Enlightened VMCS v1 only */
4391 if (static_branch_unlikely(&enable_evmcs))
4392 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4393 else
4394 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004395
Yang, Sheng002c7f72007-07-31 14:23:01 +03004396 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4397 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004398 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004399 vmcs_conf->vmexit_ctrl = _vmexit_control;
4400 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004401
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004402 if (static_branch_unlikely(&enable_evmcs))
4403 evmcs_sanitize_exec_ctrls(vmcs_conf);
4404
Avi Kivity110312c2010-12-21 12:54:20 +02004405 cpu_has_load_ia32_efer =
4406 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4407 VM_ENTRY_LOAD_IA32_EFER)
4408 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4409 VM_EXIT_LOAD_IA32_EFER);
4410
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004411 cpu_has_load_perf_global_ctrl =
4412 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4413 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4414 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4415 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4416
4417 /*
4418 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004419 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004420 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4421 *
4422 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4423 *
4424 * AAK155 (model 26)
4425 * AAP115 (model 30)
4426 * AAT100 (model 37)
4427 * BC86,AAY89,BD102 (model 44)
4428 * BA97 (model 46)
4429 *
4430 */
4431 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4432 switch (boot_cpu_data.x86_model) {
4433 case 26:
4434 case 30:
4435 case 37:
4436 case 44:
4437 case 46:
4438 cpu_has_load_perf_global_ctrl = false;
4439 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4440 "does not work properly. Using workaround\n");
4441 break;
4442 default:
4443 break;
4444 }
4445 }
4446
Borislav Petkov782511b2016-04-04 22:25:03 +02004447 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004448 rdmsrl(MSR_IA32_XSS, host_xss);
4449
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004450 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004451}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452
4453static struct vmcs *alloc_vmcs_cpu(int cpu)
4454{
4455 int node = cpu_to_node(cpu);
4456 struct page *pages;
4457 struct vmcs *vmcs;
4458
Vlastimil Babka96db8002015-09-08 15:03:50 -07004459 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460 if (!pages)
4461 return NULL;
4462 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004463 memset(vmcs, 0, vmcs_config.size);
4464 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465 return vmcs;
4466}
4467
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468static void free_vmcs(struct vmcs *vmcs)
4469{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004470 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471}
4472
Nadav Har'Eld462b812011-05-24 15:26:10 +03004473/*
4474 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4475 */
4476static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4477{
4478 if (!loaded_vmcs->vmcs)
4479 return;
4480 loaded_vmcs_clear(loaded_vmcs);
4481 free_vmcs(loaded_vmcs->vmcs);
4482 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004483 if (loaded_vmcs->msr_bitmap)
4484 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004485 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004486}
4487
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004488static struct vmcs *alloc_vmcs(void)
4489{
4490 return alloc_vmcs_cpu(raw_smp_processor_id());
4491}
4492
4493static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4494{
4495 loaded_vmcs->vmcs = alloc_vmcs();
4496 if (!loaded_vmcs->vmcs)
4497 return -ENOMEM;
4498
4499 loaded_vmcs->shadow_vmcs = NULL;
4500 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004501
4502 if (cpu_has_vmx_msr_bitmap()) {
4503 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4504 if (!loaded_vmcs->msr_bitmap)
4505 goto out_vmcs;
4506 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004507
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004508 if (IS_ENABLED(CONFIG_HYPERV) &&
4509 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004510 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4511 struct hv_enlightened_vmcs *evmcs =
4512 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4513
4514 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4515 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004516 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004517 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004518
4519out_vmcs:
4520 free_loaded_vmcs(loaded_vmcs);
4521 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004522}
4523
Sam Ravnborg39959582007-06-01 00:47:13 -07004524static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525{
4526 int cpu;
4527
Zachary Amsden3230bb42009-09-29 11:38:37 -10004528 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004530 per_cpu(vmxarea, cpu) = NULL;
4531 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532}
4533
Jim Mattsond37f4262017-12-22 12:12:16 -08004534enum vmcs_field_width {
4535 VMCS_FIELD_WIDTH_U16 = 0,
4536 VMCS_FIELD_WIDTH_U64 = 1,
4537 VMCS_FIELD_WIDTH_U32 = 2,
4538 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004539};
4540
Jim Mattsond37f4262017-12-22 12:12:16 -08004541static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004542{
4543 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004544 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004545 return (field >> 13) & 0x3 ;
4546}
4547
4548static inline int vmcs_field_readonly(unsigned long field)
4549{
4550 return (((field >> 10) & 0x3) == 1);
4551}
4552
Bandan Dasfe2b2012014-04-21 15:20:14 -04004553static void init_vmcs_shadow_fields(void)
4554{
4555 int i, j;
4556
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004557 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4558 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004559 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004560 (i + 1 == max_shadow_read_only_fields ||
4561 shadow_read_only_fields[i + 1] != field + 1))
4562 pr_err("Missing field from shadow_read_only_field %x\n",
4563 field + 1);
4564
4565 clear_bit(field, vmx_vmread_bitmap);
4566#ifdef CONFIG_X86_64
4567 if (field & 1)
4568 continue;
4569#endif
4570 if (j < i)
4571 shadow_read_only_fields[j] = field;
4572 j++;
4573 }
4574 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004575
4576 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004577 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004578 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004579 (i + 1 == max_shadow_read_write_fields ||
4580 shadow_read_write_fields[i + 1] != field + 1))
4581 pr_err("Missing field from shadow_read_write_field %x\n",
4582 field + 1);
4583
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004584 /*
4585 * PML and the preemption timer can be emulated, but the
4586 * processor cannot vmwrite to fields that don't exist
4587 * on bare metal.
4588 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004589 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004590 case GUEST_PML_INDEX:
4591 if (!cpu_has_vmx_pml())
4592 continue;
4593 break;
4594 case VMX_PREEMPTION_TIMER_VALUE:
4595 if (!cpu_has_vmx_preemption_timer())
4596 continue;
4597 break;
4598 case GUEST_INTR_STATUS:
4599 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004600 continue;
4601 break;
4602 default:
4603 break;
4604 }
4605
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004606 clear_bit(field, vmx_vmwrite_bitmap);
4607 clear_bit(field, vmx_vmread_bitmap);
4608#ifdef CONFIG_X86_64
4609 if (field & 1)
4610 continue;
4611#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004612 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004613 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004614 j++;
4615 }
4616 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004617}
4618
Avi Kivity6aa8b732006-12-10 02:21:36 -08004619static __init int alloc_kvm_area(void)
4620{
4621 int cpu;
4622
Zachary Amsden3230bb42009-09-29 11:38:37 -10004623 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004624 struct vmcs *vmcs;
4625
4626 vmcs = alloc_vmcs_cpu(cpu);
4627 if (!vmcs) {
4628 free_kvm_area();
4629 return -ENOMEM;
4630 }
4631
4632 per_cpu(vmxarea, cpu) = vmcs;
4633 }
4634 return 0;
4635}
4636
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004637static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004638 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004640 if (!emulate_invalid_guest_state) {
4641 /*
4642 * CS and SS RPL should be equal during guest entry according
4643 * to VMX spec, but in reality it is not always so. Since vcpu
4644 * is in the middle of the transition from real mode to
4645 * protected mode it is safe to assume that RPL 0 is a good
4646 * default value.
4647 */
4648 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004649 save->selector &= ~SEGMENT_RPL_MASK;
4650 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004651 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004652 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004653 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654}
4655
4656static void enter_pmode(struct kvm_vcpu *vcpu)
4657{
4658 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004659 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660
Gleb Natapovd99e4152012-12-20 16:57:45 +02004661 /*
4662 * Update real mode segment cache. It may be not up-to-date if sement
4663 * register was written while vcpu was in a guest mode.
4664 */
4665 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4666 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4667 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4668 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4669 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4670 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4671
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004672 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004673
Avi Kivity2fb92db2011-04-27 19:42:18 +03004674 vmx_segment_cache_clear(vmx);
4675
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004676 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677
4678 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004679 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4680 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681 vmcs_writel(GUEST_RFLAGS, flags);
4682
Rusty Russell66aee912007-07-17 23:34:16 +10004683 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4684 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685
4686 update_exception_bitmap(vcpu);
4687
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004688 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4689 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4690 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4691 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4692 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4693 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694}
4695
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004696static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697{
Mathias Krause772e0312012-08-30 01:30:19 +02004698 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004699 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700
Gleb Natapovd99e4152012-12-20 16:57:45 +02004701 var.dpl = 0x3;
4702 if (seg == VCPU_SREG_CS)
4703 var.type = 0x3;
4704
4705 if (!emulate_invalid_guest_state) {
4706 var.selector = var.base >> 4;
4707 var.base = var.base & 0xffff0;
4708 var.limit = 0xffff;
4709 var.g = 0;
4710 var.db = 0;
4711 var.present = 1;
4712 var.s = 1;
4713 var.l = 0;
4714 var.unusable = 0;
4715 var.type = 0x3;
4716 var.avl = 0;
4717 if (save->base & 0xf)
4718 printk_once(KERN_WARNING "kvm: segment base is not "
4719 "paragraph aligned when entering "
4720 "protected mode (seg=%d)", seg);
4721 }
4722
4723 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004724 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004725 vmcs_write32(sf->limit, var.limit);
4726 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727}
4728
4729static void enter_rmode(struct kvm_vcpu *vcpu)
4730{
4731 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004732 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004733 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004735 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4736 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4737 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4738 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4739 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004740 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4741 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004742
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004743 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744
Gleb Natapov776e58e2011-03-13 12:34:27 +02004745 /*
4746 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004747 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004748 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004749 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004750 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4751 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004752
Avi Kivity2fb92db2011-04-27 19:42:18 +03004753 vmx_segment_cache_clear(vmx);
4754
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004755 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4758
4759 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004760 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004762 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763
4764 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004765 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766 update_exception_bitmap(vcpu);
4767
Gleb Natapovd99e4152012-12-20 16:57:45 +02004768 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4769 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4770 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4771 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4772 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4773 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004774
Eddie Dong8668a3c2007-10-10 14:26:45 +08004775 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004776}
4777
Amit Shah401d10d2009-02-20 22:53:37 +05304778static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4779{
4780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004781 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4782
4783 if (!msr)
4784 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304785
Avi Kivity44ea2b12009-09-06 15:55:37 +03004786 /*
4787 * Force kernel_gs_base reloading before EFER changes, as control
4788 * of this msr depends on is_long_mode().
4789 */
4790 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004791 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304792 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004793 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304794 msr->data = efer;
4795 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004796 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304797
4798 msr->data = efer & ~EFER_LME;
4799 }
4800 setup_msrs(vmx);
4801}
4802
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004803#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804
4805static void enter_lmode(struct kvm_vcpu *vcpu)
4806{
4807 u32 guest_tr_ar;
4808
Avi Kivity2fb92db2011-04-27 19:42:18 +03004809 vmx_segment_cache_clear(to_vmx(vcpu));
4810
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004812 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004813 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4814 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004816 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4817 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818 }
Avi Kivityda38f432010-07-06 11:30:49 +03004819 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820}
4821
4822static void exit_lmode(struct kvm_vcpu *vcpu)
4823{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004824 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004825 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826}
4827
4828#endif
4829
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004830static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4831 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004832{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004833 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004834 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4835 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004836 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004837 } else {
4838 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004839 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004840}
4841
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004842static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004843{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004844 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004845}
4846
Avi Kivitye8467fd2009-12-29 18:43:06 +02004847static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4848{
4849 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4850
4851 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4852 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4853}
4854
Avi Kivityaff48ba2010-12-05 18:56:11 +02004855static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4856{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004857 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004858 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4859 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4860}
4861
Anthony Liguori25c4c272007-04-27 09:29:21 +03004862static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004863{
Avi Kivityfc78f512009-12-07 12:16:48 +02004864 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4865
4866 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4867 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004868}
4869
Sheng Yang14394422008-04-28 12:24:45 +08004870static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4871{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004872 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4873
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004874 if (!test_bit(VCPU_EXREG_PDPTR,
4875 (unsigned long *)&vcpu->arch.regs_dirty))
4876 return;
4877
Sheng Yang14394422008-04-28 12:24:45 +08004878 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004879 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4880 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4881 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4882 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004883 }
4884}
4885
Avi Kivity8f5d5492009-05-31 18:41:29 +03004886static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4887{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004888 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4889
Avi Kivity8f5d5492009-05-31 18:41:29 +03004890 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004891 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4892 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4893 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4894 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004895 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004896
4897 __set_bit(VCPU_EXREG_PDPTR,
4898 (unsigned long *)&vcpu->arch.regs_avail);
4899 __set_bit(VCPU_EXREG_PDPTR,
4900 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004901}
4902
David Matlack38991522016-11-29 18:14:08 -08004903static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4904{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004905 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4906 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004907 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4908
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004909 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004910 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4911 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4912 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4913
4914 return fixed_bits_valid(val, fixed0, fixed1);
4915}
4916
4917static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4918{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004919 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4920 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004921
4922 return fixed_bits_valid(val, fixed0, fixed1);
4923}
4924
4925static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4926{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004927 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4928 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004929
4930 return fixed_bits_valid(val, fixed0, fixed1);
4931}
4932
4933/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4934#define nested_guest_cr4_valid nested_cr4_valid
4935#define nested_host_cr4_valid nested_cr4_valid
4936
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004937static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004938
4939static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4940 unsigned long cr0,
4941 struct kvm_vcpu *vcpu)
4942{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004943 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4944 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004945 if (!(cr0 & X86_CR0_PG)) {
4946 /* From paging/starting to nonpaging */
4947 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004948 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004949 (CPU_BASED_CR3_LOAD_EXITING |
4950 CPU_BASED_CR3_STORE_EXITING));
4951 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004952 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004953 } else if (!is_paging(vcpu)) {
4954 /* From nonpaging to paging */
4955 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004956 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004957 ~(CPU_BASED_CR3_LOAD_EXITING |
4958 CPU_BASED_CR3_STORE_EXITING));
4959 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004960 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004961 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004962
4963 if (!(cr0 & X86_CR0_WP))
4964 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004965}
4966
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4968{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004970 unsigned long hw_cr0;
4971
Gleb Natapov50378782013-02-04 16:00:28 +02004972 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004973 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004974 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004975 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004976 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004977
Gleb Natapov218e7632013-01-21 15:36:45 +02004978 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4979 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980
Gleb Natapov218e7632013-01-21 15:36:45 +02004981 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4982 enter_rmode(vcpu);
4983 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004985#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004986 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004987 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004989 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990 exit_lmode(vcpu);
4991 }
4992#endif
4993
Sean Christophersonb4d18512018-03-05 12:04:40 -08004994 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004995 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4996
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004998 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004999 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005000
5001 /* depends on vcpu->arch.cr0 to be set to a new value */
5002 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005003}
5004
Yu Zhang855feb62017-08-24 20:27:55 +08005005static int get_ept_level(struct kvm_vcpu *vcpu)
5006{
5007 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5008 return 5;
5009 return 4;
5010}
5011
Peter Feiner995f00a2017-06-30 17:26:32 -07005012static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005013{
Yu Zhang855feb62017-08-24 20:27:55 +08005014 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005015
Yu Zhang855feb62017-08-24 20:27:55 +08005016 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005017
Peter Feiner995f00a2017-06-30 17:26:32 -07005018 if (enable_ept_ad_bits &&
5019 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005020 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005021 eptp |= (root_hpa & PAGE_MASK);
5022
5023 return eptp;
5024}
5025
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5027{
Sheng Yang14394422008-04-28 12:24:45 +08005028 unsigned long guest_cr3;
5029 u64 eptp;
5030
5031 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005032 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005033 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005034 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005035 if (enable_unrestricted_guest || is_paging(vcpu) ||
5036 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005037 guest_cr3 = kvm_read_cr3(vcpu);
5038 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005039 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005040 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005041 }
5042
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005043 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005044 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045}
5046
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005047static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005049 /*
5050 * Pass through host's Machine Check Enable value to hw_cr4, which
5051 * is in force while we are in guest mode. Do not let guests control
5052 * this bit, even if host CR4.MCE == 0.
5053 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005054 unsigned long hw_cr4;
5055
5056 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5057 if (enable_unrestricted_guest)
5058 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5059 else if (to_vmx(vcpu)->rmode.vm86_active)
5060 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5061 else
5062 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005063
Sean Christopherson64f7a112018-04-30 10:01:06 -07005064 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5065 if (cr4 & X86_CR4_UMIP) {
5066 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005067 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005068 hw_cr4 &= ~X86_CR4_UMIP;
5069 } else if (!is_guest_mode(vcpu) ||
5070 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5071 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5072 SECONDARY_EXEC_DESC);
5073 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005074
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005075 if (cr4 & X86_CR4_VMXE) {
5076 /*
5077 * To use VMXON (and later other VMX instructions), a guest
5078 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5079 * So basically the check on whether to allow nested VMX
5080 * is here.
5081 */
5082 if (!nested_vmx_allowed(vcpu))
5083 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005084 }
David Matlack38991522016-11-29 18:14:08 -08005085
5086 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005087 return 1;
5088
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005089 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005090
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005091 if (!enable_unrestricted_guest) {
5092 if (enable_ept) {
5093 if (!is_paging(vcpu)) {
5094 hw_cr4 &= ~X86_CR4_PAE;
5095 hw_cr4 |= X86_CR4_PSE;
5096 } else if (!(cr4 & X86_CR4_PAE)) {
5097 hw_cr4 &= ~X86_CR4_PAE;
5098 }
5099 }
5100
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005101 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005102 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5103 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5104 * to be manually disabled when guest switches to non-paging
5105 * mode.
5106 *
5107 * If !enable_unrestricted_guest, the CPU is always running
5108 * with CR0.PG=1 and CR4 needs to be modified.
5109 * If enable_unrestricted_guest, the CPU automatically
5110 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005111 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005112 if (!is_paging(vcpu))
5113 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5114 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005115
Sheng Yang14394422008-04-28 12:24:45 +08005116 vmcs_writel(CR4_READ_SHADOW, cr4);
5117 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005118 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005119}
5120
Avi Kivity6aa8b732006-12-10 02:21:36 -08005121static void vmx_get_segment(struct kvm_vcpu *vcpu,
5122 struct kvm_segment *var, int seg)
5123{
Avi Kivitya9179492011-01-03 14:28:52 +02005124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125 u32 ar;
5126
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005127 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005128 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005129 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005130 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005131 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005132 var->base = vmx_read_guest_seg_base(vmx, seg);
5133 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5134 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005135 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005136 var->base = vmx_read_guest_seg_base(vmx, seg);
5137 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5138 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5139 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005140 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141 var->type = ar & 15;
5142 var->s = (ar >> 4) & 1;
5143 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005144 /*
5145 * Some userspaces do not preserve unusable property. Since usable
5146 * segment has to be present according to VMX spec we can use present
5147 * property to amend userspace bug by making unusable segment always
5148 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5149 * segment as unusable.
5150 */
5151 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152 var->avl = (ar >> 12) & 1;
5153 var->l = (ar >> 13) & 1;
5154 var->db = (ar >> 14) & 1;
5155 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156}
5157
Avi Kivitya9179492011-01-03 14:28:52 +02005158static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5159{
Avi Kivitya9179492011-01-03 14:28:52 +02005160 struct kvm_segment s;
5161
5162 if (to_vmx(vcpu)->rmode.vm86_active) {
5163 vmx_get_segment(vcpu, &s, seg);
5164 return s.base;
5165 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005166 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005167}
5168
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005169static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005170{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005171 struct vcpu_vmx *vmx = to_vmx(vcpu);
5172
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005173 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005174 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005175 else {
5176 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005177 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005178 }
Avi Kivity69c73022011-03-07 15:26:44 +02005179}
5180
Avi Kivity653e3102007-05-07 10:55:37 +03005181static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005182{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005183 u32 ar;
5184
Avi Kivityf0495f92012-06-07 17:06:10 +03005185 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005186 ar = 1 << 16;
5187 else {
5188 ar = var->type & 15;
5189 ar |= (var->s & 1) << 4;
5190 ar |= (var->dpl & 3) << 5;
5191 ar |= (var->present & 1) << 7;
5192 ar |= (var->avl & 1) << 12;
5193 ar |= (var->l & 1) << 13;
5194 ar |= (var->db & 1) << 14;
5195 ar |= (var->g & 1) << 15;
5196 }
Avi Kivity653e3102007-05-07 10:55:37 +03005197
5198 return ar;
5199}
5200
5201static void vmx_set_segment(struct kvm_vcpu *vcpu,
5202 struct kvm_segment *var, int seg)
5203{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005204 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005205 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005206
Avi Kivity2fb92db2011-04-27 19:42:18 +03005207 vmx_segment_cache_clear(vmx);
5208
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005209 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5210 vmx->rmode.segs[seg] = *var;
5211 if (seg == VCPU_SREG_TR)
5212 vmcs_write16(sf->selector, var->selector);
5213 else if (var->s)
5214 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005215 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005216 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005217
Avi Kivity653e3102007-05-07 10:55:37 +03005218 vmcs_writel(sf->base, var->base);
5219 vmcs_write32(sf->limit, var->limit);
5220 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005221
5222 /*
5223 * Fix the "Accessed" bit in AR field of segment registers for older
5224 * qemu binaries.
5225 * IA32 arch specifies that at the time of processor reset the
5226 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005227 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005228 * state vmexit when "unrestricted guest" mode is turned on.
5229 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5230 * tree. Newer qemu binaries with that qemu fix would not need this
5231 * kvm hack.
5232 */
5233 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005234 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005235
Gleb Natapovf924d662012-12-12 19:10:55 +02005236 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005237
5238out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005239 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005240}
5241
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5243{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005244 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245
5246 *db = (ar >> 14) & 1;
5247 *l = (ar >> 13) & 1;
5248}
5249
Gleb Natapov89a27f42010-02-16 10:51:48 +02005250static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005252 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5253 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254}
5255
Gleb Natapov89a27f42010-02-16 10:51:48 +02005256static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005258 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5259 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260}
5261
Gleb Natapov89a27f42010-02-16 10:51:48 +02005262static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005264 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5265 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266}
5267
Gleb Natapov89a27f42010-02-16 10:51:48 +02005268static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005269{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005270 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5271 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005272}
5273
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005274static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5275{
5276 struct kvm_segment var;
5277 u32 ar;
5278
5279 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005280 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005281 if (seg == VCPU_SREG_CS)
5282 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005283 ar = vmx_segment_access_rights(&var);
5284
5285 if (var.base != (var.selector << 4))
5286 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005287 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005288 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005289 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005290 return false;
5291
5292 return true;
5293}
5294
5295static bool code_segment_valid(struct kvm_vcpu *vcpu)
5296{
5297 struct kvm_segment cs;
5298 unsigned int cs_rpl;
5299
5300 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005301 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005302
Avi Kivity1872a3f2009-01-04 23:26:52 +02005303 if (cs.unusable)
5304 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005305 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005306 return false;
5307 if (!cs.s)
5308 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005309 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005310 if (cs.dpl > cs_rpl)
5311 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005312 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005313 if (cs.dpl != cs_rpl)
5314 return false;
5315 }
5316 if (!cs.present)
5317 return false;
5318
5319 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5320 return true;
5321}
5322
5323static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5324{
5325 struct kvm_segment ss;
5326 unsigned int ss_rpl;
5327
5328 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005329 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005330
Avi Kivity1872a3f2009-01-04 23:26:52 +02005331 if (ss.unusable)
5332 return true;
5333 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005334 return false;
5335 if (!ss.s)
5336 return false;
5337 if (ss.dpl != ss_rpl) /* DPL != RPL */
5338 return false;
5339 if (!ss.present)
5340 return false;
5341
5342 return true;
5343}
5344
5345static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5346{
5347 struct kvm_segment var;
5348 unsigned int rpl;
5349
5350 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005351 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005352
Avi Kivity1872a3f2009-01-04 23:26:52 +02005353 if (var.unusable)
5354 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005355 if (!var.s)
5356 return false;
5357 if (!var.present)
5358 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005359 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 if (var.dpl < rpl) /* DPL < RPL */
5361 return false;
5362 }
5363
5364 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5365 * rights flags
5366 */
5367 return true;
5368}
5369
5370static bool tr_valid(struct kvm_vcpu *vcpu)
5371{
5372 struct kvm_segment tr;
5373
5374 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5375
Avi Kivity1872a3f2009-01-04 23:26:52 +02005376 if (tr.unusable)
5377 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005378 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005379 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005380 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005381 return false;
5382 if (!tr.present)
5383 return false;
5384
5385 return true;
5386}
5387
5388static bool ldtr_valid(struct kvm_vcpu *vcpu)
5389{
5390 struct kvm_segment ldtr;
5391
5392 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5393
Avi Kivity1872a3f2009-01-04 23:26:52 +02005394 if (ldtr.unusable)
5395 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005396 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005397 return false;
5398 if (ldtr.type != 2)
5399 return false;
5400 if (!ldtr.present)
5401 return false;
5402
5403 return true;
5404}
5405
5406static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5407{
5408 struct kvm_segment cs, ss;
5409
5410 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5411 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5412
Nadav Amitb32a9912015-03-29 16:33:04 +03005413 return ((cs.selector & SEGMENT_RPL_MASK) ==
5414 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005415}
5416
5417/*
5418 * Check if guest state is valid. Returns true if valid, false if
5419 * not.
5420 * We assume that registers are always usable
5421 */
5422static bool guest_state_valid(struct kvm_vcpu *vcpu)
5423{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005424 if (enable_unrestricted_guest)
5425 return true;
5426
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005427 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005428 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005429 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5430 return false;
5431 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5432 return false;
5433 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5434 return false;
5435 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5436 return false;
5437 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5438 return false;
5439 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5440 return false;
5441 } else {
5442 /* protected mode guest state checks */
5443 if (!cs_ss_rpl_check(vcpu))
5444 return false;
5445 if (!code_segment_valid(vcpu))
5446 return false;
5447 if (!stack_segment_valid(vcpu))
5448 return false;
5449 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5450 return false;
5451 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5452 return false;
5453 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5454 return false;
5455 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5456 return false;
5457 if (!tr_valid(vcpu))
5458 return false;
5459 if (!ldtr_valid(vcpu))
5460 return false;
5461 }
5462 /* TODO:
5463 * - Add checks on RIP
5464 * - Add checks on RFLAGS
5465 */
5466
5467 return true;
5468}
5469
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005470static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5471{
5472 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5473}
5474
Mike Dayd77c26f2007-10-08 09:02:08 -04005475static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005476{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005477 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005478 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005479 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005480
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005481 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005482 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005483 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5484 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005485 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005486 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005487 r = kvm_write_guest_page(kvm, fn++, &data,
5488 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005489 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005490 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005491 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5492 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005493 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005494 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5495 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005496 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005497 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005498 r = kvm_write_guest_page(kvm, fn, &data,
5499 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5500 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005501out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005502 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005503 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005504}
5505
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005506static int init_rmode_identity_map(struct kvm *kvm)
5507{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005508 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005509 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005510 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005511 u32 tmp;
5512
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005513 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005514 mutex_lock(&kvm->slots_lock);
5515
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005516 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005517 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005518
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005519 if (!kvm_vmx->ept_identity_map_addr)
5520 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5521 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005522
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005523 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005524 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005525 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005526 goto out2;
5527
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005528 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005529 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5530 if (r < 0)
5531 goto out;
5532 /* Set up identity-mapping pagetable for EPT in real mode */
5533 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5534 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5535 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5536 r = kvm_write_guest_page(kvm, identity_map_pfn,
5537 &tmp, i * sizeof(tmp), sizeof(tmp));
5538 if (r < 0)
5539 goto out;
5540 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005541 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005542
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005543out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005544 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005545
5546out2:
5547 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005548 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005549}
5550
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551static void seg_setup(int seg)
5552{
Mathias Krause772e0312012-08-30 01:30:19 +02005553 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005554 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555
5556 vmcs_write16(sf->selector, 0);
5557 vmcs_writel(sf->base, 0);
5558 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005559 ar = 0x93;
5560 if (seg == VCPU_SREG_CS)
5561 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005562
5563 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564}
5565
Sheng Yangf78e0e22007-10-29 09:40:42 +08005566static int alloc_apic_access_page(struct kvm *kvm)
5567{
Xiao Guangrong44841412012-09-07 14:14:20 +08005568 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005569 int r = 0;
5570
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005571 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005572 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005573 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005574 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5575 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005576 if (r)
5577 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005578
Tang Chen73a6d942014-09-11 13:38:00 +08005579 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005580 if (is_error_page(page)) {
5581 r = -EFAULT;
5582 goto out;
5583 }
5584
Tang Chenc24ae0d2014-09-24 15:57:58 +08005585 /*
5586 * Do not pin the page in memory, so that memory hot-unplug
5587 * is able to migrate it.
5588 */
5589 put_page(page);
5590 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005591out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005592 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005593 return r;
5594}
5595
Wanpeng Li991e7a02015-09-16 17:30:05 +08005596static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005597{
5598 int vpid;
5599
Avi Kivity919818a2009-03-23 18:01:29 +02005600 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005601 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005602 spin_lock(&vmx_vpid_lock);
5603 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005604 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005605 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005606 else
5607 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005608 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005609 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005610}
5611
Wanpeng Li991e7a02015-09-16 17:30:05 +08005612static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005613{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005614 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005615 return;
5616 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005617 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005618 spin_unlock(&vmx_vpid_lock);
5619}
5620
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005621static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5622 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005623{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005624 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005625
5626 if (!cpu_has_vmx_msr_bitmap())
5627 return;
5628
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005629 if (static_branch_unlikely(&enable_evmcs))
5630 evmcs_touch_msr_bitmap();
5631
Sheng Yang25c5f222008-03-28 13:18:56 +08005632 /*
5633 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5634 * have the write-low and read-high bitmap offsets the wrong way round.
5635 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5636 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005637 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005638 if (type & MSR_TYPE_R)
5639 /* read-low */
5640 __clear_bit(msr, msr_bitmap + 0x000 / f);
5641
5642 if (type & MSR_TYPE_W)
5643 /* write-low */
5644 __clear_bit(msr, msr_bitmap + 0x800 / f);
5645
Sheng Yang25c5f222008-03-28 13:18:56 +08005646 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5647 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005648 if (type & MSR_TYPE_R)
5649 /* read-high */
5650 __clear_bit(msr, msr_bitmap + 0x400 / f);
5651
5652 if (type & MSR_TYPE_W)
5653 /* write-high */
5654 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5655
5656 }
5657}
5658
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005659static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5660 u32 msr, int type)
5661{
5662 int f = sizeof(unsigned long);
5663
5664 if (!cpu_has_vmx_msr_bitmap())
5665 return;
5666
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005667 if (static_branch_unlikely(&enable_evmcs))
5668 evmcs_touch_msr_bitmap();
5669
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005670 /*
5671 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5672 * have the write-low and read-high bitmap offsets the wrong way round.
5673 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5674 */
5675 if (msr <= 0x1fff) {
5676 if (type & MSR_TYPE_R)
5677 /* read-low */
5678 __set_bit(msr, msr_bitmap + 0x000 / f);
5679
5680 if (type & MSR_TYPE_W)
5681 /* write-low */
5682 __set_bit(msr, msr_bitmap + 0x800 / f);
5683
5684 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5685 msr &= 0x1fff;
5686 if (type & MSR_TYPE_R)
5687 /* read-high */
5688 __set_bit(msr, msr_bitmap + 0x400 / f);
5689
5690 if (type & MSR_TYPE_W)
5691 /* write-high */
5692 __set_bit(msr, msr_bitmap + 0xc00 / f);
5693
5694 }
5695}
5696
5697static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5698 u32 msr, int type, bool value)
5699{
5700 if (value)
5701 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5702 else
5703 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5704}
5705
Wincy Vanf2b93282015-02-03 23:56:03 +08005706/*
5707 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5708 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5709 */
5710static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5711 unsigned long *msr_bitmap_nested,
5712 u32 msr, int type)
5713{
5714 int f = sizeof(unsigned long);
5715
Wincy Vanf2b93282015-02-03 23:56:03 +08005716 /*
5717 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5718 * have the write-low and read-high bitmap offsets the wrong way round.
5719 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5720 */
5721 if (msr <= 0x1fff) {
5722 if (type & MSR_TYPE_R &&
5723 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5724 /* read-low */
5725 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5726
5727 if (type & MSR_TYPE_W &&
5728 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5729 /* write-low */
5730 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5731
5732 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5733 msr &= 0x1fff;
5734 if (type & MSR_TYPE_R &&
5735 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5736 /* read-high */
5737 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5738
5739 if (type & MSR_TYPE_W &&
5740 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5741 /* write-high */
5742 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5743
5744 }
5745}
5746
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005747static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005748{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005749 u8 mode = 0;
5750
5751 if (cpu_has_secondary_exec_ctrls() &&
5752 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5753 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5754 mode |= MSR_BITMAP_MODE_X2APIC;
5755 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5756 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5757 }
5758
5759 if (is_long_mode(vcpu))
5760 mode |= MSR_BITMAP_MODE_LM;
5761
5762 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005763}
5764
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005765#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5766
5767static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5768 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005769{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005770 int msr;
5771
5772 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5773 unsigned word = msr / BITS_PER_LONG;
5774 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5775 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005776 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005777
5778 if (mode & MSR_BITMAP_MODE_X2APIC) {
5779 /*
5780 * TPR reads and writes can be virtualized even if virtual interrupt
5781 * delivery is not in use.
5782 */
5783 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5784 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5785 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5786 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5787 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5788 }
5789 }
5790}
5791
5792static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5793{
5794 struct vcpu_vmx *vmx = to_vmx(vcpu);
5795 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5796 u8 mode = vmx_msr_bitmap_mode(vcpu);
5797 u8 changed = mode ^ vmx->msr_bitmap_mode;
5798
5799 if (!changed)
5800 return;
5801
5802 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5803 !(mode & MSR_BITMAP_MODE_LM));
5804
5805 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5806 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5807
5808 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005809}
5810
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005811static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005812{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005813 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005814}
5815
David Matlackc9f04402017-08-01 14:00:40 -07005816static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5817{
5818 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5819 gfn_t gfn;
5820
5821 /*
5822 * Don't need to mark the APIC access page dirty; it is never
5823 * written to by the CPU during APIC virtualization.
5824 */
5825
5826 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5827 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5828 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5829 }
5830
5831 if (nested_cpu_has_posted_intr(vmcs12)) {
5832 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5833 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5834 }
5835}
5836
5837
David Hildenbrand6342c502017-01-25 11:58:58 +01005838static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005839{
5840 struct vcpu_vmx *vmx = to_vmx(vcpu);
5841 int max_irr;
5842 void *vapic_page;
5843 u16 status;
5844
David Matlackc9f04402017-08-01 14:00:40 -07005845 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5846 return;
Wincy Van705699a2015-02-03 23:58:17 +08005847
David Matlackc9f04402017-08-01 14:00:40 -07005848 vmx->nested.pi_pending = false;
5849 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5850 return;
Wincy Van705699a2015-02-03 23:58:17 +08005851
David Matlackc9f04402017-08-01 14:00:40 -07005852 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5853 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005854 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005855 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5856 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005857 kunmap(vmx->nested.virtual_apic_page);
5858
5859 status = vmcs_read16(GUEST_INTR_STATUS);
5860 if ((u8)max_irr > ((u8)status & 0xff)) {
5861 status &= ~0xff;
5862 status |= (u8)max_irr;
5863 vmcs_write16(GUEST_INTR_STATUS, status);
5864 }
5865 }
David Matlackc9f04402017-08-01 14:00:40 -07005866
5867 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005868}
5869
Wincy Van06a55242017-04-28 13:13:59 +08005870static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5871 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005872{
5873#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005874 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5875
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005876 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005877 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005878 * The vector of interrupt to be delivered to vcpu had
5879 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005880 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005881 * Following cases will be reached in this block, and
5882 * we always send a notification event in all cases as
5883 * explained below.
5884 *
5885 * Case 1: vcpu keeps in non-root mode. Sending a
5886 * notification event posts the interrupt to vcpu.
5887 *
5888 * Case 2: vcpu exits to root mode and is still
5889 * runnable. PIR will be synced to vIRR before the
5890 * next vcpu entry. Sending a notification event in
5891 * this case has no effect, as vcpu is not in root
5892 * mode.
5893 *
5894 * Case 3: vcpu exits to root mode and is blocked.
5895 * vcpu_block() has already synced PIR to vIRR and
5896 * never blocks vcpu if vIRR is not cleared. Therefore,
5897 * a blocked vcpu here does not wait for any requested
5898 * interrupts in PIR, and sending a notification event
5899 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005900 */
Feng Wu28b835d2015-09-18 22:29:54 +08005901
Wincy Van06a55242017-04-28 13:13:59 +08005902 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005903 return true;
5904 }
5905#endif
5906 return false;
5907}
5908
Wincy Van705699a2015-02-03 23:58:17 +08005909static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5910 int vector)
5911{
5912 struct vcpu_vmx *vmx = to_vmx(vcpu);
5913
5914 if (is_guest_mode(vcpu) &&
5915 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005916 /*
5917 * If a posted intr is not recognized by hardware,
5918 * we will accomplish it in the next vmentry.
5919 */
5920 vmx->nested.pi_pending = true;
5921 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005922 /* the PIR and ON have been set by L1. */
5923 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5924 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005925 return 0;
5926 }
5927 return -1;
5928}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005929/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005930 * Send interrupt to vcpu via posted interrupt way.
5931 * 1. If target vcpu is running(non-root mode), send posted interrupt
5932 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5933 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5934 * interrupt from PIR in next vmentry.
5935 */
5936static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5937{
5938 struct vcpu_vmx *vmx = to_vmx(vcpu);
5939 int r;
5940
Wincy Van705699a2015-02-03 23:58:17 +08005941 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5942 if (!r)
5943 return;
5944
Yang Zhanga20ed542013-04-11 19:25:15 +08005945 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5946 return;
5947
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005948 /* If a previous notification has sent the IPI, nothing to do. */
5949 if (pi_test_and_set_on(&vmx->pi_desc))
5950 return;
5951
Wincy Van06a55242017-04-28 13:13:59 +08005952 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005953 kvm_vcpu_kick(vcpu);
5954}
5955
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005957 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5958 * will not change in the lifetime of the guest.
5959 * Note that host-state that does change is set elsewhere. E.g., host-state
5960 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5961 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005962static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005963{
5964 u32 low32, high32;
5965 unsigned long tmpl;
5966 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005967 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005968
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005969 cr0 = read_cr0();
5970 WARN_ON(cr0 & X86_CR0_TS);
5971 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005972
5973 /*
5974 * Save the most likely value for this task's CR3 in the VMCS.
5975 * We can't use __get_current_cr3_fast() because we're not atomic.
5976 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005977 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005978 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005979 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005980
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005981 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005982 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005983 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005984 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005985
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005986 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005987#ifdef CONFIG_X86_64
5988 /*
5989 * Load null selectors, so we can avoid reloading them in
5990 * __vmx_load_host_state(), in case userspace uses the null selectors
5991 * too (the expected case).
5992 */
5993 vmcs_write16(HOST_DS_SELECTOR, 0);
5994 vmcs_write16(HOST_ES_SELECTOR, 0);
5995#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005996 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5997 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005998#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005999 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6000 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6001
Juergen Gross87930012017-09-04 12:25:27 +02006002 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006003 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006004 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006005
Avi Kivity83287ea422012-09-16 15:10:57 +03006006 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006007
6008 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6009 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6010 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6011 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6012
6013 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6014 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6015 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6016 }
6017}
6018
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006019static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6020{
6021 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6022 if (enable_ept)
6023 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006024 if (is_guest_mode(&vmx->vcpu))
6025 vmx->vcpu.arch.cr4_guest_owned_bits &=
6026 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006027 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6028}
6029
Yang Zhang01e439b2013-04-11 19:25:12 +08006030static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6031{
6032 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6033
Andrey Smetanind62caab2015-11-10 15:36:33 +03006034 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006035 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006036
6037 if (!enable_vnmi)
6038 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6039
Yunhong Jiang64672c92016-06-13 14:19:59 -07006040 /* Enable the preemption timer dynamically */
6041 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006042 return pin_based_exec_ctrl;
6043}
6044
Andrey Smetanind62caab2015-11-10 15:36:33 +03006045static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6046{
6047 struct vcpu_vmx *vmx = to_vmx(vcpu);
6048
6049 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006050 if (cpu_has_secondary_exec_ctrls()) {
6051 if (kvm_vcpu_apicv_active(vcpu))
6052 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6053 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6054 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6055 else
6056 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6057 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6058 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6059 }
6060
6061 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006062 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006063}
6064
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006065static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6066{
6067 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006068
6069 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6070 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6071
Paolo Bonzini35754c92015-07-29 12:05:37 +02006072 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006073 exec_control &= ~CPU_BASED_TPR_SHADOW;
6074#ifdef CONFIG_X86_64
6075 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6076 CPU_BASED_CR8_LOAD_EXITING;
6077#endif
6078 }
6079 if (!enable_ept)
6080 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6081 CPU_BASED_CR3_LOAD_EXITING |
6082 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006083 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6084 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6085 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006086 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6087 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006088 return exec_control;
6089}
6090
Jim Mattson45ec3682017-08-23 16:32:04 -07006091static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006092{
Jim Mattson45ec3682017-08-23 16:32:04 -07006093 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006094 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006095}
6096
Jim Mattson75f4fc82017-08-23 16:32:03 -07006097static bool vmx_rdseed_supported(void)
6098{
6099 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006100 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006101}
6102
Paolo Bonzini80154d72017-08-24 13:55:35 +02006103static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006104{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006105 struct kvm_vcpu *vcpu = &vmx->vcpu;
6106
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006108
Paolo Bonzini80154d72017-08-24 13:55:35 +02006109 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006110 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6111 if (vmx->vpid == 0)
6112 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6113 if (!enable_ept) {
6114 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6115 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006116 /* Enable INVPCID for non-ept guests may cause performance regression. */
6117 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006118 }
6119 if (!enable_unrestricted_guest)
6120 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006121 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006122 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006123 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006124 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6125 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006126 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006127
6128 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6129 * in vmx_set_cr4. */
6130 exec_control &= ~SECONDARY_EXEC_DESC;
6131
Abel Gordonabc4fc52013-04-18 14:35:25 +03006132 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6133 (handle_vmptrld).
6134 We can NOT enable shadow_vmcs here because we don't have yet
6135 a current VMCS12
6136 */
6137 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006138
6139 if (!enable_pml)
6140 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006141
Paolo Bonzini3db13482017-08-24 14:48:03 +02006142 if (vmx_xsaves_supported()) {
6143 /* Exposing XSAVES only when XSAVE is exposed */
6144 bool xsaves_enabled =
6145 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6146 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6147
6148 if (!xsaves_enabled)
6149 exec_control &= ~SECONDARY_EXEC_XSAVES;
6150
6151 if (nested) {
6152 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006153 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006154 SECONDARY_EXEC_XSAVES;
6155 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006156 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006157 ~SECONDARY_EXEC_XSAVES;
6158 }
6159 }
6160
Paolo Bonzini80154d72017-08-24 13:55:35 +02006161 if (vmx_rdtscp_supported()) {
6162 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6163 if (!rdtscp_enabled)
6164 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6165
6166 if (nested) {
6167 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006168 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006169 SECONDARY_EXEC_RDTSCP;
6170 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006171 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006172 ~SECONDARY_EXEC_RDTSCP;
6173 }
6174 }
6175
6176 if (vmx_invpcid_supported()) {
6177 /* Exposing INVPCID only when PCID is exposed */
6178 bool invpcid_enabled =
6179 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6180 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6181
6182 if (!invpcid_enabled) {
6183 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6184 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6185 }
6186
6187 if (nested) {
6188 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006189 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006190 SECONDARY_EXEC_ENABLE_INVPCID;
6191 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006192 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006193 ~SECONDARY_EXEC_ENABLE_INVPCID;
6194 }
6195 }
6196
Jim Mattson45ec3682017-08-23 16:32:04 -07006197 if (vmx_rdrand_supported()) {
6198 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6199 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006200 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006201
6202 if (nested) {
6203 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006204 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006205 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006206 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006207 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006208 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006209 }
6210 }
6211
Jim Mattson75f4fc82017-08-23 16:32:03 -07006212 if (vmx_rdseed_supported()) {
6213 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6214 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006215 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006216
6217 if (nested) {
6218 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006219 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006220 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006221 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006222 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006223 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006224 }
6225 }
6226
Paolo Bonzini80154d72017-08-24 13:55:35 +02006227 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006228}
6229
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006230static void ept_set_mmio_spte_mask(void)
6231{
6232 /*
6233 * EPT Misconfigurations can be generated if the value of bits 2:0
6234 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006235 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006236 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6237 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006238}
6239
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04006240static bool vmx_l1d_use_msr_save_list(void)
6241{
6242 if (!enable_ept || !boot_cpu_has_bug(X86_BUG_L1TF) ||
6243 static_cpu_has(X86_FEATURE_HYPERVISOR) ||
6244 !static_cpu_has(X86_FEATURE_FLUSH_L1D))
6245 return false;
6246
6247 return vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
6248}
6249
Wanpeng Lif53cd632014-12-02 19:14:58 +08006250#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006251/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 * Sets up the vmcs for emulated real mode.
6253 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006254static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006256#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006257 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006258#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006259 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006260
Abel Gordon4607c2d2013-04-18 14:35:55 +03006261 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006262 /*
6263 * At vCPU creation, "VMWRITE to any supported field
6264 * in the VMCS" is supported, so use the more
6265 * permissive vmx_vmread_bitmap to specify both read
6266 * and write permissions for the shadow VMCS.
6267 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006268 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006269 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006270 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006271 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006272 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006273
Avi Kivity6aa8b732006-12-10 02:21:36 -08006274 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6275
Avi Kivity6aa8b732006-12-10 02:21:36 -08006276 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006277 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006278 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006279
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006280 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006281
Dan Williamsdfa169b2016-06-02 11:17:24 -07006282 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006283 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006284 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006285 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006286 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006287
Andrey Smetanind62caab2015-11-10 15:36:33 +03006288 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006289 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6290 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6291 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6292 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6293
6294 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006295
Li RongQing0bcf2612015-12-03 13:29:34 +08006296 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006297 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006298 }
6299
Wanpeng Lib31c1142018-03-12 04:53:04 -07006300 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006301 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006302 vmx->ple_window = ple_window;
6303 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006304 }
6305
Xiao Guangrongc3707952011-07-12 03:28:04 +08006306 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6307 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6309
Avi Kivity9581d442010-10-19 16:46:55 +02006310 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6311 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006312 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006313#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314 rdmsrl(MSR_FS_BASE, a);
6315 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6316 rdmsrl(MSR_GS_BASE, a);
6317 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6318#else
6319 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6320 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6321#endif
6322
Bandan Das2a499e42017-08-03 15:54:41 -04006323 if (cpu_has_vmx_vmfunc())
6324 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6325
Eddie Dong2cc51562007-05-21 07:28:09 +03006326 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6327 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006328 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006329 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006330 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006331
Radim Krčmář74545702015-04-27 15:11:25 +02006332 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6333 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006334
Paolo Bonzini03916db2014-07-24 14:21:57 +02006335 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006336 u32 index = vmx_msr_index[i];
6337 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006338 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339
6340 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6341 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006342 if (wrmsr_safe(index, data_low, data_high) < 0)
6343 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006344 vmx->guest_msrs[j].index = i;
6345 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006346 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006347 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006348 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006349
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006350 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6351 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006352
6353 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006354
6355 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006356 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006357
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006358 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6359 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6360
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006361 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006362
Wanpeng Lif53cd632014-12-02 19:14:58 +08006363 if (vmx_xsaves_supported())
6364 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6365
Peter Feiner4e595162016-07-07 14:49:58 -07006366 if (enable_pml) {
6367 ASSERT(vmx->pml_pg);
6368 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6369 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6370 }
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04006371 /*
6372 * If flushing the L1D cache on every VMENTER is enforced and the
6373 * MSR is available, use the MSR save list.
6374 */
6375 if (vmx_l1d_use_msr_save_list())
6376 add_atomic_switch_msr(vmx, MSR_IA32_FLUSH_CMD, L1D_FLUSH, 0, true);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006377}
6378
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006379static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006380{
6381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006382 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006383 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006384
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006385 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006386 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006387
Wanpeng Li518e7b92018-02-28 14:03:31 +08006388 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006389 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006390 kvm_set_cr8(vcpu, 0);
6391
6392 if (!init_event) {
6393 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6394 MSR_IA32_APICBASE_ENABLE;
6395 if (kvm_vcpu_is_reset_bsp(vcpu))
6396 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6397 apic_base_msr.host_initiated = true;
6398 kvm_set_apic_base(vcpu, &apic_base_msr);
6399 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006400
Avi Kivity2fb92db2011-04-27 19:42:18 +03006401 vmx_segment_cache_clear(vmx);
6402
Avi Kivity5706be02008-08-20 15:07:31 +03006403 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006404 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006405 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006406
6407 seg_setup(VCPU_SREG_DS);
6408 seg_setup(VCPU_SREG_ES);
6409 seg_setup(VCPU_SREG_FS);
6410 seg_setup(VCPU_SREG_GS);
6411 seg_setup(VCPU_SREG_SS);
6412
6413 vmcs_write16(GUEST_TR_SELECTOR, 0);
6414 vmcs_writel(GUEST_TR_BASE, 0);
6415 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6416 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6417
6418 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6419 vmcs_writel(GUEST_LDTR_BASE, 0);
6420 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6421 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6422
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006423 if (!init_event) {
6424 vmcs_write32(GUEST_SYSENTER_CS, 0);
6425 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6426 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6427 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6428 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006429
Wanpeng Lic37c2872017-11-20 14:52:21 -08006430 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006431 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006432
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006433 vmcs_writel(GUEST_GDTR_BASE, 0);
6434 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6435
6436 vmcs_writel(GUEST_IDTR_BASE, 0);
6437 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6438
Anthony Liguori443381a2010-12-06 10:53:38 -06006439 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006440 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006441 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006442 if (kvm_mpx_supported())
6443 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006444
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006445 setup_msrs(vmx);
6446
Avi Kivity6aa8b732006-12-10 02:21:36 -08006447 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6448
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006449 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006450 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006451 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006452 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006453 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006454 vmcs_write32(TPR_THRESHOLD, 0);
6455 }
6456
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006457 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006458
Sheng Yang2384d2b2008-01-17 15:14:33 +08006459 if (vmx->vpid != 0)
6460 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6461
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006462 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006463 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006464 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006465 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006466 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006467
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006468 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006469
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006470 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006471 if (init_event)
6472 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006473}
6474
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006475/*
6476 * In nested virtualization, check if L1 asked to exit on external interrupts.
6477 * For most existing hypervisors, this will always return true.
6478 */
6479static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6480{
6481 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6482 PIN_BASED_EXT_INTR_MASK;
6483}
6484
Bandan Das77b0f5d2014-04-19 18:17:45 -04006485/*
6486 * In nested virtualization, check if L1 has set
6487 * VM_EXIT_ACK_INTR_ON_EXIT
6488 */
6489static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6490{
6491 return get_vmcs12(vcpu)->vm_exit_controls &
6492 VM_EXIT_ACK_INTR_ON_EXIT;
6493}
6494
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006495static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6496{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006497 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006498}
6499
Jan Kiszkac9a79532014-03-07 20:03:15 +01006500static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006501{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006502 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6503 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006504}
6505
Jan Kiszkac9a79532014-03-07 20:03:15 +01006506static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006507{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006508 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006509 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006510 enable_irq_window(vcpu);
6511 return;
6512 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006513
Paolo Bonzini47c01522016-12-19 11:44:07 +01006514 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6515 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006516}
6517
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006518static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006519{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006521 uint32_t intr;
6522 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006523
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006524 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006525
Avi Kivityfa89a812008-09-01 15:57:51 +03006526 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006527 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006528 int inc_eip = 0;
6529 if (vcpu->arch.interrupt.soft)
6530 inc_eip = vcpu->arch.event_exit_inst_len;
6531 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006532 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006533 return;
6534 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006535 intr = irq | INTR_INFO_VALID_MASK;
6536 if (vcpu->arch.interrupt.soft) {
6537 intr |= INTR_TYPE_SOFT_INTR;
6538 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6539 vmx->vcpu.arch.event_exit_inst_len);
6540 } else
6541 intr |= INTR_TYPE_EXT_INTR;
6542 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006543
6544 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006545}
6546
Sheng Yangf08864b2008-05-15 18:23:25 +08006547static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6548{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006549 struct vcpu_vmx *vmx = to_vmx(vcpu);
6550
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006551 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006552 /*
6553 * Tracking the NMI-blocked state in software is built upon
6554 * finding the next open IRQ window. This, in turn, depends on
6555 * well-behaving guests: They have to keep IRQs disabled at
6556 * least as long as the NMI handler runs. Otherwise we may
6557 * cause NMI nesting, maybe breaking the guest. But as this is
6558 * highly unlikely, we can live with the residual risk.
6559 */
6560 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6561 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6562 }
6563
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006564 ++vcpu->stat.nmi_injections;
6565 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006566
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006567 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006568 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006569 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006570 return;
6571 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006572
Sheng Yangf08864b2008-05-15 18:23:25 +08006573 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6574 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006575
6576 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006577}
6578
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006579static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6580{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006581 struct vcpu_vmx *vmx = to_vmx(vcpu);
6582 bool masked;
6583
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006584 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006585 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006586 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006587 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006588 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6589 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6590 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006591}
6592
6593static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6594{
6595 struct vcpu_vmx *vmx = to_vmx(vcpu);
6596
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006597 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006598 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6599 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6600 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6601 }
6602 } else {
6603 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6604 if (masked)
6605 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6606 GUEST_INTR_STATE_NMI);
6607 else
6608 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6609 GUEST_INTR_STATE_NMI);
6610 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006611}
6612
Jan Kiszka2505dc92013-04-14 12:12:47 +02006613static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6614{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006615 if (to_vmx(vcpu)->nested.nested_run_pending)
6616 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006617
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006618 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006619 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6620 return 0;
6621
Jan Kiszka2505dc92013-04-14 12:12:47 +02006622 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6623 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6624 | GUEST_INTR_STATE_NMI));
6625}
6626
Gleb Natapov78646122009-03-23 12:12:11 +02006627static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6628{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006629 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6630 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006631 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6632 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006633}
6634
Izik Eiduscbc94022007-10-25 00:29:55 +02006635static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6636{
6637 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006638
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006639 if (enable_unrestricted_guest)
6640 return 0;
6641
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006642 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6643 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006644 if (ret)
6645 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006646 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006647 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006648}
6649
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006650static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6651{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006652 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006653 return 0;
6654}
6655
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006656static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006657{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006658 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006659 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006660 /*
6661 * Update instruction length as we may reinject the exception
6662 * from user space while in guest debugging mode.
6663 */
6664 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6665 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006666 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006667 return false;
6668 /* fall through */
6669 case DB_VECTOR:
6670 if (vcpu->guest_debug &
6671 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6672 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006673 /* fall through */
6674 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006675 case OF_VECTOR:
6676 case BR_VECTOR:
6677 case UD_VECTOR:
6678 case DF_VECTOR:
6679 case SS_VECTOR:
6680 case GP_VECTOR:
6681 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006682 return true;
6683 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006684 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006685 return false;
6686}
6687
6688static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6689 int vec, u32 err_code)
6690{
6691 /*
6692 * Instruction with address size override prefix opcode 0x67
6693 * Cause the #SS fault with 0 error code in VM86 mode.
6694 */
6695 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6696 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6697 if (vcpu->arch.halt_request) {
6698 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006699 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006700 }
6701 return 1;
6702 }
6703 return 0;
6704 }
6705
6706 /*
6707 * Forward all other exceptions that are valid in real mode.
6708 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6709 * the required debugging infrastructure rework.
6710 */
6711 kvm_queue_exception(vcpu, vec);
6712 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006713}
6714
Andi Kleena0861c02009-06-08 17:37:09 +08006715/*
6716 * Trigger machine check on the host. We assume all the MSRs are already set up
6717 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6718 * We pass a fake environment to the machine check handler because we want
6719 * the guest to be always treated like user space, no matter what context
6720 * it used internally.
6721 */
6722static void kvm_machine_check(void)
6723{
6724#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6725 struct pt_regs regs = {
6726 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6727 .flags = X86_EFLAGS_IF,
6728 };
6729
6730 do_machine_check(&regs, 0);
6731#endif
6732}
6733
Avi Kivity851ba692009-08-24 11:10:17 +03006734static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006735{
6736 /* already handled by vcpu_run */
6737 return 1;
6738}
6739
Avi Kivity851ba692009-08-24 11:10:17 +03006740static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006741{
Avi Kivity1155f762007-11-22 11:30:47 +02006742 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006743 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006744 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006745 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006746 u32 vect_info;
6747 enum emulation_result er;
6748
Avi Kivity1155f762007-11-22 11:30:47 +02006749 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006750 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006751
Andi Kleena0861c02009-06-08 17:37:09 +08006752 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006753 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006754
Jim Mattsonef85b672016-12-12 11:01:37 -08006755 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006756 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006757
Wanpeng Li082d06e2018-04-03 16:28:48 -07006758 if (is_invalid_opcode(intr_info))
6759 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006760
Avi Kivity6aa8b732006-12-10 02:21:36 -08006761 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006762 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006763 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006764
Liran Alon9e869482018-03-12 13:12:51 +02006765 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6766 WARN_ON_ONCE(!enable_vmware_backdoor);
6767 er = emulate_instruction(vcpu,
6768 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6769 if (er == EMULATE_USER_EXIT)
6770 return 0;
6771 else if (er != EMULATE_DONE)
6772 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6773 return 1;
6774 }
6775
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006776 /*
6777 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6778 * MMIO, it is better to report an internal error.
6779 * See the comments in vmx_handle_exit.
6780 */
6781 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6782 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6783 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6784 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006785 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006786 vcpu->run->internal.data[0] = vect_info;
6787 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006788 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006789 return 0;
6790 }
6791
Avi Kivity6aa8b732006-12-10 02:21:36 -08006792 if (is_page_fault(intr_info)) {
6793 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006794 /* EPT won't cause page fault directly */
6795 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006796 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006797 }
6798
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006799 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006800
6801 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6802 return handle_rmode_exception(vcpu, ex_no, error_code);
6803
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006804 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006805 case AC_VECTOR:
6806 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6807 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006808 case DB_VECTOR:
6809 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6810 if (!(vcpu->guest_debug &
6811 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006812 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006813 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006814 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006815 skip_emulated_instruction(vcpu);
6816
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006817 kvm_queue_exception(vcpu, DB_VECTOR);
6818 return 1;
6819 }
6820 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6821 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6822 /* fall through */
6823 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006824 /*
6825 * Update instruction length as we may reinject #BP from
6826 * user space while in guest debugging mode. Reading it for
6827 * #DB as well causes no harm, it is not used in that case.
6828 */
6829 vmx->vcpu.arch.event_exit_inst_len =
6830 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006832 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006833 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6834 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006835 break;
6836 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006837 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6838 kvm_run->ex.exception = ex_no;
6839 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006841 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842 return 0;
6843}
6844
Avi Kivity851ba692009-08-24 11:10:17 +03006845static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006846{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006847 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006848 return 1;
6849}
6850
Avi Kivity851ba692009-08-24 11:10:17 +03006851static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006852{
Avi Kivity851ba692009-08-24 11:10:17 +03006853 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006854 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006855 return 0;
6856}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857
Avi Kivity851ba692009-08-24 11:10:17 +03006858static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859{
He, Qingbfdaab02007-09-12 14:18:28 +08006860 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006861 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006862 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863
He, Qingbfdaab02007-09-12 14:18:28 +08006864 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006865 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006866
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006867 ++vcpu->stat.io_exits;
6868
Sean Christopherson432baf62018-03-08 08:57:26 -08006869 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006870 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006871
6872 port = exit_qualification >> 16;
6873 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006874 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006875
Sean Christophersondca7f122018-03-08 08:57:27 -08006876 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877}
6878
Ingo Molnar102d8322007-02-19 14:37:47 +02006879static void
6880vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6881{
6882 /*
6883 * Patch in the VMCALL instruction:
6884 */
6885 hypercall[0] = 0x0f;
6886 hypercall[1] = 0x01;
6887 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006888}
6889
Guo Chao0fa06072012-06-28 15:16:19 +08006890/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006891static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6892{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006893 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006894 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6895 unsigned long orig_val = val;
6896
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006897 /*
6898 * We get here when L2 changed cr0 in a way that did not change
6899 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006900 * but did change L0 shadowed bits. So we first calculate the
6901 * effective cr0 value that L1 would like to write into the
6902 * hardware. It consists of the L2-owned bits from the new
6903 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006904 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006905 val = (val & ~vmcs12->cr0_guest_host_mask) |
6906 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6907
David Matlack38991522016-11-29 18:14:08 -08006908 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006909 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006910
6911 if (kvm_set_cr0(vcpu, val))
6912 return 1;
6913 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006914 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006915 } else {
6916 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006917 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006918 return 1;
David Matlack38991522016-11-29 18:14:08 -08006919
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006920 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006921 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006922}
6923
6924static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6925{
6926 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006927 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6928 unsigned long orig_val = val;
6929
6930 /* analogously to handle_set_cr0 */
6931 val = (val & ~vmcs12->cr4_guest_host_mask) |
6932 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6933 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006934 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006935 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006936 return 0;
6937 } else
6938 return kvm_set_cr4(vcpu, val);
6939}
6940
Paolo Bonzini0367f202016-07-12 10:44:55 +02006941static int handle_desc(struct kvm_vcpu *vcpu)
6942{
6943 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6944 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6945}
6946
Avi Kivity851ba692009-08-24 11:10:17 +03006947static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006949 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006950 int cr;
6951 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006952 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006953 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006954
He, Qingbfdaab02007-09-12 14:18:28 +08006955 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006956 cr = exit_qualification & 15;
6957 reg = (exit_qualification >> 8) & 15;
6958 switch ((exit_qualification >> 4) & 3) {
6959 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006960 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006961 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006962 switch (cr) {
6963 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006964 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006965 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006966 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006967 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006968 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006969 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006971 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006972 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006973 case 8: {
6974 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006975 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006976 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006977 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006978 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006979 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006980 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006981 return ret;
6982 /*
6983 * TODO: we might be squashing a
6984 * KVM_GUESTDBG_SINGLESTEP-triggered
6985 * KVM_EXIT_DEBUG here.
6986 */
Avi Kivity851ba692009-08-24 11:10:17 +03006987 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006988 return 0;
6989 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006990 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006991 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006992 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006993 WARN_ONCE(1, "Guest should always own CR0.TS");
6994 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006995 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006996 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006997 case 1: /*mov from cr*/
6998 switch (cr) {
6999 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007000 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007001 val = kvm_read_cr3(vcpu);
7002 kvm_register_write(vcpu, reg, val);
7003 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007004 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007005 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007006 val = kvm_get_cr8(vcpu);
7007 kvm_register_write(vcpu, reg, val);
7008 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007009 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007010 }
7011 break;
7012 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007013 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007014 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007015 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007016
Kyle Huey6affcbe2016-11-29 12:40:40 -08007017 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007018 default:
7019 break;
7020 }
Avi Kivity851ba692009-08-24 11:10:17 +03007021 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007022 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007023 (int)(exit_qualification >> 4) & 3, cr);
7024 return 0;
7025}
7026
Avi Kivity851ba692009-08-24 11:10:17 +03007027static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028{
He, Qingbfdaab02007-09-12 14:18:28 +08007029 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007030 int dr, dr7, reg;
7031
7032 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7033 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7034
7035 /* First, if DR does not exist, trigger UD */
7036 if (!kvm_require_dr(vcpu, dr))
7037 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007038
Jan Kiszkaf2483412010-01-20 18:20:20 +01007039 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007040 if (!kvm_require_cpl(vcpu, 0))
7041 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007042 dr7 = vmcs_readl(GUEST_DR7);
7043 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007044 /*
7045 * As the vm-exit takes precedence over the debug trap, we
7046 * need to emulate the latter, either for the host or the
7047 * guest debugging itself.
7048 */
7049 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007050 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007051 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007052 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007053 vcpu->run->debug.arch.exception = DB_VECTOR;
7054 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007055 return 0;
7056 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007057 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007058 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007059 kvm_queue_exception(vcpu, DB_VECTOR);
7060 return 1;
7061 }
7062 }
7063
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007064 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007065 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7066 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007067
7068 /*
7069 * No more DR vmexits; force a reload of the debug registers
7070 * and reenter on this instruction. The next vmexit will
7071 * retrieve the full state of the debug registers.
7072 */
7073 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7074 return 1;
7075 }
7076
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007077 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7078 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007079 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007080
7081 if (kvm_get_dr(vcpu, dr, &val))
7082 return 1;
7083 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007084 } else
Nadav Amit57773922014-06-18 17:19:23 +03007085 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007086 return 1;
7087
Kyle Huey6affcbe2016-11-29 12:40:40 -08007088 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007089}
7090
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007091static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7092{
7093 return vcpu->arch.dr6;
7094}
7095
7096static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7097{
7098}
7099
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007100static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7101{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007102 get_debugreg(vcpu->arch.db[0], 0);
7103 get_debugreg(vcpu->arch.db[1], 1);
7104 get_debugreg(vcpu->arch.db[2], 2);
7105 get_debugreg(vcpu->arch.db[3], 3);
7106 get_debugreg(vcpu->arch.dr6, 6);
7107 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7108
7109 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007110 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007111}
7112
Gleb Natapov020df072010-04-13 10:05:23 +03007113static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7114{
7115 vmcs_writel(GUEST_DR7, val);
7116}
7117
Avi Kivity851ba692009-08-24 11:10:17 +03007118static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007119{
Kyle Huey6a908b62016-11-29 12:40:37 -08007120 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007121}
7122
Avi Kivity851ba692009-08-24 11:10:17 +03007123static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007124{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007125 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007126 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007128 msr_info.index = ecx;
7129 msr_info.host_initiated = false;
7130 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007131 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007132 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133 return 1;
7134 }
7135
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007136 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007137
Avi Kivity6aa8b732006-12-10 02:21:36 -08007138 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007139 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7140 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007141 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142}
7143
Avi Kivity851ba692009-08-24 11:10:17 +03007144static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007145{
Will Auld8fe8ab42012-11-29 12:42:12 -08007146 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007147 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7148 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7149 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007150
Will Auld8fe8ab42012-11-29 12:42:12 -08007151 msr.data = data;
7152 msr.index = ecx;
7153 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007154 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007155 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007156 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007157 return 1;
7158 }
7159
Avi Kivity59200272010-01-25 19:47:02 +02007160 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007161 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007162}
7163
Avi Kivity851ba692009-08-24 11:10:17 +03007164static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007165{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007166 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007167 return 1;
7168}
7169
Avi Kivity851ba692009-08-24 11:10:17 +03007170static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007171{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007172 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7173 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007174
Avi Kivity3842d132010-07-27 12:30:24 +03007175 kvm_make_request(KVM_REQ_EVENT, vcpu);
7176
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007177 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178 return 1;
7179}
7180
Avi Kivity851ba692009-08-24 11:10:17 +03007181static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007182{
Avi Kivityd3bef152007-06-05 15:53:05 +03007183 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007184}
7185
Avi Kivity851ba692009-08-24 11:10:17 +03007186static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007187{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007188 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007189}
7190
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007191static int handle_invd(struct kvm_vcpu *vcpu)
7192{
Andre Przywara51d8b662010-12-21 11:12:02 +01007193 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007194}
7195
Avi Kivity851ba692009-08-24 11:10:17 +03007196static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007197{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007198 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007199
7200 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007201 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007202}
7203
Avi Kivityfee84b02011-11-10 14:57:25 +02007204static int handle_rdpmc(struct kvm_vcpu *vcpu)
7205{
7206 int err;
7207
7208 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007209 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007210}
7211
Avi Kivity851ba692009-08-24 11:10:17 +03007212static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007213{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007214 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007215}
7216
Dexuan Cui2acf9232010-06-10 11:27:12 +08007217static int handle_xsetbv(struct kvm_vcpu *vcpu)
7218{
7219 u64 new_bv = kvm_read_edx_eax(vcpu);
7220 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7221
7222 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007223 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007224 return 1;
7225}
7226
Wanpeng Lif53cd632014-12-02 19:14:58 +08007227static int handle_xsaves(struct kvm_vcpu *vcpu)
7228{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007229 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007230 WARN(1, "this should never happen\n");
7231 return 1;
7232}
7233
7234static int handle_xrstors(struct kvm_vcpu *vcpu)
7235{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007236 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007237 WARN(1, "this should never happen\n");
7238 return 1;
7239}
7240
Avi Kivity851ba692009-08-24 11:10:17 +03007241static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007242{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007243 if (likely(fasteoi)) {
7244 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7245 int access_type, offset;
7246
7247 access_type = exit_qualification & APIC_ACCESS_TYPE;
7248 offset = exit_qualification & APIC_ACCESS_OFFSET;
7249 /*
7250 * Sane guest uses MOV to write EOI, with written value
7251 * not cared. So make a short-circuit here by avoiding
7252 * heavy instruction emulation.
7253 */
7254 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7255 (offset == APIC_EOI)) {
7256 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007258 }
7259 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007260 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007261}
7262
Yang Zhangc7c9c562013-01-25 10:18:51 +08007263static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7264{
7265 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7266 int vector = exit_qualification & 0xff;
7267
7268 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7269 kvm_apic_set_eoi_accelerated(vcpu, vector);
7270 return 1;
7271}
7272
Yang Zhang83d4c282013-01-25 10:18:49 +08007273static int handle_apic_write(struct kvm_vcpu *vcpu)
7274{
7275 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7276 u32 offset = exit_qualification & 0xfff;
7277
7278 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7279 kvm_apic_write_nodecode(vcpu, offset);
7280 return 1;
7281}
7282
Avi Kivity851ba692009-08-24 11:10:17 +03007283static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007284{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007285 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007286 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007287 bool has_error_code = false;
7288 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007289 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007290 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007291
7292 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007293 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007294 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007295
7296 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7297
7298 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007299 if (reason == TASK_SWITCH_GATE && idt_v) {
7300 switch (type) {
7301 case INTR_TYPE_NMI_INTR:
7302 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007303 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007304 break;
7305 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007306 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007307 kvm_clear_interrupt_queue(vcpu);
7308 break;
7309 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007310 if (vmx->idt_vectoring_info &
7311 VECTORING_INFO_DELIVER_CODE_MASK) {
7312 has_error_code = true;
7313 error_code =
7314 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7315 }
7316 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007317 case INTR_TYPE_SOFT_EXCEPTION:
7318 kvm_clear_exception_queue(vcpu);
7319 break;
7320 default:
7321 break;
7322 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007323 }
Izik Eidus37817f22008-03-24 23:14:53 +02007324 tss_selector = exit_qualification;
7325
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007326 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7327 type != INTR_TYPE_EXT_INTR &&
7328 type != INTR_TYPE_NMI_INTR))
7329 skip_emulated_instruction(vcpu);
7330
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007331 if (kvm_task_switch(vcpu, tss_selector,
7332 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7333 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007334 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7335 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7336 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007337 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007338 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007339
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007340 /*
7341 * TODO: What about debug traps on tss switch?
7342 * Are we supposed to inject them and update dr6?
7343 */
7344
7345 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007346}
7347
Avi Kivity851ba692009-08-24 11:10:17 +03007348static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007349{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007350 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007351 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007352 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007353
Sheng Yangf9c617f2009-03-25 10:08:52 +08007354 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007355
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007356 /*
7357 * EPT violation happened while executing iret from NMI,
7358 * "blocked by NMI" bit has to be set before next VM entry.
7359 * There are errata that may cause this bit to not be set:
7360 * AAK134, BY25.
7361 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007362 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007363 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007364 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007365 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7366
Sheng Yang14394422008-04-28 12:24:45 +08007367 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007368 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007369
Junaid Shahid27959a42016-12-06 16:46:10 -08007370 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007371 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007372 ? PFERR_USER_MASK : 0;
7373 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007374 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007375 ? PFERR_WRITE_MASK : 0;
7376 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007377 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007378 ? PFERR_FETCH_MASK : 0;
7379 /* ept page table entry is present? */
7380 error_code |= (exit_qualification &
7381 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7382 EPT_VIOLATION_EXECUTABLE))
7383 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007384
Paolo Bonzinieebed242016-11-28 14:39:58 +01007385 error_code |= (exit_qualification & 0x100) != 0 ?
7386 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007387
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007388 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007389 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007390}
7391
Avi Kivity851ba692009-08-24 11:10:17 +03007392static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007393{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007394 gpa_t gpa;
7395
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007396 /*
7397 * A nested guest cannot optimize MMIO vmexits, because we have an
7398 * nGPA here instead of the required GPA.
7399 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007400 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007401 if (!is_guest_mode(vcpu) &&
7402 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007403 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007404 /*
7405 * Doing kvm_skip_emulated_instruction() depends on undefined
7406 * behavior: Intel's manual doesn't mandate
7407 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7408 * occurs and while on real hardware it was observed to be set,
7409 * other hypervisors (namely Hyper-V) don't set it, we end up
7410 * advancing IP with some random value. Disable fast mmio when
7411 * running nested and keep it for real hardware in hope that
7412 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7413 */
7414 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7415 return kvm_skip_emulated_instruction(vcpu);
7416 else
7417 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7418 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007419 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007420
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007421 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007422}
7423
Avi Kivity851ba692009-08-24 11:10:17 +03007424static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007425{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007426 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007427 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7428 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007429 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007430 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007431
7432 return 1;
7433}
7434
Mohammed Gamal80ced182009-09-01 12:48:18 +02007435static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007436{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007437 struct vcpu_vmx *vmx = to_vmx(vcpu);
7438 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007439 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007440 u32 cpu_exec_ctrl;
7441 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007442 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007443
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007444 /*
7445 * We should never reach the point where we are emulating L2
7446 * due to invalid guest state as that means we incorrectly
7447 * allowed a nested VMEntry with an invalid vmcs12.
7448 */
7449 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7450
Avi Kivity49e9d552010-09-19 14:34:08 +02007451 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7452 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007453
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007454 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007455 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007456 return handle_interrupt_window(&vmx->vcpu);
7457
Radim Krčmář72875d82017-04-26 22:32:19 +02007458 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007459 return 1;
7460
Liran Alon9b8ae632017-11-05 16:56:34 +02007461 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007462
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007463 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007464 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007465 ret = 0;
7466 goto out;
7467 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007468
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007469 if (err != EMULATE_DONE)
7470 goto emulation_error;
7471
7472 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7473 vcpu->arch.exception.pending)
7474 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007475
Gleb Natapov8d76c492013-05-08 18:38:44 +03007476 if (vcpu->arch.halt_request) {
7477 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007478 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007479 goto out;
7480 }
7481
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007482 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007483 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007484 if (need_resched())
7485 schedule();
7486 }
7487
Mohammed Gamal80ced182009-09-01 12:48:18 +02007488out:
7489 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007490
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007491emulation_error:
7492 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7493 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7494 vcpu->run->internal.ndata = 0;
7495 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007496}
7497
7498static void grow_ple_window(struct kvm_vcpu *vcpu)
7499{
7500 struct vcpu_vmx *vmx = to_vmx(vcpu);
7501 int old = vmx->ple_window;
7502
Babu Mogerc8e88712018-03-16 16:37:24 -04007503 vmx->ple_window = __grow_ple_window(old, ple_window,
7504 ple_window_grow,
7505 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007506
7507 if (vmx->ple_window != old)
7508 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007509
7510 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007511}
7512
7513static void shrink_ple_window(struct kvm_vcpu *vcpu)
7514{
7515 struct vcpu_vmx *vmx = to_vmx(vcpu);
7516 int old = vmx->ple_window;
7517
Babu Mogerc8e88712018-03-16 16:37:24 -04007518 vmx->ple_window = __shrink_ple_window(old, ple_window,
7519 ple_window_shrink,
7520 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007521
7522 if (vmx->ple_window != old)
7523 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007524
7525 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007526}
7527
7528/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007529 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7530 */
7531static void wakeup_handler(void)
7532{
7533 struct kvm_vcpu *vcpu;
7534 int cpu = smp_processor_id();
7535
7536 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7537 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7538 blocked_vcpu_list) {
7539 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7540
7541 if (pi_test_on(pi_desc) == 1)
7542 kvm_vcpu_kick(vcpu);
7543 }
7544 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7545}
7546
Peng Haoe01bca22018-04-07 05:47:32 +08007547static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007548{
7549 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7550 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7551 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7552 0ull, VMX_EPT_EXECUTABLE_MASK,
7553 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007554 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007555
7556 ept_set_mmio_spte_mask();
7557 kvm_enable_tdp();
7558}
7559
Tiejun Chenf2c76482014-10-28 10:14:47 +08007560static __init int hardware_setup(void)
7561{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007562 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007563
7564 rdmsrl_safe(MSR_EFER, &host_efer);
7565
7566 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7567 kvm_define_shared_msr(i, vmx_msr_index[i]);
7568
Radim Krčmář23611332016-09-29 22:41:33 +02007569 for (i = 0; i < VMX_BITMAP_NR; i++) {
7570 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7571 if (!vmx_bitmap[i])
7572 goto out;
7573 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007574
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007575 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7576 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7577
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007578 if (setup_vmcs_config(&vmcs_config) < 0) {
7579 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007580 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007581 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007582
7583 if (boot_cpu_has(X86_FEATURE_NX))
7584 kvm_enable_efer_bits(EFER_NX);
7585
Wanpeng Li08d839c2017-03-23 05:30:08 -07007586 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7587 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007588 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007589
Tiejun Chenf2c76482014-10-28 10:14:47 +08007590 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007591 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007592 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007593 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007594 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007595
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007596 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007597 enable_ept_ad_bits = 0;
7598
Wanpeng Li8ad81822017-10-09 15:51:53 -07007599 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600 enable_unrestricted_guest = 0;
7601
Paolo Bonziniad15a292015-01-30 16:18:49 +01007602 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007603 flexpriority_enabled = 0;
7604
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007605 if (!cpu_has_virtual_nmis())
7606 enable_vnmi = 0;
7607
Paolo Bonziniad15a292015-01-30 16:18:49 +01007608 /*
7609 * set_apic_access_page_addr() is used to reload apic access
7610 * page upon invalidation. No need to do anything if not
7611 * using the APIC_ACCESS_ADDR VMCS field.
7612 */
7613 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007614 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007615
7616 if (!cpu_has_vmx_tpr_shadow())
7617 kvm_x86_ops->update_cr8_intercept = NULL;
7618
7619 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7620 kvm_disable_largepages();
7621
Wanpeng Li0f107682017-09-28 18:06:24 -07007622 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007623 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007624 ple_window = 0;
7625 ple_window_grow = 0;
7626 ple_window_max = 0;
7627 ple_window_shrink = 0;
7628 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007629
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007630 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007631 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007632 kvm_x86_ops->sync_pir_to_irr = NULL;
7633 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007634
Haozhong Zhang64903d62015-10-20 15:39:09 +08007635 if (cpu_has_vmx_tsc_scaling()) {
7636 kvm_has_tsc_control = true;
7637 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7638 kvm_tsc_scaling_ratio_frac_bits = 48;
7639 }
7640
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007641 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7642
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007643 if (enable_ept)
7644 vmx_enable_tdp();
7645 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007646 kvm_disable_tdp();
7647
Kai Huang843e4332015-01-28 10:54:28 +08007648 /*
7649 * Only enable PML when hardware supports PML feature, and both EPT
7650 * and EPT A/D bit features are enabled -- PML depends on them to work.
7651 */
7652 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7653 enable_pml = 0;
7654
7655 if (!enable_pml) {
7656 kvm_x86_ops->slot_enable_log_dirty = NULL;
7657 kvm_x86_ops->slot_disable_log_dirty = NULL;
7658 kvm_x86_ops->flush_log_dirty = NULL;
7659 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7660 }
7661
Yunhong Jiang64672c92016-06-13 14:19:59 -07007662 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7663 u64 vmx_msr;
7664
7665 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7666 cpu_preemption_timer_multi =
7667 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7668 } else {
7669 kvm_x86_ops->set_hv_timer = NULL;
7670 kvm_x86_ops->cancel_hv_timer = NULL;
7671 }
7672
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007673 if (!cpu_has_vmx_shadow_vmcs())
7674 enable_shadow_vmcs = 0;
7675 if (enable_shadow_vmcs)
7676 init_vmcs_shadow_fields();
7677
Feng Wubf9f6ac2015-09-18 22:29:55 +08007678 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007679 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007680
Ashok Rajc45dcc72016-06-22 14:59:56 +08007681 kvm_mce_cap_supported |= MCG_LMCE_P;
7682
Tiejun Chenf2c76482014-10-28 10:14:47 +08007683 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007684
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007685out:
Radim Krčmář23611332016-09-29 22:41:33 +02007686 for (i = 0; i < VMX_BITMAP_NR; i++)
7687 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007688
7689 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007690}
7691
7692static __exit void hardware_unsetup(void)
7693{
Radim Krčmář23611332016-09-29 22:41:33 +02007694 int i;
7695
7696 for (i = 0; i < VMX_BITMAP_NR; i++)
7697 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007698
Tiejun Chenf2c76482014-10-28 10:14:47 +08007699 free_kvm_area();
7700}
7701
Avi Kivity6aa8b732006-12-10 02:21:36 -08007702/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007703 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7704 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7705 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007706static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007707{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007708 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007709 grow_ple_window(vcpu);
7710
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007711 /*
7712 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7713 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7714 * never set PAUSE_EXITING and just set PLE if supported,
7715 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7716 */
7717 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007718 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007719}
7720
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007721static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007722{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007723 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007724}
7725
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007726static int handle_mwait(struct kvm_vcpu *vcpu)
7727{
7728 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7729 return handle_nop(vcpu);
7730}
7731
Jim Mattson45ec3682017-08-23 16:32:04 -07007732static int handle_invalid_op(struct kvm_vcpu *vcpu)
7733{
7734 kvm_queue_exception(vcpu, UD_VECTOR);
7735 return 1;
7736}
7737
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007738static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7739{
7740 return 1;
7741}
7742
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007743static int handle_monitor(struct kvm_vcpu *vcpu)
7744{
7745 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7746 return handle_nop(vcpu);
7747}
7748
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007749/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007750 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7751 * set the success or error code of an emulated VMX instruction, as specified
7752 * by Vol 2B, VMX Instruction Reference, "Conventions".
7753 */
7754static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7755{
7756 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7757 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7758 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7759}
7760
7761static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7762{
7763 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7764 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7765 X86_EFLAGS_SF | X86_EFLAGS_OF))
7766 | X86_EFLAGS_CF);
7767}
7768
Abel Gordon145c28d2013-04-18 14:36:55 +03007769static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007770 u32 vm_instruction_error)
7771{
7772 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7773 /*
7774 * failValid writes the error number to the current VMCS, which
7775 * can't be done there isn't a current VMCS.
7776 */
7777 nested_vmx_failInvalid(vcpu);
7778 return;
7779 }
7780 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7781 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7782 X86_EFLAGS_SF | X86_EFLAGS_OF))
7783 | X86_EFLAGS_ZF);
7784 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7785 /*
7786 * We don't need to force a shadow sync because
7787 * VM_INSTRUCTION_ERROR is not shadowed
7788 */
7789}
Abel Gordon145c28d2013-04-18 14:36:55 +03007790
Wincy Vanff651cb2014-12-11 08:52:58 +03007791static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7792{
7793 /* TODO: not to reset guest simply here. */
7794 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007795 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007796}
7797
Jan Kiszkaf4124502014-03-07 20:03:13 +01007798static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7799{
7800 struct vcpu_vmx *vmx =
7801 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7802
7803 vmx->nested.preemption_timer_expired = true;
7804 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7805 kvm_vcpu_kick(&vmx->vcpu);
7806
7807 return HRTIMER_NORESTART;
7808}
7809
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007810/*
Bandan Das19677e32014-05-06 02:19:15 -04007811 * Decode the memory-address operand of a vmx instruction, as recorded on an
7812 * exit caused by such an instruction (run by a guest hypervisor).
7813 * On success, returns 0. When the operand is invalid, returns 1 and throws
7814 * #UD or #GP.
7815 */
7816static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7817 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007818 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007819{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007820 gva_t off;
7821 bool exn;
7822 struct kvm_segment s;
7823
Bandan Das19677e32014-05-06 02:19:15 -04007824 /*
7825 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7826 * Execution", on an exit, vmx_instruction_info holds most of the
7827 * addressing components of the operand. Only the displacement part
7828 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7829 * For how an actual address is calculated from all these components,
7830 * refer to Vol. 1, "Operand Addressing".
7831 */
7832 int scaling = vmx_instruction_info & 3;
7833 int addr_size = (vmx_instruction_info >> 7) & 7;
7834 bool is_reg = vmx_instruction_info & (1u << 10);
7835 int seg_reg = (vmx_instruction_info >> 15) & 7;
7836 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7837 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7838 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7839 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7840
7841 if (is_reg) {
7842 kvm_queue_exception(vcpu, UD_VECTOR);
7843 return 1;
7844 }
7845
7846 /* Addr = segment_base + offset */
7847 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007848 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007849 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007850 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007851 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007852 off += kvm_register_read(vcpu, index_reg)<<scaling;
7853 vmx_get_segment(vcpu, &s, seg_reg);
7854 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007855
7856 if (addr_size == 1) /* 32 bit */
7857 *ret &= 0xffffffff;
7858
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007859 /* Checks for #GP/#SS exceptions. */
7860 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007861 if (is_long_mode(vcpu)) {
7862 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7863 * non-canonical form. This is the only check on the memory
7864 * destination for long mode!
7865 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007866 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007867 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007868 /* Protected mode: apply checks for segment validity in the
7869 * following order:
7870 * - segment type check (#GP(0) may be thrown)
7871 * - usability check (#GP(0)/#SS(0))
7872 * - limit check (#GP(0)/#SS(0))
7873 */
7874 if (wr)
7875 /* #GP(0) if the destination operand is located in a
7876 * read-only data segment or any code segment.
7877 */
7878 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7879 else
7880 /* #GP(0) if the source operand is located in an
7881 * execute-only code segment
7882 */
7883 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007884 if (exn) {
7885 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7886 return 1;
7887 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007888 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7889 */
7890 exn = (s.unusable != 0);
7891 /* Protected mode: #GP(0)/#SS(0) if the memory
7892 * operand is outside the segment limit.
7893 */
7894 exn = exn || (off + sizeof(u64) > s.limit);
7895 }
7896 if (exn) {
7897 kvm_queue_exception_e(vcpu,
7898 seg_reg == VCPU_SREG_SS ?
7899 SS_VECTOR : GP_VECTOR,
7900 0);
7901 return 1;
7902 }
7903
Bandan Das19677e32014-05-06 02:19:15 -04007904 return 0;
7905}
7906
Radim Krčmářcbf71272017-05-19 15:48:51 +02007907static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007908{
7909 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007910 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007911
7912 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007913 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007914 return 1;
7915
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007916 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007917 kvm_inject_page_fault(vcpu, &e);
7918 return 1;
7919 }
7920
Bandan Das3573e222014-05-06 02:19:16 -04007921 return 0;
7922}
7923
Jim Mattsone29acc52016-11-30 12:03:43 -08007924static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7925{
7926 struct vcpu_vmx *vmx = to_vmx(vcpu);
7927 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007928 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007929
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007930 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7931 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007932 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007933
7934 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7935 if (!vmx->nested.cached_vmcs12)
7936 goto out_cached_vmcs12;
7937
7938 if (enable_shadow_vmcs) {
7939 shadow_vmcs = alloc_vmcs();
7940 if (!shadow_vmcs)
7941 goto out_shadow_vmcs;
7942 /* mark vmcs as shadow */
7943 shadow_vmcs->revision_id |= (1u << 31);
7944 /* init shadow vmcs */
7945 vmcs_clear(shadow_vmcs);
7946 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7947 }
7948
Jim Mattsone29acc52016-11-30 12:03:43 -08007949 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7950 HRTIMER_MODE_REL_PINNED);
7951 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7952
7953 vmx->nested.vmxon = true;
7954 return 0;
7955
7956out_shadow_vmcs:
7957 kfree(vmx->nested.cached_vmcs12);
7958
7959out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007960 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007961
Jim Mattsonde3a0022017-11-27 17:22:25 -06007962out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007963 return -ENOMEM;
7964}
7965
Bandan Das3573e222014-05-06 02:19:16 -04007966/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007967 * Emulate the VMXON instruction.
7968 * Currently, we just remember that VMX is active, and do not save or even
7969 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7970 * do not currently need to store anything in that guest-allocated memory
7971 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7972 * argument is different from the VMXON pointer (which the spec says they do).
7973 */
7974static int handle_vmon(struct kvm_vcpu *vcpu)
7975{
Jim Mattsone29acc52016-11-30 12:03:43 -08007976 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007977 gpa_t vmptr;
7978 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007979 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007980 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7981 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007982
Jim Mattson70f3aac2017-04-26 08:53:46 -07007983 /*
7984 * The Intel VMX Instruction Reference lists a bunch of bits that are
7985 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7986 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7987 * Otherwise, we should fail with #UD. But most faulting conditions
7988 * have already been checked by hardware, prior to the VM-exit for
7989 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7990 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007991 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007992 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007993 kvm_queue_exception(vcpu, UD_VECTOR);
7994 return 1;
7995 }
7996
Felix Wilhelm727ba742018-06-11 09:43:44 +02007997 /* CPL=0 must be checked manually. */
7998 if (vmx_get_cpl(vcpu)) {
7999 kvm_queue_exception(vcpu, UD_VECTOR);
8000 return 1;
8001 }
8002
Abel Gordon145c28d2013-04-18 14:36:55 +03008003 if (vmx->nested.vmxon) {
8004 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008005 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008006 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008007
Haozhong Zhang3b840802016-06-22 14:59:54 +08008008 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008009 != VMXON_NEEDED_FEATURES) {
8010 kvm_inject_gp(vcpu, 0);
8011 return 1;
8012 }
8013
Radim Krčmářcbf71272017-05-19 15:48:51 +02008014 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008015 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008016
8017 /*
8018 * SDM 3: 24.11.5
8019 * The first 4 bytes of VMXON region contain the supported
8020 * VMCS revision identifier
8021 *
8022 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8023 * which replaces physical address width with 32
8024 */
8025 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8026 nested_vmx_failInvalid(vcpu);
8027 return kvm_skip_emulated_instruction(vcpu);
8028 }
8029
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008030 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8031 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008032 nested_vmx_failInvalid(vcpu);
8033 return kvm_skip_emulated_instruction(vcpu);
8034 }
8035 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8036 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008037 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008038 nested_vmx_failInvalid(vcpu);
8039 return kvm_skip_emulated_instruction(vcpu);
8040 }
8041 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008042 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008043
8044 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008045 ret = enter_vmx_operation(vcpu);
8046 if (ret)
8047 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008048
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008049 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008050 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008051}
8052
8053/*
8054 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8055 * for running VMX instructions (except VMXON, whose prerequisites are
8056 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008057 * Note that many of these exceptions have priority over VM exits, so they
8058 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008059 */
8060static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8061{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008062 if (vmx_get_cpl(vcpu)) {
8063 kvm_queue_exception(vcpu, UD_VECTOR);
8064 return 0;
8065 }
8066
Jim Mattson70f3aac2017-04-26 08:53:46 -07008067 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008068 kvm_queue_exception(vcpu, UD_VECTOR);
8069 return 0;
8070 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008071 return 1;
8072}
8073
David Matlack8ca44e82017-08-01 14:00:39 -07008074static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8075{
8076 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8077 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8078}
8079
Abel Gordone7953d72013-04-18 14:37:55 +03008080static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8081{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008082 if (vmx->nested.current_vmptr == -1ull)
8083 return;
8084
Abel Gordon012f83c2013-04-18 14:39:25 +03008085 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008086 /* copy to memory all shadowed fields in case
8087 they were modified */
8088 copy_shadow_to_vmcs12(vmx);
8089 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008090 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008091 }
Wincy Van705699a2015-02-03 23:58:17 +08008092 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008093
8094 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008095 kvm_vcpu_write_guest_page(&vmx->vcpu,
8096 vmx->nested.current_vmptr >> PAGE_SHIFT,
8097 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008098
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008099 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008100}
8101
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008102/*
8103 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8104 * just stops using VMX.
8105 */
8106static void free_nested(struct vcpu_vmx *vmx)
8107{
Wanpeng Lib7455822017-11-22 14:04:00 -08008108 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008109 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008110
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008111 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008112 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008113 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008114 vmx->nested.posted_intr_nv = -1;
8115 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008116 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008117 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008118 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8119 free_vmcs(vmx->vmcs01.shadow_vmcs);
8120 vmx->vmcs01.shadow_vmcs = NULL;
8121 }
David Matlack4f2777b2016-07-13 17:16:37 -07008122 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008123 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008124 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008125 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008126 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008127 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008128 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008129 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008130 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008131 }
Wincy Van705699a2015-02-03 23:58:17 +08008132 if (vmx->nested.pi_desc_page) {
8133 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008134 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008135 vmx->nested.pi_desc_page = NULL;
8136 vmx->nested.pi_desc = NULL;
8137 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008138
Jim Mattsonde3a0022017-11-27 17:22:25 -06008139 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008140}
8141
8142/* Emulate the VMXOFF instruction */
8143static int handle_vmoff(struct kvm_vcpu *vcpu)
8144{
8145 if (!nested_vmx_check_permission(vcpu))
8146 return 1;
8147 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008148 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008149 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008150}
8151
Nadav Har'El27d6c862011-05-25 23:06:59 +03008152/* Emulate the VMCLEAR instruction */
8153static int handle_vmclear(struct kvm_vcpu *vcpu)
8154{
8155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008156 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008157 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008158
8159 if (!nested_vmx_check_permission(vcpu))
8160 return 1;
8161
Radim Krčmářcbf71272017-05-19 15:48:51 +02008162 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008163 return 1;
8164
Radim Krčmářcbf71272017-05-19 15:48:51 +02008165 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8166 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8167 return kvm_skip_emulated_instruction(vcpu);
8168 }
8169
8170 if (vmptr == vmx->nested.vmxon_ptr) {
8171 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8172 return kvm_skip_emulated_instruction(vcpu);
8173 }
8174
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008175 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008176 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008177
Jim Mattson587d7e722017-03-02 12:41:48 -08008178 kvm_vcpu_write_guest(vcpu,
8179 vmptr + offsetof(struct vmcs12, launch_state),
8180 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008181
Nadav Har'El27d6c862011-05-25 23:06:59 +03008182 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008183 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008184}
8185
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008186static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8187
8188/* Emulate the VMLAUNCH instruction */
8189static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8190{
8191 return nested_vmx_run(vcpu, true);
8192}
8193
8194/* Emulate the VMRESUME instruction */
8195static int handle_vmresume(struct kvm_vcpu *vcpu)
8196{
8197
8198 return nested_vmx_run(vcpu, false);
8199}
8200
Nadav Har'El49f705c2011-05-25 23:08:30 +03008201/*
8202 * Read a vmcs12 field. Since these can have varying lengths and we return
8203 * one type, we chose the biggest type (u64) and zero-extend the return value
8204 * to that size. Note that the caller, handle_vmread, might need to use only
8205 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8206 * 64-bit fields are to be returned).
8207 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008208static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8209 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008210{
8211 short offset = vmcs_field_to_offset(field);
8212 char *p;
8213
8214 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008215 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008216
8217 p = ((char *)(get_vmcs12(vcpu))) + offset;
8218
Jim Mattsond37f4262017-12-22 12:12:16 -08008219 switch (vmcs_field_width(field)) {
8220 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008221 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008222 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008223 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008224 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008225 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008226 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008227 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008228 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008229 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008230 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008231 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008232 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008233 WARN_ON(1);
8234 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008235 }
8236}
8237
Abel Gordon20b97fe2013-04-18 14:36:25 +03008238
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008239static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8240 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008241 short offset = vmcs_field_to_offset(field);
8242 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8243 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008244 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008245
Jim Mattsond37f4262017-12-22 12:12:16 -08008246 switch (vmcs_field_width(field)) {
8247 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008248 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008249 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008250 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008251 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008252 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008253 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008254 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008255 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008256 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008257 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008258 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008259 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008260 WARN_ON(1);
8261 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008262 }
8263
8264}
8265
Jim Mattsonf4160e42018-05-29 09:11:33 -07008266/*
8267 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8268 * they have been modified by the L1 guest. Note that the "read-only"
8269 * VM-exit information fields are actually writable if the vCPU is
8270 * configured to support "VMWRITE to any supported field in the VMCS."
8271 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008272static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8273{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008274 const u16 *fields[] = {
8275 shadow_read_write_fields,
8276 shadow_read_only_fields
8277 };
8278 const int max_fields[] = {
8279 max_shadow_read_write_fields,
8280 max_shadow_read_only_fields
8281 };
8282 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008283 unsigned long field;
8284 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008285 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008286
Jan Kiszka282da872014-10-08 18:05:39 +02008287 preempt_disable();
8288
Abel Gordon16f5b902013-04-18 14:38:25 +03008289 vmcs_load(shadow_vmcs);
8290
Jim Mattsonf4160e42018-05-29 09:11:33 -07008291 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8292 for (i = 0; i < max_fields[q]; i++) {
8293 field = fields[q][i];
8294 field_value = __vmcs_readl(field);
8295 vmcs12_write_any(&vmx->vcpu, field, field_value);
8296 }
8297 /*
8298 * Skip the VM-exit information fields if they are read-only.
8299 */
8300 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8301 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008302 }
8303
8304 vmcs_clear(shadow_vmcs);
8305 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008306
8307 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008308}
8309
Abel Gordonc3114422013-04-18 14:38:55 +03008310static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8311{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008312 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008313 shadow_read_write_fields,
8314 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008315 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008316 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008317 max_shadow_read_write_fields,
8318 max_shadow_read_only_fields
8319 };
8320 int i, q;
8321 unsigned long field;
8322 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008323 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008324
8325 vmcs_load(shadow_vmcs);
8326
Mathias Krausec2bae892013-06-26 20:36:21 +02008327 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008328 for (i = 0; i < max_fields[q]; i++) {
8329 field = fields[q][i];
8330 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008331 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008332 }
8333 }
8334
8335 vmcs_clear(shadow_vmcs);
8336 vmcs_load(vmx->loaded_vmcs->vmcs);
8337}
8338
Nadav Har'El49f705c2011-05-25 23:08:30 +03008339/*
8340 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8341 * used before) all generate the same failure when it is missing.
8342 */
8343static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8344{
8345 struct vcpu_vmx *vmx = to_vmx(vcpu);
8346 if (vmx->nested.current_vmptr == -1ull) {
8347 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008348 return 0;
8349 }
8350 return 1;
8351}
8352
8353static int handle_vmread(struct kvm_vcpu *vcpu)
8354{
8355 unsigned long field;
8356 u64 field_value;
8357 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8358 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8359 gva_t gva = 0;
8360
Kyle Hueyeb277562016-11-29 12:40:39 -08008361 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008362 return 1;
8363
Kyle Huey6affcbe2016-11-29 12:40:40 -08008364 if (!nested_vmx_check_vmcs12(vcpu))
8365 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008366
Nadav Har'El49f705c2011-05-25 23:08:30 +03008367 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008368 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008369 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008370 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008371 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008372 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 }
8374 /*
8375 * Now copy part of this value to register or memory, as requested.
8376 * Note that the number of bits actually copied is 32 or 64 depending
8377 * on the guest's mode (32 or 64 bit), not on the given field's length.
8378 */
8379 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008380 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008381 field_value);
8382 } else {
8383 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008384 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008385 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008386 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008387 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8388 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008389 }
8390
8391 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008392 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008393}
8394
8395
8396static int handle_vmwrite(struct kvm_vcpu *vcpu)
8397{
8398 unsigned long field;
8399 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008400 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008401 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8402 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008403
Nadav Har'El49f705c2011-05-25 23:08:30 +03008404 /* The value to write might be 32 or 64 bits, depending on L1's long
8405 * mode, and eventually we need to write that into a field of several
8406 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008407 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008408 * bits into the vmcs12 field.
8409 */
8410 u64 field_value = 0;
8411 struct x86_exception e;
8412
Kyle Hueyeb277562016-11-29 12:40:39 -08008413 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008414 return 1;
8415
Kyle Huey6affcbe2016-11-29 12:40:40 -08008416 if (!nested_vmx_check_vmcs12(vcpu))
8417 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008418
Nadav Har'El49f705c2011-05-25 23:08:30 +03008419 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008420 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421 (((vmx_instruction_info) >> 3) & 0xf));
8422 else {
8423 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008424 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008426 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8427 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008428 kvm_inject_page_fault(vcpu, &e);
8429 return 1;
8430 }
8431 }
8432
8433
Nadav Amit27e6fb52014-06-18 17:19:26 +03008434 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008435 /*
8436 * If the vCPU supports "VMWRITE to any supported field in the
8437 * VMCS," then the "read-only" fields are actually read/write.
8438 */
8439 if (vmcs_field_readonly(field) &&
8440 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441 nested_vmx_failValid(vcpu,
8442 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008443 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444 }
8445
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008446 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008447 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008448 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008449 }
8450
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008451 switch (field) {
8452#define SHADOW_FIELD_RW(x) case x:
8453#include "vmx_shadow_fields.h"
8454 /*
8455 * The fields that can be updated by L1 without a vmexit are
8456 * always updated in the vmcs02, the others go down the slow
8457 * path of prepare_vmcs02.
8458 */
8459 break;
8460 default:
8461 vmx->nested.dirty_vmcs12 = true;
8462 break;
8463 }
8464
Nadav Har'El49f705c2011-05-25 23:08:30 +03008465 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008466 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008467}
8468
Jim Mattsona8bc2842016-11-30 12:03:44 -08008469static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8470{
8471 vmx->nested.current_vmptr = vmptr;
8472 if (enable_shadow_vmcs) {
8473 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8474 SECONDARY_EXEC_SHADOW_VMCS);
8475 vmcs_write64(VMCS_LINK_POINTER,
8476 __pa(vmx->vmcs01.shadow_vmcs));
8477 vmx->nested.sync_shadow_vmcs = true;
8478 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008479 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008480}
8481
Nadav Har'El63846662011-05-25 23:07:29 +03008482/* Emulate the VMPTRLD instruction */
8483static int handle_vmptrld(struct kvm_vcpu *vcpu)
8484{
8485 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008486 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008487
8488 if (!nested_vmx_check_permission(vcpu))
8489 return 1;
8490
Radim Krčmářcbf71272017-05-19 15:48:51 +02008491 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008492 return 1;
8493
Radim Krčmářcbf71272017-05-19 15:48:51 +02008494 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8495 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8496 return kvm_skip_emulated_instruction(vcpu);
8497 }
8498
8499 if (vmptr == vmx->nested.vmxon_ptr) {
8500 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8501 return kvm_skip_emulated_instruction(vcpu);
8502 }
8503
Nadav Har'El63846662011-05-25 23:07:29 +03008504 if (vmx->nested.current_vmptr != vmptr) {
8505 struct vmcs12 *new_vmcs12;
8506 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008507 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8508 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008509 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008510 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008511 }
8512 new_vmcs12 = kmap(page);
8513 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8514 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008515 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008516 nested_vmx_failValid(vcpu,
8517 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008518 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008519 }
Nadav Har'El63846662011-05-25 23:07:29 +03008520
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008521 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008522 /*
8523 * Load VMCS12 from guest memory since it is not already
8524 * cached.
8525 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008526 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8527 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008528 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008529
Jim Mattsona8bc2842016-11-30 12:03:44 -08008530 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008531 }
8532
8533 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008534 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008535}
8536
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008537/* Emulate the VMPTRST instruction */
8538static int handle_vmptrst(struct kvm_vcpu *vcpu)
8539{
8540 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8541 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8542 gva_t vmcs_gva;
8543 struct x86_exception e;
8544
8545 if (!nested_vmx_check_permission(vcpu))
8546 return 1;
8547
8548 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008549 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008550 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008551 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008552 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8553 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8554 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008555 kvm_inject_page_fault(vcpu, &e);
8556 return 1;
8557 }
8558 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008559 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008560}
8561
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008562/* Emulate the INVEPT instruction */
8563static int handle_invept(struct kvm_vcpu *vcpu)
8564{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008565 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008566 u32 vmx_instruction_info, types;
8567 unsigned long type;
8568 gva_t gva;
8569 struct x86_exception e;
8570 struct {
8571 u64 eptp, gpa;
8572 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008573
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008574 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008575 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008576 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008577 kvm_queue_exception(vcpu, UD_VECTOR);
8578 return 1;
8579 }
8580
8581 if (!nested_vmx_check_permission(vcpu))
8582 return 1;
8583
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008584 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008585 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008586
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008587 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008588
Jim Mattson85c856b2016-10-26 08:38:38 -07008589 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008590 nested_vmx_failValid(vcpu,
8591 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008592 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008593 }
8594
8595 /* According to the Intel VMX instruction reference, the memory
8596 * operand is read even if it isn't needed (e.g., for type==global)
8597 */
8598 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008599 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008600 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008601 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008602 kvm_inject_page_fault(vcpu, &e);
8603 return 1;
8604 }
8605
8606 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008607 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008608 /*
8609 * TODO: track mappings and invalidate
8610 * single context requests appropriately
8611 */
8612 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008613 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008614 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008615 nested_vmx_succeed(vcpu);
8616 break;
8617 default:
8618 BUG_ON(1);
8619 break;
8620 }
8621
Kyle Huey6affcbe2016-11-29 12:40:40 -08008622 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008623}
8624
Petr Matouseka642fc32014-09-23 20:22:30 +02008625static int handle_invvpid(struct kvm_vcpu *vcpu)
8626{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008627 struct vcpu_vmx *vmx = to_vmx(vcpu);
8628 u32 vmx_instruction_info;
8629 unsigned long type, types;
8630 gva_t gva;
8631 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008632 struct {
8633 u64 vpid;
8634 u64 gla;
8635 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008636
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008637 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008638 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008639 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008640 kvm_queue_exception(vcpu, UD_VECTOR);
8641 return 1;
8642 }
8643
8644 if (!nested_vmx_check_permission(vcpu))
8645 return 1;
8646
8647 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8648 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8649
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008650 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008651 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008652
Jim Mattson85c856b2016-10-26 08:38:38 -07008653 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008654 nested_vmx_failValid(vcpu,
8655 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008656 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008657 }
8658
8659 /* according to the intel vmx instruction reference, the memory
8660 * operand is read even if it isn't needed (e.g., for type==global)
8661 */
8662 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8663 vmx_instruction_info, false, &gva))
8664 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008665 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008666 kvm_inject_page_fault(vcpu, &e);
8667 return 1;
8668 }
Jim Mattson40352602017-06-28 09:37:37 -07008669 if (operand.vpid >> 16) {
8670 nested_vmx_failValid(vcpu,
8671 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8672 return kvm_skip_emulated_instruction(vcpu);
8673 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008674
8675 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008676 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008677 if (!operand.vpid ||
8678 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008679 nested_vmx_failValid(vcpu,
8680 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8681 return kvm_skip_emulated_instruction(vcpu);
8682 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008683 if (cpu_has_vmx_invvpid_individual_addr() &&
8684 vmx->nested.vpid02) {
8685 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8686 vmx->nested.vpid02, operand.gla);
8687 } else
8688 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8689 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008690 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008691 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008692 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008693 nested_vmx_failValid(vcpu,
8694 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008695 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008696 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008697 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008698 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008699 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008700 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008701 break;
8702 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008703 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008704 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008705 }
8706
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008707 nested_vmx_succeed(vcpu);
8708
Kyle Huey6affcbe2016-11-29 12:40:40 -08008709 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008710}
8711
Kai Huang843e4332015-01-28 10:54:28 +08008712static int handle_pml_full(struct kvm_vcpu *vcpu)
8713{
8714 unsigned long exit_qualification;
8715
8716 trace_kvm_pml_full(vcpu->vcpu_id);
8717
8718 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8719
8720 /*
8721 * PML buffer FULL happened while executing iret from NMI,
8722 * "blocked by NMI" bit has to be set before next VM entry.
8723 */
8724 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008725 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008726 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8727 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8728 GUEST_INTR_STATE_NMI);
8729
8730 /*
8731 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8732 * here.., and there's no userspace involvement needed for PML.
8733 */
8734 return 1;
8735}
8736
Yunhong Jiang64672c92016-06-13 14:19:59 -07008737static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8738{
8739 kvm_lapic_expired_hv_timer(vcpu);
8740 return 1;
8741}
8742
Bandan Das41ab9372017-08-03 15:54:43 -04008743static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8744{
8745 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008746 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8747
8748 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008749 switch (address & VMX_EPTP_MT_MASK) {
8750 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008751 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008752 return false;
8753 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008754 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008755 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008756 return false;
8757 break;
8758 default:
8759 return false;
8760 }
8761
David Hildenbrandbb97a012017-08-10 23:15:28 +02008762 /* only 4 levels page-walk length are valid */
8763 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008764 return false;
8765
8766 /* Reserved bits should not be set */
8767 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8768 return false;
8769
8770 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008771 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008772 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008773 return false;
8774 }
8775
8776 return true;
8777}
8778
8779static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8780 struct vmcs12 *vmcs12)
8781{
8782 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8783 u64 address;
8784 bool accessed_dirty;
8785 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8786
8787 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8788 !nested_cpu_has_ept(vmcs12))
8789 return 1;
8790
8791 if (index >= VMFUNC_EPTP_ENTRIES)
8792 return 1;
8793
8794
8795 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8796 &address, index * 8, 8))
8797 return 1;
8798
David Hildenbrandbb97a012017-08-10 23:15:28 +02008799 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008800
8801 /*
8802 * If the (L2) guest does a vmfunc to the currently
8803 * active ept pointer, we don't have to do anything else
8804 */
8805 if (vmcs12->ept_pointer != address) {
8806 if (!valid_ept_address(vcpu, address))
8807 return 1;
8808
8809 kvm_mmu_unload(vcpu);
8810 mmu->ept_ad = accessed_dirty;
8811 mmu->base_role.ad_disabled = !accessed_dirty;
8812 vmcs12->ept_pointer = address;
8813 /*
8814 * TODO: Check what's the correct approach in case
8815 * mmu reload fails. Currently, we just let the next
8816 * reload potentially fail
8817 */
8818 kvm_mmu_reload(vcpu);
8819 }
8820
8821 return 0;
8822}
8823
Bandan Das2a499e42017-08-03 15:54:41 -04008824static int handle_vmfunc(struct kvm_vcpu *vcpu)
8825{
Bandan Das27c42a12017-08-03 15:54:42 -04008826 struct vcpu_vmx *vmx = to_vmx(vcpu);
8827 struct vmcs12 *vmcs12;
8828 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8829
8830 /*
8831 * VMFUNC is only supported for nested guests, but we always enable the
8832 * secondary control for simplicity; for non-nested mode, fake that we
8833 * didn't by injecting #UD.
8834 */
8835 if (!is_guest_mode(vcpu)) {
8836 kvm_queue_exception(vcpu, UD_VECTOR);
8837 return 1;
8838 }
8839
8840 vmcs12 = get_vmcs12(vcpu);
8841 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8842 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008843
8844 switch (function) {
8845 case 0:
8846 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8847 goto fail;
8848 break;
8849 default:
8850 goto fail;
8851 }
8852 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008853
8854fail:
8855 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8856 vmcs_read32(VM_EXIT_INTR_INFO),
8857 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008858 return 1;
8859}
8860
Nadav Har'El0140cae2011-05-25 23:06:28 +03008861/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008862 * The exit handlers return 1 if the exit was handled fully and guest execution
8863 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8864 * to be done to userspace and return 0.
8865 */
Mathias Krause772e0312012-08-30 01:30:19 +02008866static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008867 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8868 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008869 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008870 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008871 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008872 [EXIT_REASON_CR_ACCESS] = handle_cr,
8873 [EXIT_REASON_DR_ACCESS] = handle_dr,
8874 [EXIT_REASON_CPUID] = handle_cpuid,
8875 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8876 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8877 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8878 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008879 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008880 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008881 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008882 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008883 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008884 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008885 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008886 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008887 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008888 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008889 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008890 [EXIT_REASON_VMOFF] = handle_vmoff,
8891 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008892 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8893 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008894 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008895 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008896 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008897 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008898 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008899 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008900 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8901 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008902 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8903 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008904 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008905 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008906 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008907 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008908 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008909 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008910 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008911 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008912 [EXIT_REASON_XSAVES] = handle_xsaves,
8913 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008914 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008915 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008916 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008917};
8918
8919static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008920 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008921
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008922static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8923 struct vmcs12 *vmcs12)
8924{
8925 unsigned long exit_qualification;
8926 gpa_t bitmap, last_bitmap;
8927 unsigned int port;
8928 int size;
8929 u8 b;
8930
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008931 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008932 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008933
8934 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8935
8936 port = exit_qualification >> 16;
8937 size = (exit_qualification & 7) + 1;
8938
8939 last_bitmap = (gpa_t)-1;
8940 b = -1;
8941
8942 while (size > 0) {
8943 if (port < 0x8000)
8944 bitmap = vmcs12->io_bitmap_a;
8945 else if (port < 0x10000)
8946 bitmap = vmcs12->io_bitmap_b;
8947 else
Joe Perches1d804d02015-03-30 16:46:09 -07008948 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008949 bitmap += (port & 0x7fff) / 8;
8950
8951 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008952 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008953 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008954 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008955 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008956
8957 port++;
8958 size--;
8959 last_bitmap = bitmap;
8960 }
8961
Joe Perches1d804d02015-03-30 16:46:09 -07008962 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008963}
8964
Nadav Har'El644d7112011-05-25 23:12:35 +03008965/*
8966 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8967 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8968 * disinterest in the current event (read or write a specific MSR) by using an
8969 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8970 */
8971static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8972 struct vmcs12 *vmcs12, u32 exit_reason)
8973{
8974 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8975 gpa_t bitmap;
8976
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008977 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008978 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008979
8980 /*
8981 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8982 * for the four combinations of read/write and low/high MSR numbers.
8983 * First we need to figure out which of the four to use:
8984 */
8985 bitmap = vmcs12->msr_bitmap;
8986 if (exit_reason == EXIT_REASON_MSR_WRITE)
8987 bitmap += 2048;
8988 if (msr_index >= 0xc0000000) {
8989 msr_index -= 0xc0000000;
8990 bitmap += 1024;
8991 }
8992
8993 /* Then read the msr_index'th bit from this bitmap: */
8994 if (msr_index < 1024*8) {
8995 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008996 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008997 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008998 return 1 & (b >> (msr_index & 7));
8999 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009000 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009001}
9002
9003/*
9004 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9005 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9006 * intercept (via guest_host_mask etc.) the current event.
9007 */
9008static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9009 struct vmcs12 *vmcs12)
9010{
9011 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9012 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009013 int reg;
9014 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009015
9016 switch ((exit_qualification >> 4) & 3) {
9017 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009018 reg = (exit_qualification >> 8) & 15;
9019 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009020 switch (cr) {
9021 case 0:
9022 if (vmcs12->cr0_guest_host_mask &
9023 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009024 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009025 break;
9026 case 3:
9027 if ((vmcs12->cr3_target_count >= 1 &&
9028 vmcs12->cr3_target_value0 == val) ||
9029 (vmcs12->cr3_target_count >= 2 &&
9030 vmcs12->cr3_target_value1 == val) ||
9031 (vmcs12->cr3_target_count >= 3 &&
9032 vmcs12->cr3_target_value2 == val) ||
9033 (vmcs12->cr3_target_count >= 4 &&
9034 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009035 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009036 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009037 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009038 break;
9039 case 4:
9040 if (vmcs12->cr4_guest_host_mask &
9041 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009042 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009043 break;
9044 case 8:
9045 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009046 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009047 break;
9048 }
9049 break;
9050 case 2: /* clts */
9051 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9052 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009053 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009054 break;
9055 case 1: /* mov from cr */
9056 switch (cr) {
9057 case 3:
9058 if (vmcs12->cpu_based_vm_exec_control &
9059 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009060 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 break;
9062 case 8:
9063 if (vmcs12->cpu_based_vm_exec_control &
9064 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009066 break;
9067 }
9068 break;
9069 case 3: /* lmsw */
9070 /*
9071 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9072 * cr0. Other attempted changes are ignored, with no exit.
9073 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009074 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009075 if (vmcs12->cr0_guest_host_mask & 0xe &
9076 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009078 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9079 !(vmcs12->cr0_read_shadow & 0x1) &&
9080 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009081 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009082 break;
9083 }
Joe Perches1d804d02015-03-30 16:46:09 -07009084 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009085}
9086
9087/*
9088 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9089 * should handle it ourselves in L0 (and then continue L2). Only call this
9090 * when in is_guest_mode (L2).
9091 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009092static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009093{
Nadav Har'El644d7112011-05-25 23:12:35 +03009094 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9095 struct vcpu_vmx *vmx = to_vmx(vcpu);
9096 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9097
Jim Mattson4f350c62017-09-14 16:31:44 -07009098 if (vmx->nested.nested_run_pending)
9099 return false;
9100
9101 if (unlikely(vmx->fail)) {
9102 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9103 vmcs_read32(VM_INSTRUCTION_ERROR));
9104 return true;
9105 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009106
David Matlackc9f04402017-08-01 14:00:40 -07009107 /*
9108 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009109 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9110 * Page). The CPU may write to these pages via their host
9111 * physical address while L2 is running, bypassing any
9112 * address-translation-based dirty tracking (e.g. EPT write
9113 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009114 *
9115 * Mark them dirty on every exit from L2 to prevent them from
9116 * getting out of sync with dirty tracking.
9117 */
9118 nested_mark_vmcs12_pages_dirty(vcpu);
9119
Jim Mattson4f350c62017-09-14 16:31:44 -07009120 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9121 vmcs_readl(EXIT_QUALIFICATION),
9122 vmx->idt_vectoring_info,
9123 intr_info,
9124 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9125 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009126
9127 switch (exit_reason) {
9128 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009129 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009130 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009131 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009132 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009133 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009134 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009135 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009136 else if (is_debug(intr_info) &&
9137 vcpu->guest_debug &
9138 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9139 return false;
9140 else if (is_breakpoint(intr_info) &&
9141 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9142 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009143 return vmcs12->exception_bitmap &
9144 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9145 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009146 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009147 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009149 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009150 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009151 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009152 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009153 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009154 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009155 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009156 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009157 case EXIT_REASON_HLT:
9158 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9159 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009160 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009161 case EXIT_REASON_INVLPG:
9162 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9163 case EXIT_REASON_RDPMC:
9164 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009165 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009166 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009167 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009168 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009169 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009170 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9171 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9172 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9173 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9174 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9175 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009176 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009177 /*
9178 * VMX instructions trap unconditionally. This allows L1 to
9179 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9180 */
Joe Perches1d804d02015-03-30 16:46:09 -07009181 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009182 case EXIT_REASON_CR_ACCESS:
9183 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9184 case EXIT_REASON_DR_ACCESS:
9185 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9186 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009187 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009188 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9189 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009190 case EXIT_REASON_MSR_READ:
9191 case EXIT_REASON_MSR_WRITE:
9192 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9193 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009194 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009195 case EXIT_REASON_MWAIT_INSTRUCTION:
9196 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009197 case EXIT_REASON_MONITOR_TRAP_FLAG:
9198 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009199 case EXIT_REASON_MONITOR_INSTRUCTION:
9200 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9201 case EXIT_REASON_PAUSE_INSTRUCTION:
9202 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9203 nested_cpu_has2(vmcs12,
9204 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9205 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009206 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009207 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009208 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009209 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009210 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009211 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009212 /*
9213 * The controls for "virtualize APIC accesses," "APIC-
9214 * register virtualization," and "virtual-interrupt
9215 * delivery" only come from vmcs12.
9216 */
Joe Perches1d804d02015-03-30 16:46:09 -07009217 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009218 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009219 /*
9220 * L0 always deals with the EPT violation. If nested EPT is
9221 * used, and the nested mmu code discovers that the address is
9222 * missing in the guest EPT table (EPT12), the EPT violation
9223 * will be injected with nested_ept_inject_page_fault()
9224 */
Joe Perches1d804d02015-03-30 16:46:09 -07009225 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009226 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009227 /*
9228 * L2 never uses directly L1's EPT, but rather L0's own EPT
9229 * table (shadow on EPT) or a merged EPT table that L0 built
9230 * (EPT on EPT). So any problems with the structure of the
9231 * table is L0's fault.
9232 */
Joe Perches1d804d02015-03-30 16:46:09 -07009233 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009234 case EXIT_REASON_INVPCID:
9235 return
9236 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9237 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009238 case EXIT_REASON_WBINVD:
9239 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9240 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009241 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009242 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9243 /*
9244 * This should never happen, since it is not possible to
9245 * set XSS to a non-zero value---neither in L1 nor in L2.
9246 * If if it were, XSS would have to be checked against
9247 * the XSS exit bitmap in vmcs12.
9248 */
9249 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009250 case EXIT_REASON_PREEMPTION_TIMER:
9251 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009252 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009253 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009254 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009255 case EXIT_REASON_VMFUNC:
9256 /* VM functions are emulated through L2->L0 vmexits. */
9257 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009258 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009259 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009260 }
9261}
9262
Paolo Bonzini7313c692017-07-27 10:31:25 +02009263static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9264{
9265 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9266
9267 /*
9268 * At this point, the exit interruption info in exit_intr_info
9269 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9270 * we need to query the in-kernel LAPIC.
9271 */
9272 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9273 if ((exit_intr_info &
9274 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9275 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9276 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9277 vmcs12->vm_exit_intr_error_code =
9278 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9279 }
9280
9281 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9282 vmcs_readl(EXIT_QUALIFICATION));
9283 return 1;
9284}
9285
Avi Kivity586f9602010-11-18 13:09:54 +02009286static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9287{
9288 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9289 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9290}
9291
Kai Huanga3eaa862015-11-04 13:46:05 +08009292static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009293{
Kai Huanga3eaa862015-11-04 13:46:05 +08009294 if (vmx->pml_pg) {
9295 __free_page(vmx->pml_pg);
9296 vmx->pml_pg = NULL;
9297 }
Kai Huang843e4332015-01-28 10:54:28 +08009298}
9299
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009300static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009301{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009302 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009303 u64 *pml_buf;
9304 u16 pml_idx;
9305
9306 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9307
9308 /* Do nothing if PML buffer is empty */
9309 if (pml_idx == (PML_ENTITY_NUM - 1))
9310 return;
9311
9312 /* PML index always points to next available PML buffer entity */
9313 if (pml_idx >= PML_ENTITY_NUM)
9314 pml_idx = 0;
9315 else
9316 pml_idx++;
9317
9318 pml_buf = page_address(vmx->pml_pg);
9319 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9320 u64 gpa;
9321
9322 gpa = pml_buf[pml_idx];
9323 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009324 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009325 }
9326
9327 /* reset PML index */
9328 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9329}
9330
9331/*
9332 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9333 * Called before reporting dirty_bitmap to userspace.
9334 */
9335static void kvm_flush_pml_buffers(struct kvm *kvm)
9336{
9337 int i;
9338 struct kvm_vcpu *vcpu;
9339 /*
9340 * We only need to kick vcpu out of guest mode here, as PML buffer
9341 * is flushed at beginning of all VMEXITs, and it's obvious that only
9342 * vcpus running in guest are possible to have unflushed GPAs in PML
9343 * buffer.
9344 */
9345 kvm_for_each_vcpu(i, vcpu, kvm)
9346 kvm_vcpu_kick(vcpu);
9347}
9348
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009349static void vmx_dump_sel(char *name, uint32_t sel)
9350{
9351 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009352 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009353 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9354 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9355 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9356}
9357
9358static void vmx_dump_dtsel(char *name, uint32_t limit)
9359{
9360 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9361 name, vmcs_read32(limit),
9362 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9363}
9364
9365static void dump_vmcs(void)
9366{
9367 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9368 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9369 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9370 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9371 u32 secondary_exec_control = 0;
9372 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009373 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009374 int i, n;
9375
9376 if (cpu_has_secondary_exec_ctrls())
9377 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9378
9379 pr_err("*** Guest State ***\n");
9380 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9381 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9382 vmcs_readl(CR0_GUEST_HOST_MASK));
9383 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9384 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9385 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9386 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9387 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9388 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009389 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9390 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9391 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9392 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009393 }
9394 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9395 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9396 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9397 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9398 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9399 vmcs_readl(GUEST_SYSENTER_ESP),
9400 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9401 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9402 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9403 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9404 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9405 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9406 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9407 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9408 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9409 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9410 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9411 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9412 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009413 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9414 efer, vmcs_read64(GUEST_IA32_PAT));
9415 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9416 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009417 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009418 if (cpu_has_load_perf_global_ctrl &&
9419 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009420 pr_err("PerfGlobCtl = 0x%016llx\n",
9421 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009422 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009423 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009424 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9425 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9426 vmcs_read32(GUEST_ACTIVITY_STATE));
9427 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9428 pr_err("InterruptStatus = %04x\n",
9429 vmcs_read16(GUEST_INTR_STATUS));
9430
9431 pr_err("*** Host State ***\n");
9432 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9433 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9434 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9435 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9436 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9437 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9438 vmcs_read16(HOST_TR_SELECTOR));
9439 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9440 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9441 vmcs_readl(HOST_TR_BASE));
9442 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9443 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9444 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9445 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9446 vmcs_readl(HOST_CR4));
9447 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9448 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9449 vmcs_read32(HOST_IA32_SYSENTER_CS),
9450 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9451 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009452 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9453 vmcs_read64(HOST_IA32_EFER),
9454 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009455 if (cpu_has_load_perf_global_ctrl &&
9456 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009457 pr_err("PerfGlobCtl = 0x%016llx\n",
9458 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009459
9460 pr_err("*** Control State ***\n");
9461 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9462 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9463 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9464 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9465 vmcs_read32(EXCEPTION_BITMAP),
9466 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9467 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9468 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9469 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9470 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9471 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9472 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9473 vmcs_read32(VM_EXIT_INTR_INFO),
9474 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9475 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9476 pr_err(" reason=%08x qualification=%016lx\n",
9477 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9478 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9479 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9480 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009481 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009482 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009483 pr_err("TSC Multiplier = 0x%016llx\n",
9484 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009485 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9486 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9487 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9488 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9489 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009490 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009491 n = vmcs_read32(CR3_TARGET_COUNT);
9492 for (i = 0; i + 1 < n; i += 4)
9493 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9494 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9495 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9496 if (i < n)
9497 pr_err("CR3 target%u=%016lx\n",
9498 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9499 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9500 pr_err("PLE Gap=%08x Window=%08x\n",
9501 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9502 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9503 pr_err("Virtual processor ID = 0x%04x\n",
9504 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9505}
9506
Avi Kivity6aa8b732006-12-10 02:21:36 -08009507/*
9508 * The guest has exited. See if we can fix it or if we need userspace
9509 * assistance.
9510 */
Avi Kivity851ba692009-08-24 11:10:17 +03009511static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009512{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009513 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009514 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009515 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009516
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009517 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9518
Kai Huang843e4332015-01-28 10:54:28 +08009519 /*
9520 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9521 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9522 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9523 * mode as if vcpus is in root mode, the PML buffer must has been
9524 * flushed already.
9525 */
9526 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009527 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009528
Mohammed Gamal80ced182009-09-01 12:48:18 +02009529 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009530 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009531 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009532
Paolo Bonzini7313c692017-07-27 10:31:25 +02009533 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9534 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009535
Mohammed Gamal51207022010-05-31 22:40:54 +03009536 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009537 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009538 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9539 vcpu->run->fail_entry.hardware_entry_failure_reason
9540 = exit_reason;
9541 return 0;
9542 }
9543
Avi Kivity29bd8a72007-09-10 17:27:03 +03009544 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009545 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9546 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009547 = vmcs_read32(VM_INSTRUCTION_ERROR);
9548 return 0;
9549 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009550
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009551 /*
9552 * Note:
9553 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9554 * delivery event since it indicates guest is accessing MMIO.
9555 * The vm-exit can be triggered again after return to guest that
9556 * will cause infinite loop.
9557 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009558 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009559 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009560 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009561 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009562 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9563 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9564 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009565 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009566 vcpu->run->internal.data[0] = vectoring_info;
9567 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009568 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9569 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9570 vcpu->run->internal.ndata++;
9571 vcpu->run->internal.data[3] =
9572 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9573 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009574 return 0;
9575 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009576
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009577 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009578 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9579 if (vmx_interrupt_allowed(vcpu)) {
9580 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9581 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9582 vcpu->arch.nmi_pending) {
9583 /*
9584 * This CPU don't support us in finding the end of an
9585 * NMI-blocked window if the guest runs with IRQs
9586 * disabled. So we pull the trigger after 1 s of
9587 * futile waiting, but inform the user about this.
9588 */
9589 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9590 "state on VCPU %d after 1 s timeout\n",
9591 __func__, vcpu->vcpu_id);
9592 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9593 }
9594 }
9595
Avi Kivity6aa8b732006-12-10 02:21:36 -08009596 if (exit_reason < kvm_vmx_max_exit_handlers
9597 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009598 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009599 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009600 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9601 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009602 kvm_queue_exception(vcpu, UD_VECTOR);
9603 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009604 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009605}
9606
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009607/*
9608 * Software based L1D cache flush which is used when microcode providing
9609 * the cache control MSR is not loaded.
9610 *
9611 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9612 * flush it is required to read in 64 KiB because the replacement algorithm
9613 * is not exactly LRU. This could be sized at runtime via topology
9614 * information but as all relevant affected CPUs have 32KiB L1D cache size
9615 * there is no point in doing so.
9616 */
9617#define L1D_CACHE_ORDER 4
9618static void *vmx_l1d_flush_pages;
9619
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009620static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009621{
9622 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009623 bool always;
9624
9625 /*
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009626 * This code is only executed when:
9627 * - the flush mode is 'cond'
9628 * - the flush mode is 'always' and the flush MSR is not
9629 * available
9630 *
9631 * If the CPU has the flush MSR then clear the flush bit because
9632 * 'always' mode is handled via the MSR save list.
9633 *
9634 * If the MSR is not avaibable then act depending on the mitigation
9635 * mode: If 'flush always', keep the flush bit set, otherwise clear
9636 * it.
9637 *
9638 * The flush bit gets set again either from vcpu_run() or from one
9639 * of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009640 */
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009641 if (static_cpu_has(X86_FEATURE_FLUSH_L1D))
9642 always = false;
9643 else
9644 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
9645
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009646 vcpu->arch.l1tf_flush_l1d = always;
9647
9648 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009649
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009650 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9651 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9652 return;
9653 }
9654
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009655 asm volatile(
9656 /* First ensure the pages are in the TLB */
9657 "xorl %%eax, %%eax\n"
9658 ".Lpopulate_tlb:\n\t"
9659 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9660 "addl $4096, %%eax\n\t"
9661 "cmpl %%eax, %[size]\n\t"
9662 "jne .Lpopulate_tlb\n\t"
9663 "xorl %%eax, %%eax\n\t"
9664 "cpuid\n\t"
9665 /* Now fill the cache */
9666 "xorl %%eax, %%eax\n"
9667 ".Lfill_cache:\n"
9668 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9669 "addl $64, %%eax\n\t"
9670 "cmpl %%eax, %[size]\n\t"
9671 "jne .Lfill_cache\n\t"
9672 "lfence\n"
9673 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9674 [size] "r" (size)
9675 : "eax", "ebx", "ecx", "edx");
9676}
9677
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009678static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009679{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009680 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9681
9682 if (is_guest_mode(vcpu) &&
9683 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9684 return;
9685
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009686 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009687 vmcs_write32(TPR_THRESHOLD, 0);
9688 return;
9689 }
9690
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009691 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009692}
9693
Jim Mattson8d860bb2018-05-09 16:56:05 -04009694static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009695{
9696 u32 sec_exec_control;
9697
Jim Mattson8d860bb2018-05-09 16:56:05 -04009698 if (!lapic_in_kernel(vcpu))
9699 return;
9700
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009701 /* Postpone execution until vmcs01 is the current VMCS. */
9702 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009703 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009704 return;
9705 }
9706
Paolo Bonzini35754c92015-07-29 12:05:37 +02009707 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009708 return;
9709
9710 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009711 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9712 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009713
Jim Mattson8d860bb2018-05-09 16:56:05 -04009714 switch (kvm_get_apic_mode(vcpu)) {
9715 case LAPIC_MODE_INVALID:
9716 WARN_ONCE(true, "Invalid local APIC state");
9717 case LAPIC_MODE_DISABLED:
9718 break;
9719 case LAPIC_MODE_XAPIC:
9720 if (flexpriority_enabled) {
9721 sec_exec_control |=
9722 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9723 vmx_flush_tlb(vcpu, true);
9724 }
9725 break;
9726 case LAPIC_MODE_X2APIC:
9727 if (cpu_has_vmx_virtualize_x2apic_mode())
9728 sec_exec_control |=
9729 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9730 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009731 }
9732 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9733
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009734 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009735}
9736
Tang Chen38b99172014-09-24 15:57:54 +08009737static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9738{
Jim Mattsonab5df312018-05-09 17:02:03 -04009739 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009740 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009741 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009742 }
Tang Chen38b99172014-09-24 15:57:54 +08009743}
9744
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009745static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009746{
9747 u16 status;
9748 u8 old;
9749
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009750 if (max_isr == -1)
9751 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009752
9753 status = vmcs_read16(GUEST_INTR_STATUS);
9754 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009755 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009756 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009757 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009758 vmcs_write16(GUEST_INTR_STATUS, status);
9759 }
9760}
9761
9762static void vmx_set_rvi(int vector)
9763{
9764 u16 status;
9765 u8 old;
9766
Wei Wang4114c272014-11-05 10:53:43 +08009767 if (vector == -1)
9768 vector = 0;
9769
Yang Zhangc7c9c562013-01-25 10:18:51 +08009770 status = vmcs_read16(GUEST_INTR_STATUS);
9771 old = (u8)status & 0xff;
9772 if ((u8)vector != old) {
9773 status &= ~0xff;
9774 status |= (u8)vector;
9775 vmcs_write16(GUEST_INTR_STATUS, status);
9776 }
9777}
9778
9779static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9780{
Liran Alon851c1a182017-12-24 18:12:56 +02009781 /*
9782 * When running L2, updating RVI is only relevant when
9783 * vmcs12 virtual-interrupt-delivery enabled.
9784 * However, it can be enabled only when L1 also
9785 * intercepts external-interrupts and in that case
9786 * we should not update vmcs02 RVI but instead intercept
9787 * interrupt. Therefore, do nothing when running L2.
9788 */
9789 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009790 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009791}
9792
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009793static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009794{
9795 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009796 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009797 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009798
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009799 WARN_ON(!vcpu->arch.apicv_active);
9800 if (pi_test_on(&vmx->pi_desc)) {
9801 pi_clear_on(&vmx->pi_desc);
9802 /*
9803 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9804 * But on x86 this is just a compiler barrier anyway.
9805 */
9806 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009807 max_irr_updated =
9808 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9809
9810 /*
9811 * If we are running L2 and L1 has a new pending interrupt
9812 * which can be injected, we should re-evaluate
9813 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009814 * If L1 intercepts external-interrupts, we should
9815 * exit from L2 to L1. Otherwise, interrupt should be
9816 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009817 */
Liran Alon851c1a182017-12-24 18:12:56 +02009818 if (is_guest_mode(vcpu) && max_irr_updated) {
9819 if (nested_exit_on_intr(vcpu))
9820 kvm_vcpu_exiting_guest_mode(vcpu);
9821 else
9822 kvm_make_request(KVM_REQ_EVENT, vcpu);
9823 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009824 } else {
9825 max_irr = kvm_lapic_find_highest_irr(vcpu);
9826 }
9827 vmx_hwapic_irr_update(vcpu, max_irr);
9828 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009829}
9830
Andrey Smetanin63086302015-11-10 15:36:32 +03009831static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009832{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009833 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009834 return;
9835
Yang Zhangc7c9c562013-01-25 10:18:51 +08009836 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9837 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9838 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9839 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9840}
9841
Paolo Bonzini967235d2016-12-19 14:03:45 +01009842static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9843{
9844 struct vcpu_vmx *vmx = to_vmx(vcpu);
9845
9846 pi_clear_on(&vmx->pi_desc);
9847 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9848}
9849
Avi Kivity51aa01d2010-07-20 14:31:20 +03009850static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009851{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009852 u32 exit_intr_info = 0;
9853 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009854
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009855 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9856 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009857 return;
9858
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009859 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9860 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9861 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009862
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009863 /* if exit due to PF check for async PF */
9864 if (is_page_fault(exit_intr_info))
9865 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9866
Andi Kleena0861c02009-06-08 17:37:09 +08009867 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009868 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9869 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009870 kvm_machine_check();
9871
Gleb Natapov20f65982009-05-11 13:35:55 +03009872 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009873 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009874 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009875 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009876 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009877 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009878}
Gleb Natapov20f65982009-05-11 13:35:55 +03009879
Yang Zhanga547c6d2013-04-11 19:25:10 +08009880static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9881{
9882 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9883
Yang Zhanga547c6d2013-04-11 19:25:10 +08009884 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9885 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9886 unsigned int vector;
9887 unsigned long entry;
9888 gate_desc *desc;
9889 struct vcpu_vmx *vmx = to_vmx(vcpu);
9890#ifdef CONFIG_X86_64
9891 unsigned long tmp;
9892#endif
9893
9894 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9895 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009896 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009897 asm volatile(
9898#ifdef CONFIG_X86_64
9899 "mov %%" _ASM_SP ", %[sp]\n\t"
9900 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9901 "push $%c[ss]\n\t"
9902 "push %[sp]\n\t"
9903#endif
9904 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009905 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009906 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009907 :
9908#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009909 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009910#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009911 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009912 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009913 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009914 [ss]"i"(__KERNEL_DS),
9915 [cs]"i"(__KERNEL_CS)
9916 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009917 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009918 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009919}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009920STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009921
Tom Lendackybc226f02018-05-10 22:06:39 +02009922static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009923{
Tom Lendackybc226f02018-05-10 22:06:39 +02009924 switch (index) {
9925 case MSR_IA32_SMBASE:
9926 /*
9927 * We cannot do SMM unless we can run the guest in big
9928 * real mode.
9929 */
9930 return enable_unrestricted_guest || emulate_invalid_guest_state;
9931 case MSR_AMD64_VIRT_SPEC_CTRL:
9932 /* This is AMD only. */
9933 return false;
9934 default:
9935 return true;
9936 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009937}
9938
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009939static bool vmx_mpx_supported(void)
9940{
9941 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9942 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9943}
9944
Wanpeng Li55412b22014-12-02 19:21:30 +08009945static bool vmx_xsaves_supported(void)
9946{
9947 return vmcs_config.cpu_based_2nd_exec_ctrl &
9948 SECONDARY_EXEC_XSAVES;
9949}
9950
Avi Kivity51aa01d2010-07-20 14:31:20 +03009951static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9952{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009953 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009954 bool unblock_nmi;
9955 u8 vector;
9956 bool idtv_info_valid;
9957
9958 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009959
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009960 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009961 if (vmx->loaded_vmcs->nmi_known_unmasked)
9962 return;
9963 /*
9964 * Can't use vmx->exit_intr_info since we're not sure what
9965 * the exit reason is.
9966 */
9967 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9968 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9969 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9970 /*
9971 * SDM 3: 27.7.1.2 (September 2008)
9972 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9973 * a guest IRET fault.
9974 * SDM 3: 23.2.2 (September 2008)
9975 * Bit 12 is undefined in any of the following cases:
9976 * If the VM exit sets the valid bit in the IDT-vectoring
9977 * information field.
9978 * If the VM exit is due to a double fault.
9979 */
9980 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9981 vector != DF_VECTOR && !idtv_info_valid)
9982 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9983 GUEST_INTR_STATE_NMI);
9984 else
9985 vmx->loaded_vmcs->nmi_known_unmasked =
9986 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9987 & GUEST_INTR_STATE_NMI);
9988 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9989 vmx->loaded_vmcs->vnmi_blocked_time +=
9990 ktime_to_ns(ktime_sub(ktime_get(),
9991 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009992}
9993
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009994static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009995 u32 idt_vectoring_info,
9996 int instr_len_field,
9997 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009998{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009999 u8 vector;
10000 int type;
10001 bool idtv_info_valid;
10002
10003 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010004
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010005 vcpu->arch.nmi_injected = false;
10006 kvm_clear_exception_queue(vcpu);
10007 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010008
10009 if (!idtv_info_valid)
10010 return;
10011
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010012 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010013
Avi Kivity668f6122008-07-02 09:28:55 +030010014 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10015 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010016
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010017 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010018 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010019 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010020 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010021 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010022 * Clear bit "block by NMI" before VM entry if a NMI
10023 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010024 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010025 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010026 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010027 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010028 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010029 /* fall through */
10030 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010031 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010032 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010033 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010034 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010035 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010036 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010037 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010038 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010039 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010040 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010041 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010042 break;
10043 default:
10044 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010045 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010046}
10047
Avi Kivity83422e12010-07-20 14:43:23 +030010048static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10049{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010050 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010051 VM_EXIT_INSTRUCTION_LEN,
10052 IDT_VECTORING_ERROR_CODE);
10053}
10054
Avi Kivityb463a6f2010-07-20 15:06:17 +030010055static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10056{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010057 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010058 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10059 VM_ENTRY_INSTRUCTION_LEN,
10060 VM_ENTRY_EXCEPTION_ERROR_CODE);
10061
10062 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10063}
10064
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010065static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10066{
10067 int i, nr_msrs;
10068 struct perf_guest_switch_msr *msrs;
10069
10070 msrs = perf_guest_get_msrs(&nr_msrs);
10071
10072 if (!msrs)
10073 return;
10074
10075 for (i = 0; i < nr_msrs; i++)
10076 if (msrs[i].host == msrs[i].guest)
10077 clear_atomic_switch_msr(vmx, msrs[i].msr);
10078 else
10079 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010080 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010081}
10082
Jiang Biao33365e72016-11-03 15:03:37 +080010083static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010084{
10085 struct vcpu_vmx *vmx = to_vmx(vcpu);
10086 u64 tscl;
10087 u32 delta_tsc;
10088
10089 if (vmx->hv_deadline_tsc == -1)
10090 return;
10091
10092 tscl = rdtsc();
10093 if (vmx->hv_deadline_tsc > tscl)
10094 /* sure to be 32 bit only because checked on set_hv_timer */
10095 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10096 cpu_preemption_timer_multi);
10097 else
10098 delta_tsc = 0;
10099
10100 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10101}
10102
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010103static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010104{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010105 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010106 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010107
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010108 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010109 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010110 vmx->loaded_vmcs->soft_vnmi_blocked))
10111 vmx->loaded_vmcs->entry_time = ktime_get();
10112
Avi Kivity104f2262010-11-18 13:12:52 +020010113 /* Don't enter VMX if guest state is invalid, let the exit handler
10114 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010115 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010116 return;
10117
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010118 if (vmx->ple_window_dirty) {
10119 vmx->ple_window_dirty = false;
10120 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10121 }
10122
Abel Gordon012f83c2013-04-18 14:39:25 +030010123 if (vmx->nested.sync_shadow_vmcs) {
10124 copy_vmcs12_to_shadow(vmx);
10125 vmx->nested.sync_shadow_vmcs = false;
10126 }
10127
Avi Kivity104f2262010-11-18 13:12:52 +020010128 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10129 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10130 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10131 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10132
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010133 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010134 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010135 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010136 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010137 }
10138
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010139 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010140 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010141 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010142 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010143 }
10144
Avi Kivity104f2262010-11-18 13:12:52 +020010145 /* When single-stepping over STI and MOV SS, we must clear the
10146 * corresponding interruptibility bits in the guest state. Otherwise
10147 * vmentry fails as it then expects bit 14 (BS) in pending debug
10148 * exceptions being set, but that's not correct for the guest debugging
10149 * case. */
10150 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10151 vmx_set_interrupt_shadow(vcpu, 0);
10152
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010153 if (static_cpu_has(X86_FEATURE_PKU) &&
10154 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10155 vcpu->arch.pkru != vmx->host_pkru)
10156 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010157
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010158 atomic_switch_perf_msrs(vmx);
10159
Yunhong Jiang64672c92016-06-13 14:19:59 -070010160 vmx_arm_hv_timer(vcpu);
10161
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010162 /*
10163 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10164 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10165 * is no need to worry about the conditional branch over the wrmsr
10166 * being speculatively taken.
10167 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010168 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010169
Nadav Har'Eld462b812011-05-24 15:26:10 +030010170 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010171
10172 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10173 (unsigned long)&current_evmcs->host_rsp : 0;
10174
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010175 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10176 if (vcpu->arch.l1tf_flush_l1d)
10177 vmx_l1d_flush(vcpu);
10178 }
10179
Avi Kivity104f2262010-11-18 13:12:52 +020010180 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010181 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010182 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10183 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10184 "push %%" _ASM_CX " \n\t"
10185 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010186 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010187 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010188 /* Avoid VMWRITE when Enlightened VMCS is in use */
10189 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10190 "jz 2f \n\t"
10191 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10192 "jmp 1f \n\t"
10193 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010194 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010195 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010196 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010197 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10198 "mov %%cr2, %%" _ASM_DX " \n\t"
10199 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010200 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010201 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010202 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010203 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010204 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010205 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010206 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10207 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10208 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10209 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10210 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10211 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010212#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010213 "mov %c[r8](%0), %%r8 \n\t"
10214 "mov %c[r9](%0), %%r9 \n\t"
10215 "mov %c[r10](%0), %%r10 \n\t"
10216 "mov %c[r11](%0), %%r11 \n\t"
10217 "mov %c[r12](%0), %%r12 \n\t"
10218 "mov %c[r13](%0), %%r13 \n\t"
10219 "mov %c[r14](%0), %%r14 \n\t"
10220 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010221#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010222 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010223
Avi Kivity6aa8b732006-12-10 02:21:36 -080010224 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010225 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010226 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010227 "jmp 2f \n\t"
10228 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10229 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010230 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010231 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010232 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010233 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010234 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10235 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10236 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10237 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10238 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10239 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10240 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010241#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010242 "mov %%r8, %c[r8](%0) \n\t"
10243 "mov %%r9, %c[r9](%0) \n\t"
10244 "mov %%r10, %c[r10](%0) \n\t"
10245 "mov %%r11, %c[r11](%0) \n\t"
10246 "mov %%r12, %c[r12](%0) \n\t"
10247 "mov %%r13, %c[r13](%0) \n\t"
10248 "mov %%r14, %c[r14](%0) \n\t"
10249 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010250 "xor %%r8d, %%r8d \n\t"
10251 "xor %%r9d, %%r9d \n\t"
10252 "xor %%r10d, %%r10d \n\t"
10253 "xor %%r11d, %%r11d \n\t"
10254 "xor %%r12d, %%r12d \n\t"
10255 "xor %%r13d, %%r13d \n\t"
10256 "xor %%r14d, %%r14d \n\t"
10257 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010258#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010259 "mov %%cr2, %%" _ASM_AX " \n\t"
10260 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010261
Jim Mattson0cb5b302018-01-03 14:31:38 -080010262 "xor %%eax, %%eax \n\t"
10263 "xor %%ebx, %%ebx \n\t"
10264 "xor %%esi, %%esi \n\t"
10265 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010266 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010267 ".pushsection .rodata \n\t"
10268 ".global vmx_return \n\t"
10269 "vmx_return: " _ASM_PTR " 2b \n\t"
10270 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010271 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010272 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010273 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010274 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010275 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10276 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10277 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10278 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10279 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10280 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10281 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010282#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010283 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10284 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10285 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10286 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10287 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10288 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10289 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10290 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010291#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010292 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10293 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010294 : "cc", "memory"
10295#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010296 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010297 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010298#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010299 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010300#endif
10301 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010302
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010303 /*
10304 * We do not use IBRS in the kernel. If this vCPU has used the
10305 * SPEC_CTRL MSR it may have left it on; save the value and
10306 * turn it off. This is much more efficient than blindly adding
10307 * it to the atomic save/restore list. Especially as the former
10308 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10309 *
10310 * For non-nested case:
10311 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10312 * save it.
10313 *
10314 * For nested case:
10315 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10316 * save it.
10317 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010318 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010319 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010320
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010321 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010322
David Woodhouse117cc7a2018-01-12 11:11:27 +000010323 /* Eliminate branch target predictions from guest mode */
10324 vmexit_fill_RSB();
10325
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010326 /* All fields are clean at this point */
10327 if (static_branch_unlikely(&enable_evmcs))
10328 current_evmcs->hv_clean_fields |=
10329 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10330
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010331 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010332 if (vmx->host_debugctlmsr)
10333 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010334
Avi Kivityaa67f602012-08-01 16:48:03 +030010335#ifndef CONFIG_X86_64
10336 /*
10337 * The sysexit path does not restore ds/es, so we must set them to
10338 * a reasonable value ourselves.
10339 *
10340 * We can't defer this to vmx_load_host_state() since that function
10341 * may be executed in interrupt context, which saves and restore segments
10342 * around it, nullifying its effect.
10343 */
10344 loadsegment(ds, __USER_DS);
10345 loadsegment(es, __USER_DS);
10346#endif
10347
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010348 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010349 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010350 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010351 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010352 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010353 vcpu->arch.regs_dirty = 0;
10354
Gleb Natapove0b890d2013-09-25 12:51:33 +030010355 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010356 * eager fpu is enabled if PKEY is supported and CR4 is switched
10357 * back on host, so it is safe to read guest PKRU from current
10358 * XSAVE.
10359 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010360 if (static_cpu_has(X86_FEATURE_PKU) &&
10361 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10362 vcpu->arch.pkru = __read_pkru();
10363 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010364 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010365 }
10366
Gleb Natapove0b890d2013-09-25 12:51:33 +030010367 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010368 vmx->idt_vectoring_info = 0;
10369
10370 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10371 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10372 return;
10373
10374 vmx->loaded_vmcs->launched = 1;
10375 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010376
Avi Kivity51aa01d2010-07-20 14:31:20 +030010377 vmx_complete_atomic_exit(vmx);
10378 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010379 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010380}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010381STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010382
Sean Christopherson434a1e92018-03-20 12:17:18 -070010383static struct kvm *vmx_vm_alloc(void)
10384{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010385 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010386 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010387}
10388
10389static void vmx_vm_free(struct kvm *kvm)
10390{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010391 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010392}
10393
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010394static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010395{
10396 struct vcpu_vmx *vmx = to_vmx(vcpu);
10397 int cpu;
10398
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010399 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010400 return;
10401
10402 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010403 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010404 vmx_vcpu_put(vcpu);
10405 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010406 put_cpu();
10407}
10408
Jim Mattson2f1fe812016-07-08 15:36:06 -070010409/*
10410 * Ensure that the current vmcs of the logical processor is the
10411 * vmcs01 of the vcpu before calling free_nested().
10412 */
10413static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10414{
10415 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010416
Christoffer Dallec7660c2017-12-04 21:35:23 +010010417 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010418 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010419 free_nested(vmx);
10420 vcpu_put(vcpu);
10421}
10422
Avi Kivity6aa8b732006-12-10 02:21:36 -080010423static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10424{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010425 struct vcpu_vmx *vmx = to_vmx(vcpu);
10426
Kai Huang843e4332015-01-28 10:54:28 +080010427 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010428 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010429 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010430 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010431 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010432 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010433 kfree(vmx->guest_msrs);
10434 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010435 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010436}
10437
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010438static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010439{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010440 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010441 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010442 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010443 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010444
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010445 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010446 return ERR_PTR(-ENOMEM);
10447
Wanpeng Li991e7a02015-09-16 17:30:05 +080010448 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010449
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010450 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10451 if (err)
10452 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010453
Peter Feiner4e595162016-07-07 14:49:58 -070010454 err = -ENOMEM;
10455
10456 /*
10457 * If PML is turned on, failure on enabling PML just results in failure
10458 * of creating the vcpu, therefore we can simplify PML logic (by
10459 * avoiding dealing with cases, such as enabling PML partially on vcpus
10460 * for the guest, etc.
10461 */
10462 if (enable_pml) {
10463 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10464 if (!vmx->pml_pg)
10465 goto uninit_vcpu;
10466 }
10467
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010468 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010469 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10470 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010471
Peter Feiner4e595162016-07-07 14:49:58 -070010472 if (!vmx->guest_msrs)
10473 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010474
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010475 err = alloc_loaded_vmcs(&vmx->vmcs01);
10476 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010477 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010478
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010479 msr_bitmap = vmx->vmcs01.msr_bitmap;
10480 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10481 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10482 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10483 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10484 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10485 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10486 vmx->msr_bitmap_mode = 0;
10487
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010488 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010489 cpu = get_cpu();
10490 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010491 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010492 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010493 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010494 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010495 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010496 err = alloc_apic_access_page(kvm);
10497 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010498 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010499 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010500
Sean Christophersone90008d2018-03-05 12:04:37 -080010501 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010502 err = init_rmode_identity_map(kvm);
10503 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010504 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010505 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010506
Wanpeng Li5c614b32015-10-13 09:18:36 -070010507 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010508 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10509 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010510 vmx->nested.vpid02 = allocate_vpid();
10511 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010512
Wincy Van705699a2015-02-03 23:58:17 +080010513 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010514 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010515
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010516 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10517
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010518 /*
10519 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10520 * or POSTED_INTR_WAKEUP_VECTOR.
10521 */
10522 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10523 vmx->pi_desc.sn = 1;
10524
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010525 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010526
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010527free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010528 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010529 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010530free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010531 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010532free_pml:
10533 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010534uninit_vcpu:
10535 kvm_vcpu_uninit(&vmx->vcpu);
10536free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010537 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010538 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010539 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010540}
10541
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010542#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10543
Wanpeng Lib31c1142018-03-12 04:53:04 -070010544static int vmx_vm_init(struct kvm *kvm)
10545{
10546 if (!ple_gap)
10547 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010548
10549 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10550 if (nosmt) {
10551 pr_err(L1TF_MSG);
10552 return -EOPNOTSUPP;
10553 }
10554 pr_warn(L1TF_MSG);
10555 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010556 return 0;
10557}
10558
Yang, Sheng002c7f72007-07-31 14:23:01 +030010559static void __init vmx_check_processor_compat(void *rtn)
10560{
10561 struct vmcs_config vmcs_conf;
10562
10563 *(int *)rtn = 0;
10564 if (setup_vmcs_config(&vmcs_conf) < 0)
10565 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010566 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010567 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10568 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10569 smp_processor_id());
10570 *(int *)rtn = -EIO;
10571 }
10572}
10573
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010574static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010575{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010576 u8 cache;
10577 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010578
Sheng Yang522c68c2009-04-27 20:35:43 +080010579 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010580 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010581 * 2. EPT with VT-d:
10582 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010583 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010584 * b. VT-d with snooping control feature: snooping control feature of
10585 * VT-d engine can guarantee the cache correctness. Just set it
10586 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010587 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010588 * consistent with host MTRR
10589 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010590 if (is_mmio) {
10591 cache = MTRR_TYPE_UNCACHABLE;
10592 goto exit;
10593 }
10594
10595 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010596 ipat = VMX_EPT_IPAT_BIT;
10597 cache = MTRR_TYPE_WRBACK;
10598 goto exit;
10599 }
10600
10601 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10602 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010603 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010604 cache = MTRR_TYPE_WRBACK;
10605 else
10606 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010607 goto exit;
10608 }
10609
Xiao Guangrongff536042015-06-15 16:55:22 +080010610 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010611
10612exit:
10613 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010614}
10615
Sheng Yang17cc3932010-01-05 19:02:27 +080010616static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010617{
Sheng Yang878403b2010-01-05 19:02:29 +080010618 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10619 return PT_DIRECTORY_LEVEL;
10620 else
10621 /* For shadow and EPT supported 1GB page */
10622 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010623}
10624
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010625static void vmcs_set_secondary_exec_control(u32 new_ctl)
10626{
10627 /*
10628 * These bits in the secondary execution controls field
10629 * are dynamic, the others are mostly based on the hypervisor
10630 * architecture and the guest's CPUID. Do not touch the
10631 * dynamic bits.
10632 */
10633 u32 mask =
10634 SECONDARY_EXEC_SHADOW_VMCS |
10635 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010636 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10637 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010638
10639 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10640
10641 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10642 (new_ctl & ~mask) | (cur_ctl & mask));
10643}
10644
David Matlack8322ebb2016-11-29 18:14:09 -080010645/*
10646 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10647 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10648 */
10649static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10650{
10651 struct vcpu_vmx *vmx = to_vmx(vcpu);
10652 struct kvm_cpuid_entry2 *entry;
10653
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010654 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10655 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010656
10657#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10658 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010659 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010660} while (0)
10661
10662 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10663 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10664 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10665 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10666 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10667 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10668 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10669 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10670 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10671 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10672 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10673 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10674 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10675 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10676 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10677
10678 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10679 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10680 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10681 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10682 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010683 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010684
10685#undef cr4_fixed1_update
10686}
10687
Sheng Yang0e851882009-12-18 16:48:46 +080010688static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10689{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010690 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010691
Paolo Bonzini80154d72017-08-24 13:55:35 +020010692 if (cpu_has_secondary_exec_ctrls()) {
10693 vmx_compute_secondary_exec_control(vmx);
10694 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010695 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010696
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010697 if (nested_vmx_allowed(vcpu))
10698 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10699 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10700 else
10701 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10702 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010703
10704 if (nested_vmx_allowed(vcpu))
10705 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010706}
10707
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010708static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10709{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010710 if (func == 1 && nested)
10711 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010712}
10713
Yang Zhang25d92082013-08-06 12:00:32 +030010714static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10715 struct x86_exception *fault)
10716{
Jan Kiszka533558b2014-01-04 18:47:20 +010010717 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010718 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010719 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010720 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010721
Bandan Dasc5f983f2017-05-05 15:25:14 -040010722 if (vmx->nested.pml_full) {
10723 exit_reason = EXIT_REASON_PML_FULL;
10724 vmx->nested.pml_full = false;
10725 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10726 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010727 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010728 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010729 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010730
10731 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010732 vmcs12->guest_physical_address = fault->address;
10733}
10734
Peter Feiner995f00a2017-06-30 17:26:32 -070010735static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10736{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010737 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010738}
10739
Nadav Har'El155a97a2013-08-05 11:07:16 +030010740/* Callbacks for nested_ept_init_mmu_context: */
10741
10742static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10743{
10744 /* return the page table to be shadowed - in our case, EPT12 */
10745 return get_vmcs12(vcpu)->ept_pointer;
10746}
10747
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010748static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010749{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010750 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010751 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010752 return 1;
10753
10754 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010755 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010756 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010757 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010758 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010759 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10760 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10761 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10762
10763 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010764 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010765}
10766
10767static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10768{
10769 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10770}
10771
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010772static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10773 u16 error_code)
10774{
10775 bool inequality, bit;
10776
10777 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10778 inequality =
10779 (error_code & vmcs12->page_fault_error_code_mask) !=
10780 vmcs12->page_fault_error_code_match;
10781 return inequality ^ bit;
10782}
10783
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010784static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10785 struct x86_exception *fault)
10786{
10787 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10788
10789 WARN_ON(!is_guest_mode(vcpu));
10790
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010791 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10792 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010793 vmcs12->vm_exit_intr_error_code = fault->error_code;
10794 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10795 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10796 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10797 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010798 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010799 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010800 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010801}
10802
Paolo Bonzinic9923842017-12-13 14:16:30 +010010803static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10804 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010805
10806static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010807 struct vmcs12 *vmcs12)
10808{
10809 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010810 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010811 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010812
10813 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010814 /*
10815 * Translate L1 physical address to host physical
10816 * address for vmcs02. Keep the page pinned, so this
10817 * physical address remains valid. We keep a reference
10818 * to it so we can release it later.
10819 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010820 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010821 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010822 vmx->nested.apic_access_page = NULL;
10823 }
10824 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010825 /*
10826 * If translation failed, no matter: This feature asks
10827 * to exit when accessing the given address, and if it
10828 * can never be accessed, this feature won't do
10829 * anything anyway.
10830 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010831 if (!is_error_page(page)) {
10832 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010833 hpa = page_to_phys(vmx->nested.apic_access_page);
10834 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10835 } else {
10836 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10837 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10838 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010839 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010840
10841 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010842 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010843 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010844 vmx->nested.virtual_apic_page = NULL;
10845 }
10846 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010847
10848 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010849 * If translation failed, VM entry will fail because
10850 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10851 * Failing the vm entry is _not_ what the processor
10852 * does but it's basically the only possibility we
10853 * have. We could still enter the guest if CR8 load
10854 * exits are enabled, CR8 store exits are enabled, and
10855 * virtualize APIC access is disabled; in this case
10856 * the processor would never use the TPR shadow and we
10857 * could simply clear the bit from the execution
10858 * control. But such a configuration is useless, so
10859 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010860 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010861 if (!is_error_page(page)) {
10862 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010863 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10864 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10865 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010866 }
10867
Wincy Van705699a2015-02-03 23:58:17 +080010868 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010869 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10870 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010871 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010872 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010873 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010874 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10875 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010876 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010877 vmx->nested.pi_desc_page = page;
10878 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010879 vmx->nested.pi_desc =
10880 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10881 (unsigned long)(vmcs12->posted_intr_desc_addr &
10882 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010883 vmcs_write64(POSTED_INTR_DESC_ADDR,
10884 page_to_phys(vmx->nested.pi_desc_page) +
10885 (unsigned long)(vmcs12->posted_intr_desc_addr &
10886 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010887 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010888 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010889 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10890 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010891 else
10892 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10893 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010894}
10895
Jan Kiszkaf4124502014-03-07 20:03:13 +010010896static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10897{
10898 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10899 struct vcpu_vmx *vmx = to_vmx(vcpu);
10900
10901 if (vcpu->arch.virtual_tsc_khz == 0)
10902 return;
10903
10904 /* Make sure short timeouts reliably trigger an immediate vmexit.
10905 * hrtimer_start does not guarantee this. */
10906 if (preemption_timeout <= 1) {
10907 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10908 return;
10909 }
10910
10911 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10912 preemption_timeout *= 1000000;
10913 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10914 hrtimer_start(&vmx->nested.preemption_timer,
10915 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10916}
10917
Jim Mattson56a20512017-07-06 16:33:06 -070010918static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10919 struct vmcs12 *vmcs12)
10920{
10921 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10922 return 0;
10923
10924 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10925 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10926 return -EINVAL;
10927
10928 return 0;
10929}
10930
Wincy Van3af18d92015-02-03 23:49:31 +080010931static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10932 struct vmcs12 *vmcs12)
10933{
Wincy Van3af18d92015-02-03 23:49:31 +080010934 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10935 return 0;
10936
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010937 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010938 return -EINVAL;
10939
10940 return 0;
10941}
10942
Jim Mattson712b12d2017-08-24 13:24:47 -070010943static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10944 struct vmcs12 *vmcs12)
10945{
10946 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10947 return 0;
10948
10949 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10950 return -EINVAL;
10951
10952 return 0;
10953}
10954
Wincy Van3af18d92015-02-03 23:49:31 +080010955/*
10956 * Merge L0's and L1's MSR bitmap, return false to indicate that
10957 * we do not use the hardware.
10958 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010959static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10960 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010961{
Wincy Van82f0dd42015-02-03 23:57:18 +080010962 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010963 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010964 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010965 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010966 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010967 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010968 *
10969 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10970 * ensures that we do not accidentally generate an L02 MSR bitmap
10971 * from the L12 MSR bitmap that is too permissive.
10972 * 2. That L1 or L2s have actually used the MSR. This avoids
10973 * unnecessarily merging of the bitmap if the MSR is unused. This
10974 * works properly because we only update the L01 MSR bitmap lazily.
10975 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10976 * updated to reflect this when L1 (or its L2s) actually write to
10977 * the MSR.
10978 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010979 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10980 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010981
Paolo Bonzinic9923842017-12-13 14:16:30 +010010982 /* Nothing to do if the MSR bitmap is not in use. */
10983 if (!cpu_has_vmx_msr_bitmap() ||
10984 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10985 return false;
10986
Ashok Raj15d45072018-02-01 22:59:43 +010010987 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010988 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010989 return false;
10990
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010991 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10992 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010993 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010994
Radim Krčmářd048c092016-08-08 20:16:22 +020010995 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010996 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10997 /*
10998 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10999 * just lets the processor take the value from the virtual-APIC page;
11000 * take those 256 bits directly from the L1 bitmap.
11001 */
11002 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11003 unsigned word = msr / BITS_PER_LONG;
11004 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11005 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011006 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011007 } else {
11008 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11009 unsigned word = msr / BITS_PER_LONG;
11010 msr_bitmap_l0[word] = ~0;
11011 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11012 }
11013 }
11014
11015 nested_vmx_disable_intercept_for_msr(
11016 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011017 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011018 MSR_TYPE_W);
11019
11020 if (nested_cpu_has_vid(vmcs12)) {
11021 nested_vmx_disable_intercept_for_msr(
11022 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011023 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011024 MSR_TYPE_W);
11025 nested_vmx_disable_intercept_for_msr(
11026 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011027 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011028 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011029 }
Ashok Raj15d45072018-02-01 22:59:43 +010011030
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011031 if (spec_ctrl)
11032 nested_vmx_disable_intercept_for_msr(
11033 msr_bitmap_l1, msr_bitmap_l0,
11034 MSR_IA32_SPEC_CTRL,
11035 MSR_TYPE_R | MSR_TYPE_W);
11036
Ashok Raj15d45072018-02-01 22:59:43 +010011037 if (pred_cmd)
11038 nested_vmx_disable_intercept_for_msr(
11039 msr_bitmap_l1, msr_bitmap_l0,
11040 MSR_IA32_PRED_CMD,
11041 MSR_TYPE_W);
11042
Wincy Vanf2b93282015-02-03 23:56:03 +080011043 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011044 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011045
11046 return true;
11047}
11048
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011049static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11050 struct vmcs12 *vmcs12)
11051{
11052 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11053 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11054 return -EINVAL;
11055 else
11056 return 0;
11057}
11058
Wincy Vanf2b93282015-02-03 23:56:03 +080011059static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11060 struct vmcs12 *vmcs12)
11061{
Wincy Van82f0dd42015-02-03 23:57:18 +080011062 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011063 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011064 !nested_cpu_has_vid(vmcs12) &&
11065 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011066 return 0;
11067
11068 /*
11069 * If virtualize x2apic mode is enabled,
11070 * virtualize apic access must be disabled.
11071 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011072 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11073 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011074 return -EINVAL;
11075
Wincy Van608406e2015-02-03 23:57:51 +080011076 /*
11077 * If virtual interrupt delivery is enabled,
11078 * we must exit on external interrupts.
11079 */
11080 if (nested_cpu_has_vid(vmcs12) &&
11081 !nested_exit_on_intr(vcpu))
11082 return -EINVAL;
11083
Wincy Van705699a2015-02-03 23:58:17 +080011084 /*
11085 * bits 15:8 should be zero in posted_intr_nv,
11086 * the descriptor address has been already checked
11087 * in nested_get_vmcs12_pages.
11088 */
11089 if (nested_cpu_has_posted_intr(vmcs12) &&
11090 (!nested_cpu_has_vid(vmcs12) ||
11091 !nested_exit_intr_ack_set(vcpu) ||
11092 vmcs12->posted_intr_nv & 0xff00))
11093 return -EINVAL;
11094
Wincy Vanf2b93282015-02-03 23:56:03 +080011095 /* tpr shadow is needed by all apicv features. */
11096 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11097 return -EINVAL;
11098
11099 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011100}
11101
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011102static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11103 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011104 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011105{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011106 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011107 u64 count, addr;
11108
11109 if (vmcs12_read_any(vcpu, count_field, &count) ||
11110 vmcs12_read_any(vcpu, addr_field, &addr)) {
11111 WARN_ON(1);
11112 return -EINVAL;
11113 }
11114 if (count == 0)
11115 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011116 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011117 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11118 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011119 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011120 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11121 addr_field, maxphyaddr, count, addr);
11122 return -EINVAL;
11123 }
11124 return 0;
11125}
11126
11127static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11128 struct vmcs12 *vmcs12)
11129{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011130 if (vmcs12->vm_exit_msr_load_count == 0 &&
11131 vmcs12->vm_exit_msr_store_count == 0 &&
11132 vmcs12->vm_entry_msr_load_count == 0)
11133 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011134 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011135 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011136 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011137 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011138 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011139 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011140 return -EINVAL;
11141 return 0;
11142}
11143
Bandan Dasc5f983f2017-05-05 15:25:14 -040011144static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11145 struct vmcs12 *vmcs12)
11146{
11147 u64 address = vmcs12->pml_address;
11148 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11149
11150 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11151 if (!nested_cpu_has_ept(vmcs12) ||
11152 !IS_ALIGNED(address, 4096) ||
11153 address >> maxphyaddr)
11154 return -EINVAL;
11155 }
11156
11157 return 0;
11158}
11159
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011160static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11161 struct vmx_msr_entry *e)
11162{
11163 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011164 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011165 return -EINVAL;
11166 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11167 e->index == MSR_IA32_UCODE_REV)
11168 return -EINVAL;
11169 if (e->reserved != 0)
11170 return -EINVAL;
11171 return 0;
11172}
11173
11174static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11175 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011176{
11177 if (e->index == MSR_FS_BASE ||
11178 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011179 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11180 nested_vmx_msr_check_common(vcpu, e))
11181 return -EINVAL;
11182 return 0;
11183}
11184
11185static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11186 struct vmx_msr_entry *e)
11187{
11188 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11189 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011190 return -EINVAL;
11191 return 0;
11192}
11193
11194/*
11195 * Load guest's/host's msr at nested entry/exit.
11196 * return 0 for success, entry index for failure.
11197 */
11198static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11199{
11200 u32 i;
11201 struct vmx_msr_entry e;
11202 struct msr_data msr;
11203
11204 msr.host_initiated = false;
11205 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011206 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11207 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011208 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011209 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11210 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011211 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011212 }
11213 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011214 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011215 "%s check failed (%u, 0x%x, 0x%x)\n",
11216 __func__, i, e.index, e.reserved);
11217 goto fail;
11218 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011219 msr.index = e.index;
11220 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011221 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011222 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011223 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11224 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011225 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011226 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011227 }
11228 return 0;
11229fail:
11230 return i + 1;
11231}
11232
11233static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11234{
11235 u32 i;
11236 struct vmx_msr_entry e;
11237
11238 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011239 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011240 if (kvm_vcpu_read_guest(vcpu,
11241 gpa + i * sizeof(e),
11242 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011243 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011244 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11245 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011246 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011247 }
11248 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011249 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011250 "%s check failed (%u, 0x%x, 0x%x)\n",
11251 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011252 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011253 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011254 msr_info.host_initiated = false;
11255 msr_info.index = e.index;
11256 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011257 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011258 "%s cannot read MSR (%u, 0x%x)\n",
11259 __func__, i, e.index);
11260 return -EINVAL;
11261 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011262 if (kvm_vcpu_write_guest(vcpu,
11263 gpa + i * sizeof(e) +
11264 offsetof(struct vmx_msr_entry, value),
11265 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011266 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011267 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011268 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011269 return -EINVAL;
11270 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011271 }
11272 return 0;
11273}
11274
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011275static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11276{
11277 unsigned long invalid_mask;
11278
11279 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11280 return (val & invalid_mask) == 0;
11281}
11282
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011283/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011284 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11285 * emulating VM entry into a guest with EPT enabled.
11286 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11287 * is assigned to entry_failure_code on failure.
11288 */
11289static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011290 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011291{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011292 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011293 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011294 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11295 return 1;
11296 }
11297
11298 /*
11299 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11300 * must not be dereferenced.
11301 */
11302 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11303 !nested_ept) {
11304 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11305 *entry_failure_code = ENTRY_FAIL_PDPTE;
11306 return 1;
11307 }
11308 }
11309
11310 vcpu->arch.cr3 = cr3;
11311 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11312 }
11313
11314 kvm_mmu_reset_context(vcpu);
11315 return 0;
11316}
11317
Jim Mattson6514dc32018-04-26 16:09:12 -070011318static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011319{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011320 struct vcpu_vmx *vmx = to_vmx(vcpu);
11321
11322 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11323 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11324 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11325 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11326 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11327 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11328 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11329 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11330 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11331 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11332 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11333 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11334 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11335 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11336 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11337 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11338 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11339 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11340 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11341 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11342 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11343 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11344 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11345 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11346 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11347 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11348 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11349 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11350 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11351 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11352 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011353
11354 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11355 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11356 vmcs12->guest_pending_dbg_exceptions);
11357 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11358 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11359
11360 if (nested_cpu_has_xsaves(vmcs12))
11361 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11362 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11363
11364 if (cpu_has_vmx_posted_intr())
11365 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11366
11367 /*
11368 * Whether page-faults are trapped is determined by a combination of
11369 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11370 * If enable_ept, L0 doesn't care about page faults and we should
11371 * set all of these to L1's desires. However, if !enable_ept, L0 does
11372 * care about (at least some) page faults, and because it is not easy
11373 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11374 * to exit on each and every L2 page fault. This is done by setting
11375 * MASK=MATCH=0 and (see below) EB.PF=1.
11376 * Note that below we don't need special code to set EB.PF beyond the
11377 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11378 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11379 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11380 */
11381 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11382 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11383 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11384 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11385
11386 /* All VMFUNCs are currently emulated through L0 vmexits. */
11387 if (cpu_has_vmx_vmfunc())
11388 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11389
11390 if (cpu_has_vmx_apicv()) {
11391 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11392 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11393 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11394 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11395 }
11396
11397 /*
11398 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11399 * Some constant fields are set here by vmx_set_constant_host_state().
11400 * Other fields are different per CPU, and will be set later when
11401 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11402 */
11403 vmx_set_constant_host_state(vmx);
11404
11405 /*
11406 * Set the MSR load/store lists to match L0's settings.
11407 */
11408 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011409 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11410 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11411 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11412 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011413
11414 set_cr4_guest_host_mask(vmx);
11415
11416 if (vmx_mpx_supported())
11417 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11418
11419 if (enable_vpid) {
11420 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11421 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11422 else
11423 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11424 }
11425
11426 /*
11427 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11428 */
11429 if (enable_ept) {
11430 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11431 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11432 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11433 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11434 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011435
11436 if (cpu_has_vmx_msr_bitmap())
11437 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011438}
11439
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011440/*
11441 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11442 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011443 * 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 +030011444 * guest in a way that will both be appropriate to L1's requests, and our
11445 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11446 * function also has additional necessary side-effects, like setting various
11447 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011448 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11449 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011450 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011451static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011452 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011453{
11454 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011455 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011456
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011457 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011458 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011459 vmx->nested.dirty_vmcs12 = false;
11460 }
11461
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011462 /*
11463 * First, the fields that are shadowed. This must be kept in sync
11464 * with vmx_shadow_fields.h.
11465 */
11466
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011467 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011468 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011469 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011470 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11471 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011472
11473 /*
11474 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11475 * HOST_FS_BASE, HOST_GS_BASE.
11476 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011477
Jim Mattson6514dc32018-04-26 16:09:12 -070011478 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011479 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011480 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11481 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11482 } else {
11483 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11484 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11485 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011486 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011487 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11488 vmcs12->vm_entry_intr_info_field);
11489 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11490 vmcs12->vm_entry_exception_error_code);
11491 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11492 vmcs12->vm_entry_instruction_len);
11493 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11494 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011495 vmx->loaded_vmcs->nmi_known_unmasked =
11496 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011497 } else {
11498 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11499 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011500 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011501
Jan Kiszkaf4124502014-03-07 20:03:13 +010011502 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011503
Paolo Bonzini93140062016-07-06 13:23:51 +020011504 /* Preemption timer setting is only taken from vmcs01. */
11505 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11506 exec_control |= vmcs_config.pin_based_exec_ctrl;
11507 if (vmx->hv_deadline_tsc == -1)
11508 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11509
11510 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011511 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011512 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11513 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011514 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011515 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011516 }
Wincy Van705699a2015-02-03 23:58:17 +080011517
Jan Kiszkaf4124502014-03-07 20:03:13 +010011518 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011519
Jan Kiszkaf4124502014-03-07 20:03:13 +010011520 vmx->nested.preemption_timer_expired = false;
11521 if (nested_cpu_has_preemption_timer(vmcs12))
11522 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011523
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011524 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011525 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011526
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011527 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011528 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011529 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011530 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011531 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011532 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011533 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11534 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011535 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011536 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11537 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11538 ~SECONDARY_EXEC_ENABLE_PML;
11539 exec_control |= vmcs12_exec_ctrl;
11540 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011541
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011542 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011543 vmcs_write16(GUEST_INTR_STATUS,
11544 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011545
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011546 /*
11547 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11548 * nested_get_vmcs12_pages will either fix it up or
11549 * remove the VM execution control.
11550 */
11551 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11552 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11553
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011554 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11555 }
11556
Jim Mattson83bafef2016-10-04 10:48:38 -070011557 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011558 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11559 * entry, but only if the current (host) sp changed from the value
11560 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11561 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11562 * here we just force the write to happen on entry.
11563 */
11564 vmx->host_rsp = 0;
11565
11566 exec_control = vmx_exec_control(vmx); /* L0's desires */
11567 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11568 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11569 exec_control &= ~CPU_BASED_TPR_SHADOW;
11570 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011571
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011572 /*
11573 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11574 * nested_get_vmcs12_pages can't fix it up, the illegal value
11575 * will result in a VM entry failure.
11576 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011577 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011578 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011579 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011580 } else {
11581#ifdef CONFIG_X86_64
11582 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11583 CPU_BASED_CR8_STORE_EXITING;
11584#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011585 }
11586
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011587 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011588 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11589 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011590 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011591 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11592 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11593
11594 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11595
11596 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11597 * bitwise-or of what L1 wants to trap for L2, and what we want to
11598 * trap. Note that CR0.TS also needs updating - we do this later.
11599 */
11600 update_exception_bitmap(vcpu);
11601 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11602 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11603
Nadav Har'El8049d652013-08-05 11:07:06 +030011604 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11605 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11606 * bits are further modified by vmx_set_efer() below.
11607 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011608 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011609
11610 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11611 * emulated by vmx_set_efer(), below.
11612 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011613 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011614 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11615 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011616 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11617
Jim Mattson6514dc32018-04-26 16:09:12 -070011618 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011619 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011620 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011621 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011622 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011623 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011624 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011625
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011626 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11627
Peter Feinerc95ba922016-08-17 09:36:47 -070011628 if (kvm_has_tsc_control)
11629 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011630
11631 if (enable_vpid) {
11632 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011633 * There is no direct mapping between vpid02 and vpid12, the
11634 * vpid02 is per-vCPU for L0 and reused while the value of
11635 * vpid12 is changed w/ one invvpid during nested vmentry.
11636 * The vpid12 is allocated by L1 for L2, so it will not
11637 * influence global bitmap(for vpid01 and vpid02 allocation)
11638 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011639 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011640 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011641 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11642 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011643 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011644 }
11645 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011646 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011647 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011648 }
11649
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011650 if (enable_pml) {
11651 /*
11652 * Conceptually we want to copy the PML address and index from
11653 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11654 * since we always flush the log on each vmexit, this happens
11655 * to be equivalent to simply resetting the fields in vmcs02.
11656 */
11657 ASSERT(vmx->pml_pg);
11658 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11659 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11660 }
11661
Nadav Har'El155a97a2013-08-05 11:07:16 +030011662 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011663 if (nested_ept_init_mmu_context(vcpu)) {
11664 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11665 return 1;
11666 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011667 } else if (nested_cpu_has2(vmcs12,
11668 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011669 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011670 }
11671
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011672 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011673 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11674 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011675 * The CR0_READ_SHADOW is what L2 should have expected to read given
11676 * the specifications by L1; It's not enough to take
11677 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11678 * have more bits than L1 expected.
11679 */
11680 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11681 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11682
11683 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11684 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11685
Jim Mattson6514dc32018-04-26 16:09:12 -070011686 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011687 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011688 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11689 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11690 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11691 else
11692 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11693 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11694 vmx_set_efer(vcpu, vcpu->arch.efer);
11695
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011696 /*
11697 * Guest state is invalid and unrestricted guest is disabled,
11698 * which means L1 attempted VMEntry to L2 with invalid state.
11699 * Fail the VMEntry.
11700 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011701 if (vmx->emulation_required) {
11702 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011703 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011704 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011705
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011706 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011707 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011708 entry_failure_code))
11709 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011710
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011711 if (!enable_ept)
11712 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11713
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011714 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11715 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011716 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011717}
11718
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011719static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11720{
11721 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11722 nested_cpu_has_virtual_nmis(vmcs12))
11723 return -EINVAL;
11724
11725 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11726 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11727 return -EINVAL;
11728
11729 return 0;
11730}
11731
Jim Mattsonca0bde22016-11-30 12:03:46 -080011732static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11733{
11734 struct vcpu_vmx *vmx = to_vmx(vcpu);
11735
11736 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11737 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11738 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11739
Jim Mattson56a20512017-07-06 16:33:06 -070011740 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11741 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11742
Jim Mattsonca0bde22016-11-30 12:03:46 -080011743 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11744 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11745
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011746 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11747 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11748
Jim Mattson712b12d2017-08-24 13:24:47 -070011749 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11750 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11751
Jim Mattsonca0bde22016-11-30 12:03:46 -080011752 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11753 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11754
11755 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11756 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11757
Bandan Dasc5f983f2017-05-05 15:25:14 -040011758 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11759 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11760
Jim Mattsonca0bde22016-11-30 12:03:46 -080011761 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011762 vmx->nested.msrs.procbased_ctls_low,
11763 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011764 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11765 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011766 vmx->nested.msrs.secondary_ctls_low,
11767 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011768 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011769 vmx->nested.msrs.pinbased_ctls_low,
11770 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011771 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011772 vmx->nested.msrs.exit_ctls_low,
11773 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011774 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011775 vmx->nested.msrs.entry_ctls_low,
11776 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011777 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11778
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011779 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011780 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11781
Bandan Das41ab9372017-08-03 15:54:43 -040011782 if (nested_cpu_has_vmfunc(vmcs12)) {
11783 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011784 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011785 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11786
11787 if (nested_cpu_has_eptp_switching(vmcs12)) {
11788 if (!nested_cpu_has_ept(vmcs12) ||
11789 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11790 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11791 }
11792 }
Bandan Das27c42a12017-08-03 15:54:42 -040011793
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011794 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11795 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11796
Jim Mattsonca0bde22016-11-30 12:03:46 -080011797 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11798 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11799 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11800 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11801
11802 return 0;
11803}
11804
11805static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11806 u32 *exit_qual)
11807{
11808 bool ia32e;
11809
11810 *exit_qual = ENTRY_FAIL_DEFAULT;
11811
11812 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11813 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11814 return 1;
11815
11816 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11817 vmcs12->vmcs_link_pointer != -1ull) {
11818 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11819 return 1;
11820 }
11821
11822 /*
11823 * If the load IA32_EFER VM-entry control is 1, the following checks
11824 * are performed on the field for the IA32_EFER MSR:
11825 * - Bits reserved in the IA32_EFER MSR must be 0.
11826 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11827 * the IA-32e mode guest VM-exit control. It must also be identical
11828 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11829 * CR0.PG) is 1.
11830 */
11831 if (to_vmx(vcpu)->nested.nested_run_pending &&
11832 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11833 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11834 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11835 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11836 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11837 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11838 return 1;
11839 }
11840
11841 /*
11842 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11843 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11844 * the values of the LMA and LME bits in the field must each be that of
11845 * the host address-space size VM-exit control.
11846 */
11847 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11848 ia32e = (vmcs12->vm_exit_controls &
11849 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11850 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11851 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11852 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11853 return 1;
11854 }
11855
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011856 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11857 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11858 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11859 return 1;
11860
Jim Mattsonca0bde22016-11-30 12:03:46 -080011861 return 0;
11862}
11863
Jim Mattson6514dc32018-04-26 16:09:12 -070011864static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011865{
11866 struct vcpu_vmx *vmx = to_vmx(vcpu);
11867 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011868 u32 msr_entry_idx;
11869 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011870 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011871
Jim Mattson858e25c2016-11-30 12:03:47 -080011872 enter_guest_mode(vcpu);
11873
11874 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11875 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11876
Jim Mattsonde3a0022017-11-27 17:22:25 -060011877 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011878 vmx_segment_cache_clear(vmx);
11879
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011880 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11881 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11882
11883 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011884 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011885 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011886
11887 nested_get_vmcs12_pages(vcpu, vmcs12);
11888
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011889 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011890 msr_entry_idx = nested_vmx_load_msr(vcpu,
11891 vmcs12->vm_entry_msr_load_addr,
11892 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011893 if (msr_entry_idx)
11894 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011895
Jim Mattson858e25c2016-11-30 12:03:47 -080011896 /*
11897 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11898 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11899 * returned as far as L1 is concerned. It will only return (and set
11900 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11901 */
11902 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011903
11904fail:
11905 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11906 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11907 leave_guest_mode(vcpu);
11908 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11909 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11910 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011911}
11912
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011913/*
11914 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11915 * for running an L2 nested guest.
11916 */
11917static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11918{
11919 struct vmcs12 *vmcs12;
11920 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011921 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011922 u32 exit_qual;
11923 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011924
Kyle Hueyeb277562016-11-29 12:40:39 -080011925 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011926 return 1;
11927
Kyle Hueyeb277562016-11-29 12:40:39 -080011928 if (!nested_vmx_check_vmcs12(vcpu))
11929 goto out;
11930
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011931 vmcs12 = get_vmcs12(vcpu);
11932
Abel Gordon012f83c2013-04-18 14:39:25 +030011933 if (enable_shadow_vmcs)
11934 copy_shadow_to_vmcs12(vmx);
11935
Nadav Har'El7c177932011-05-25 23:12:04 +030011936 /*
11937 * The nested entry process starts with enforcing various prerequisites
11938 * on vmcs12 as required by the Intel SDM, and act appropriately when
11939 * they fail: As the SDM explains, some conditions should cause the
11940 * instruction to fail, while others will cause the instruction to seem
11941 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11942 * To speed up the normal (success) code path, we should avoid checking
11943 * for misconfigurations which will anyway be caught by the processor
11944 * when using the merged vmcs02.
11945 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011946 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11947 nested_vmx_failValid(vcpu,
11948 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11949 goto out;
11950 }
11951
Nadav Har'El7c177932011-05-25 23:12:04 +030011952 if (vmcs12->launch_state == launch) {
11953 nested_vmx_failValid(vcpu,
11954 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11955 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011956 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011957 }
11958
Jim Mattsonca0bde22016-11-30 12:03:46 -080011959 ret = check_vmentry_prereqs(vcpu, vmcs12);
11960 if (ret) {
11961 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011962 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011963 }
11964
Nadav Har'El7c177932011-05-25 23:12:04 +030011965 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011966 * After this point, the trap flag no longer triggers a singlestep trap
11967 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11968 * This is not 100% correct; for performance reasons, we delegate most
11969 * of the checks on host state to the processor. If those fail,
11970 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011971 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011972 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011973
Jim Mattsonca0bde22016-11-30 12:03:46 -080011974 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11975 if (ret) {
11976 nested_vmx_entry_failure(vcpu, vmcs12,
11977 EXIT_REASON_INVALID_STATE, exit_qual);
11978 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011979 }
11980
11981 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011982 * We're finally done with prerequisite checking, and can start with
11983 * the nested entry.
11984 */
11985
Jim Mattson6514dc32018-04-26 16:09:12 -070011986 vmx->nested.nested_run_pending = 1;
11987 ret = enter_vmx_non_root_mode(vcpu);
11988 if (ret) {
11989 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011990 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011991 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011992
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011993 /* Hide L1D cache contents from the nested guest. */
11994 vmx->vcpu.arch.l1tf_flush_l1d = true;
11995
Chao Gao135a06c2018-02-11 10:06:30 +080011996 /*
11997 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11998 * by event injection, halt vcpu.
11999 */
12000 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012001 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12002 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012003 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012004 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012005 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012006
12007out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012008 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012009}
12010
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012011/*
12012 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12013 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12014 * This function returns the new value we should put in vmcs12.guest_cr0.
12015 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12016 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12017 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12018 * didn't trap the bit, because if L1 did, so would L0).
12019 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12020 * been modified by L2, and L1 knows it. So just leave the old value of
12021 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12022 * isn't relevant, because if L0 traps this bit it can set it to anything.
12023 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12024 * changed these bits, and therefore they need to be updated, but L0
12025 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12026 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12027 */
12028static inline unsigned long
12029vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12030{
12031 return
12032 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12033 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12034 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12035 vcpu->arch.cr0_guest_owned_bits));
12036}
12037
12038static inline unsigned long
12039vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12040{
12041 return
12042 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12043 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12044 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12045 vcpu->arch.cr4_guest_owned_bits));
12046}
12047
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012048static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12049 struct vmcs12 *vmcs12)
12050{
12051 u32 idt_vectoring;
12052 unsigned int nr;
12053
Wanpeng Li664f8e22017-08-24 03:35:09 -070012054 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012055 nr = vcpu->arch.exception.nr;
12056 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12057
12058 if (kvm_exception_is_soft(nr)) {
12059 vmcs12->vm_exit_instruction_len =
12060 vcpu->arch.event_exit_inst_len;
12061 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12062 } else
12063 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12064
12065 if (vcpu->arch.exception.has_error_code) {
12066 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12067 vmcs12->idt_vectoring_error_code =
12068 vcpu->arch.exception.error_code;
12069 }
12070
12071 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012072 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012073 vmcs12->idt_vectoring_info_field =
12074 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012075 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012076 nr = vcpu->arch.interrupt.nr;
12077 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12078
12079 if (vcpu->arch.interrupt.soft) {
12080 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12081 vmcs12->vm_entry_instruction_len =
12082 vcpu->arch.event_exit_inst_len;
12083 } else
12084 idt_vectoring |= INTR_TYPE_EXT_INTR;
12085
12086 vmcs12->idt_vectoring_info_field = idt_vectoring;
12087 }
12088}
12089
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012090static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12091{
12092 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012093 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012094 bool block_nested_events =
12095 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012096
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012097 if (vcpu->arch.exception.pending &&
12098 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012099 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012100 return -EBUSY;
12101 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012102 return 0;
12103 }
12104
Jan Kiszkaf4124502014-03-07 20:03:13 +010012105 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12106 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012107 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012108 return -EBUSY;
12109 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12110 return 0;
12111 }
12112
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012113 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012114 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012115 return -EBUSY;
12116 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12117 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12118 INTR_INFO_VALID_MASK, 0);
12119 /*
12120 * The NMI-triggered VM exit counts as injection:
12121 * clear this one and block further NMIs.
12122 */
12123 vcpu->arch.nmi_pending = 0;
12124 vmx_set_nmi_mask(vcpu, true);
12125 return 0;
12126 }
12127
12128 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12129 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012130 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012131 return -EBUSY;
12132 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012133 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012134 }
12135
David Hildenbrand6342c502017-01-25 11:58:58 +010012136 vmx_complete_nested_posted_interrupt(vcpu);
12137 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012138}
12139
Jan Kiszkaf4124502014-03-07 20:03:13 +010012140static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12141{
12142 ktime_t remaining =
12143 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12144 u64 value;
12145
12146 if (ktime_to_ns(remaining) <= 0)
12147 return 0;
12148
12149 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12150 do_div(value, 1000000);
12151 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12152}
12153
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012154/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012155 * Update the guest state fields of vmcs12 to reflect changes that
12156 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12157 * VM-entry controls is also updated, since this is really a guest
12158 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012159 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012160static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012161{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012162 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12163 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12164
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012165 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12166 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12167 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12168
12169 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12170 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12171 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12172 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12173 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12174 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12175 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12176 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12177 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12178 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12179 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12180 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12181 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12182 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12183 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12184 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12185 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12186 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12187 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12188 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12189 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12190 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12191 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12192 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12193 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12194 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12195 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12196 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12197 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12198 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12199 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12200 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12201 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12202 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12203 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12204 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12205
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012206 vmcs12->guest_interruptibility_info =
12207 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12208 vmcs12->guest_pending_dbg_exceptions =
12209 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012210 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12211 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12212 else
12213 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012214
Jan Kiszkaf4124502014-03-07 20:03:13 +010012215 if (nested_cpu_has_preemption_timer(vmcs12)) {
12216 if (vmcs12->vm_exit_controls &
12217 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12218 vmcs12->vmx_preemption_timer_value =
12219 vmx_get_preemption_timer_value(vcpu);
12220 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12221 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012222
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012223 /*
12224 * In some cases (usually, nested EPT), L2 is allowed to change its
12225 * own CR3 without exiting. If it has changed it, we must keep it.
12226 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12227 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12228 *
12229 * Additionally, restore L2's PDPTR to vmcs12.
12230 */
12231 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012232 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012233 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12234 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12235 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12236 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12237 }
12238
Jim Mattsond281e132017-06-01 12:44:46 -070012239 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012240
Wincy Van608406e2015-02-03 23:57:51 +080012241 if (nested_cpu_has_vid(vmcs12))
12242 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12243
Jan Kiszkac18911a2013-03-13 16:06:41 +010012244 vmcs12->vm_entry_controls =
12245 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012246 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012247
Jan Kiszka2996fca2014-06-16 13:59:43 +020012248 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12249 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12250 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12251 }
12252
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012253 /* TODO: These cannot have changed unless we have MSR bitmaps and
12254 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012255 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012256 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012257 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12258 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012259 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12260 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12261 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012262 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012263 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012264}
12265
12266/*
12267 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12268 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12269 * and this function updates it to reflect the changes to the guest state while
12270 * L2 was running (and perhaps made some exits which were handled directly by L0
12271 * without going back to L1), and to reflect the exit reason.
12272 * Note that we do not have to copy here all VMCS fields, just those that
12273 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12274 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12275 * which already writes to vmcs12 directly.
12276 */
12277static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12278 u32 exit_reason, u32 exit_intr_info,
12279 unsigned long exit_qualification)
12280{
12281 /* update guest state fields: */
12282 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012283
12284 /* update exit information fields: */
12285
Jan Kiszka533558b2014-01-04 18:47:20 +010012286 vmcs12->vm_exit_reason = exit_reason;
12287 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012288 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012289
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012290 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012291 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12292 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12293
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012294 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012295 vmcs12->launch_state = 1;
12296
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012297 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12298 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012299 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012300
12301 /*
12302 * Transfer the event that L0 or L1 may wanted to inject into
12303 * L2 to IDT_VECTORING_INFO_FIELD.
12304 */
12305 vmcs12_save_pending_event(vcpu, vmcs12);
12306 }
12307
12308 /*
12309 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12310 * preserved above and would only end up incorrectly in L1.
12311 */
12312 vcpu->arch.nmi_injected = false;
12313 kvm_clear_exception_queue(vcpu);
12314 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012315}
12316
Wanpeng Li5af41572017-11-05 16:54:49 -080012317static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12318 struct vmcs12 *vmcs12)
12319{
12320 u32 entry_failure_code;
12321
12322 nested_ept_uninit_mmu_context(vcpu);
12323
12324 /*
12325 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12326 * couldn't have changed.
12327 */
12328 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12329 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12330
12331 if (!enable_ept)
12332 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12333}
12334
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012335/*
12336 * A part of what we need to when the nested L2 guest exits and we want to
12337 * run its L1 parent, is to reset L1's guest state to the host state specified
12338 * in vmcs12.
12339 * This function is to be called not only on normal nested exit, but also on
12340 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12341 * Failures During or After Loading Guest State").
12342 * This function should be called when the active VMCS is L1's (vmcs01).
12343 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012344static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12345 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012346{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012347 struct kvm_segment seg;
12348
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012349 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12350 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012351 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012352 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12353 else
12354 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12355 vmx_set_efer(vcpu, vcpu->arch.efer);
12356
12357 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12358 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012359 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012360 /*
12361 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012362 * actually changed, because vmx_set_cr0 refers to efer set above.
12363 *
12364 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12365 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012366 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012367 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012368 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012369
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012370 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012371 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012372 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012373
Wanpeng Li5af41572017-11-05 16:54:49 -080012374 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012375
Liran Alon6f1e03b2018-05-22 17:16:14 +030012376 /*
12377 * If vmcs01 don't use VPID, CPU flushes TLB on every
12378 * VMEntry/VMExit. Thus, no need to flush TLB.
12379 *
12380 * If vmcs12 uses VPID, TLB entries populated by L2 are
12381 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12382 * with vmx->vpid. Thus, no need to flush TLB.
12383 *
12384 * Therefore, flush TLB only in case vmcs01 uses VPID and
12385 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12386 * are both tagged with vmx->vpid.
12387 */
12388 if (enable_vpid &&
12389 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012390 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012391 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012392
12393 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12394 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12395 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12396 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12397 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012398 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12399 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012400
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012401 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12402 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12403 vmcs_write64(GUEST_BNDCFGS, 0);
12404
Jan Kiszka44811c02013-08-04 17:17:27 +020012405 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012406 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012407 vcpu->arch.pat = vmcs12->host_ia32_pat;
12408 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012409 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12410 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12411 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012412
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012413 /* Set L1 segment info according to Intel SDM
12414 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12415 seg = (struct kvm_segment) {
12416 .base = 0,
12417 .limit = 0xFFFFFFFF,
12418 .selector = vmcs12->host_cs_selector,
12419 .type = 11,
12420 .present = 1,
12421 .s = 1,
12422 .g = 1
12423 };
12424 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12425 seg.l = 1;
12426 else
12427 seg.db = 1;
12428 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12429 seg = (struct kvm_segment) {
12430 .base = 0,
12431 .limit = 0xFFFFFFFF,
12432 .type = 3,
12433 .present = 1,
12434 .s = 1,
12435 .db = 1,
12436 .g = 1
12437 };
12438 seg.selector = vmcs12->host_ds_selector;
12439 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12440 seg.selector = vmcs12->host_es_selector;
12441 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12442 seg.selector = vmcs12->host_ss_selector;
12443 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12444 seg.selector = vmcs12->host_fs_selector;
12445 seg.base = vmcs12->host_fs_base;
12446 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12447 seg.selector = vmcs12->host_gs_selector;
12448 seg.base = vmcs12->host_gs_base;
12449 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12450 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012451 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012452 .limit = 0x67,
12453 .selector = vmcs12->host_tr_selector,
12454 .type = 11,
12455 .present = 1
12456 };
12457 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12458
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012459 kvm_set_dr(vcpu, 7, 0x400);
12460 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012461
Wincy Van3af18d92015-02-03 23:49:31 +080012462 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012463 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012464
Wincy Vanff651cb2014-12-11 08:52:58 +030012465 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12466 vmcs12->vm_exit_msr_load_count))
12467 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012468}
12469
12470/*
12471 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12472 * and modify vmcs12 to make it see what it would expect to see there if
12473 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12474 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012475static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12476 u32 exit_intr_info,
12477 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012478{
12479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012480 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12481
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012482 /* trying to cancel vmlaunch/vmresume is a bug */
12483 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12484
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012485 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012486 * The only expected VM-instruction error is "VM entry with
12487 * invalid control field(s)." Anything else indicates a
12488 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012489 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012490 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12491 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12492
12493 leave_guest_mode(vcpu);
12494
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012495 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12496 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12497
Jim Mattson4f350c62017-09-14 16:31:44 -070012498 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012499 if (exit_reason == -1)
12500 sync_vmcs12(vcpu, vmcs12);
12501 else
12502 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12503 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012504
12505 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12506 vmcs12->vm_exit_msr_store_count))
12507 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012508 }
12509
Jim Mattson4f350c62017-09-14 16:31:44 -070012510 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012511 vm_entry_controls_reset_shadow(vmx);
12512 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012513 vmx_segment_cache_clear(vmx);
12514
Paolo Bonzini93140062016-07-06 13:23:51 +020012515 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012516 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12517 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012518 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012519 if (vmx->hv_deadline_tsc == -1)
12520 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12521 PIN_BASED_VMX_PREEMPTION_TIMER);
12522 else
12523 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12524 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012525 if (kvm_has_tsc_control)
12526 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012527
Jim Mattson8d860bb2018-05-09 16:56:05 -040012528 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12529 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12530 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012531 } else if (!nested_cpu_has_ept(vmcs12) &&
12532 nested_cpu_has2(vmcs12,
12533 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012534 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012535 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012536
12537 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12538 vmx->host_rsp = 0;
12539
12540 /* Unpin physical memory we referred to in vmcs02 */
12541 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012542 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012543 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012544 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012545 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012546 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012547 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012548 }
Wincy Van705699a2015-02-03 23:58:17 +080012549 if (vmx->nested.pi_desc_page) {
12550 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012551 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012552 vmx->nested.pi_desc_page = NULL;
12553 vmx->nested.pi_desc = NULL;
12554 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012555
12556 /*
Tang Chen38b99172014-09-24 15:57:54 +080012557 * We are now running in L2, mmu_notifier will force to reload the
12558 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12559 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012560 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012561
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012562 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012563 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012564
12565 /* in case we halted in L2 */
12566 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012567
12568 if (likely(!vmx->fail)) {
12569 /*
12570 * TODO: SDM says that with acknowledge interrupt on
12571 * exit, bit 31 of the VM-exit interrupt information
12572 * (valid interrupt) is always set to 1 on
12573 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12574 * need kvm_cpu_has_interrupt(). See the commit
12575 * message for details.
12576 */
12577 if (nested_exit_intr_ack_set(vcpu) &&
12578 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12579 kvm_cpu_has_interrupt(vcpu)) {
12580 int irq = kvm_cpu_get_interrupt(vcpu);
12581 WARN_ON(irq < 0);
12582 vmcs12->vm_exit_intr_info = irq |
12583 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12584 }
12585
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012586 if (exit_reason != -1)
12587 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12588 vmcs12->exit_qualification,
12589 vmcs12->idt_vectoring_info_field,
12590 vmcs12->vm_exit_intr_info,
12591 vmcs12->vm_exit_intr_error_code,
12592 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012593
12594 load_vmcs12_host_state(vcpu, vmcs12);
12595
12596 return;
12597 }
12598
12599 /*
12600 * After an early L2 VM-entry failure, we're now back
12601 * in L1 which thinks it just finished a VMLAUNCH or
12602 * VMRESUME instruction, so we need to set the failure
12603 * flag and the VM-instruction error field of the VMCS
12604 * accordingly.
12605 */
12606 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012607
12608 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12609
Jim Mattson4f350c62017-09-14 16:31:44 -070012610 /*
12611 * The emulated instruction was already skipped in
12612 * nested_vmx_run, but the updated RIP was never
12613 * written back to the vmcs01.
12614 */
12615 skip_emulated_instruction(vcpu);
12616 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012617}
12618
Nadav Har'El7c177932011-05-25 23:12:04 +030012619/*
Jan Kiszka42124922014-01-04 18:47:19 +010012620 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12621 */
12622static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12623{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012624 if (is_guest_mode(vcpu)) {
12625 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012626 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012627 }
Jan Kiszka42124922014-01-04 18:47:19 +010012628 free_nested(to_vmx(vcpu));
12629}
12630
12631/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012632 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12633 * 23.7 "VM-entry failures during or after loading guest state" (this also
12634 * lists the acceptable exit-reason and exit-qualification parameters).
12635 * It should only be called before L2 actually succeeded to run, and when
12636 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12637 */
12638static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12639 struct vmcs12 *vmcs12,
12640 u32 reason, unsigned long qualification)
12641{
12642 load_vmcs12_host_state(vcpu, vmcs12);
12643 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12644 vmcs12->exit_qualification = qualification;
12645 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012646 if (enable_shadow_vmcs)
12647 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012648}
12649
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012650static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12651 struct x86_instruction_info *info,
12652 enum x86_intercept_stage stage)
12653{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012654 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12655 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12656
12657 /*
12658 * RDPID causes #UD if disabled through secondary execution controls.
12659 * Because it is marked as EmulateOnUD, we need to intercept it here.
12660 */
12661 if (info->intercept == x86_intercept_rdtscp &&
12662 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12663 ctxt->exception.vector = UD_VECTOR;
12664 ctxt->exception.error_code_valid = false;
12665 return X86EMUL_PROPAGATE_FAULT;
12666 }
12667
12668 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012669 return X86EMUL_CONTINUE;
12670}
12671
Yunhong Jiang64672c92016-06-13 14:19:59 -070012672#ifdef CONFIG_X86_64
12673/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12674static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12675 u64 divisor, u64 *result)
12676{
12677 u64 low = a << shift, high = a >> (64 - shift);
12678
12679 /* To avoid the overflow on divq */
12680 if (high >= divisor)
12681 return 1;
12682
12683 /* Low hold the result, high hold rem which is discarded */
12684 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12685 "rm" (divisor), "0" (low), "1" (high));
12686 *result = low;
12687
12688 return 0;
12689}
12690
12691static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12692{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012693 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012694 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012695
12696 if (kvm_mwait_in_guest(vcpu->kvm))
12697 return -EOPNOTSUPP;
12698
12699 vmx = to_vmx(vcpu);
12700 tscl = rdtsc();
12701 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12702 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012703 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12704
12705 if (delta_tsc > lapic_timer_advance_cycles)
12706 delta_tsc -= lapic_timer_advance_cycles;
12707 else
12708 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012709
12710 /* Convert to host delta tsc if tsc scaling is enabled */
12711 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12712 u64_shl_div_u64(delta_tsc,
12713 kvm_tsc_scaling_ratio_frac_bits,
12714 vcpu->arch.tsc_scaling_ratio,
12715 &delta_tsc))
12716 return -ERANGE;
12717
12718 /*
12719 * If the delta tsc can't fit in the 32 bit after the multi shift,
12720 * we can't use the preemption timer.
12721 * It's possible that it fits on later vmentries, but checking
12722 * on every vmentry is costly so we just use an hrtimer.
12723 */
12724 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12725 return -ERANGE;
12726
12727 vmx->hv_deadline_tsc = tscl + delta_tsc;
12728 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12729 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012730
12731 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012732}
12733
12734static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12735{
12736 struct vcpu_vmx *vmx = to_vmx(vcpu);
12737 vmx->hv_deadline_tsc = -1;
12738 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12739 PIN_BASED_VMX_PREEMPTION_TIMER);
12740}
12741#endif
12742
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012743static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012744{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012745 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012746 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012747}
12748
Kai Huang843e4332015-01-28 10:54:28 +080012749static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12750 struct kvm_memory_slot *slot)
12751{
12752 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12753 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12754}
12755
12756static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12757 struct kvm_memory_slot *slot)
12758{
12759 kvm_mmu_slot_set_dirty(kvm, slot);
12760}
12761
12762static void vmx_flush_log_dirty(struct kvm *kvm)
12763{
12764 kvm_flush_pml_buffers(kvm);
12765}
12766
Bandan Dasc5f983f2017-05-05 15:25:14 -040012767static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12768{
12769 struct vmcs12 *vmcs12;
12770 struct vcpu_vmx *vmx = to_vmx(vcpu);
12771 gpa_t gpa;
12772 struct page *page = NULL;
12773 u64 *pml_address;
12774
12775 if (is_guest_mode(vcpu)) {
12776 WARN_ON_ONCE(vmx->nested.pml_full);
12777
12778 /*
12779 * Check if PML is enabled for the nested guest.
12780 * Whether eptp bit 6 is set is already checked
12781 * as part of A/D emulation.
12782 */
12783 vmcs12 = get_vmcs12(vcpu);
12784 if (!nested_cpu_has_pml(vmcs12))
12785 return 0;
12786
Dan Carpenter47698862017-05-10 22:43:17 +030012787 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012788 vmx->nested.pml_full = true;
12789 return 1;
12790 }
12791
12792 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12793
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012794 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12795 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012796 return 0;
12797
12798 pml_address = kmap(page);
12799 pml_address[vmcs12->guest_pml_index--] = gpa;
12800 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012801 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012802 }
12803
12804 return 0;
12805}
12806
Kai Huang843e4332015-01-28 10:54:28 +080012807static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12808 struct kvm_memory_slot *memslot,
12809 gfn_t offset, unsigned long mask)
12810{
12811 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12812}
12813
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012814static void __pi_post_block(struct kvm_vcpu *vcpu)
12815{
12816 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12817 struct pi_desc old, new;
12818 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012819
12820 do {
12821 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012822 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12823 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012824
12825 dest = cpu_physical_id(vcpu->cpu);
12826
12827 if (x2apic_enabled())
12828 new.ndst = dest;
12829 else
12830 new.ndst = (dest << 8) & 0xFF00;
12831
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012832 /* set 'NV' to 'notification vector' */
12833 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012834 } while (cmpxchg64(&pi_desc->control, old.control,
12835 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012836
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012837 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12838 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012839 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012840 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012841 vcpu->pre_pcpu = -1;
12842 }
12843}
12844
Feng Wuefc64402015-09-18 22:29:51 +080012845/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012846 * This routine does the following things for vCPU which is going
12847 * to be blocked if VT-d PI is enabled.
12848 * - Store the vCPU to the wakeup list, so when interrupts happen
12849 * we can find the right vCPU to wake up.
12850 * - Change the Posted-interrupt descriptor as below:
12851 * 'NDST' <-- vcpu->pre_pcpu
12852 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12853 * - If 'ON' is set during this process, which means at least one
12854 * interrupt is posted for this vCPU, we cannot block it, in
12855 * this case, return 1, otherwise, return 0.
12856 *
12857 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012858static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012859{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012860 unsigned int dest;
12861 struct pi_desc old, new;
12862 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12863
12864 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012865 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12866 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012867 return 0;
12868
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012869 WARN_ON(irqs_disabled());
12870 local_irq_disable();
12871 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12872 vcpu->pre_pcpu = vcpu->cpu;
12873 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12874 list_add_tail(&vcpu->blocked_vcpu_list,
12875 &per_cpu(blocked_vcpu_on_cpu,
12876 vcpu->pre_pcpu));
12877 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12878 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012879
12880 do {
12881 old.control = new.control = pi_desc->control;
12882
Feng Wubf9f6ac2015-09-18 22:29:55 +080012883 WARN((pi_desc->sn == 1),
12884 "Warning: SN field of posted-interrupts "
12885 "is set before blocking\n");
12886
12887 /*
12888 * Since vCPU can be preempted during this process,
12889 * vcpu->cpu could be different with pre_pcpu, we
12890 * need to set pre_pcpu as the destination of wakeup
12891 * notification event, then we can find the right vCPU
12892 * to wakeup in wakeup handler if interrupts happen
12893 * when the vCPU is in blocked state.
12894 */
12895 dest = cpu_physical_id(vcpu->pre_pcpu);
12896
12897 if (x2apic_enabled())
12898 new.ndst = dest;
12899 else
12900 new.ndst = (dest << 8) & 0xFF00;
12901
12902 /* set 'NV' to 'wakeup vector' */
12903 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012904 } while (cmpxchg64(&pi_desc->control, old.control,
12905 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012906
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012907 /* We should not block the vCPU if an interrupt is posted for it. */
12908 if (pi_test_on(pi_desc) == 1)
12909 __pi_post_block(vcpu);
12910
12911 local_irq_enable();
12912 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012913}
12914
Yunhong Jiangbc225122016-06-13 14:19:58 -070012915static int vmx_pre_block(struct kvm_vcpu *vcpu)
12916{
12917 if (pi_pre_block(vcpu))
12918 return 1;
12919
Yunhong Jiang64672c92016-06-13 14:19:59 -070012920 if (kvm_lapic_hv_timer_in_use(vcpu))
12921 kvm_lapic_switch_to_sw_timer(vcpu);
12922
Yunhong Jiangbc225122016-06-13 14:19:58 -070012923 return 0;
12924}
12925
12926static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012927{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012928 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012929 return;
12930
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012931 WARN_ON(irqs_disabled());
12932 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012933 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012934 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012935}
12936
Yunhong Jiangbc225122016-06-13 14:19:58 -070012937static void vmx_post_block(struct kvm_vcpu *vcpu)
12938{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012939 if (kvm_x86_ops->set_hv_timer)
12940 kvm_lapic_switch_to_hv_timer(vcpu);
12941
Yunhong Jiangbc225122016-06-13 14:19:58 -070012942 pi_post_block(vcpu);
12943}
12944
Feng Wubf9f6ac2015-09-18 22:29:55 +080012945/*
Feng Wuefc64402015-09-18 22:29:51 +080012946 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12947 *
12948 * @kvm: kvm
12949 * @host_irq: host irq of the interrupt
12950 * @guest_irq: gsi of the interrupt
12951 * @set: set or unset PI
12952 * returns 0 on success, < 0 on failure
12953 */
12954static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12955 uint32_t guest_irq, bool set)
12956{
12957 struct kvm_kernel_irq_routing_entry *e;
12958 struct kvm_irq_routing_table *irq_rt;
12959 struct kvm_lapic_irq irq;
12960 struct kvm_vcpu *vcpu;
12961 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012962 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012963
12964 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012965 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12966 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012967 return 0;
12968
12969 idx = srcu_read_lock(&kvm->irq_srcu);
12970 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012971 if (guest_irq >= irq_rt->nr_rt_entries ||
12972 hlist_empty(&irq_rt->map[guest_irq])) {
12973 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12974 guest_irq, irq_rt->nr_rt_entries);
12975 goto out;
12976 }
Feng Wuefc64402015-09-18 22:29:51 +080012977
12978 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12979 if (e->type != KVM_IRQ_ROUTING_MSI)
12980 continue;
12981 /*
12982 * VT-d PI cannot support posting multicast/broadcast
12983 * interrupts to a vCPU, we still use interrupt remapping
12984 * for these kind of interrupts.
12985 *
12986 * For lowest-priority interrupts, we only support
12987 * those with single CPU as the destination, e.g. user
12988 * configures the interrupts via /proc/irq or uses
12989 * irqbalance to make the interrupts single-CPU.
12990 *
12991 * We will support full lowest-priority interrupt later.
12992 */
12993
Radim Krčmář371313132016-07-12 22:09:27 +020012994 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012995 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12996 /*
12997 * Make sure the IRTE is in remapped mode if
12998 * we don't handle it in posted mode.
12999 */
13000 ret = irq_set_vcpu_affinity(host_irq, NULL);
13001 if (ret < 0) {
13002 printk(KERN_INFO
13003 "failed to back to remapped mode, irq: %u\n",
13004 host_irq);
13005 goto out;
13006 }
13007
Feng Wuefc64402015-09-18 22:29:51 +080013008 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013009 }
Feng Wuefc64402015-09-18 22:29:51 +080013010
13011 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13012 vcpu_info.vector = irq.vector;
13013
hu huajun2698d822018-04-11 15:16:40 +080013014 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013015 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13016
13017 if (set)
13018 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013019 else
Feng Wuefc64402015-09-18 22:29:51 +080013020 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013021
13022 if (ret < 0) {
13023 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13024 __func__);
13025 goto out;
13026 }
13027 }
13028
13029 ret = 0;
13030out:
13031 srcu_read_unlock(&kvm->irq_srcu, idx);
13032 return ret;
13033}
13034
Ashok Rajc45dcc72016-06-22 14:59:56 +080013035static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13036{
13037 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13038 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13039 FEATURE_CONTROL_LMCE;
13040 else
13041 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13042 ~FEATURE_CONTROL_LMCE;
13043}
13044
Ladi Prosek72d7b372017-10-11 16:54:41 +020013045static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13046{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013047 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13048 if (to_vmx(vcpu)->nested.nested_run_pending)
13049 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013050 return 1;
13051}
13052
Ladi Prosek0234bf82017-10-11 16:54:40 +020013053static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13054{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013055 struct vcpu_vmx *vmx = to_vmx(vcpu);
13056
13057 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13058 if (vmx->nested.smm.guest_mode)
13059 nested_vmx_vmexit(vcpu, -1, 0, 0);
13060
13061 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13062 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013063 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013064 return 0;
13065}
13066
13067static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13068{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013069 struct vcpu_vmx *vmx = to_vmx(vcpu);
13070 int ret;
13071
13072 if (vmx->nested.smm.vmxon) {
13073 vmx->nested.vmxon = true;
13074 vmx->nested.smm.vmxon = false;
13075 }
13076
13077 if (vmx->nested.smm.guest_mode) {
13078 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013079 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013080 vcpu->arch.hflags |= HF_SMM_MASK;
13081 if (ret)
13082 return ret;
13083
13084 vmx->nested.smm.guest_mode = false;
13085 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013086 return 0;
13087}
13088
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013089static int enable_smi_window(struct kvm_vcpu *vcpu)
13090{
13091 return 0;
13092}
13093
Kees Cook404f6aa2016-08-08 16:29:06 -070013094static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013095 .cpu_has_kvm_support = cpu_has_kvm_support,
13096 .disabled_by_bios = vmx_disabled_by_bios,
13097 .hardware_setup = hardware_setup,
13098 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013099 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013100 .hardware_enable = hardware_enable,
13101 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013102 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013103 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013104
Wanpeng Lib31c1142018-03-12 04:53:04 -070013105 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013106 .vm_alloc = vmx_vm_alloc,
13107 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013108
Avi Kivity6aa8b732006-12-10 02:21:36 -080013109 .vcpu_create = vmx_create_vcpu,
13110 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013111 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013112
Avi Kivity04d2cc72007-09-10 18:10:54 +030013113 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013114 .vcpu_load = vmx_vcpu_load,
13115 .vcpu_put = vmx_vcpu_put,
13116
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013117 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013118 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013119 .get_msr = vmx_get_msr,
13120 .set_msr = vmx_set_msr,
13121 .get_segment_base = vmx_get_segment_base,
13122 .get_segment = vmx_get_segment,
13123 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013124 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013125 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013126 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013127 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013128 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013129 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013130 .set_cr3 = vmx_set_cr3,
13131 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013132 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013133 .get_idt = vmx_get_idt,
13134 .set_idt = vmx_set_idt,
13135 .get_gdt = vmx_get_gdt,
13136 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013137 .get_dr6 = vmx_get_dr6,
13138 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013139 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013140 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013141 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013142 .get_rflags = vmx_get_rflags,
13143 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013144
Avi Kivity6aa8b732006-12-10 02:21:36 -080013145 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013146
Avi Kivity6aa8b732006-12-10 02:21:36 -080013147 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013148 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013149 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013150 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13151 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013152 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013153 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013154 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013155 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013156 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013157 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013158 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013159 .get_nmi_mask = vmx_get_nmi_mask,
13160 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013161 .enable_nmi_window = enable_nmi_window,
13162 .enable_irq_window = enable_irq_window,
13163 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013164 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013165 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013166 .get_enable_apicv = vmx_get_enable_apicv,
13167 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013168 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013169 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013170 .hwapic_irr_update = vmx_hwapic_irr_update,
13171 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013172 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13173 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013174
Izik Eiduscbc94022007-10-25 00:29:55 +020013175 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013176 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013177 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013178 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013179
Avi Kivity586f9602010-11-18 13:09:54 +020013180 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013181
Sheng Yang17cc3932010-01-05 19:02:27 +080013182 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013183
13184 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013185
13186 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013187 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013188
13189 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013190
13191 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013192
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013193 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013194 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013195
13196 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013197
13198 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013199 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013200 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013201 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013202 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013203
13204 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013205
13206 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013207
13208 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13209 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13210 .flush_log_dirty = vmx_flush_log_dirty,
13211 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013212 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013213
Feng Wubf9f6ac2015-09-18 22:29:55 +080013214 .pre_block = vmx_pre_block,
13215 .post_block = vmx_post_block,
13216
Wei Huang25462f72015-06-19 15:45:05 +020013217 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013218
13219 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013220
13221#ifdef CONFIG_X86_64
13222 .set_hv_timer = vmx_set_hv_timer,
13223 .cancel_hv_timer = vmx_cancel_hv_timer,
13224#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013225
13226 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013227
Ladi Prosek72d7b372017-10-11 16:54:41 +020013228 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013229 .pre_enter_smm = vmx_pre_enter_smm,
13230 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013231 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013232};
13233
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013234static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013235{
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013236 struct page *page;
13237
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013238 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -040013239 !boot_cpu_has_bug(X86_BUG_L1TF) ||
13240 vmx_l1d_use_msr_save_list())
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013241 return 0;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013242
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020013243 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
13244 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
13245 if (!page)
13246 return -ENOMEM;
13247 vmx_l1d_flush_pages = page_address(page);
13248 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013249
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013250 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013251 return 0;
13252}
13253
13254static void vmx_free_l1d_flush_pages(void)
13255{
13256 if (vmx_l1d_flush_pages) {
13257 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13258 vmx_l1d_flush_pages = NULL;
13259 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013260}
13261
Avi Kivity6aa8b732006-12-10 02:21:36 -080013262static int __init vmx_init(void)
13263{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013264 int r;
13265
13266#if IS_ENABLED(CONFIG_HYPERV)
13267 /*
13268 * Enlightened VMCS usage should be recommended and the host needs
13269 * to support eVMCS v1 or above. We can also disable eVMCS support
13270 * with module parameter.
13271 */
13272 if (enlightened_vmcs &&
13273 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13274 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13275 KVM_EVMCS_VERSION) {
13276 int cpu;
13277
13278 /* Check that we have assist pages on all online CPUs */
13279 for_each_online_cpu(cpu) {
13280 if (!hv_get_vp_assist_page(cpu)) {
13281 enlightened_vmcs = false;
13282 break;
13283 }
13284 }
13285
13286 if (enlightened_vmcs) {
13287 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13288 static_branch_enable(&enable_evmcs);
13289 }
13290 } else {
13291 enlightened_vmcs = false;
13292 }
13293#endif
13294
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013295 r = vmx_setup_l1d_flush();
He, Qingfdef3ad2007-04-30 09:45:24 +030013296 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013297 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013298
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013299 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13300 __alignof__(struct vcpu_vmx), THIS_MODULE);
13301 if (r) {
13302 vmx_free_l1d_flush_pages();
13303 return r;
13304 }
13305
Dave Young2965faa2015-09-09 15:38:55 -070013306#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013307 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13308 crash_vmclear_local_loaded_vmcss);
13309#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013310 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013311
He, Qingfdef3ad2007-04-30 09:45:24 +030013312 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013313}
13314
13315static void __exit vmx_exit(void)
13316{
Dave Young2965faa2015-09-09 15:38:55 -070013317#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013318 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013319 synchronize_rcu();
13320#endif
13321
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013322 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013323
13324#if IS_ENABLED(CONFIG_HYPERV)
13325 if (static_branch_unlikely(&enable_evmcs)) {
13326 int cpu;
13327 struct hv_vp_assist_page *vp_ap;
13328 /*
13329 * Reset everything to support using non-enlightened VMCS
13330 * access later (e.g. when we reload the module with
13331 * enlightened_vmcs=0)
13332 */
13333 for_each_online_cpu(cpu) {
13334 vp_ap = hv_get_vp_assist_page(cpu);
13335
13336 if (!vp_ap)
13337 continue;
13338
13339 vp_ap->current_nested_vmcs = 0;
13340 vp_ap->enlighten_vmentry = 0;
13341 }
13342
13343 static_branch_disable(&enable_evmcs);
13344 }
13345#endif
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013346 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080013347}
13348
13349module_init(vmx_init)
13350module_exit(vmx_exit)