blob: e18805f8ed310069a084414f8caea1ba23469c67 [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)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002460 return;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002461 --m->guest.nr;
2462 --m->host.nr;
2463 m->guest.val[i] = m->guest.val[m->guest.nr];
2464 m->host.val[i] = m->host.val[m->host.nr];
2465 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2466 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002467}
2468
Gleb Natapov2961e8762013-11-25 15:37:13 +02002469static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2470 unsigned long entry, unsigned long exit,
2471 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2472 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002473{
2474 vmcs_write64(guest_val_vmcs, guest_val);
2475 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002476 vm_entry_controls_setbit(vmx, entry);
2477 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002478}
2479
Avi Kivity61d2ef22010-04-28 16:40:38 +03002480static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2481 u64 guest_val, u64 host_val)
2482{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002483 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002484 struct msr_autoload *m = &vmx->msr_autoload;
2485
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002486 switch (msr) {
2487 case MSR_EFER:
2488 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002489 add_atomic_switch_msr_special(vmx,
2490 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002491 VM_EXIT_LOAD_IA32_EFER,
2492 GUEST_IA32_EFER,
2493 HOST_IA32_EFER,
2494 guest_val, host_val);
2495 return;
2496 }
2497 break;
2498 case MSR_CORE_PERF_GLOBAL_CTRL:
2499 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002500 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002501 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2502 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2503 GUEST_IA32_PERF_GLOBAL_CTRL,
2504 HOST_IA32_PERF_GLOBAL_CTRL,
2505 guest_val, host_val);
2506 return;
2507 }
2508 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002509 case MSR_IA32_PEBS_ENABLE:
2510 /* PEBS needs a quiescent period after being disabled (to write
2511 * a record). Disabling PEBS through VMX MSR swapping doesn't
2512 * provide that period, so a CPU could write host's record into
2513 * guest's memory.
2514 */
2515 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002516 }
2517
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002518 i = find_msr(&m->guest, msr);
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002519 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002520 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002521 "Can't add msr %x\n", msr);
2522 return;
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002523 } else if (i < 0) {
2524 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002525 ++m->host.nr;
2526 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2527 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002528 }
2529
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002530 m->guest.val[i].index = msr;
2531 m->guest.val[i].value = guest_val;
2532 m->host.val[i].index = msr;
2533 m->host.val[i].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002534}
2535
Avi Kivity92c0d902009-10-29 11:00:16 +02002536static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002537{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002538 u64 guest_efer = vmx->vcpu.arch.efer;
2539 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002540
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002541 if (!enable_ept) {
2542 /*
2543 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2544 * host CPUID is more efficient than testing guest CPUID
2545 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2546 */
2547 if (boot_cpu_has(X86_FEATURE_SMEP))
2548 guest_efer |= EFER_NX;
2549 else if (!(guest_efer & EFER_NX))
2550 ignore_bits |= EFER_NX;
2551 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002552
Avi Kivity51c6cf62007-08-29 03:48:05 +03002553 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002554 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002555 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002556 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002557#ifdef CONFIG_X86_64
2558 ignore_bits |= EFER_LMA | EFER_LME;
2559 /* SCE is meaningful only in long mode on Intel */
2560 if (guest_efer & EFER_LMA)
2561 ignore_bits &= ~(u64)EFER_SCE;
2562#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002563
2564 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002565
2566 /*
2567 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2568 * On CPUs that support "load IA32_EFER", always switch EFER
2569 * atomically, since it's faster than switching it manually.
2570 */
2571 if (cpu_has_load_ia32_efer ||
2572 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002573 if (!(guest_efer & EFER_LMA))
2574 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002575 if (guest_efer != host_efer)
2576 add_atomic_switch_msr(vmx, MSR_EFER,
2577 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002578 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002579 } else {
2580 guest_efer &= ~ignore_bits;
2581 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002582
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002583 vmx->guest_msrs[efer_offset].data = guest_efer;
2584 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2585
2586 return true;
2587 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002588}
2589
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002590#ifdef CONFIG_X86_32
2591/*
2592 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2593 * VMCS rather than the segment table. KVM uses this helper to figure
2594 * out the current bases to poke them into the VMCS before entry.
2595 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002596static unsigned long segment_base(u16 selector)
2597{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002598 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002599 unsigned long v;
2600
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002601 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002602 return 0;
2603
Thomas Garnier45fc8752017-03-14 10:05:08 -07002604 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002605
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002606 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002607 u16 ldt_selector = kvm_read_ldt();
2608
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002609 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002610 return 0;
2611
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002612 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002613 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002614 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002615 return v;
2616}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002617#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002618
Avi Kivity04d2cc72007-09-10 18:10:54 +03002619static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002620{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002621 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002622#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002623 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002624#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002625 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002626
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002627 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002628 return;
2629
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002630 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002631 /*
2632 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2633 * allow segment selectors with cpl > 0 or ti == 1.
2634 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002635 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002636 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002637
2638#ifdef CONFIG_X86_64
2639 save_fsgs_for_kvm();
2640 vmx->host_state.fs_sel = current->thread.fsindex;
2641 vmx->host_state.gs_sel = current->thread.gsindex;
2642#else
Avi Kivity9581d442010-10-19 16:46:55 +02002643 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002644 savesegment(gs, vmx->host_state.gs_sel);
2645#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002646 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002647 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002648 vmx->host_state.fs_reload_needed = 0;
2649 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002650 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002651 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002652 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002653 if (!(vmx->host_state.gs_sel & 7))
2654 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002655 else {
2656 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002657 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002658 }
2659
2660#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002661 savesegment(ds, vmx->host_state.ds_sel);
2662 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002663
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002664 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002665 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002666
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002667 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002668 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002669 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002670#else
2671 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2672 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2673#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002674 if (boot_cpu_has(X86_FEATURE_MPX))
2675 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002676 for (i = 0; i < vmx->save_nmsrs; ++i)
2677 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002678 vmx->guest_msrs[i].data,
2679 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002680}
2681
Avi Kivitya9b21b62008-06-24 11:48:49 +03002682static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002683{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002684 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002685 return;
2686
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002687 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002688 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002689#ifdef CONFIG_X86_64
2690 if (is_long_mode(&vmx->vcpu))
2691 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2692#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002693 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002694 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002695#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002696 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002697#else
2698 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002699#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002700 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002701 if (vmx->host_state.fs_reload_needed)
2702 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002703#ifdef CONFIG_X86_64
2704 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2705 loadsegment(ds, vmx->host_state.ds_sel);
2706 loadsegment(es, vmx->host_state.es_sel);
2707 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002708#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002709 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002710#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002711 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002712#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002713 if (vmx->host_state.msr_host_bndcfgs)
2714 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002715 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002716}
2717
Avi Kivitya9b21b62008-06-24 11:48:49 +03002718static void vmx_load_host_state(struct vcpu_vmx *vmx)
2719{
2720 preempt_disable();
2721 __vmx_load_host_state(vmx);
2722 preempt_enable();
2723}
2724
Feng Wu28b835d2015-09-18 22:29:54 +08002725static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2726{
2727 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2728 struct pi_desc old, new;
2729 unsigned int dest;
2730
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002731 /*
2732 * In case of hot-plug or hot-unplug, we may have to undo
2733 * vmx_vcpu_pi_put even if there is no assigned device. And we
2734 * always keep PI.NDST up to date for simplicity: it makes the
2735 * code easier, and CPU migration is not a fast path.
2736 */
2737 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002738 return;
2739
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002740 /*
2741 * First handle the simple case where no cmpxchg is necessary; just
2742 * allow posting non-urgent interrupts.
2743 *
2744 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2745 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2746 * expects the VCPU to be on the blocked_vcpu_list that matches
2747 * PI.NDST.
2748 */
2749 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2750 vcpu->cpu == cpu) {
2751 pi_clear_sn(pi_desc);
2752 return;
2753 }
2754
2755 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002756 do {
2757 old.control = new.control = pi_desc->control;
2758
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002759 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002760
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002761 if (x2apic_enabled())
2762 new.ndst = dest;
2763 else
2764 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002765
Feng Wu28b835d2015-09-18 22:29:54 +08002766 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002767 } while (cmpxchg64(&pi_desc->control, old.control,
2768 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002769}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002770
Peter Feinerc95ba922016-08-17 09:36:47 -07002771static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2772{
2773 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2774 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2775}
2776
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777/*
2778 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2779 * vcpu mutex is already taken.
2780 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002781static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002783 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002784 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002786 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002787 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002788 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002789 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002790
2791 /*
2792 * Read loaded_vmcs->cpu should be before fetching
2793 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2794 * See the comments in __loaded_vmcs_clear().
2795 */
2796 smp_rmb();
2797
Nadav Har'Eld462b812011-05-24 15:26:10 +03002798 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2799 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002800 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002801 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002802 }
2803
2804 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2805 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2806 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002807 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002808 }
2809
2810 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002811 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002812 unsigned long sysenter_esp;
2813
2814 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002815
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 /*
2817 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002818 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002820 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002821 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002822 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002824 /*
2825 * VM exits change the host TR limit to 0x67 after a VM
2826 * exit. This is okay, since 0x67 covers everything except
2827 * the IO bitmap and have have code to handle the IO bitmap
2828 * being lost after a VM exit.
2829 */
2830 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2831
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2833 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002834
Nadav Har'Eld462b812011-05-24 15:26:10 +03002835 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 }
Feng Wu28b835d2015-09-18 22:29:54 +08002837
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002838 /* Setup TSC multiplier */
2839 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002840 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2841 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002842
Feng Wu28b835d2015-09-18 22:29:54 +08002843 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002844 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002845 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002846}
2847
2848static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2849{
2850 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2851
2852 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002853 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2854 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002855 return;
2856
2857 /* Set SN when the vCPU is preempted */
2858 if (vcpu->preempted)
2859 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860}
2861
2862static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2863{
Feng Wu28b835d2015-09-18 22:29:54 +08002864 vmx_vcpu_pi_put(vcpu);
2865
Avi Kivitya9b21b62008-06-24 11:48:49 +03002866 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867}
2868
Wanpeng Lif244dee2017-07-20 01:11:54 -07002869static bool emulation_required(struct kvm_vcpu *vcpu)
2870{
2871 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2872}
2873
Avi Kivityedcafe32009-12-30 18:07:40 +02002874static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2875
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002876/*
2877 * Return the cr0 value that a nested guest would read. This is a combination
2878 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2879 * its hypervisor (cr0_read_shadow).
2880 */
2881static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2882{
2883 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2884 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2885}
2886static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2887{
2888 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2889 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2890}
2891
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2893{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002894 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002895
Avi Kivity6de12732011-03-07 12:51:22 +02002896 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2897 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2898 rflags = vmcs_readl(GUEST_RFLAGS);
2899 if (to_vmx(vcpu)->rmode.vm86_active) {
2900 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2901 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2902 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2903 }
2904 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002905 }
Avi Kivity6de12732011-03-07 12:51:22 +02002906 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907}
2908
2909static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2910{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002911 unsigned long old_rflags = vmx_get_rflags(vcpu);
2912
Avi Kivity6de12732011-03-07 12:51:22 +02002913 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2914 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002915 if (to_vmx(vcpu)->rmode.vm86_active) {
2916 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002917 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002918 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002920
2921 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2922 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923}
2924
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002925static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002926{
2927 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2928 int ret = 0;
2929
2930 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002931 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002932 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002933 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002934
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002935 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002936}
2937
2938static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2939{
2940 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2941 u32 interruptibility = interruptibility_old;
2942
2943 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2944
Jan Kiszka48005f62010-02-19 19:38:07 +01002945 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002946 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002947 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002948 interruptibility |= GUEST_INTR_STATE_STI;
2949
2950 if ((interruptibility != interruptibility_old))
2951 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2952}
2953
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2955{
2956 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002958 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002960 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961
Glauber Costa2809f5d2009-05-12 16:21:05 -04002962 /* skipping an emulated instruction also counts */
2963 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964}
2965
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002966static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2967 unsigned long exit_qual)
2968{
2969 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2970 unsigned int nr = vcpu->arch.exception.nr;
2971 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2972
2973 if (vcpu->arch.exception.has_error_code) {
2974 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2975 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2976 }
2977
2978 if (kvm_exception_is_soft(nr))
2979 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2980 else
2981 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2982
2983 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2984 vmx_get_nmi_mask(vcpu))
2985 intr_info |= INTR_INFO_UNBLOCK_NMI;
2986
2987 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2988}
2989
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002990/*
2991 * KVM wants to inject page-faults which it got to the guest. This function
2992 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002993 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002994static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002995{
2996 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002997 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002998
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002999 if (nr == PF_VECTOR) {
3000 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003001 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003002 return 1;
3003 }
3004 /*
3005 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3006 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3007 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3008 * can be written only when inject_pending_event runs. This should be
3009 * conditional on a new capability---if the capability is disabled,
3010 * kvm_multiple_exception would write the ancillary information to
3011 * CR2 or DR6, for backwards ABI-compatibility.
3012 */
3013 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3014 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003015 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003016 return 1;
3017 }
3018 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003019 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003020 if (nr == DB_VECTOR)
3021 *exit_qual = vcpu->arch.dr6;
3022 else
3023 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003024 return 1;
3025 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003026 }
3027
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003028 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003029}
3030
Wanpeng Licaa057a2018-03-12 04:53:03 -07003031static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3032{
3033 /*
3034 * Ensure that we clear the HLT state in the VMCS. We don't need to
3035 * explicitly skip the instruction because if the HLT state is set,
3036 * then the instruction is already executing and RIP has already been
3037 * advanced.
3038 */
3039 if (kvm_hlt_in_guest(vcpu->kvm) &&
3040 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3041 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3042}
3043
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003044static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003045{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003047 unsigned nr = vcpu->arch.exception.nr;
3048 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003049 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003050 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003051
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003052 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003053 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003054 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3055 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003056
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003057 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003058 int inc_eip = 0;
3059 if (kvm_exception_is_soft(nr))
3060 inc_eip = vcpu->arch.event_exit_inst_len;
3061 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003062 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003063 return;
3064 }
3065
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003066 WARN_ON_ONCE(vmx->emulation_required);
3067
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003068 if (kvm_exception_is_soft(nr)) {
3069 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3070 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003071 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3072 } else
3073 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3074
3075 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003076
3077 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003078}
3079
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003080static bool vmx_rdtscp_supported(void)
3081{
3082 return cpu_has_vmx_rdtscp();
3083}
3084
Mao, Junjiead756a12012-07-02 01:18:48 +00003085static bool vmx_invpcid_supported(void)
3086{
3087 return cpu_has_vmx_invpcid() && enable_ept;
3088}
3089
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090/*
Eddie Donga75beee2007-05-17 18:55:15 +03003091 * Swap MSR entry in host/guest MSR entry array.
3092 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003093static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003094{
Avi Kivity26bb0982009-09-07 11:14:12 +03003095 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003096
3097 tmp = vmx->guest_msrs[to];
3098 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3099 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003100}
3101
3102/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003103 * Set up the vmcs to automatically save and restore system
3104 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3105 * mode, as fiddling with msrs is very expensive.
3106 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003107static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003108{
Avi Kivity26bb0982009-09-07 11:14:12 +03003109 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003110
Eddie Donga75beee2007-05-17 18:55:15 +03003111 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003112#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003113 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003114 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003115 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003116 move_msr_up(vmx, index, save_nmsrs++);
3117 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003118 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003119 move_msr_up(vmx, index, save_nmsrs++);
3120 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003121 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003122 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003123 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003124 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003125 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003126 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003127 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003128 * if efer.sce is enabled.
3129 */
Brian Gerst8c065852010-07-17 09:03:26 -04003130 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003131 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003132 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003133 }
Eddie Donga75beee2007-05-17 18:55:15 +03003134#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003135 index = __find_msr_index(vmx, MSR_EFER);
3136 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003137 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003138
Avi Kivity26bb0982009-09-07 11:14:12 +03003139 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003140
Yang Zhang8d146952013-01-25 10:18:50 +08003141 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003142 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003143}
3144
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003145static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003147 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003149 if (is_guest_mode(vcpu) &&
3150 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3151 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3152
3153 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154}
3155
3156/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003157 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003159static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003161 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003162 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003163 * We're here if L1 chose not to trap WRMSR to TSC. According
3164 * to the spec, this should set L1's TSC; The offset that L1
3165 * set for L2 remains unchanged, and still needs to be added
3166 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003167 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003168 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003169 /* recalculate vmcs02.TSC_OFFSET: */
3170 vmcs12 = get_vmcs12(vcpu);
3171 vmcs_write64(TSC_OFFSET, offset +
3172 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3173 vmcs12->tsc_offset : 0));
3174 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003175 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3176 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003177 vmcs_write64(TSC_OFFSET, offset);
3178 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179}
3180
Nadav Har'El801d3422011-05-25 23:02:23 +03003181/*
3182 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3183 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3184 * all guests if the "nested" module option is off, and can also be disabled
3185 * for a single guest by disabling its VMX cpuid bit.
3186 */
3187static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3188{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003189 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003190}
3191
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003193 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3194 * returned for the various VMX controls MSRs when nested VMX is enabled.
3195 * The same values should also be used to verify that vmcs12 control fields are
3196 * valid during nested entry from L1 to L2.
3197 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3198 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3199 * bit in the high half is on if the corresponding bit in the control field
3200 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003202static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203{
Paolo Bonzini13893092018-02-26 13:40:09 +01003204 if (!nested) {
3205 memset(msrs, 0, sizeof(*msrs));
3206 return;
3207 }
3208
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003209 /*
3210 * Note that as a general rule, the high half of the MSRs (bits in
3211 * the control fields which may be 1) should be initialized by the
3212 * intersection of the underlying hardware's MSR (i.e., features which
3213 * can be supported) and the list of features we want to expose -
3214 * because they are known to be properly supported in our code.
3215 * Also, usually, the low half of the MSRs (bits which must be 1) can
3216 * be set to 0, meaning that L1 may turn off any of these bits. The
3217 * reason is that if one of these bits is necessary, it will appear
3218 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3219 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003220 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003221 * These rules have exceptions below.
3222 */
3223
3224 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003225 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003226 msrs->pinbased_ctls_low,
3227 msrs->pinbased_ctls_high);
3228 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003229 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003230 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003231 PIN_BASED_EXT_INTR_MASK |
3232 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003233 PIN_BASED_VIRTUAL_NMIS |
3234 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003236 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003237 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003238
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003239 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003240 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->exit_ctls_low,
3242 msrs->exit_ctls_high);
3243 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003244 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003245
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003246 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003247#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003248 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003250 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003252 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003253 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003254 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3255
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003256 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003257 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003258
Jan Kiszka2996fca2014-06-16 13:59:43 +02003259 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003260 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003261
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003262 /* entry controls */
3263 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003264 msrs->entry_ctls_low,
3265 msrs->entry_ctls_high);
3266 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003267 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003268 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003269#ifdef CONFIG_X86_64
3270 VM_ENTRY_IA32E_MODE |
3271#endif
3272 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003274 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003275 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003276 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003277
Jan Kiszka2996fca2014-06-16 13:59:43 +02003278 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003280
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003281 /* cpu-based controls */
3282 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003283 msrs->procbased_ctls_low,
3284 msrs->procbased_ctls_high);
3285 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003286 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003287 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003288 CPU_BASED_VIRTUAL_INTR_PENDING |
3289 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003290 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3291 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3292 CPU_BASED_CR3_STORE_EXITING |
3293#ifdef CONFIG_X86_64
3294 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3295#endif
3296 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003297 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3298 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3299 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3300 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003301 /*
3302 * We can allow some features even when not supported by the
3303 * hardware. For example, L1 can specify an MSR bitmap - and we
3304 * can use it to avoid exits to L1 - even when L0 runs L2
3305 * without MSR bitmaps.
3306 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003307 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003308 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003309 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003310
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003311 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003312 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003313 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3314
Paolo Bonzini80154d72017-08-24 13:55:35 +02003315 /*
3316 * secondary cpu-based controls. Do not include those that
3317 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3318 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003319 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003320 msrs->secondary_ctls_low,
3321 msrs->secondary_ctls_high);
3322 msrs->secondary_ctls_low = 0;
3323 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003324 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003325 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003326 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003327 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003328 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003329 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003330
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003331 if (enable_ept) {
3332 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003334 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003336 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003337 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003338 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003339 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->ept_caps &= vmx_capability.ept;
3341 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003342 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3343 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003344 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003345 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003346 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003348 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003349 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003350
Bandan Das27c42a12017-08-03 15:54:42 -04003351 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003352 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003353 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003354 /*
3355 * Advertise EPTP switching unconditionally
3356 * since we emulate it
3357 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003358 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003359 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003360 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003361 }
3362
Paolo Bonzinief697a72016-03-18 16:58:38 +01003363 /*
3364 * Old versions of KVM use the single-context version without
3365 * checking for support, so declare that it is supported even
3366 * though it is treated as global context. The alternative is
3367 * not failing the single-context invvpid, and it is worse.
3368 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003369 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003370 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003371 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003372 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003373 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003374 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003375
Radim Krčmář0790ec12015-03-17 14:02:32 +01003376 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003377 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003378 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3379
Jan Kiszkac18911a2013-03-13 16:06:41 +01003380 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003381 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003382 msrs->misc_low,
3383 msrs->misc_high);
3384 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3385 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003386 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003387 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003388 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003389 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003390
3391 /*
3392 * This MSR reports some information about VMX support. We
3393 * should return information about the VMX we emulate for the
3394 * guest, and the VMCS structure we give it - not about the
3395 * VMX support of the underlying hardware.
3396 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003398 VMCS12_REVISION |
3399 VMX_BASIC_TRUE_CTLS |
3400 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3401 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3402
3403 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003405
3406 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003407 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003408 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3409 * We picked the standard core2 setting.
3410 */
3411#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3412#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003413 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3414 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003415
3416 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003417 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3418 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003419
3420 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003421 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003422}
3423
David Matlack38991522016-11-29 18:14:08 -08003424/*
3425 * if fixed0[i] == 1: val[i] must be 1
3426 * if fixed1[i] == 0: val[i] must be 0
3427 */
3428static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3429{
3430 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003431}
3432
3433static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3434{
David Matlack38991522016-11-29 18:14:08 -08003435 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003436}
3437
3438static inline u64 vmx_control_msr(u32 low, u32 high)
3439{
3440 return low | ((u64)high << 32);
3441}
3442
David Matlack62cc6b9d2016-11-29 18:14:07 -08003443static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3444{
3445 superset &= mask;
3446 subset &= mask;
3447
3448 return (superset | subset) == superset;
3449}
3450
3451static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3452{
3453 const u64 feature_and_reserved =
3454 /* feature (except bit 48; see below) */
3455 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3456 /* reserved */
3457 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003459
3460 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3461 return -EINVAL;
3462
3463 /*
3464 * KVM does not emulate a version of VMX that constrains physical
3465 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3466 */
3467 if (data & BIT_ULL(48))
3468 return -EINVAL;
3469
3470 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3471 vmx_basic_vmcs_revision_id(data))
3472 return -EINVAL;
3473
3474 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3475 return -EINVAL;
3476
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003477 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003478 return 0;
3479}
3480
3481static int
3482vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3483{
3484 u64 supported;
3485 u32 *lowp, *highp;
3486
3487 switch (msr_index) {
3488 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3490 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003491 break;
3492 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003493 lowp = &vmx->nested.msrs.procbased_ctls_low;
3494 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495 break;
3496 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 lowp = &vmx->nested.msrs.exit_ctls_low;
3498 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003499 break;
3500 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003501 lowp = &vmx->nested.msrs.entry_ctls_low;
3502 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003503 break;
3504 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 lowp = &vmx->nested.msrs.secondary_ctls_low;
3506 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003507 break;
3508 default:
3509 BUG();
3510 }
3511
3512 supported = vmx_control_msr(*lowp, *highp);
3513
3514 /* Check must-be-1 bits are still 1. */
3515 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3516 return -EINVAL;
3517
3518 /* Check must-be-0 bits are still 0. */
3519 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3520 return -EINVAL;
3521
3522 *lowp = data;
3523 *highp = data >> 32;
3524 return 0;
3525}
3526
3527static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3528{
3529 const u64 feature_and_reserved_bits =
3530 /* feature */
3531 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3532 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3533 /* reserved */
3534 GENMASK_ULL(13, 9) | BIT_ULL(31);
3535 u64 vmx_misc;
3536
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3538 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003539
3540 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3541 return -EINVAL;
3542
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003544 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3545 vmx_misc_preemption_timer_rate(data) !=
3546 vmx_misc_preemption_timer_rate(vmx_misc))
3547 return -EINVAL;
3548
3549 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3550 return -EINVAL;
3551
3552 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3553 return -EINVAL;
3554
3555 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3556 return -EINVAL;
3557
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 vmx->nested.msrs.misc_low = data;
3559 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003560
3561 /*
3562 * If L1 has read-only VM-exit information fields, use the
3563 * less permissive vmx_vmwrite_bitmap to specify write
3564 * permissions for the shadow VMCS.
3565 */
3566 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3567 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3568
David Matlack62cc6b9d2016-11-29 18:14:07 -08003569 return 0;
3570}
3571
3572static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3573{
3574 u64 vmx_ept_vpid_cap;
3575
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3577 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003578
3579 /* Every bit is either reserved or a feature bit. */
3580 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3581 return -EINVAL;
3582
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 vmx->nested.msrs.ept_caps = data;
3584 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003585 return 0;
3586}
3587
3588static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3589{
3590 u64 *msr;
3591
3592 switch (msr_index) {
3593 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003594 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003595 break;
3596 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003597 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003598 break;
3599 default:
3600 BUG();
3601 }
3602
3603 /*
3604 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3605 * must be 1 in the restored value.
3606 */
3607 if (!is_bitwise_subset(data, *msr, -1ULL))
3608 return -EINVAL;
3609
3610 *msr = data;
3611 return 0;
3612}
3613
3614/*
3615 * Called when userspace is restoring VMX MSRs.
3616 *
3617 * Returns 0 on success, non-0 otherwise.
3618 */
3619static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3620{
3621 struct vcpu_vmx *vmx = to_vmx(vcpu);
3622
Jim Mattsona943ac52018-05-29 09:11:32 -07003623 /*
3624 * Don't allow changes to the VMX capability MSRs while the vCPU
3625 * is in VMX operation.
3626 */
3627 if (vmx->nested.vmxon)
3628 return -EBUSY;
3629
David Matlack62cc6b9d2016-11-29 18:14:07 -08003630 switch (msr_index) {
3631 case MSR_IA32_VMX_BASIC:
3632 return vmx_restore_vmx_basic(vmx, data);
3633 case MSR_IA32_VMX_PINBASED_CTLS:
3634 case MSR_IA32_VMX_PROCBASED_CTLS:
3635 case MSR_IA32_VMX_EXIT_CTLS:
3636 case MSR_IA32_VMX_ENTRY_CTLS:
3637 /*
3638 * The "non-true" VMX capability MSRs are generated from the
3639 * "true" MSRs, so we do not support restoring them directly.
3640 *
3641 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3642 * should restore the "true" MSRs with the must-be-1 bits
3643 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3644 * DEFAULT SETTINGS".
3645 */
3646 return -EINVAL;
3647 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3648 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3649 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3650 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3651 case MSR_IA32_VMX_PROCBASED_CTLS2:
3652 return vmx_restore_control_msr(vmx, msr_index, data);
3653 case MSR_IA32_VMX_MISC:
3654 return vmx_restore_vmx_misc(vmx, data);
3655 case MSR_IA32_VMX_CR0_FIXED0:
3656 case MSR_IA32_VMX_CR4_FIXED0:
3657 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3658 case MSR_IA32_VMX_CR0_FIXED1:
3659 case MSR_IA32_VMX_CR4_FIXED1:
3660 /*
3661 * These MSRs are generated based on the vCPU's CPUID, so we
3662 * do not support restoring them directly.
3663 */
3664 return -EINVAL;
3665 case MSR_IA32_VMX_EPT_VPID_CAP:
3666 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3667 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003669 return 0;
3670 default:
3671 /*
3672 * The rest of the VMX capability MSRs do not support restore.
3673 */
3674 return -EINVAL;
3675 }
3676}
3677
Jan Kiszkacae50132014-01-04 18:47:22 +01003678/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003679static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003680{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003682 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003684 break;
3685 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3686 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003687 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688 msrs->pinbased_ctls_low,
3689 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003690 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3691 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692 break;
3693 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3694 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003695 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003696 msrs->procbased_ctls_low,
3697 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003698 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3699 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700 break;
3701 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3702 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003703 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003704 msrs->exit_ctls_low,
3705 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003706 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3707 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 break;
3709 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3710 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003711 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003712 msrs->entry_ctls_low,
3713 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003714 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3715 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003716 break;
3717 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003718 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003719 msrs->misc_low,
3720 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003721 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003722 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003723 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003724 break;
3725 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003726 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003727 break;
3728 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003729 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730 break;
3731 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003732 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003733 break;
3734 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
3737 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003738 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 msrs->secondary_ctls_low,
3740 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003741 break;
3742 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003743 *pdata = msrs->ept_caps |
3744 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003745 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003746 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003747 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003748 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003749 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003750 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003751 }
3752
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003753 return 0;
3754}
3755
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003756static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3757 uint64_t val)
3758{
3759 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3760
3761 return !(val & ~valid_bits);
3762}
3763
Tom Lendacky801e4592018-02-21 13:39:51 -06003764static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3765{
Paolo Bonzini13893092018-02-26 13:40:09 +01003766 switch (msr->index) {
3767 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3768 if (!nested)
3769 return 1;
3770 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3771 default:
3772 return 1;
3773 }
3774
3775 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003776}
3777
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003778/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 * Reads an msr value (of 'msr_index') into 'pdata'.
3780 * Returns 0 on success, non-0 otherwise.
3781 * Assumes vcpu_load() was already called.
3782 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003783static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003785 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003786 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003788 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003789#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003791 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003792 break;
3793 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003794 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003796 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003797 vmx_load_host_state(vmx);
3798 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003799 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003800#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003802 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003803 case MSR_IA32_SPEC_CTRL:
3804 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003805 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3806 return 1;
3807
3808 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3809 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003810 case MSR_IA32_ARCH_CAPABILITIES:
3811 if (!msr_info->host_initiated &&
3812 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3813 return 1;
3814 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3815 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003817 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818 break;
3819 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003820 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003821 break;
3822 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003823 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003825 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003826 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003827 (!msr_info->host_initiated &&
3828 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003829 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003830 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003831 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003832 case MSR_IA32_MCG_EXT_CTL:
3833 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003834 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003835 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003836 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003837 msr_info->data = vcpu->arch.mcg_ext_ctl;
3838 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003839 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003840 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003841 break;
3842 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3843 if (!nested_vmx_allowed(vcpu))
3844 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003845 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3846 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003847 case MSR_IA32_XSS:
3848 if (!vmx_xsaves_supported())
3849 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003851 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003852 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003853 if (!msr_info->host_initiated &&
3854 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003855 return 1;
3856 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003858 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003859 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003860 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003863 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 }
3865
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 return 0;
3867}
3868
Jan Kiszkacae50132014-01-04 18:47:22 +01003869static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3870
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871/*
3872 * Writes msr value into into the appropriate "register".
3873 * Returns 0 on success, non-0 otherwise.
3874 * Assumes vcpu_load() was already called.
3875 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003876static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003879 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003880 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003881 u32 msr_index = msr_info->index;
3882 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003883
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003885 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003886 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003887 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003888#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003890 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891 vmcs_writel(GUEST_FS_BASE, data);
3892 break;
3893 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003894 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895 vmcs_writel(GUEST_GS_BASE, data);
3896 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003897 case MSR_KERNEL_GS_BASE:
3898 vmx_load_host_state(vmx);
3899 vmx->msr_guest_kernel_gs_base = data;
3900 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901#endif
3902 case MSR_IA32_SYSENTER_CS:
3903 vmcs_write32(GUEST_SYSENTER_CS, data);
3904 break;
3905 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003906 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 break;
3908 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003909 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003911 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003912 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003913 (!msr_info->host_initiated &&
3914 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003915 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003916 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003917 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003919 vmcs_write64(GUEST_BNDCFGS, data);
3920 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003921 case MSR_IA32_SPEC_CTRL:
3922 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003923 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3924 return 1;
3925
3926 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003927 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003928 return 1;
3929
3930 vmx->spec_ctrl = data;
3931
3932 if (!data)
3933 break;
3934
3935 /*
3936 * For non-nested:
3937 * When it's written (to non-zero) for the first time, pass
3938 * it through.
3939 *
3940 * For nested:
3941 * The handling of the MSR bitmap for L2 guests is done in
3942 * nested_vmx_merge_msr_bitmap. We should not touch the
3943 * vmcs02.msr_bitmap here since it gets completely overwritten
3944 * in the merging. We update the vmcs01 here for L1 as well
3945 * since it will end up touching the MSR anyway now.
3946 */
3947 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3948 MSR_IA32_SPEC_CTRL,
3949 MSR_TYPE_RW);
3950 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003951 case MSR_IA32_PRED_CMD:
3952 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003953 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3954 return 1;
3955
3956 if (data & ~PRED_CMD_IBPB)
3957 return 1;
3958
3959 if (!data)
3960 break;
3961
3962 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3963
3964 /*
3965 * For non-nested:
3966 * When it's written (to non-zero) for the first time, pass
3967 * it through.
3968 *
3969 * For nested:
3970 * The handling of the MSR bitmap for L2 guests is done in
3971 * nested_vmx_merge_msr_bitmap. We should not touch the
3972 * vmcs02.msr_bitmap here since it gets completely overwritten
3973 * in the merging.
3974 */
3975 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3976 MSR_TYPE_W);
3977 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003978 case MSR_IA32_ARCH_CAPABILITIES:
3979 if (!msr_info->host_initiated)
3980 return 1;
3981 vmx->arch_capabilities = data;
3982 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003983 case MSR_IA32_CR_PAT:
3984 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003985 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3986 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003987 vmcs_write64(GUEST_IA32_PAT, data);
3988 vcpu->arch.pat = data;
3989 break;
3990 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003991 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003992 break;
Will Auldba904632012-11-29 12:42:50 -08003993 case MSR_IA32_TSC_ADJUST:
3994 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003995 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003996 case MSR_IA32_MCG_EXT_CTL:
3997 if ((!msr_info->host_initiated &&
3998 !(to_vmx(vcpu)->msr_ia32_feature_control &
3999 FEATURE_CONTROL_LMCE)) ||
4000 (data & ~MCG_EXT_CTL_LMCE_EN))
4001 return 1;
4002 vcpu->arch.mcg_ext_ctl = data;
4003 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004004 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004005 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004006 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004007 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4008 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004009 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004010 if (msr_info->host_initiated && data == 0)
4011 vmx_leave_nested(vcpu);
4012 break;
4013 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004014 if (!msr_info->host_initiated)
4015 return 1; /* they are read-only */
4016 if (!nested_vmx_allowed(vcpu))
4017 return 1;
4018 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004019 case MSR_IA32_XSS:
4020 if (!vmx_xsaves_supported())
4021 return 1;
4022 /*
4023 * The only supported bit as of Skylake is bit 8, but
4024 * it is not supported on KVM.
4025 */
4026 if (data != 0)
4027 return 1;
4028 vcpu->arch.ia32_xss = data;
4029 if (vcpu->arch.ia32_xss != host_xss)
4030 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4031 vcpu->arch.ia32_xss, host_xss);
4032 else
4033 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4034 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004035 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004036 if (!msr_info->host_initiated &&
4037 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004038 return 1;
4039 /* Check reserved bit, higher 32 bits should be zero */
4040 if ((data >> 32) != 0)
4041 return 1;
4042 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004044 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004045 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004046 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004047 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004048 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4049 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004050 ret = kvm_set_shared_msr(msr->index, msr->data,
4051 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004052 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004053 if (ret)
4054 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004055 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004056 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004058 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 }
4060
Eddie Dong2cc51562007-05-21 07:28:09 +03004061 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062}
4063
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004064static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004066 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4067 switch (reg) {
4068 case VCPU_REGS_RSP:
4069 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4070 break;
4071 case VCPU_REGS_RIP:
4072 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4073 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004074 case VCPU_EXREG_PDPTR:
4075 if (enable_ept)
4076 ept_save_pdptrs(vcpu);
4077 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004078 default:
4079 break;
4080 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081}
4082
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083static __init int cpu_has_kvm_support(void)
4084{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004085 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086}
4087
4088static __init int vmx_disabled_by_bios(void)
4089{
4090 u64 msr;
4091
4092 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004093 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004094 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004095 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4096 && tboot_enabled())
4097 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004098 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004099 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004100 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004101 && !tboot_enabled()) {
4102 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004103 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004104 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004105 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004106 /* launched w/o TXT and VMX disabled */
4107 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4108 && !tboot_enabled())
4109 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004110 }
4111
4112 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113}
4114
Dongxiao Xu7725b892010-05-11 18:29:38 +08004115static void kvm_cpu_vmxon(u64 addr)
4116{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004117 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004118 intel_pt_handle_vmx(1);
4119
Dongxiao Xu7725b892010-05-11 18:29:38 +08004120 asm volatile (ASM_VMX_VMXON_RAX
4121 : : "a"(&addr), "m"(addr)
4122 : "memory", "cc");
4123}
4124
Radim Krčmář13a34e02014-08-28 15:13:03 +02004125static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126{
4127 int cpu = raw_smp_processor_id();
4128 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004129 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004131 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004132 return -EBUSY;
4133
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004134 /*
4135 * This can happen if we hot-added a CPU but failed to allocate
4136 * VP assist page for it.
4137 */
4138 if (static_branch_unlikely(&enable_evmcs) &&
4139 !hv_get_vp_assist_page(cpu))
4140 return -EFAULT;
4141
Nadav Har'Eld462b812011-05-24 15:26:10 +03004142 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004143 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4144 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004145
4146 /*
4147 * Now we can enable the vmclear operation in kdump
4148 * since the loaded_vmcss_on_cpu list on this cpu
4149 * has been initialized.
4150 *
4151 * Though the cpu is not in VMX operation now, there
4152 * is no problem to enable the vmclear operation
4153 * for the loaded_vmcss_on_cpu list is empty!
4154 */
4155 crash_enable_local_vmclear(cpu);
4156
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004158
4159 test_bits = FEATURE_CONTROL_LOCKED;
4160 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4161 if (tboot_enabled())
4162 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4163
4164 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004166 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4167 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004168 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004169 if (enable_ept)
4170 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004171
4172 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173}
4174
Nadav Har'Eld462b812011-05-24 15:26:10 +03004175static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004176{
4177 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004178 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004179
Nadav Har'Eld462b812011-05-24 15:26:10 +03004180 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4181 loaded_vmcss_on_cpu_link)
4182 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004183}
4184
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004185
4186/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4187 * tricks.
4188 */
4189static void kvm_cpu_vmxoff(void)
4190{
4191 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004192
4193 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004194 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004195}
4196
Radim Krčmář13a34e02014-08-28 15:13:03 +02004197static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004199 vmclear_local_loaded_vmcss();
4200 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201}
4202
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004203static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004204 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205{
4206 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004207 u32 ctl = ctl_min | ctl_opt;
4208
4209 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4210
4211 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4212 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4213
4214 /* Ensure minimum (required) set of control bits are supported. */
4215 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004216 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004217
4218 *result = ctl;
4219 return 0;
4220}
4221
Avi Kivity110312c2010-12-21 12:54:20 +02004222static __init bool allow_1_setting(u32 msr, u32 ctl)
4223{
4224 u32 vmx_msr_low, vmx_msr_high;
4225
4226 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4227 return vmx_msr_high & ctl;
4228}
4229
Yang, Sheng002c7f72007-07-31 14:23:01 +03004230static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004231{
4232 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004233 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004234 u32 _pin_based_exec_control = 0;
4235 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004236 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004237 u32 _vmexit_control = 0;
4238 u32 _vmentry_control = 0;
4239
Paolo Bonzini13893092018-02-26 13:40:09 +01004240 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304241 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004242#ifdef CONFIG_X86_64
4243 CPU_BASED_CR8_LOAD_EXITING |
4244 CPU_BASED_CR8_STORE_EXITING |
4245#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004246 CPU_BASED_CR3_LOAD_EXITING |
4247 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004248 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004249 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004250 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004251 CPU_BASED_MWAIT_EXITING |
4252 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004253 CPU_BASED_INVLPG_EXITING |
4254 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004255
Sheng Yangf78e0e22007-10-29 09:40:42 +08004256 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004257 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004258 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004259 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4260 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004261 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004262#ifdef CONFIG_X86_64
4263 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4264 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4265 ~CPU_BASED_CR8_STORE_EXITING;
4266#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004267 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004268 min2 = 0;
4269 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004270 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004271 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004272 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004273 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004274 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004275 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004276 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004277 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004278 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004279 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004280 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004281 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004282 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004283 SECONDARY_EXEC_RDSEED_EXITING |
4284 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004285 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004286 SECONDARY_EXEC_TSC_SCALING |
4287 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004288 if (adjust_vmx_controls(min2, opt2,
4289 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004290 &_cpu_based_2nd_exec_control) < 0)
4291 return -EIO;
4292 }
4293#ifndef CONFIG_X86_64
4294 if (!(_cpu_based_2nd_exec_control &
4295 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4296 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4297#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004298
4299 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4300 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004301 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004302 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4303 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004304
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004305 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4306 &vmx_capability.ept, &vmx_capability.vpid);
4307
Sheng Yangd56f5462008-04-25 10:13:16 +08004308 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004309 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4310 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004311 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4312 CPU_BASED_CR3_STORE_EXITING |
4313 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004314 } else if (vmx_capability.ept) {
4315 vmx_capability.ept = 0;
4316 pr_warn_once("EPT CAP should not exist if not support "
4317 "1-setting enable EPT VM-execution control\n");
4318 }
4319 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4320 vmx_capability.vpid) {
4321 vmx_capability.vpid = 0;
4322 pr_warn_once("VPID CAP should not exist if not support "
4323 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004324 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004325
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004326 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004327#ifdef CONFIG_X86_64
4328 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4329#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004330 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004331 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004332 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4333 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004335
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004336 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4337 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4338 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004339 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4340 &_pin_based_exec_control) < 0)
4341 return -EIO;
4342
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004343 if (cpu_has_broken_vmx_preemption_timer())
4344 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004345 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004346 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004347 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4348
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004349 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004350 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004351 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4352 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004353 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004355 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004356
4357 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4358 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004359 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004360
4361#ifdef CONFIG_X86_64
4362 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4363 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004364 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004365#endif
4366
4367 /* Require Write-Back (WB) memory type for VMCS accesses. */
4368 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004369 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004370
Yang, Sheng002c7f72007-07-31 14:23:01 +03004371 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004372 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004373 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004374
4375 /* KVM supports Enlightened VMCS v1 only */
4376 if (static_branch_unlikely(&enable_evmcs))
4377 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4378 else
4379 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004380
Yang, Sheng002c7f72007-07-31 14:23:01 +03004381 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4382 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004383 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004384 vmcs_conf->vmexit_ctrl = _vmexit_control;
4385 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004386
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004387 if (static_branch_unlikely(&enable_evmcs))
4388 evmcs_sanitize_exec_ctrls(vmcs_conf);
4389
Avi Kivity110312c2010-12-21 12:54:20 +02004390 cpu_has_load_ia32_efer =
4391 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4392 VM_ENTRY_LOAD_IA32_EFER)
4393 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4394 VM_EXIT_LOAD_IA32_EFER);
4395
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004396 cpu_has_load_perf_global_ctrl =
4397 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4398 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4399 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4400 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4401
4402 /*
4403 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004404 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004405 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4406 *
4407 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4408 *
4409 * AAK155 (model 26)
4410 * AAP115 (model 30)
4411 * AAT100 (model 37)
4412 * BC86,AAY89,BD102 (model 44)
4413 * BA97 (model 46)
4414 *
4415 */
4416 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4417 switch (boot_cpu_data.x86_model) {
4418 case 26:
4419 case 30:
4420 case 37:
4421 case 44:
4422 case 46:
4423 cpu_has_load_perf_global_ctrl = false;
4424 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4425 "does not work properly. Using workaround\n");
4426 break;
4427 default:
4428 break;
4429 }
4430 }
4431
Borislav Petkov782511b2016-04-04 22:25:03 +02004432 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004433 rdmsrl(MSR_IA32_XSS, host_xss);
4434
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004435 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004436}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004437
4438static struct vmcs *alloc_vmcs_cpu(int cpu)
4439{
4440 int node = cpu_to_node(cpu);
4441 struct page *pages;
4442 struct vmcs *vmcs;
4443
Vlastimil Babka96db8002015-09-08 15:03:50 -07004444 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004445 if (!pages)
4446 return NULL;
4447 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004448 memset(vmcs, 0, vmcs_config.size);
4449 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004450 return vmcs;
4451}
4452
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453static void free_vmcs(struct vmcs *vmcs)
4454{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004455 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456}
4457
Nadav Har'Eld462b812011-05-24 15:26:10 +03004458/*
4459 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4460 */
4461static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4462{
4463 if (!loaded_vmcs->vmcs)
4464 return;
4465 loaded_vmcs_clear(loaded_vmcs);
4466 free_vmcs(loaded_vmcs->vmcs);
4467 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004468 if (loaded_vmcs->msr_bitmap)
4469 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004470 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004471}
4472
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004473static struct vmcs *alloc_vmcs(void)
4474{
4475 return alloc_vmcs_cpu(raw_smp_processor_id());
4476}
4477
4478static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4479{
4480 loaded_vmcs->vmcs = alloc_vmcs();
4481 if (!loaded_vmcs->vmcs)
4482 return -ENOMEM;
4483
4484 loaded_vmcs->shadow_vmcs = NULL;
4485 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004486
4487 if (cpu_has_vmx_msr_bitmap()) {
4488 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4489 if (!loaded_vmcs->msr_bitmap)
4490 goto out_vmcs;
4491 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004492
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004493 if (IS_ENABLED(CONFIG_HYPERV) &&
4494 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004495 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4496 struct hv_enlightened_vmcs *evmcs =
4497 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4498
4499 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4500 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004501 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004502 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004503
4504out_vmcs:
4505 free_loaded_vmcs(loaded_vmcs);
4506 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004507}
4508
Sam Ravnborg39959582007-06-01 00:47:13 -07004509static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510{
4511 int cpu;
4512
Zachary Amsden3230bb42009-09-29 11:38:37 -10004513 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004515 per_cpu(vmxarea, cpu) = NULL;
4516 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517}
4518
Jim Mattsond37f4262017-12-22 12:12:16 -08004519enum vmcs_field_width {
4520 VMCS_FIELD_WIDTH_U16 = 0,
4521 VMCS_FIELD_WIDTH_U64 = 1,
4522 VMCS_FIELD_WIDTH_U32 = 2,
4523 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004524};
4525
Jim Mattsond37f4262017-12-22 12:12:16 -08004526static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004527{
4528 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004529 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004530 return (field >> 13) & 0x3 ;
4531}
4532
4533static inline int vmcs_field_readonly(unsigned long field)
4534{
4535 return (((field >> 10) & 0x3) == 1);
4536}
4537
Bandan Dasfe2b2012014-04-21 15:20:14 -04004538static void init_vmcs_shadow_fields(void)
4539{
4540 int i, j;
4541
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004542 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4543 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004544 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004545 (i + 1 == max_shadow_read_only_fields ||
4546 shadow_read_only_fields[i + 1] != field + 1))
4547 pr_err("Missing field from shadow_read_only_field %x\n",
4548 field + 1);
4549
4550 clear_bit(field, vmx_vmread_bitmap);
4551#ifdef CONFIG_X86_64
4552 if (field & 1)
4553 continue;
4554#endif
4555 if (j < i)
4556 shadow_read_only_fields[j] = field;
4557 j++;
4558 }
4559 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004560
4561 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004562 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004563 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004564 (i + 1 == max_shadow_read_write_fields ||
4565 shadow_read_write_fields[i + 1] != field + 1))
4566 pr_err("Missing field from shadow_read_write_field %x\n",
4567 field + 1);
4568
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004569 /*
4570 * PML and the preemption timer can be emulated, but the
4571 * processor cannot vmwrite to fields that don't exist
4572 * on bare metal.
4573 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004574 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004575 case GUEST_PML_INDEX:
4576 if (!cpu_has_vmx_pml())
4577 continue;
4578 break;
4579 case VMX_PREEMPTION_TIMER_VALUE:
4580 if (!cpu_has_vmx_preemption_timer())
4581 continue;
4582 break;
4583 case GUEST_INTR_STATUS:
4584 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004585 continue;
4586 break;
4587 default:
4588 break;
4589 }
4590
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004591 clear_bit(field, vmx_vmwrite_bitmap);
4592 clear_bit(field, vmx_vmread_bitmap);
4593#ifdef CONFIG_X86_64
4594 if (field & 1)
4595 continue;
4596#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004597 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004598 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004599 j++;
4600 }
4601 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004602}
4603
Avi Kivity6aa8b732006-12-10 02:21:36 -08004604static __init int alloc_kvm_area(void)
4605{
4606 int cpu;
4607
Zachary Amsden3230bb42009-09-29 11:38:37 -10004608 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 struct vmcs *vmcs;
4610
4611 vmcs = alloc_vmcs_cpu(cpu);
4612 if (!vmcs) {
4613 free_kvm_area();
4614 return -ENOMEM;
4615 }
4616
4617 per_cpu(vmxarea, cpu) = vmcs;
4618 }
4619 return 0;
4620}
4621
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004622static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004623 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004624{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004625 if (!emulate_invalid_guest_state) {
4626 /*
4627 * CS and SS RPL should be equal during guest entry according
4628 * to VMX spec, but in reality it is not always so. Since vcpu
4629 * is in the middle of the transition from real mode to
4630 * protected mode it is safe to assume that RPL 0 is a good
4631 * default value.
4632 */
4633 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004634 save->selector &= ~SEGMENT_RPL_MASK;
4635 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004636 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004637 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004638 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639}
4640
4641static void enter_pmode(struct kvm_vcpu *vcpu)
4642{
4643 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004644 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004645
Gleb Natapovd99e4152012-12-20 16:57:45 +02004646 /*
4647 * Update real mode segment cache. It may be not up-to-date if sement
4648 * register was written while vcpu was in a guest mode.
4649 */
4650 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4651 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4652 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4653 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4654 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4655 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4656
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004657 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004658
Avi Kivity2fb92db2011-04-27 19:42:18 +03004659 vmx_segment_cache_clear(vmx);
4660
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004661 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662
4663 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004664 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4665 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666 vmcs_writel(GUEST_RFLAGS, flags);
4667
Rusty Russell66aee912007-07-17 23:34:16 +10004668 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4669 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670
4671 update_exception_bitmap(vcpu);
4672
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004673 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4674 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4675 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4676 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4677 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4678 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679}
4680
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004681static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682{
Mathias Krause772e0312012-08-30 01:30:19 +02004683 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004684 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685
Gleb Natapovd99e4152012-12-20 16:57:45 +02004686 var.dpl = 0x3;
4687 if (seg == VCPU_SREG_CS)
4688 var.type = 0x3;
4689
4690 if (!emulate_invalid_guest_state) {
4691 var.selector = var.base >> 4;
4692 var.base = var.base & 0xffff0;
4693 var.limit = 0xffff;
4694 var.g = 0;
4695 var.db = 0;
4696 var.present = 1;
4697 var.s = 1;
4698 var.l = 0;
4699 var.unusable = 0;
4700 var.type = 0x3;
4701 var.avl = 0;
4702 if (save->base & 0xf)
4703 printk_once(KERN_WARNING "kvm: segment base is not "
4704 "paragraph aligned when entering "
4705 "protected mode (seg=%d)", seg);
4706 }
4707
4708 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004709 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004710 vmcs_write32(sf->limit, var.limit);
4711 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712}
4713
4714static void enter_rmode(struct kvm_vcpu *vcpu)
4715{
4716 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004717 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004718 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004727
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004728 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729
Gleb Natapov776e58e2011-03-13 12:34:27 +02004730 /*
4731 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004732 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004733 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004734 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004735 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4736 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004737
Avi Kivity2fb92db2011-04-27 19:42:18 +03004738 vmx_segment_cache_clear(vmx);
4739
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004740 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4743
4744 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004745 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004747 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748
4749 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004750 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751 update_exception_bitmap(vcpu);
4752
Gleb Natapovd99e4152012-12-20 16:57:45 +02004753 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4754 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4755 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4756 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4757 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4758 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004759
Eddie Dong8668a3c2007-10-10 14:26:45 +08004760 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761}
4762
Amit Shah401d10d2009-02-20 22:53:37 +05304763static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4764{
4765 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004766 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4767
4768 if (!msr)
4769 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304770
Avi Kivity44ea2b12009-09-06 15:55:37 +03004771 /*
4772 * Force kernel_gs_base reloading before EFER changes, as control
4773 * of this msr depends on is_long_mode().
4774 */
4775 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004776 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304777 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004778 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304779 msr->data = efer;
4780 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004781 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304782
4783 msr->data = efer & ~EFER_LME;
4784 }
4785 setup_msrs(vmx);
4786}
4787
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004788#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789
4790static void enter_lmode(struct kvm_vcpu *vcpu)
4791{
4792 u32 guest_tr_ar;
4793
Avi Kivity2fb92db2011-04-27 19:42:18 +03004794 vmx_segment_cache_clear(to_vmx(vcpu));
4795
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004797 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004798 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4799 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004801 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4802 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803 }
Avi Kivityda38f432010-07-06 11:30:49 +03004804 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805}
4806
4807static void exit_lmode(struct kvm_vcpu *vcpu)
4808{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004809 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004810 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811}
4812
4813#endif
4814
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004815static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4816 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004817{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004818 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004819 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4820 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004821 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004822 } else {
4823 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004824 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004825}
4826
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004827static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004828{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004829 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004830}
4831
Avi Kivitye8467fd2009-12-29 18:43:06 +02004832static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4833{
4834 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4835
4836 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4837 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4838}
4839
Avi Kivityaff48ba2010-12-05 18:56:11 +02004840static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4841{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004842 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004843 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4844 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4845}
4846
Anthony Liguori25c4c272007-04-27 09:29:21 +03004847static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004848{
Avi Kivityfc78f512009-12-07 12:16:48 +02004849 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4850
4851 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4852 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004853}
4854
Sheng Yang14394422008-04-28 12:24:45 +08004855static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4856{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004857 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4858
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004859 if (!test_bit(VCPU_EXREG_PDPTR,
4860 (unsigned long *)&vcpu->arch.regs_dirty))
4861 return;
4862
Sheng Yang14394422008-04-28 12:24:45 +08004863 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004864 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4865 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4866 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4867 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004868 }
4869}
4870
Avi Kivity8f5d5492009-05-31 18:41:29 +03004871static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4872{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004873 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4874
Avi Kivity8f5d5492009-05-31 18:41:29 +03004875 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004876 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4877 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4878 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4879 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004880 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004881
4882 __set_bit(VCPU_EXREG_PDPTR,
4883 (unsigned long *)&vcpu->arch.regs_avail);
4884 __set_bit(VCPU_EXREG_PDPTR,
4885 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004886}
4887
David Matlack38991522016-11-29 18:14:08 -08004888static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4889{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004890 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4891 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4893
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004894 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004895 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4896 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4897 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4898
4899 return fixed_bits_valid(val, fixed0, fixed1);
4900}
4901
4902static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4903{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004904 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4905 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004906
4907 return fixed_bits_valid(val, fixed0, fixed1);
4908}
4909
4910static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4911{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004912 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4913 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004914
4915 return fixed_bits_valid(val, fixed0, fixed1);
4916}
4917
4918/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4919#define nested_guest_cr4_valid nested_cr4_valid
4920#define nested_host_cr4_valid nested_cr4_valid
4921
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004922static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004923
4924static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4925 unsigned long cr0,
4926 struct kvm_vcpu *vcpu)
4927{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004928 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4929 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004930 if (!(cr0 & X86_CR0_PG)) {
4931 /* From paging/starting to nonpaging */
4932 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004933 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004934 (CPU_BASED_CR3_LOAD_EXITING |
4935 CPU_BASED_CR3_STORE_EXITING));
4936 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004937 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004938 } else if (!is_paging(vcpu)) {
4939 /* From nonpaging to paging */
4940 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004941 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004942 ~(CPU_BASED_CR3_LOAD_EXITING |
4943 CPU_BASED_CR3_STORE_EXITING));
4944 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004945 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004946 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004947
4948 if (!(cr0 & X86_CR0_WP))
4949 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004950}
4951
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4953{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004954 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004955 unsigned long hw_cr0;
4956
Gleb Natapov50378782013-02-04 16:00:28 +02004957 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004958 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004959 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004960 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004961 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004962
Gleb Natapov218e7632013-01-21 15:36:45 +02004963 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4964 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965
Gleb Natapov218e7632013-01-21 15:36:45 +02004966 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4967 enter_rmode(vcpu);
4968 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004970#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004971 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004972 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004974 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975 exit_lmode(vcpu);
4976 }
4977#endif
4978
Sean Christophersonb4d18512018-03-05 12:04:40 -08004979 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004980 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4981
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004983 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004984 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004985
4986 /* depends on vcpu->arch.cr0 to be set to a new value */
4987 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988}
4989
Yu Zhang855feb62017-08-24 20:27:55 +08004990static int get_ept_level(struct kvm_vcpu *vcpu)
4991{
4992 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4993 return 5;
4994 return 4;
4995}
4996
Peter Feiner995f00a2017-06-30 17:26:32 -07004997static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004998{
Yu Zhang855feb62017-08-24 20:27:55 +08004999 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005000
Yu Zhang855feb62017-08-24 20:27:55 +08005001 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005002
Peter Feiner995f00a2017-06-30 17:26:32 -07005003 if (enable_ept_ad_bits &&
5004 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005005 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005006 eptp |= (root_hpa & PAGE_MASK);
5007
5008 return eptp;
5009}
5010
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5012{
Sheng Yang14394422008-04-28 12:24:45 +08005013 unsigned long guest_cr3;
5014 u64 eptp;
5015
5016 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005017 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005018 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005019 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005020 if (enable_unrestricted_guest || is_paging(vcpu) ||
5021 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005022 guest_cr3 = kvm_read_cr3(vcpu);
5023 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005024 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005025 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005026 }
5027
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005028 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005029 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030}
5031
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005032static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005034 /*
5035 * Pass through host's Machine Check Enable value to hw_cr4, which
5036 * is in force while we are in guest mode. Do not let guests control
5037 * this bit, even if host CR4.MCE == 0.
5038 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005039 unsigned long hw_cr4;
5040
5041 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5042 if (enable_unrestricted_guest)
5043 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5044 else if (to_vmx(vcpu)->rmode.vm86_active)
5045 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5046 else
5047 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005048
Sean Christopherson64f7a112018-04-30 10:01:06 -07005049 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5050 if (cr4 & X86_CR4_UMIP) {
5051 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005052 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005053 hw_cr4 &= ~X86_CR4_UMIP;
5054 } else if (!is_guest_mode(vcpu) ||
5055 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5056 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5057 SECONDARY_EXEC_DESC);
5058 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005059
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005060 if (cr4 & X86_CR4_VMXE) {
5061 /*
5062 * To use VMXON (and later other VMX instructions), a guest
5063 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5064 * So basically the check on whether to allow nested VMX
5065 * is here.
5066 */
5067 if (!nested_vmx_allowed(vcpu))
5068 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005069 }
David Matlack38991522016-11-29 18:14:08 -08005070
5071 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005072 return 1;
5073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005074 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005075
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005076 if (!enable_unrestricted_guest) {
5077 if (enable_ept) {
5078 if (!is_paging(vcpu)) {
5079 hw_cr4 &= ~X86_CR4_PAE;
5080 hw_cr4 |= X86_CR4_PSE;
5081 } else if (!(cr4 & X86_CR4_PAE)) {
5082 hw_cr4 &= ~X86_CR4_PAE;
5083 }
5084 }
5085
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005086 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005087 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5088 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5089 * to be manually disabled when guest switches to non-paging
5090 * mode.
5091 *
5092 * If !enable_unrestricted_guest, the CPU is always running
5093 * with CR0.PG=1 and CR4 needs to be modified.
5094 * If enable_unrestricted_guest, the CPU automatically
5095 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005096 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005097 if (!is_paging(vcpu))
5098 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5099 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005100
Sheng Yang14394422008-04-28 12:24:45 +08005101 vmcs_writel(CR4_READ_SHADOW, cr4);
5102 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005103 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104}
5105
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106static void vmx_get_segment(struct kvm_vcpu *vcpu,
5107 struct kvm_segment *var, int seg)
5108{
Avi Kivitya9179492011-01-03 14:28:52 +02005109 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005110 u32 ar;
5111
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005112 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005113 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005114 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005115 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005116 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005117 var->base = vmx_read_guest_seg_base(vmx, seg);
5118 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5119 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005120 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005121 var->base = vmx_read_guest_seg_base(vmx, seg);
5122 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5123 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5124 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005125 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005126 var->type = ar & 15;
5127 var->s = (ar >> 4) & 1;
5128 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005129 /*
5130 * Some userspaces do not preserve unusable property. Since usable
5131 * segment has to be present according to VMX spec we can use present
5132 * property to amend userspace bug by making unusable segment always
5133 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5134 * segment as unusable.
5135 */
5136 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137 var->avl = (ar >> 12) & 1;
5138 var->l = (ar >> 13) & 1;
5139 var->db = (ar >> 14) & 1;
5140 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141}
5142
Avi Kivitya9179492011-01-03 14:28:52 +02005143static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5144{
Avi Kivitya9179492011-01-03 14:28:52 +02005145 struct kvm_segment s;
5146
5147 if (to_vmx(vcpu)->rmode.vm86_active) {
5148 vmx_get_segment(vcpu, &s, seg);
5149 return s.base;
5150 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005151 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005152}
5153
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005154static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005155{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005156 struct vcpu_vmx *vmx = to_vmx(vcpu);
5157
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005158 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005159 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005160 else {
5161 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005162 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005163 }
Avi Kivity69c73022011-03-07 15:26:44 +02005164}
5165
Avi Kivity653e3102007-05-07 10:55:37 +03005166static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168 u32 ar;
5169
Avi Kivityf0495f92012-06-07 17:06:10 +03005170 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171 ar = 1 << 16;
5172 else {
5173 ar = var->type & 15;
5174 ar |= (var->s & 1) << 4;
5175 ar |= (var->dpl & 3) << 5;
5176 ar |= (var->present & 1) << 7;
5177 ar |= (var->avl & 1) << 12;
5178 ar |= (var->l & 1) << 13;
5179 ar |= (var->db & 1) << 14;
5180 ar |= (var->g & 1) << 15;
5181 }
Avi Kivity653e3102007-05-07 10:55:37 +03005182
5183 return ar;
5184}
5185
5186static void vmx_set_segment(struct kvm_vcpu *vcpu,
5187 struct kvm_segment *var, int seg)
5188{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005189 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005190 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005191
Avi Kivity2fb92db2011-04-27 19:42:18 +03005192 vmx_segment_cache_clear(vmx);
5193
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005194 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5195 vmx->rmode.segs[seg] = *var;
5196 if (seg == VCPU_SREG_TR)
5197 vmcs_write16(sf->selector, var->selector);
5198 else if (var->s)
5199 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005200 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005201 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005202
Avi Kivity653e3102007-05-07 10:55:37 +03005203 vmcs_writel(sf->base, var->base);
5204 vmcs_write32(sf->limit, var->limit);
5205 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005206
5207 /*
5208 * Fix the "Accessed" bit in AR field of segment registers for older
5209 * qemu binaries.
5210 * IA32 arch specifies that at the time of processor reset the
5211 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005212 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005213 * state vmexit when "unrestricted guest" mode is turned on.
5214 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5215 * tree. Newer qemu binaries with that qemu fix would not need this
5216 * kvm hack.
5217 */
5218 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005219 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005220
Gleb Natapovf924d662012-12-12 19:10:55 +02005221 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005222
5223out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005224 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005225}
5226
Avi Kivity6aa8b732006-12-10 02:21:36 -08005227static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5228{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005229 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230
5231 *db = (ar >> 14) & 1;
5232 *l = (ar >> 13) & 1;
5233}
5234
Gleb Natapov89a27f42010-02-16 10:51:48 +02005235static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005237 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5238 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239}
5240
Gleb Natapov89a27f42010-02-16 10:51:48 +02005241static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005243 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5244 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245}
5246
Gleb Natapov89a27f42010-02-16 10:51:48 +02005247static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005249 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5250 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251}
5252
Gleb Natapov89a27f42010-02-16 10:51:48 +02005253static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005255 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5256 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257}
5258
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005259static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5260{
5261 struct kvm_segment var;
5262 u32 ar;
5263
5264 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005265 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005266 if (seg == VCPU_SREG_CS)
5267 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005268 ar = vmx_segment_access_rights(&var);
5269
5270 if (var.base != (var.selector << 4))
5271 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005272 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005273 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005274 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005275 return false;
5276
5277 return true;
5278}
5279
5280static bool code_segment_valid(struct kvm_vcpu *vcpu)
5281{
5282 struct kvm_segment cs;
5283 unsigned int cs_rpl;
5284
5285 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005286 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005287
Avi Kivity1872a3f2009-01-04 23:26:52 +02005288 if (cs.unusable)
5289 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005290 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005291 return false;
5292 if (!cs.s)
5293 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005294 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005295 if (cs.dpl > cs_rpl)
5296 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005297 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005298 if (cs.dpl != cs_rpl)
5299 return false;
5300 }
5301 if (!cs.present)
5302 return false;
5303
5304 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5305 return true;
5306}
5307
5308static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5309{
5310 struct kvm_segment ss;
5311 unsigned int ss_rpl;
5312
5313 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005314 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005315
Avi Kivity1872a3f2009-01-04 23:26:52 +02005316 if (ss.unusable)
5317 return true;
5318 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005319 return false;
5320 if (!ss.s)
5321 return false;
5322 if (ss.dpl != ss_rpl) /* DPL != RPL */
5323 return false;
5324 if (!ss.present)
5325 return false;
5326
5327 return true;
5328}
5329
5330static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5331{
5332 struct kvm_segment var;
5333 unsigned int rpl;
5334
5335 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005336 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005337
Avi Kivity1872a3f2009-01-04 23:26:52 +02005338 if (var.unusable)
5339 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005340 if (!var.s)
5341 return false;
5342 if (!var.present)
5343 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005344 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005345 if (var.dpl < rpl) /* DPL < RPL */
5346 return false;
5347 }
5348
5349 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5350 * rights flags
5351 */
5352 return true;
5353}
5354
5355static bool tr_valid(struct kvm_vcpu *vcpu)
5356{
5357 struct kvm_segment tr;
5358
5359 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5360
Avi Kivity1872a3f2009-01-04 23:26:52 +02005361 if (tr.unusable)
5362 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005363 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005364 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005365 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005366 return false;
5367 if (!tr.present)
5368 return false;
5369
5370 return true;
5371}
5372
5373static bool ldtr_valid(struct kvm_vcpu *vcpu)
5374{
5375 struct kvm_segment ldtr;
5376
5377 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5378
Avi Kivity1872a3f2009-01-04 23:26:52 +02005379 if (ldtr.unusable)
5380 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005381 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005382 return false;
5383 if (ldtr.type != 2)
5384 return false;
5385 if (!ldtr.present)
5386 return false;
5387
5388 return true;
5389}
5390
5391static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5392{
5393 struct kvm_segment cs, ss;
5394
5395 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5396 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5397
Nadav Amitb32a9912015-03-29 16:33:04 +03005398 return ((cs.selector & SEGMENT_RPL_MASK) ==
5399 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005400}
5401
5402/*
5403 * Check if guest state is valid. Returns true if valid, false if
5404 * not.
5405 * We assume that registers are always usable
5406 */
5407static bool guest_state_valid(struct kvm_vcpu *vcpu)
5408{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005409 if (enable_unrestricted_guest)
5410 return true;
5411
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005412 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005413 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005414 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5415 return false;
5416 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5417 return false;
5418 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5419 return false;
5420 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5421 return false;
5422 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5423 return false;
5424 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5425 return false;
5426 } else {
5427 /* protected mode guest state checks */
5428 if (!cs_ss_rpl_check(vcpu))
5429 return false;
5430 if (!code_segment_valid(vcpu))
5431 return false;
5432 if (!stack_segment_valid(vcpu))
5433 return false;
5434 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5435 return false;
5436 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5437 return false;
5438 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5439 return false;
5440 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5441 return false;
5442 if (!tr_valid(vcpu))
5443 return false;
5444 if (!ldtr_valid(vcpu))
5445 return false;
5446 }
5447 /* TODO:
5448 * - Add checks on RIP
5449 * - Add checks on RFLAGS
5450 */
5451
5452 return true;
5453}
5454
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005455static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5456{
5457 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5458}
5459
Mike Dayd77c26f2007-10-08 09:02:08 -04005460static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005462 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005463 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005464 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005465
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005466 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005467 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005468 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5469 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005470 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005471 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005472 r = kvm_write_guest_page(kvm, fn++, &data,
5473 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005474 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005475 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005476 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5477 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005478 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005479 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5480 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005481 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005482 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005483 r = kvm_write_guest_page(kvm, fn, &data,
5484 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5485 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005486out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005487 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005488 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489}
5490
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005491static int init_rmode_identity_map(struct kvm *kvm)
5492{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005493 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005494 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005495 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005496 u32 tmp;
5497
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005498 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005499 mutex_lock(&kvm->slots_lock);
5500
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005501 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005502 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005503
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005504 if (!kvm_vmx->ept_identity_map_addr)
5505 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5506 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005507
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005508 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005509 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005510 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005511 goto out2;
5512
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005513 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005514 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5515 if (r < 0)
5516 goto out;
5517 /* Set up identity-mapping pagetable for EPT in real mode */
5518 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5519 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5520 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5521 r = kvm_write_guest_page(kvm, identity_map_pfn,
5522 &tmp, i * sizeof(tmp), sizeof(tmp));
5523 if (r < 0)
5524 goto out;
5525 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005526 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005527
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005528out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005529 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005530
5531out2:
5532 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005533 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005534}
5535
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536static void seg_setup(int seg)
5537{
Mathias Krause772e0312012-08-30 01:30:19 +02005538 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005539 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540
5541 vmcs_write16(sf->selector, 0);
5542 vmcs_writel(sf->base, 0);
5543 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005544 ar = 0x93;
5545 if (seg == VCPU_SREG_CS)
5546 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005547
5548 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549}
5550
Sheng Yangf78e0e22007-10-29 09:40:42 +08005551static int alloc_apic_access_page(struct kvm *kvm)
5552{
Xiao Guangrong44841412012-09-07 14:14:20 +08005553 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005554 int r = 0;
5555
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005556 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005557 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005558 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005559 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5560 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005561 if (r)
5562 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005563
Tang Chen73a6d942014-09-11 13:38:00 +08005564 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005565 if (is_error_page(page)) {
5566 r = -EFAULT;
5567 goto out;
5568 }
5569
Tang Chenc24ae0d2014-09-24 15:57:58 +08005570 /*
5571 * Do not pin the page in memory, so that memory hot-unplug
5572 * is able to migrate it.
5573 */
5574 put_page(page);
5575 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005576out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005577 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005578 return r;
5579}
5580
Wanpeng Li991e7a02015-09-16 17:30:05 +08005581static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005582{
5583 int vpid;
5584
Avi Kivity919818a2009-03-23 18:01:29 +02005585 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005586 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005587 spin_lock(&vmx_vpid_lock);
5588 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005589 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005590 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005591 else
5592 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005593 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005594 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005595}
5596
Wanpeng Li991e7a02015-09-16 17:30:05 +08005597static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005598{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005599 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005600 return;
5601 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005602 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005603 spin_unlock(&vmx_vpid_lock);
5604}
5605
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005606static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5607 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005608{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005609 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005610
5611 if (!cpu_has_vmx_msr_bitmap())
5612 return;
5613
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005614 if (static_branch_unlikely(&enable_evmcs))
5615 evmcs_touch_msr_bitmap();
5616
Sheng Yang25c5f222008-03-28 13:18:56 +08005617 /*
5618 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5619 * have the write-low and read-high bitmap offsets the wrong way round.
5620 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5621 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005622 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005623 if (type & MSR_TYPE_R)
5624 /* read-low */
5625 __clear_bit(msr, msr_bitmap + 0x000 / f);
5626
5627 if (type & MSR_TYPE_W)
5628 /* write-low */
5629 __clear_bit(msr, msr_bitmap + 0x800 / f);
5630
Sheng Yang25c5f222008-03-28 13:18:56 +08005631 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5632 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005633 if (type & MSR_TYPE_R)
5634 /* read-high */
5635 __clear_bit(msr, msr_bitmap + 0x400 / f);
5636
5637 if (type & MSR_TYPE_W)
5638 /* write-high */
5639 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5640
5641 }
5642}
5643
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005644static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5645 u32 msr, int type)
5646{
5647 int f = sizeof(unsigned long);
5648
5649 if (!cpu_has_vmx_msr_bitmap())
5650 return;
5651
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005652 if (static_branch_unlikely(&enable_evmcs))
5653 evmcs_touch_msr_bitmap();
5654
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005655 /*
5656 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5657 * have the write-low and read-high bitmap offsets the wrong way round.
5658 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5659 */
5660 if (msr <= 0x1fff) {
5661 if (type & MSR_TYPE_R)
5662 /* read-low */
5663 __set_bit(msr, msr_bitmap + 0x000 / f);
5664
5665 if (type & MSR_TYPE_W)
5666 /* write-low */
5667 __set_bit(msr, msr_bitmap + 0x800 / f);
5668
5669 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5670 msr &= 0x1fff;
5671 if (type & MSR_TYPE_R)
5672 /* read-high */
5673 __set_bit(msr, msr_bitmap + 0x400 / f);
5674
5675 if (type & MSR_TYPE_W)
5676 /* write-high */
5677 __set_bit(msr, msr_bitmap + 0xc00 / f);
5678
5679 }
5680}
5681
5682static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5683 u32 msr, int type, bool value)
5684{
5685 if (value)
5686 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5687 else
5688 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5689}
5690
Wincy Vanf2b93282015-02-03 23:56:03 +08005691/*
5692 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5693 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5694 */
5695static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5696 unsigned long *msr_bitmap_nested,
5697 u32 msr, int type)
5698{
5699 int f = sizeof(unsigned long);
5700
Wincy Vanf2b93282015-02-03 23:56:03 +08005701 /*
5702 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5703 * have the write-low and read-high bitmap offsets the wrong way round.
5704 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5705 */
5706 if (msr <= 0x1fff) {
5707 if (type & MSR_TYPE_R &&
5708 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5709 /* read-low */
5710 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5711
5712 if (type & MSR_TYPE_W &&
5713 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5714 /* write-low */
5715 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5716
5717 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5718 msr &= 0x1fff;
5719 if (type & MSR_TYPE_R &&
5720 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5721 /* read-high */
5722 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5723
5724 if (type & MSR_TYPE_W &&
5725 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5726 /* write-high */
5727 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5728
5729 }
5730}
5731
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005732static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005733{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005734 u8 mode = 0;
5735
5736 if (cpu_has_secondary_exec_ctrls() &&
5737 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5738 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5739 mode |= MSR_BITMAP_MODE_X2APIC;
5740 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5741 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5742 }
5743
5744 if (is_long_mode(vcpu))
5745 mode |= MSR_BITMAP_MODE_LM;
5746
5747 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005748}
5749
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005750#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5751
5752static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5753 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005754{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005755 int msr;
5756
5757 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5758 unsigned word = msr / BITS_PER_LONG;
5759 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5760 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005761 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005762
5763 if (mode & MSR_BITMAP_MODE_X2APIC) {
5764 /*
5765 * TPR reads and writes can be virtualized even if virtual interrupt
5766 * delivery is not in use.
5767 */
5768 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5769 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5770 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5771 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5772 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5773 }
5774 }
5775}
5776
5777static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5778{
5779 struct vcpu_vmx *vmx = to_vmx(vcpu);
5780 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5781 u8 mode = vmx_msr_bitmap_mode(vcpu);
5782 u8 changed = mode ^ vmx->msr_bitmap_mode;
5783
5784 if (!changed)
5785 return;
5786
5787 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5788 !(mode & MSR_BITMAP_MODE_LM));
5789
5790 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5791 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5792
5793 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005794}
5795
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005796static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005797{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005798 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005799}
5800
David Matlackc9f04402017-08-01 14:00:40 -07005801static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5802{
5803 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5804 gfn_t gfn;
5805
5806 /*
5807 * Don't need to mark the APIC access page dirty; it is never
5808 * written to by the CPU during APIC virtualization.
5809 */
5810
5811 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5812 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5813 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5814 }
5815
5816 if (nested_cpu_has_posted_intr(vmcs12)) {
5817 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5818 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5819 }
5820}
5821
5822
David Hildenbrand6342c502017-01-25 11:58:58 +01005823static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005824{
5825 struct vcpu_vmx *vmx = to_vmx(vcpu);
5826 int max_irr;
5827 void *vapic_page;
5828 u16 status;
5829
David Matlackc9f04402017-08-01 14:00:40 -07005830 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5831 return;
Wincy Van705699a2015-02-03 23:58:17 +08005832
David Matlackc9f04402017-08-01 14:00:40 -07005833 vmx->nested.pi_pending = false;
5834 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5835 return;
Wincy Van705699a2015-02-03 23:58:17 +08005836
David Matlackc9f04402017-08-01 14:00:40 -07005837 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5838 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005839 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005840 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5841 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005842 kunmap(vmx->nested.virtual_apic_page);
5843
5844 status = vmcs_read16(GUEST_INTR_STATUS);
5845 if ((u8)max_irr > ((u8)status & 0xff)) {
5846 status &= ~0xff;
5847 status |= (u8)max_irr;
5848 vmcs_write16(GUEST_INTR_STATUS, status);
5849 }
5850 }
David Matlackc9f04402017-08-01 14:00:40 -07005851
5852 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005853}
5854
Wincy Van06a55242017-04-28 13:13:59 +08005855static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5856 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005857{
5858#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005859 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5860
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005861 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005862 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005863 * The vector of interrupt to be delivered to vcpu had
5864 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005865 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005866 * Following cases will be reached in this block, and
5867 * we always send a notification event in all cases as
5868 * explained below.
5869 *
5870 * Case 1: vcpu keeps in non-root mode. Sending a
5871 * notification event posts the interrupt to vcpu.
5872 *
5873 * Case 2: vcpu exits to root mode and is still
5874 * runnable. PIR will be synced to vIRR before the
5875 * next vcpu entry. Sending a notification event in
5876 * this case has no effect, as vcpu is not in root
5877 * mode.
5878 *
5879 * Case 3: vcpu exits to root mode and is blocked.
5880 * vcpu_block() has already synced PIR to vIRR and
5881 * never blocks vcpu if vIRR is not cleared. Therefore,
5882 * a blocked vcpu here does not wait for any requested
5883 * interrupts in PIR, and sending a notification event
5884 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005885 */
Feng Wu28b835d2015-09-18 22:29:54 +08005886
Wincy Van06a55242017-04-28 13:13:59 +08005887 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005888 return true;
5889 }
5890#endif
5891 return false;
5892}
5893
Wincy Van705699a2015-02-03 23:58:17 +08005894static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5895 int vector)
5896{
5897 struct vcpu_vmx *vmx = to_vmx(vcpu);
5898
5899 if (is_guest_mode(vcpu) &&
5900 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005901 /*
5902 * If a posted intr is not recognized by hardware,
5903 * we will accomplish it in the next vmentry.
5904 */
5905 vmx->nested.pi_pending = true;
5906 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005907 /* the PIR and ON have been set by L1. */
5908 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5909 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005910 return 0;
5911 }
5912 return -1;
5913}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005914/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005915 * Send interrupt to vcpu via posted interrupt way.
5916 * 1. If target vcpu is running(non-root mode), send posted interrupt
5917 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5918 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5919 * interrupt from PIR in next vmentry.
5920 */
5921static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5922{
5923 struct vcpu_vmx *vmx = to_vmx(vcpu);
5924 int r;
5925
Wincy Van705699a2015-02-03 23:58:17 +08005926 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5927 if (!r)
5928 return;
5929
Yang Zhanga20ed542013-04-11 19:25:15 +08005930 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5931 return;
5932
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005933 /* If a previous notification has sent the IPI, nothing to do. */
5934 if (pi_test_and_set_on(&vmx->pi_desc))
5935 return;
5936
Wincy Van06a55242017-04-28 13:13:59 +08005937 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005938 kvm_vcpu_kick(vcpu);
5939}
5940
Avi Kivity6aa8b732006-12-10 02:21:36 -08005941/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005942 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5943 * will not change in the lifetime of the guest.
5944 * Note that host-state that does change is set elsewhere. E.g., host-state
5945 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5946 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005947static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005948{
5949 u32 low32, high32;
5950 unsigned long tmpl;
5951 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005952 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005953
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005954 cr0 = read_cr0();
5955 WARN_ON(cr0 & X86_CR0_TS);
5956 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005957
5958 /*
5959 * Save the most likely value for this task's CR3 in the VMCS.
5960 * We can't use __get_current_cr3_fast() because we're not atomic.
5961 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005962 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005963 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005964 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005965
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005966 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005967 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005968 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005969 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005970
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005971 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005972#ifdef CONFIG_X86_64
5973 /*
5974 * Load null selectors, so we can avoid reloading them in
5975 * __vmx_load_host_state(), in case userspace uses the null selectors
5976 * too (the expected case).
5977 */
5978 vmcs_write16(HOST_DS_SELECTOR, 0);
5979 vmcs_write16(HOST_ES_SELECTOR, 0);
5980#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005981 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5982 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005983#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005984 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5985 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5986
Juergen Gross87930012017-09-04 12:25:27 +02005987 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005988 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005989 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005990
Avi Kivity83287ea422012-09-16 15:10:57 +03005991 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005992
5993 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5994 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5995 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5996 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5997
5998 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5999 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6000 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6001 }
6002}
6003
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006004static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6005{
6006 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6007 if (enable_ept)
6008 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006009 if (is_guest_mode(&vmx->vcpu))
6010 vmx->vcpu.arch.cr4_guest_owned_bits &=
6011 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006012 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6013}
6014
Yang Zhang01e439b2013-04-11 19:25:12 +08006015static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6016{
6017 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6018
Andrey Smetanind62caab2015-11-10 15:36:33 +03006019 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006020 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006021
6022 if (!enable_vnmi)
6023 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6024
Yunhong Jiang64672c92016-06-13 14:19:59 -07006025 /* Enable the preemption timer dynamically */
6026 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006027 return pin_based_exec_ctrl;
6028}
6029
Andrey Smetanind62caab2015-11-10 15:36:33 +03006030static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6031{
6032 struct vcpu_vmx *vmx = to_vmx(vcpu);
6033
6034 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006035 if (cpu_has_secondary_exec_ctrls()) {
6036 if (kvm_vcpu_apicv_active(vcpu))
6037 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6038 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6039 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6040 else
6041 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6042 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6043 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6044 }
6045
6046 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006047 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006048}
6049
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006050static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6051{
6052 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006053
6054 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6055 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6056
Paolo Bonzini35754c92015-07-29 12:05:37 +02006057 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006058 exec_control &= ~CPU_BASED_TPR_SHADOW;
6059#ifdef CONFIG_X86_64
6060 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6061 CPU_BASED_CR8_LOAD_EXITING;
6062#endif
6063 }
6064 if (!enable_ept)
6065 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6066 CPU_BASED_CR3_LOAD_EXITING |
6067 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006068 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6069 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6070 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006071 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6072 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006073 return exec_control;
6074}
6075
Jim Mattson45ec3682017-08-23 16:32:04 -07006076static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006077{
Jim Mattson45ec3682017-08-23 16:32:04 -07006078 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006079 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006080}
6081
Jim Mattson75f4fc82017-08-23 16:32:03 -07006082static bool vmx_rdseed_supported(void)
6083{
6084 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006085 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006086}
6087
Paolo Bonzini80154d72017-08-24 13:55:35 +02006088static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006089{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006090 struct kvm_vcpu *vcpu = &vmx->vcpu;
6091
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006092 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006093
Paolo Bonzini80154d72017-08-24 13:55:35 +02006094 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006095 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6096 if (vmx->vpid == 0)
6097 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6098 if (!enable_ept) {
6099 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6100 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006101 /* Enable INVPCID for non-ept guests may cause performance regression. */
6102 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006103 }
6104 if (!enable_unrestricted_guest)
6105 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006106 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006108 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006109 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6110 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006111 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006112
6113 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6114 * in vmx_set_cr4. */
6115 exec_control &= ~SECONDARY_EXEC_DESC;
6116
Abel Gordonabc4fc52013-04-18 14:35:25 +03006117 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6118 (handle_vmptrld).
6119 We can NOT enable shadow_vmcs here because we don't have yet
6120 a current VMCS12
6121 */
6122 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006123
6124 if (!enable_pml)
6125 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006126
Paolo Bonzini3db13482017-08-24 14:48:03 +02006127 if (vmx_xsaves_supported()) {
6128 /* Exposing XSAVES only when XSAVE is exposed */
6129 bool xsaves_enabled =
6130 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6131 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6132
6133 if (!xsaves_enabled)
6134 exec_control &= ~SECONDARY_EXEC_XSAVES;
6135
6136 if (nested) {
6137 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006138 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006139 SECONDARY_EXEC_XSAVES;
6140 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006141 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006142 ~SECONDARY_EXEC_XSAVES;
6143 }
6144 }
6145
Paolo Bonzini80154d72017-08-24 13:55:35 +02006146 if (vmx_rdtscp_supported()) {
6147 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6148 if (!rdtscp_enabled)
6149 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6150
6151 if (nested) {
6152 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006153 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006154 SECONDARY_EXEC_RDTSCP;
6155 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006156 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006157 ~SECONDARY_EXEC_RDTSCP;
6158 }
6159 }
6160
6161 if (vmx_invpcid_supported()) {
6162 /* Exposing INVPCID only when PCID is exposed */
6163 bool invpcid_enabled =
6164 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6165 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6166
6167 if (!invpcid_enabled) {
6168 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6169 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6170 }
6171
6172 if (nested) {
6173 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006174 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006175 SECONDARY_EXEC_ENABLE_INVPCID;
6176 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006177 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006178 ~SECONDARY_EXEC_ENABLE_INVPCID;
6179 }
6180 }
6181
Jim Mattson45ec3682017-08-23 16:32:04 -07006182 if (vmx_rdrand_supported()) {
6183 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6184 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006185 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006186
6187 if (nested) {
6188 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006189 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006190 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006191 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006192 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006193 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006194 }
6195 }
6196
Jim Mattson75f4fc82017-08-23 16:32:03 -07006197 if (vmx_rdseed_supported()) {
6198 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6199 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006200 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006201
6202 if (nested) {
6203 if (rdseed_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_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -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_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006209 }
6210 }
6211
Paolo Bonzini80154d72017-08-24 13:55:35 +02006212 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006213}
6214
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006215static void ept_set_mmio_spte_mask(void)
6216{
6217 /*
6218 * EPT Misconfigurations can be generated if the value of bits 2:0
6219 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006220 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006221 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6222 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006223}
6224
Wanpeng Lif53cd632014-12-02 19:14:58 +08006225#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006226/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006227 * Sets up the vmcs for emulated real mode.
6228 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006229static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006230{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006231#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006232 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006233#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006234 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006235
Abel Gordon4607c2d2013-04-18 14:35:55 +03006236 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006237 /*
6238 * At vCPU creation, "VMWRITE to any supported field
6239 * in the VMCS" is supported, so use the more
6240 * permissive vmx_vmread_bitmap to specify both read
6241 * and write permissions for the shadow VMCS.
6242 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006243 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006244 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006245 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006246 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006247 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006248
Avi Kivity6aa8b732006-12-10 02:21:36 -08006249 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6250
Avi Kivity6aa8b732006-12-10 02:21:36 -08006251 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006252 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006253 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006254
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006255 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006256
Dan Williamsdfa169b2016-06-02 11:17:24 -07006257 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006258 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006259 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006260 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006261 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006262
Andrey Smetanind62caab2015-11-10 15:36:33 +03006263 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006264 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6265 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6266 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6267 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6268
6269 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006270
Li RongQing0bcf2612015-12-03 13:29:34 +08006271 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006272 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006273 }
6274
Wanpeng Lib31c1142018-03-12 04:53:04 -07006275 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006276 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006277 vmx->ple_window = ple_window;
6278 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006279 }
6280
Xiao Guangrongc3707952011-07-12 03:28:04 +08006281 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6282 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006283 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6284
Avi Kivity9581d442010-10-19 16:46:55 +02006285 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6286 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006287 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006288#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006289 rdmsrl(MSR_FS_BASE, a);
6290 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6291 rdmsrl(MSR_GS_BASE, a);
6292 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6293#else
6294 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6295 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6296#endif
6297
Bandan Das2a499e42017-08-03 15:54:41 -04006298 if (cpu_has_vmx_vmfunc())
6299 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6300
Eddie Dong2cc51562007-05-21 07:28:09 +03006301 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6302 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006303 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006304 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006305 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006306
Radim Krčmář74545702015-04-27 15:11:25 +02006307 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6308 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006309
Paolo Bonzini03916db2014-07-24 14:21:57 +02006310 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006311 u32 index = vmx_msr_index[i];
6312 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006313 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314
6315 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6316 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006317 if (wrmsr_safe(index, data_low, data_high) < 0)
6318 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006319 vmx->guest_msrs[j].index = i;
6320 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006321 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006322 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006325 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6326 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006327
6328 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006329
6330 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006331 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006332
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006333 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6334 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6335
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006336 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006337
Wanpeng Lif53cd632014-12-02 19:14:58 +08006338 if (vmx_xsaves_supported())
6339 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6340
Peter Feiner4e595162016-07-07 14:49:58 -07006341 if (enable_pml) {
6342 ASSERT(vmx->pml_pg);
6343 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6344 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6345 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006346}
6347
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006348static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006349{
6350 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006351 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006352 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006353
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006354 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006355 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006356
Wanpeng Li518e7b92018-02-28 14:03:31 +08006357 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006358 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006359 kvm_set_cr8(vcpu, 0);
6360
6361 if (!init_event) {
6362 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6363 MSR_IA32_APICBASE_ENABLE;
6364 if (kvm_vcpu_is_reset_bsp(vcpu))
6365 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6366 apic_base_msr.host_initiated = true;
6367 kvm_set_apic_base(vcpu, &apic_base_msr);
6368 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006369
Avi Kivity2fb92db2011-04-27 19:42:18 +03006370 vmx_segment_cache_clear(vmx);
6371
Avi Kivity5706be02008-08-20 15:07:31 +03006372 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006373 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006374 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006375
6376 seg_setup(VCPU_SREG_DS);
6377 seg_setup(VCPU_SREG_ES);
6378 seg_setup(VCPU_SREG_FS);
6379 seg_setup(VCPU_SREG_GS);
6380 seg_setup(VCPU_SREG_SS);
6381
6382 vmcs_write16(GUEST_TR_SELECTOR, 0);
6383 vmcs_writel(GUEST_TR_BASE, 0);
6384 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6385 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6386
6387 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6388 vmcs_writel(GUEST_LDTR_BASE, 0);
6389 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6390 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6391
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006392 if (!init_event) {
6393 vmcs_write32(GUEST_SYSENTER_CS, 0);
6394 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6395 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6396 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6397 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006398
Wanpeng Lic37c2872017-11-20 14:52:21 -08006399 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006400 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006401
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006402 vmcs_writel(GUEST_GDTR_BASE, 0);
6403 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6404
6405 vmcs_writel(GUEST_IDTR_BASE, 0);
6406 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6407
Anthony Liguori443381a2010-12-06 10:53:38 -06006408 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006409 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006410 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006411 if (kvm_mpx_supported())
6412 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006413
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006414 setup_msrs(vmx);
6415
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6417
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006418 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006419 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006420 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006421 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006422 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006423 vmcs_write32(TPR_THRESHOLD, 0);
6424 }
6425
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006426 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006427
Sheng Yang2384d2b2008-01-17 15:14:33 +08006428 if (vmx->vpid != 0)
6429 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6430
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006431 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006432 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006433 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006434 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006435 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006436
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006437 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006438
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006439 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006440 if (init_event)
6441 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006442}
6443
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006444/*
6445 * In nested virtualization, check if L1 asked to exit on external interrupts.
6446 * For most existing hypervisors, this will always return true.
6447 */
6448static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6449{
6450 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6451 PIN_BASED_EXT_INTR_MASK;
6452}
6453
Bandan Das77b0f5d2014-04-19 18:17:45 -04006454/*
6455 * In nested virtualization, check if L1 has set
6456 * VM_EXIT_ACK_INTR_ON_EXIT
6457 */
6458static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6459{
6460 return get_vmcs12(vcpu)->vm_exit_controls &
6461 VM_EXIT_ACK_INTR_ON_EXIT;
6462}
6463
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006464static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6465{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006466 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006467}
6468
Jan Kiszkac9a79532014-03-07 20:03:15 +01006469static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006470{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006471 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6472 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006473}
6474
Jan Kiszkac9a79532014-03-07 20:03:15 +01006475static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006476{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006477 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006478 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006479 enable_irq_window(vcpu);
6480 return;
6481 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006482
Paolo Bonzini47c01522016-12-19 11:44:07 +01006483 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6484 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006485}
6486
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006487static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006488{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006489 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006490 uint32_t intr;
6491 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006492
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006493 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006494
Avi Kivityfa89a812008-09-01 15:57:51 +03006495 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006496 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006497 int inc_eip = 0;
6498 if (vcpu->arch.interrupt.soft)
6499 inc_eip = vcpu->arch.event_exit_inst_len;
6500 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006501 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006502 return;
6503 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006504 intr = irq | INTR_INFO_VALID_MASK;
6505 if (vcpu->arch.interrupt.soft) {
6506 intr |= INTR_TYPE_SOFT_INTR;
6507 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6508 vmx->vcpu.arch.event_exit_inst_len);
6509 } else
6510 intr |= INTR_TYPE_EXT_INTR;
6511 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006512
6513 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006514}
6515
Sheng Yangf08864b2008-05-15 18:23:25 +08006516static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6517{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006518 struct vcpu_vmx *vmx = to_vmx(vcpu);
6519
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006520 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006521 /*
6522 * Tracking the NMI-blocked state in software is built upon
6523 * finding the next open IRQ window. This, in turn, depends on
6524 * well-behaving guests: They have to keep IRQs disabled at
6525 * least as long as the NMI handler runs. Otherwise we may
6526 * cause NMI nesting, maybe breaking the guest. But as this is
6527 * highly unlikely, we can live with the residual risk.
6528 */
6529 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6530 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6531 }
6532
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006533 ++vcpu->stat.nmi_injections;
6534 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006535
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006536 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006537 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006538 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006539 return;
6540 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006541
Sheng Yangf08864b2008-05-15 18:23:25 +08006542 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6543 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006544
6545 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006546}
6547
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006548static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6549{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006550 struct vcpu_vmx *vmx = to_vmx(vcpu);
6551 bool masked;
6552
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006553 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006554 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006555 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006556 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006557 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6558 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6559 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006560}
6561
6562static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6563{
6564 struct vcpu_vmx *vmx = to_vmx(vcpu);
6565
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006566 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006567 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6568 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6569 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6570 }
6571 } else {
6572 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6573 if (masked)
6574 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6575 GUEST_INTR_STATE_NMI);
6576 else
6577 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6578 GUEST_INTR_STATE_NMI);
6579 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006580}
6581
Jan Kiszka2505dc92013-04-14 12:12:47 +02006582static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6583{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006584 if (to_vmx(vcpu)->nested.nested_run_pending)
6585 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006586
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006587 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006588 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6589 return 0;
6590
Jan Kiszka2505dc92013-04-14 12:12:47 +02006591 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6592 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6593 | GUEST_INTR_STATE_NMI));
6594}
6595
Gleb Natapov78646122009-03-23 12:12:11 +02006596static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6597{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006598 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6599 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006600 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6601 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006602}
6603
Izik Eiduscbc94022007-10-25 00:29:55 +02006604static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6605{
6606 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006607
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006608 if (enable_unrestricted_guest)
6609 return 0;
6610
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006611 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6612 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006613 if (ret)
6614 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006615 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006616 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006617}
6618
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006619static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6620{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006621 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006622 return 0;
6623}
6624
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006625static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006626{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006627 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006628 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006629 /*
6630 * Update instruction length as we may reinject the exception
6631 * from user space while in guest debugging mode.
6632 */
6633 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6634 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006635 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006636 return false;
6637 /* fall through */
6638 case DB_VECTOR:
6639 if (vcpu->guest_debug &
6640 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6641 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006642 /* fall through */
6643 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006644 case OF_VECTOR:
6645 case BR_VECTOR:
6646 case UD_VECTOR:
6647 case DF_VECTOR:
6648 case SS_VECTOR:
6649 case GP_VECTOR:
6650 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006651 return true;
6652 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006653 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006654 return false;
6655}
6656
6657static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6658 int vec, u32 err_code)
6659{
6660 /*
6661 * Instruction with address size override prefix opcode 0x67
6662 * Cause the #SS fault with 0 error code in VM86 mode.
6663 */
6664 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6665 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6666 if (vcpu->arch.halt_request) {
6667 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006668 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006669 }
6670 return 1;
6671 }
6672 return 0;
6673 }
6674
6675 /*
6676 * Forward all other exceptions that are valid in real mode.
6677 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6678 * the required debugging infrastructure rework.
6679 */
6680 kvm_queue_exception(vcpu, vec);
6681 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006682}
6683
Andi Kleena0861c02009-06-08 17:37:09 +08006684/*
6685 * Trigger machine check on the host. We assume all the MSRs are already set up
6686 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6687 * We pass a fake environment to the machine check handler because we want
6688 * the guest to be always treated like user space, no matter what context
6689 * it used internally.
6690 */
6691static void kvm_machine_check(void)
6692{
6693#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6694 struct pt_regs regs = {
6695 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6696 .flags = X86_EFLAGS_IF,
6697 };
6698
6699 do_machine_check(&regs, 0);
6700#endif
6701}
6702
Avi Kivity851ba692009-08-24 11:10:17 +03006703static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006704{
6705 /* already handled by vcpu_run */
6706 return 1;
6707}
6708
Avi Kivity851ba692009-08-24 11:10:17 +03006709static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006710{
Avi Kivity1155f762007-11-22 11:30:47 +02006711 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006712 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006713 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006714 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006715 u32 vect_info;
6716 enum emulation_result er;
6717
Avi Kivity1155f762007-11-22 11:30:47 +02006718 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006719 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006720
Andi Kleena0861c02009-06-08 17:37:09 +08006721 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006722 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006723
Jim Mattsonef85b672016-12-12 11:01:37 -08006724 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006725 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006726
Wanpeng Li082d06e2018-04-03 16:28:48 -07006727 if (is_invalid_opcode(intr_info))
6728 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006729
Avi Kivity6aa8b732006-12-10 02:21:36 -08006730 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006731 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006732 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006733
Liran Alon9e869482018-03-12 13:12:51 +02006734 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6735 WARN_ON_ONCE(!enable_vmware_backdoor);
6736 er = emulate_instruction(vcpu,
6737 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6738 if (er == EMULATE_USER_EXIT)
6739 return 0;
6740 else if (er != EMULATE_DONE)
6741 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6742 return 1;
6743 }
6744
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006745 /*
6746 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6747 * MMIO, it is better to report an internal error.
6748 * See the comments in vmx_handle_exit.
6749 */
6750 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6751 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6752 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6753 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006754 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006755 vcpu->run->internal.data[0] = vect_info;
6756 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006757 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006758 return 0;
6759 }
6760
Avi Kivity6aa8b732006-12-10 02:21:36 -08006761 if (is_page_fault(intr_info)) {
6762 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006763 /* EPT won't cause page fault directly */
6764 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006765 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006766 }
6767
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006768 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006769
6770 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6771 return handle_rmode_exception(vcpu, ex_no, error_code);
6772
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006773 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006774 case AC_VECTOR:
6775 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6776 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006777 case DB_VECTOR:
6778 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6779 if (!(vcpu->guest_debug &
6780 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006781 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006782 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006783 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006784 skip_emulated_instruction(vcpu);
6785
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006786 kvm_queue_exception(vcpu, DB_VECTOR);
6787 return 1;
6788 }
6789 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6790 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6791 /* fall through */
6792 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006793 /*
6794 * Update instruction length as we may reinject #BP from
6795 * user space while in guest debugging mode. Reading it for
6796 * #DB as well causes no harm, it is not used in that case.
6797 */
6798 vmx->vcpu.arch.event_exit_inst_len =
6799 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006801 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006802 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6803 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006804 break;
6805 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006806 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6807 kvm_run->ex.exception = ex_no;
6808 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006809 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006810 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811 return 0;
6812}
6813
Avi Kivity851ba692009-08-24 11:10:17 +03006814static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006815{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006816 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817 return 1;
6818}
6819
Avi Kivity851ba692009-08-24 11:10:17 +03006820static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006821{
Avi Kivity851ba692009-08-24 11:10:17 +03006822 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006823 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006824 return 0;
6825}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006826
Avi Kivity851ba692009-08-24 11:10:17 +03006827static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828{
He, Qingbfdaab02007-09-12 14:18:28 +08006829 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006830 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006831 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006832
He, Qingbfdaab02007-09-12 14:18:28 +08006833 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006834 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006835
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006836 ++vcpu->stat.io_exits;
6837
Sean Christopherson432baf62018-03-08 08:57:26 -08006838 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006839 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006840
6841 port = exit_qualification >> 16;
6842 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006843 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006844
Sean Christophersondca7f122018-03-08 08:57:27 -08006845 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006846}
6847
Ingo Molnar102d8322007-02-19 14:37:47 +02006848static void
6849vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6850{
6851 /*
6852 * Patch in the VMCALL instruction:
6853 */
6854 hypercall[0] = 0x0f;
6855 hypercall[1] = 0x01;
6856 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006857}
6858
Guo Chao0fa06072012-06-28 15:16:19 +08006859/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006860static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6861{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006862 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006863 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6864 unsigned long orig_val = val;
6865
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006866 /*
6867 * We get here when L2 changed cr0 in a way that did not change
6868 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006869 * but did change L0 shadowed bits. So we first calculate the
6870 * effective cr0 value that L1 would like to write into the
6871 * hardware. It consists of the L2-owned bits from the new
6872 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006873 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006874 val = (val & ~vmcs12->cr0_guest_host_mask) |
6875 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6876
David Matlack38991522016-11-29 18:14:08 -08006877 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006878 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006879
6880 if (kvm_set_cr0(vcpu, val))
6881 return 1;
6882 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006883 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006884 } else {
6885 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006886 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006887 return 1;
David Matlack38991522016-11-29 18:14:08 -08006888
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006889 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006890 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006891}
6892
6893static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6894{
6895 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6897 unsigned long orig_val = val;
6898
6899 /* analogously to handle_set_cr0 */
6900 val = (val & ~vmcs12->cr4_guest_host_mask) |
6901 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6902 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006903 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006904 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006905 return 0;
6906 } else
6907 return kvm_set_cr4(vcpu, val);
6908}
6909
Paolo Bonzini0367f202016-07-12 10:44:55 +02006910static int handle_desc(struct kvm_vcpu *vcpu)
6911{
6912 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6913 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6914}
6915
Avi Kivity851ba692009-08-24 11:10:17 +03006916static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006917{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006918 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919 int cr;
6920 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006921 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006922 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006923
He, Qingbfdaab02007-09-12 14:18:28 +08006924 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006925 cr = exit_qualification & 15;
6926 reg = (exit_qualification >> 8) & 15;
6927 switch ((exit_qualification >> 4) & 3) {
6928 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006929 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006930 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006931 switch (cr) {
6932 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006933 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006934 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006936 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006937 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006938 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006939 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006940 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006941 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006942 case 8: {
6943 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006944 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006945 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006946 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006947 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006948 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006949 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 return ret;
6951 /*
6952 * TODO: we might be squashing a
6953 * KVM_GUESTDBG_SINGLESTEP-triggered
6954 * KVM_EXIT_DEBUG here.
6955 */
Avi Kivity851ba692009-08-24 11:10:17 +03006956 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006957 return 0;
6958 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006959 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006960 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006961 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006962 WARN_ONCE(1, "Guest should always own CR0.TS");
6963 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006964 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006965 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006966 case 1: /*mov from cr*/
6967 switch (cr) {
6968 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006969 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006970 val = kvm_read_cr3(vcpu);
6971 kvm_register_write(vcpu, reg, val);
6972 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006973 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006974 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006975 val = kvm_get_cr8(vcpu);
6976 kvm_register_write(vcpu, reg, val);
6977 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006978 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006979 }
6980 break;
6981 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006982 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006983 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006984 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006985
Kyle Huey6affcbe2016-11-29 12:40:40 -08006986 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006987 default:
6988 break;
6989 }
Avi Kivity851ba692009-08-24 11:10:17 +03006990 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006991 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006992 (int)(exit_qualification >> 4) & 3, cr);
6993 return 0;
6994}
6995
Avi Kivity851ba692009-08-24 11:10:17 +03006996static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006997{
He, Qingbfdaab02007-09-12 14:18:28 +08006998 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006999 int dr, dr7, reg;
7000
7001 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7002 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7003
7004 /* First, if DR does not exist, trigger UD */
7005 if (!kvm_require_dr(vcpu, dr))
7006 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007007
Jan Kiszkaf2483412010-01-20 18:20:20 +01007008 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007009 if (!kvm_require_cpl(vcpu, 0))
7010 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007011 dr7 = vmcs_readl(GUEST_DR7);
7012 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007013 /*
7014 * As the vm-exit takes precedence over the debug trap, we
7015 * need to emulate the latter, either for the host or the
7016 * guest debugging itself.
7017 */
7018 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007019 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007020 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007021 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007022 vcpu->run->debug.arch.exception = DB_VECTOR;
7023 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007024 return 0;
7025 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007026 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007027 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007028 kvm_queue_exception(vcpu, DB_VECTOR);
7029 return 1;
7030 }
7031 }
7032
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007033 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007034 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7035 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007036
7037 /*
7038 * No more DR vmexits; force a reload of the debug registers
7039 * and reenter on this instruction. The next vmexit will
7040 * retrieve the full state of the debug registers.
7041 */
7042 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7043 return 1;
7044 }
7045
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007046 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7047 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007048 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007049
7050 if (kvm_get_dr(vcpu, dr, &val))
7051 return 1;
7052 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007053 } else
Nadav Amit57773922014-06-18 17:19:23 +03007054 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007055 return 1;
7056
Kyle Huey6affcbe2016-11-29 12:40:40 -08007057 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058}
7059
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007060static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7061{
7062 return vcpu->arch.dr6;
7063}
7064
7065static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7066{
7067}
7068
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007069static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7070{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007071 get_debugreg(vcpu->arch.db[0], 0);
7072 get_debugreg(vcpu->arch.db[1], 1);
7073 get_debugreg(vcpu->arch.db[2], 2);
7074 get_debugreg(vcpu->arch.db[3], 3);
7075 get_debugreg(vcpu->arch.dr6, 6);
7076 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7077
7078 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007079 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007080}
7081
Gleb Natapov020df072010-04-13 10:05:23 +03007082static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7083{
7084 vmcs_writel(GUEST_DR7, val);
7085}
7086
Avi Kivity851ba692009-08-24 11:10:17 +03007087static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088{
Kyle Huey6a908b62016-11-29 12:40:37 -08007089 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007090}
7091
Avi Kivity851ba692009-08-24 11:10:17 +03007092static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007094 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007095 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007096
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007097 msr_info.index = ecx;
7098 msr_info.host_initiated = false;
7099 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007100 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007101 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007102 return 1;
7103 }
7104
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007105 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007106
Avi Kivity6aa8b732006-12-10 02:21:36 -08007107 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007108 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7109 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007110 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007111}
7112
Avi Kivity851ba692009-08-24 11:10:17 +03007113static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007114{
Will Auld8fe8ab42012-11-29 12:42:12 -08007115 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007116 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7117 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7118 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007119
Will Auld8fe8ab42012-11-29 12:42:12 -08007120 msr.data = data;
7121 msr.index = ecx;
7122 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007123 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007124 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007125 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007126 return 1;
7127 }
7128
Avi Kivity59200272010-01-25 19:47:02 +02007129 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007130 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131}
7132
Avi Kivity851ba692009-08-24 11:10:17 +03007133static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007134{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007135 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007136 return 1;
7137}
7138
Avi Kivity851ba692009-08-24 11:10:17 +03007139static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007141 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7142 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007143
Avi Kivity3842d132010-07-27 12:30:24 +03007144 kvm_make_request(KVM_REQ_EVENT, vcpu);
7145
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007146 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147 return 1;
7148}
7149
Avi Kivity851ba692009-08-24 11:10:17 +03007150static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007151{
Avi Kivityd3bef152007-06-05 15:53:05 +03007152 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007153}
7154
Avi Kivity851ba692009-08-24 11:10:17 +03007155static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007156{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007157 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007158}
7159
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007160static int handle_invd(struct kvm_vcpu *vcpu)
7161{
Andre Przywara51d8b662010-12-21 11:12:02 +01007162 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007163}
7164
Avi Kivity851ba692009-08-24 11:10:17 +03007165static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007166{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007167 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007168
7169 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007170 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007171}
7172
Avi Kivityfee84b02011-11-10 14:57:25 +02007173static int handle_rdpmc(struct kvm_vcpu *vcpu)
7174{
7175 int err;
7176
7177 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007178 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007179}
7180
Avi Kivity851ba692009-08-24 11:10:17 +03007181static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007182{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007183 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007184}
7185
Dexuan Cui2acf9232010-06-10 11:27:12 +08007186static int handle_xsetbv(struct kvm_vcpu *vcpu)
7187{
7188 u64 new_bv = kvm_read_edx_eax(vcpu);
7189 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7190
7191 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007192 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007193 return 1;
7194}
7195
Wanpeng Lif53cd632014-12-02 19:14:58 +08007196static int handle_xsaves(struct kvm_vcpu *vcpu)
7197{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007198 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007199 WARN(1, "this should never happen\n");
7200 return 1;
7201}
7202
7203static int handle_xrstors(struct kvm_vcpu *vcpu)
7204{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007205 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007206 WARN(1, "this should never happen\n");
7207 return 1;
7208}
7209
Avi Kivity851ba692009-08-24 11:10:17 +03007210static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007211{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007212 if (likely(fasteoi)) {
7213 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7214 int access_type, offset;
7215
7216 access_type = exit_qualification & APIC_ACCESS_TYPE;
7217 offset = exit_qualification & APIC_ACCESS_OFFSET;
7218 /*
7219 * Sane guest uses MOV to write EOI, with written value
7220 * not cared. So make a short-circuit here by avoiding
7221 * heavy instruction emulation.
7222 */
7223 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7224 (offset == APIC_EOI)) {
7225 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007226 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007227 }
7228 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007229 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007230}
7231
Yang Zhangc7c9c562013-01-25 10:18:51 +08007232static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7233{
7234 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7235 int vector = exit_qualification & 0xff;
7236
7237 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7238 kvm_apic_set_eoi_accelerated(vcpu, vector);
7239 return 1;
7240}
7241
Yang Zhang83d4c282013-01-25 10:18:49 +08007242static int handle_apic_write(struct kvm_vcpu *vcpu)
7243{
7244 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7245 u32 offset = exit_qualification & 0xfff;
7246
7247 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7248 kvm_apic_write_nodecode(vcpu, offset);
7249 return 1;
7250}
7251
Avi Kivity851ba692009-08-24 11:10:17 +03007252static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007253{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007254 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007255 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007256 bool has_error_code = false;
7257 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007258 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007259 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007260
7261 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007262 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007263 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007264
7265 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7266
7267 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007268 if (reason == TASK_SWITCH_GATE && idt_v) {
7269 switch (type) {
7270 case INTR_TYPE_NMI_INTR:
7271 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007272 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007273 break;
7274 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007275 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007276 kvm_clear_interrupt_queue(vcpu);
7277 break;
7278 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007279 if (vmx->idt_vectoring_info &
7280 VECTORING_INFO_DELIVER_CODE_MASK) {
7281 has_error_code = true;
7282 error_code =
7283 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7284 }
7285 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007286 case INTR_TYPE_SOFT_EXCEPTION:
7287 kvm_clear_exception_queue(vcpu);
7288 break;
7289 default:
7290 break;
7291 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007292 }
Izik Eidus37817f22008-03-24 23:14:53 +02007293 tss_selector = exit_qualification;
7294
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007295 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7296 type != INTR_TYPE_EXT_INTR &&
7297 type != INTR_TYPE_NMI_INTR))
7298 skip_emulated_instruction(vcpu);
7299
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007300 if (kvm_task_switch(vcpu, tss_selector,
7301 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7302 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007303 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7304 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7305 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007306 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007307 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007308
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007309 /*
7310 * TODO: What about debug traps on tss switch?
7311 * Are we supposed to inject them and update dr6?
7312 */
7313
7314 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007315}
7316
Avi Kivity851ba692009-08-24 11:10:17 +03007317static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007318{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007319 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007320 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007321 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007322
Sheng Yangf9c617f2009-03-25 10:08:52 +08007323 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007324
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007325 /*
7326 * EPT violation happened while executing iret from NMI,
7327 * "blocked by NMI" bit has to be set before next VM entry.
7328 * There are errata that may cause this bit to not be set:
7329 * AAK134, BY25.
7330 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007331 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007332 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007333 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007334 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7335
Sheng Yang14394422008-04-28 12:24:45 +08007336 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007337 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007338
Junaid Shahid27959a42016-12-06 16:46:10 -08007339 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007340 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007341 ? PFERR_USER_MASK : 0;
7342 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007343 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007344 ? PFERR_WRITE_MASK : 0;
7345 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007346 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007347 ? PFERR_FETCH_MASK : 0;
7348 /* ept page table entry is present? */
7349 error_code |= (exit_qualification &
7350 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7351 EPT_VIOLATION_EXECUTABLE))
7352 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007353
Paolo Bonzinieebed242016-11-28 14:39:58 +01007354 error_code |= (exit_qualification & 0x100) != 0 ?
7355 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007356
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007357 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007358 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007359}
7360
Avi Kivity851ba692009-08-24 11:10:17 +03007361static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007362{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007363 gpa_t gpa;
7364
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007365 /*
7366 * A nested guest cannot optimize MMIO vmexits, because we have an
7367 * nGPA here instead of the required GPA.
7368 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007369 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007370 if (!is_guest_mode(vcpu) &&
7371 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007372 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007373 /*
7374 * Doing kvm_skip_emulated_instruction() depends on undefined
7375 * behavior: Intel's manual doesn't mandate
7376 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7377 * occurs and while on real hardware it was observed to be set,
7378 * other hypervisors (namely Hyper-V) don't set it, we end up
7379 * advancing IP with some random value. Disable fast mmio when
7380 * running nested and keep it for real hardware in hope that
7381 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7382 */
7383 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7384 return kvm_skip_emulated_instruction(vcpu);
7385 else
7386 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7387 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007388 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007389
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007390 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007391}
7392
Avi Kivity851ba692009-08-24 11:10:17 +03007393static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007394{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007395 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007396 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7397 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007398 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007399 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007400
7401 return 1;
7402}
7403
Mohammed Gamal80ced182009-09-01 12:48:18 +02007404static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007405{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007406 struct vcpu_vmx *vmx = to_vmx(vcpu);
7407 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007408 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007409 u32 cpu_exec_ctrl;
7410 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007411 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007412
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007413 /*
7414 * We should never reach the point where we are emulating L2
7415 * due to invalid guest state as that means we incorrectly
7416 * allowed a nested VMEntry with an invalid vmcs12.
7417 */
7418 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7419
Avi Kivity49e9d552010-09-19 14:34:08 +02007420 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7421 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007422
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007423 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007424 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007425 return handle_interrupt_window(&vmx->vcpu);
7426
Radim Krčmář72875d82017-04-26 22:32:19 +02007427 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007428 return 1;
7429
Liran Alon9b8ae632017-11-05 16:56:34 +02007430 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007431
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007432 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007433 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007434 ret = 0;
7435 goto out;
7436 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007437
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007438 if (err != EMULATE_DONE)
7439 goto emulation_error;
7440
7441 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7442 vcpu->arch.exception.pending)
7443 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007444
Gleb Natapov8d76c492013-05-08 18:38:44 +03007445 if (vcpu->arch.halt_request) {
7446 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007447 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007448 goto out;
7449 }
7450
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007451 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007452 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007453 if (need_resched())
7454 schedule();
7455 }
7456
Mohammed Gamal80ced182009-09-01 12:48:18 +02007457out:
7458 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007459
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007460emulation_error:
7461 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7462 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7463 vcpu->run->internal.ndata = 0;
7464 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007465}
7466
7467static void grow_ple_window(struct kvm_vcpu *vcpu)
7468{
7469 struct vcpu_vmx *vmx = to_vmx(vcpu);
7470 int old = vmx->ple_window;
7471
Babu Mogerc8e88712018-03-16 16:37:24 -04007472 vmx->ple_window = __grow_ple_window(old, ple_window,
7473 ple_window_grow,
7474 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007475
7476 if (vmx->ple_window != old)
7477 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007478
7479 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007480}
7481
7482static void shrink_ple_window(struct kvm_vcpu *vcpu)
7483{
7484 struct vcpu_vmx *vmx = to_vmx(vcpu);
7485 int old = vmx->ple_window;
7486
Babu Mogerc8e88712018-03-16 16:37:24 -04007487 vmx->ple_window = __shrink_ple_window(old, ple_window,
7488 ple_window_shrink,
7489 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007490
7491 if (vmx->ple_window != old)
7492 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007493
7494 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007495}
7496
7497/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007498 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7499 */
7500static void wakeup_handler(void)
7501{
7502 struct kvm_vcpu *vcpu;
7503 int cpu = smp_processor_id();
7504
7505 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7506 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7507 blocked_vcpu_list) {
7508 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7509
7510 if (pi_test_on(pi_desc) == 1)
7511 kvm_vcpu_kick(vcpu);
7512 }
7513 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7514}
7515
Peng Haoe01bca22018-04-07 05:47:32 +08007516static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007517{
7518 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7519 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7520 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7521 0ull, VMX_EPT_EXECUTABLE_MASK,
7522 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007523 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007524
7525 ept_set_mmio_spte_mask();
7526 kvm_enable_tdp();
7527}
7528
Tiejun Chenf2c76482014-10-28 10:14:47 +08007529static __init int hardware_setup(void)
7530{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007531 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007532
7533 rdmsrl_safe(MSR_EFER, &host_efer);
7534
7535 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7536 kvm_define_shared_msr(i, vmx_msr_index[i]);
7537
Radim Krčmář23611332016-09-29 22:41:33 +02007538 for (i = 0; i < VMX_BITMAP_NR; i++) {
7539 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7540 if (!vmx_bitmap[i])
7541 goto out;
7542 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007543
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007544 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7545 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7546
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007547 if (setup_vmcs_config(&vmcs_config) < 0) {
7548 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007549 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007550 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007551
7552 if (boot_cpu_has(X86_FEATURE_NX))
7553 kvm_enable_efer_bits(EFER_NX);
7554
Wanpeng Li08d839c2017-03-23 05:30:08 -07007555 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7556 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007557 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007558
Tiejun Chenf2c76482014-10-28 10:14:47 +08007559 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007560 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007561 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007562 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007563 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007564
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007565 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007566 enable_ept_ad_bits = 0;
7567
Wanpeng Li8ad81822017-10-09 15:51:53 -07007568 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007569 enable_unrestricted_guest = 0;
7570
Paolo Bonziniad15a292015-01-30 16:18:49 +01007571 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007572 flexpriority_enabled = 0;
7573
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007574 if (!cpu_has_virtual_nmis())
7575 enable_vnmi = 0;
7576
Paolo Bonziniad15a292015-01-30 16:18:49 +01007577 /*
7578 * set_apic_access_page_addr() is used to reload apic access
7579 * page upon invalidation. No need to do anything if not
7580 * using the APIC_ACCESS_ADDR VMCS field.
7581 */
7582 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007583 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007584
7585 if (!cpu_has_vmx_tpr_shadow())
7586 kvm_x86_ops->update_cr8_intercept = NULL;
7587
7588 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7589 kvm_disable_largepages();
7590
Wanpeng Li0f107682017-09-28 18:06:24 -07007591 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007592 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007593 ple_window = 0;
7594 ple_window_grow = 0;
7595 ple_window_max = 0;
7596 ple_window_shrink = 0;
7597 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007598
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007599 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007601 kvm_x86_ops->sync_pir_to_irr = NULL;
7602 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007603
Haozhong Zhang64903d62015-10-20 15:39:09 +08007604 if (cpu_has_vmx_tsc_scaling()) {
7605 kvm_has_tsc_control = true;
7606 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7607 kvm_tsc_scaling_ratio_frac_bits = 48;
7608 }
7609
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007610 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7611
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007612 if (enable_ept)
7613 vmx_enable_tdp();
7614 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007615 kvm_disable_tdp();
7616
Kai Huang843e4332015-01-28 10:54:28 +08007617 /*
7618 * Only enable PML when hardware supports PML feature, and both EPT
7619 * and EPT A/D bit features are enabled -- PML depends on them to work.
7620 */
7621 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7622 enable_pml = 0;
7623
7624 if (!enable_pml) {
7625 kvm_x86_ops->slot_enable_log_dirty = NULL;
7626 kvm_x86_ops->slot_disable_log_dirty = NULL;
7627 kvm_x86_ops->flush_log_dirty = NULL;
7628 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7629 }
7630
Yunhong Jiang64672c92016-06-13 14:19:59 -07007631 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7632 u64 vmx_msr;
7633
7634 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7635 cpu_preemption_timer_multi =
7636 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7637 } else {
7638 kvm_x86_ops->set_hv_timer = NULL;
7639 kvm_x86_ops->cancel_hv_timer = NULL;
7640 }
7641
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007642 if (!cpu_has_vmx_shadow_vmcs())
7643 enable_shadow_vmcs = 0;
7644 if (enable_shadow_vmcs)
7645 init_vmcs_shadow_fields();
7646
Feng Wubf9f6ac2015-09-18 22:29:55 +08007647 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007648 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007649
Ashok Rajc45dcc72016-06-22 14:59:56 +08007650 kvm_mce_cap_supported |= MCG_LMCE_P;
7651
Tiejun Chenf2c76482014-10-28 10:14:47 +08007652 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007653
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007654out:
Radim Krčmář23611332016-09-29 22:41:33 +02007655 for (i = 0; i < VMX_BITMAP_NR; i++)
7656 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007657
7658 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659}
7660
7661static __exit void hardware_unsetup(void)
7662{
Radim Krčmář23611332016-09-29 22:41:33 +02007663 int i;
7664
7665 for (i = 0; i < VMX_BITMAP_NR; i++)
7666 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007667
Tiejun Chenf2c76482014-10-28 10:14:47 +08007668 free_kvm_area();
7669}
7670
Avi Kivity6aa8b732006-12-10 02:21:36 -08007671/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007672 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7673 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7674 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007675static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007676{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007677 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007678 grow_ple_window(vcpu);
7679
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007680 /*
7681 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7682 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7683 * never set PAUSE_EXITING and just set PLE if supported,
7684 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7685 */
7686 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007687 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007688}
7689
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007690static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007691{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007692 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007693}
7694
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007695static int handle_mwait(struct kvm_vcpu *vcpu)
7696{
7697 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7698 return handle_nop(vcpu);
7699}
7700
Jim Mattson45ec3682017-08-23 16:32:04 -07007701static int handle_invalid_op(struct kvm_vcpu *vcpu)
7702{
7703 kvm_queue_exception(vcpu, UD_VECTOR);
7704 return 1;
7705}
7706
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007707static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7708{
7709 return 1;
7710}
7711
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007712static int handle_monitor(struct kvm_vcpu *vcpu)
7713{
7714 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7715 return handle_nop(vcpu);
7716}
7717
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007718/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007719 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7720 * set the success or error code of an emulated VMX instruction, as specified
7721 * by Vol 2B, VMX Instruction Reference, "Conventions".
7722 */
7723static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7724{
7725 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7726 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7727 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7728}
7729
7730static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7731{
7732 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7733 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7734 X86_EFLAGS_SF | X86_EFLAGS_OF))
7735 | X86_EFLAGS_CF);
7736}
7737
Abel Gordon145c28d2013-04-18 14:36:55 +03007738static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007739 u32 vm_instruction_error)
7740{
7741 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7742 /*
7743 * failValid writes the error number to the current VMCS, which
7744 * can't be done there isn't a current VMCS.
7745 */
7746 nested_vmx_failInvalid(vcpu);
7747 return;
7748 }
7749 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7750 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7751 X86_EFLAGS_SF | X86_EFLAGS_OF))
7752 | X86_EFLAGS_ZF);
7753 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7754 /*
7755 * We don't need to force a shadow sync because
7756 * VM_INSTRUCTION_ERROR is not shadowed
7757 */
7758}
Abel Gordon145c28d2013-04-18 14:36:55 +03007759
Wincy Vanff651cb2014-12-11 08:52:58 +03007760static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7761{
7762 /* TODO: not to reset guest simply here. */
7763 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007764 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007765}
7766
Jan Kiszkaf4124502014-03-07 20:03:13 +01007767static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7768{
7769 struct vcpu_vmx *vmx =
7770 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7771
7772 vmx->nested.preemption_timer_expired = true;
7773 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7774 kvm_vcpu_kick(&vmx->vcpu);
7775
7776 return HRTIMER_NORESTART;
7777}
7778
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007779/*
Bandan Das19677e32014-05-06 02:19:15 -04007780 * Decode the memory-address operand of a vmx instruction, as recorded on an
7781 * exit caused by such an instruction (run by a guest hypervisor).
7782 * On success, returns 0. When the operand is invalid, returns 1 and throws
7783 * #UD or #GP.
7784 */
7785static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7786 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007787 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007788{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007789 gva_t off;
7790 bool exn;
7791 struct kvm_segment s;
7792
Bandan Das19677e32014-05-06 02:19:15 -04007793 /*
7794 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7795 * Execution", on an exit, vmx_instruction_info holds most of the
7796 * addressing components of the operand. Only the displacement part
7797 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7798 * For how an actual address is calculated from all these components,
7799 * refer to Vol. 1, "Operand Addressing".
7800 */
7801 int scaling = vmx_instruction_info & 3;
7802 int addr_size = (vmx_instruction_info >> 7) & 7;
7803 bool is_reg = vmx_instruction_info & (1u << 10);
7804 int seg_reg = (vmx_instruction_info >> 15) & 7;
7805 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7806 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7807 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7808 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7809
7810 if (is_reg) {
7811 kvm_queue_exception(vcpu, UD_VECTOR);
7812 return 1;
7813 }
7814
7815 /* Addr = segment_base + offset */
7816 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007817 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007818 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007819 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007820 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007821 off += kvm_register_read(vcpu, index_reg)<<scaling;
7822 vmx_get_segment(vcpu, &s, seg_reg);
7823 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007824
7825 if (addr_size == 1) /* 32 bit */
7826 *ret &= 0xffffffff;
7827
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007828 /* Checks for #GP/#SS exceptions. */
7829 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007830 if (is_long_mode(vcpu)) {
7831 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7832 * non-canonical form. This is the only check on the memory
7833 * destination for long mode!
7834 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007835 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007836 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007837 /* Protected mode: apply checks for segment validity in the
7838 * following order:
7839 * - segment type check (#GP(0) may be thrown)
7840 * - usability check (#GP(0)/#SS(0))
7841 * - limit check (#GP(0)/#SS(0))
7842 */
7843 if (wr)
7844 /* #GP(0) if the destination operand is located in a
7845 * read-only data segment or any code segment.
7846 */
7847 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7848 else
7849 /* #GP(0) if the source operand is located in an
7850 * execute-only code segment
7851 */
7852 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007853 if (exn) {
7854 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7855 return 1;
7856 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007857 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7858 */
7859 exn = (s.unusable != 0);
7860 /* Protected mode: #GP(0)/#SS(0) if the memory
7861 * operand is outside the segment limit.
7862 */
7863 exn = exn || (off + sizeof(u64) > s.limit);
7864 }
7865 if (exn) {
7866 kvm_queue_exception_e(vcpu,
7867 seg_reg == VCPU_SREG_SS ?
7868 SS_VECTOR : GP_VECTOR,
7869 0);
7870 return 1;
7871 }
7872
Bandan Das19677e32014-05-06 02:19:15 -04007873 return 0;
7874}
7875
Radim Krčmářcbf71272017-05-19 15:48:51 +02007876static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007877{
7878 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007879 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007880
7881 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007882 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007883 return 1;
7884
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007885 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007886 kvm_inject_page_fault(vcpu, &e);
7887 return 1;
7888 }
7889
Bandan Das3573e222014-05-06 02:19:16 -04007890 return 0;
7891}
7892
Jim Mattsone29acc52016-11-30 12:03:43 -08007893static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7894{
7895 struct vcpu_vmx *vmx = to_vmx(vcpu);
7896 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007897 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007898
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007899 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7900 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007901 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007902
7903 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7904 if (!vmx->nested.cached_vmcs12)
7905 goto out_cached_vmcs12;
7906
7907 if (enable_shadow_vmcs) {
7908 shadow_vmcs = alloc_vmcs();
7909 if (!shadow_vmcs)
7910 goto out_shadow_vmcs;
7911 /* mark vmcs as shadow */
7912 shadow_vmcs->revision_id |= (1u << 31);
7913 /* init shadow vmcs */
7914 vmcs_clear(shadow_vmcs);
7915 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7916 }
7917
Jim Mattsone29acc52016-11-30 12:03:43 -08007918 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7919 HRTIMER_MODE_REL_PINNED);
7920 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7921
7922 vmx->nested.vmxon = true;
7923 return 0;
7924
7925out_shadow_vmcs:
7926 kfree(vmx->nested.cached_vmcs12);
7927
7928out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007929 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007930
Jim Mattsonde3a0022017-11-27 17:22:25 -06007931out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007932 return -ENOMEM;
7933}
7934
Bandan Das3573e222014-05-06 02:19:16 -04007935/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007936 * Emulate the VMXON instruction.
7937 * Currently, we just remember that VMX is active, and do not save or even
7938 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7939 * do not currently need to store anything in that guest-allocated memory
7940 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7941 * argument is different from the VMXON pointer (which the spec says they do).
7942 */
7943static int handle_vmon(struct kvm_vcpu *vcpu)
7944{
Jim Mattsone29acc52016-11-30 12:03:43 -08007945 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007946 gpa_t vmptr;
7947 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007948 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007949 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7950 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007951
Jim Mattson70f3aac2017-04-26 08:53:46 -07007952 /*
7953 * The Intel VMX Instruction Reference lists a bunch of bits that are
7954 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7955 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7956 * Otherwise, we should fail with #UD. But most faulting conditions
7957 * have already been checked by hardware, prior to the VM-exit for
7958 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7959 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007960 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007961 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007962 kvm_queue_exception(vcpu, UD_VECTOR);
7963 return 1;
7964 }
7965
Felix Wilhelm727ba742018-06-11 09:43:44 +02007966 /* CPL=0 must be checked manually. */
7967 if (vmx_get_cpl(vcpu)) {
7968 kvm_queue_exception(vcpu, UD_VECTOR);
7969 return 1;
7970 }
7971
Abel Gordon145c28d2013-04-18 14:36:55 +03007972 if (vmx->nested.vmxon) {
7973 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007974 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007975 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007976
Haozhong Zhang3b840802016-06-22 14:59:54 +08007977 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007978 != VMXON_NEEDED_FEATURES) {
7979 kvm_inject_gp(vcpu, 0);
7980 return 1;
7981 }
7982
Radim Krčmářcbf71272017-05-19 15:48:51 +02007983 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007984 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007985
7986 /*
7987 * SDM 3: 24.11.5
7988 * The first 4 bytes of VMXON region contain the supported
7989 * VMCS revision identifier
7990 *
7991 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7992 * which replaces physical address width with 32
7993 */
7994 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7995 nested_vmx_failInvalid(vcpu);
7996 return kvm_skip_emulated_instruction(vcpu);
7997 }
7998
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007999 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8000 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008001 nested_vmx_failInvalid(vcpu);
8002 return kvm_skip_emulated_instruction(vcpu);
8003 }
8004 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8005 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008006 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008007 nested_vmx_failInvalid(vcpu);
8008 return kvm_skip_emulated_instruction(vcpu);
8009 }
8010 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008011 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008012
8013 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008014 ret = enter_vmx_operation(vcpu);
8015 if (ret)
8016 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008017
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008018 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008019 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008020}
8021
8022/*
8023 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8024 * for running VMX instructions (except VMXON, whose prerequisites are
8025 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008026 * Note that many of these exceptions have priority over VM exits, so they
8027 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008028 */
8029static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8030{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008031 if (vmx_get_cpl(vcpu)) {
8032 kvm_queue_exception(vcpu, UD_VECTOR);
8033 return 0;
8034 }
8035
Jim Mattson70f3aac2017-04-26 08:53:46 -07008036 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008037 kvm_queue_exception(vcpu, UD_VECTOR);
8038 return 0;
8039 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008040 return 1;
8041}
8042
David Matlack8ca44e82017-08-01 14:00:39 -07008043static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8044{
8045 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8046 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8047}
8048
Abel Gordone7953d72013-04-18 14:37:55 +03008049static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8050{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008051 if (vmx->nested.current_vmptr == -1ull)
8052 return;
8053
Abel Gordon012f83c2013-04-18 14:39:25 +03008054 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008055 /* copy to memory all shadowed fields in case
8056 they were modified */
8057 copy_shadow_to_vmcs12(vmx);
8058 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008059 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008060 }
Wincy Van705699a2015-02-03 23:58:17 +08008061 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008062
8063 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008064 kvm_vcpu_write_guest_page(&vmx->vcpu,
8065 vmx->nested.current_vmptr >> PAGE_SHIFT,
8066 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008067
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008068 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008069}
8070
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008071/*
8072 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8073 * just stops using VMX.
8074 */
8075static void free_nested(struct vcpu_vmx *vmx)
8076{
Wanpeng Lib7455822017-11-22 14:04:00 -08008077 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008078 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008079
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008080 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008081 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008082 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008083 vmx->nested.posted_intr_nv = -1;
8084 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008085 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008086 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008087 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8088 free_vmcs(vmx->vmcs01.shadow_vmcs);
8089 vmx->vmcs01.shadow_vmcs = NULL;
8090 }
David Matlack4f2777b2016-07-13 17:16:37 -07008091 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008092 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008093 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008094 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008095 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008096 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008097 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008098 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008099 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008100 }
Wincy Van705699a2015-02-03 23:58:17 +08008101 if (vmx->nested.pi_desc_page) {
8102 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008103 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008104 vmx->nested.pi_desc_page = NULL;
8105 vmx->nested.pi_desc = NULL;
8106 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008107
Jim Mattsonde3a0022017-11-27 17:22:25 -06008108 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008109}
8110
8111/* Emulate the VMXOFF instruction */
8112static int handle_vmoff(struct kvm_vcpu *vcpu)
8113{
8114 if (!nested_vmx_check_permission(vcpu))
8115 return 1;
8116 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008117 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008118 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008119}
8120
Nadav Har'El27d6c862011-05-25 23:06:59 +03008121/* Emulate the VMCLEAR instruction */
8122static int handle_vmclear(struct kvm_vcpu *vcpu)
8123{
8124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008125 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008126 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008127
8128 if (!nested_vmx_check_permission(vcpu))
8129 return 1;
8130
Radim Krčmářcbf71272017-05-19 15:48:51 +02008131 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008132 return 1;
8133
Radim Krčmářcbf71272017-05-19 15:48:51 +02008134 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8135 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8136 return kvm_skip_emulated_instruction(vcpu);
8137 }
8138
8139 if (vmptr == vmx->nested.vmxon_ptr) {
8140 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8141 return kvm_skip_emulated_instruction(vcpu);
8142 }
8143
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008144 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008145 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008146
Jim Mattson587d7e722017-03-02 12:41:48 -08008147 kvm_vcpu_write_guest(vcpu,
8148 vmptr + offsetof(struct vmcs12, launch_state),
8149 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008150
Nadav Har'El27d6c862011-05-25 23:06:59 +03008151 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008152 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008153}
8154
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008155static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8156
8157/* Emulate the VMLAUNCH instruction */
8158static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8159{
8160 return nested_vmx_run(vcpu, true);
8161}
8162
8163/* Emulate the VMRESUME instruction */
8164static int handle_vmresume(struct kvm_vcpu *vcpu)
8165{
8166
8167 return nested_vmx_run(vcpu, false);
8168}
8169
Nadav Har'El49f705c2011-05-25 23:08:30 +03008170/*
8171 * Read a vmcs12 field. Since these can have varying lengths and we return
8172 * one type, we chose the biggest type (u64) and zero-extend the return value
8173 * to that size. Note that the caller, handle_vmread, might need to use only
8174 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8175 * 64-bit fields are to be returned).
8176 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008177static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8178 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008179{
8180 short offset = vmcs_field_to_offset(field);
8181 char *p;
8182
8183 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008184 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008185
8186 p = ((char *)(get_vmcs12(vcpu))) + offset;
8187
Jim Mattsond37f4262017-12-22 12:12:16 -08008188 switch (vmcs_field_width(field)) {
8189 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008190 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008191 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008192 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008193 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008194 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008195 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008196 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008197 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008198 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008199 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008200 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008201 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008202 WARN_ON(1);
8203 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008204 }
8205}
8206
Abel Gordon20b97fe2013-04-18 14:36:25 +03008207
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008208static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8209 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008210 short offset = vmcs_field_to_offset(field);
8211 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8212 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008213 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008214
Jim Mattsond37f4262017-12-22 12:12:16 -08008215 switch (vmcs_field_width(field)) {
8216 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008217 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008218 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008219 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008220 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008221 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008222 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008223 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008224 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008225 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008226 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008227 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008228 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008229 WARN_ON(1);
8230 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008231 }
8232
8233}
8234
Jim Mattsonf4160e42018-05-29 09:11:33 -07008235/*
8236 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8237 * they have been modified by the L1 guest. Note that the "read-only"
8238 * VM-exit information fields are actually writable if the vCPU is
8239 * configured to support "VMWRITE to any supported field in the VMCS."
8240 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008241static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8242{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008243 const u16 *fields[] = {
8244 shadow_read_write_fields,
8245 shadow_read_only_fields
8246 };
8247 const int max_fields[] = {
8248 max_shadow_read_write_fields,
8249 max_shadow_read_only_fields
8250 };
8251 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008252 unsigned long field;
8253 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008254 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008255
Jan Kiszka282da872014-10-08 18:05:39 +02008256 preempt_disable();
8257
Abel Gordon16f5b902013-04-18 14:38:25 +03008258 vmcs_load(shadow_vmcs);
8259
Jim Mattsonf4160e42018-05-29 09:11:33 -07008260 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8261 for (i = 0; i < max_fields[q]; i++) {
8262 field = fields[q][i];
8263 field_value = __vmcs_readl(field);
8264 vmcs12_write_any(&vmx->vcpu, field, field_value);
8265 }
8266 /*
8267 * Skip the VM-exit information fields if they are read-only.
8268 */
8269 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8270 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008271 }
8272
8273 vmcs_clear(shadow_vmcs);
8274 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008275
8276 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008277}
8278
Abel Gordonc3114422013-04-18 14:38:55 +03008279static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8280{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008281 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008282 shadow_read_write_fields,
8283 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008284 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008285 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008286 max_shadow_read_write_fields,
8287 max_shadow_read_only_fields
8288 };
8289 int i, q;
8290 unsigned long field;
8291 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008292 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008293
8294 vmcs_load(shadow_vmcs);
8295
Mathias Krausec2bae892013-06-26 20:36:21 +02008296 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008297 for (i = 0; i < max_fields[q]; i++) {
8298 field = fields[q][i];
8299 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008300 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008301 }
8302 }
8303
8304 vmcs_clear(shadow_vmcs);
8305 vmcs_load(vmx->loaded_vmcs->vmcs);
8306}
8307
Nadav Har'El49f705c2011-05-25 23:08:30 +03008308/*
8309 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8310 * used before) all generate the same failure when it is missing.
8311 */
8312static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8313{
8314 struct vcpu_vmx *vmx = to_vmx(vcpu);
8315 if (vmx->nested.current_vmptr == -1ull) {
8316 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008317 return 0;
8318 }
8319 return 1;
8320}
8321
8322static int handle_vmread(struct kvm_vcpu *vcpu)
8323{
8324 unsigned long field;
8325 u64 field_value;
8326 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8327 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8328 gva_t gva = 0;
8329
Kyle Hueyeb277562016-11-29 12:40:39 -08008330 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008331 return 1;
8332
Kyle Huey6affcbe2016-11-29 12:40:40 -08008333 if (!nested_vmx_check_vmcs12(vcpu))
8334 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008335
Nadav Har'El49f705c2011-05-25 23:08:30 +03008336 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008337 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008338 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008339 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008341 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008342 }
8343 /*
8344 * Now copy part of this value to register or memory, as requested.
8345 * Note that the number of bits actually copied is 32 or 64 depending
8346 * on the guest's mode (32 or 64 bit), not on the given field's length.
8347 */
8348 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008349 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008350 field_value);
8351 } else {
8352 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008353 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008354 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008355 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008356 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8357 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008358 }
8359
8360 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008361 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008362}
8363
8364
8365static int handle_vmwrite(struct kvm_vcpu *vcpu)
8366{
8367 unsigned long field;
8368 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008369 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008370 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8371 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008372
Nadav Har'El49f705c2011-05-25 23:08:30 +03008373 /* The value to write might be 32 or 64 bits, depending on L1's long
8374 * mode, and eventually we need to write that into a field of several
8375 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008376 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008377 * bits into the vmcs12 field.
8378 */
8379 u64 field_value = 0;
8380 struct x86_exception e;
8381
Kyle Hueyeb277562016-11-29 12:40:39 -08008382 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008383 return 1;
8384
Kyle Huey6affcbe2016-11-29 12:40:40 -08008385 if (!nested_vmx_check_vmcs12(vcpu))
8386 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008387
Nadav Har'El49f705c2011-05-25 23:08:30 +03008388 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008389 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008390 (((vmx_instruction_info) >> 3) & 0xf));
8391 else {
8392 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008393 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008394 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008395 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8396 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008397 kvm_inject_page_fault(vcpu, &e);
8398 return 1;
8399 }
8400 }
8401
8402
Nadav Amit27e6fb52014-06-18 17:19:26 +03008403 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008404 /*
8405 * If the vCPU supports "VMWRITE to any supported field in the
8406 * VMCS," then the "read-only" fields are actually read/write.
8407 */
8408 if (vmcs_field_readonly(field) &&
8409 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008410 nested_vmx_failValid(vcpu,
8411 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008412 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008413 }
8414
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008415 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008416 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008417 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008418 }
8419
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008420 switch (field) {
8421#define SHADOW_FIELD_RW(x) case x:
8422#include "vmx_shadow_fields.h"
8423 /*
8424 * The fields that can be updated by L1 without a vmexit are
8425 * always updated in the vmcs02, the others go down the slow
8426 * path of prepare_vmcs02.
8427 */
8428 break;
8429 default:
8430 vmx->nested.dirty_vmcs12 = true;
8431 break;
8432 }
8433
Nadav Har'El49f705c2011-05-25 23:08:30 +03008434 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008435 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008436}
8437
Jim Mattsona8bc2842016-11-30 12:03:44 -08008438static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8439{
8440 vmx->nested.current_vmptr = vmptr;
8441 if (enable_shadow_vmcs) {
8442 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8443 SECONDARY_EXEC_SHADOW_VMCS);
8444 vmcs_write64(VMCS_LINK_POINTER,
8445 __pa(vmx->vmcs01.shadow_vmcs));
8446 vmx->nested.sync_shadow_vmcs = true;
8447 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008448 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008449}
8450
Nadav Har'El63846662011-05-25 23:07:29 +03008451/* Emulate the VMPTRLD instruction */
8452static int handle_vmptrld(struct kvm_vcpu *vcpu)
8453{
8454 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008455 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008456
8457 if (!nested_vmx_check_permission(vcpu))
8458 return 1;
8459
Radim Krčmářcbf71272017-05-19 15:48:51 +02008460 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008461 return 1;
8462
Radim Krčmářcbf71272017-05-19 15:48:51 +02008463 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8464 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8465 return kvm_skip_emulated_instruction(vcpu);
8466 }
8467
8468 if (vmptr == vmx->nested.vmxon_ptr) {
8469 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8470 return kvm_skip_emulated_instruction(vcpu);
8471 }
8472
Nadav Har'El63846662011-05-25 23:07:29 +03008473 if (vmx->nested.current_vmptr != vmptr) {
8474 struct vmcs12 *new_vmcs12;
8475 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008476 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8477 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008478 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008479 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008480 }
8481 new_vmcs12 = kmap(page);
8482 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8483 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008484 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008485 nested_vmx_failValid(vcpu,
8486 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008487 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008488 }
Nadav Har'El63846662011-05-25 23:07:29 +03008489
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008490 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008491 /*
8492 * Load VMCS12 from guest memory since it is not already
8493 * cached.
8494 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008495 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8496 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008497 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008498
Jim Mattsona8bc2842016-11-30 12:03:44 -08008499 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008500 }
8501
8502 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008503 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008504}
8505
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008506/* Emulate the VMPTRST instruction */
8507static int handle_vmptrst(struct kvm_vcpu *vcpu)
8508{
8509 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8510 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8511 gva_t vmcs_gva;
8512 struct x86_exception e;
8513
8514 if (!nested_vmx_check_permission(vcpu))
8515 return 1;
8516
8517 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008518 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008519 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008520 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008521 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8522 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8523 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008524 kvm_inject_page_fault(vcpu, &e);
8525 return 1;
8526 }
8527 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008528 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008529}
8530
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008531/* Emulate the INVEPT instruction */
8532static int handle_invept(struct kvm_vcpu *vcpu)
8533{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008534 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008535 u32 vmx_instruction_info, types;
8536 unsigned long type;
8537 gva_t gva;
8538 struct x86_exception e;
8539 struct {
8540 u64 eptp, gpa;
8541 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008542
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008543 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008544 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008545 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008546 kvm_queue_exception(vcpu, UD_VECTOR);
8547 return 1;
8548 }
8549
8550 if (!nested_vmx_check_permission(vcpu))
8551 return 1;
8552
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008553 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008554 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008555
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008556 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008557
Jim Mattson85c856b2016-10-26 08:38:38 -07008558 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008559 nested_vmx_failValid(vcpu,
8560 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008561 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008562 }
8563
8564 /* According to the Intel VMX instruction reference, the memory
8565 * operand is read even if it isn't needed (e.g., for type==global)
8566 */
8567 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008568 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008569 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008570 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008571 kvm_inject_page_fault(vcpu, &e);
8572 return 1;
8573 }
8574
8575 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008576 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008577 /*
8578 * TODO: track mappings and invalidate
8579 * single context requests appropriately
8580 */
8581 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008582 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008583 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008584 nested_vmx_succeed(vcpu);
8585 break;
8586 default:
8587 BUG_ON(1);
8588 break;
8589 }
8590
Kyle Huey6affcbe2016-11-29 12:40:40 -08008591 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008592}
8593
Petr Matouseka642fc32014-09-23 20:22:30 +02008594static int handle_invvpid(struct kvm_vcpu *vcpu)
8595{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008596 struct vcpu_vmx *vmx = to_vmx(vcpu);
8597 u32 vmx_instruction_info;
8598 unsigned long type, types;
8599 gva_t gva;
8600 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008601 struct {
8602 u64 vpid;
8603 u64 gla;
8604 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008605
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008606 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008607 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008608 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008609 kvm_queue_exception(vcpu, UD_VECTOR);
8610 return 1;
8611 }
8612
8613 if (!nested_vmx_check_permission(vcpu))
8614 return 1;
8615
8616 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8617 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8618
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008619 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008620 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008621
Jim Mattson85c856b2016-10-26 08:38:38 -07008622 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008623 nested_vmx_failValid(vcpu,
8624 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008625 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008626 }
8627
8628 /* according to the intel vmx instruction reference, the memory
8629 * operand is read even if it isn't needed (e.g., for type==global)
8630 */
8631 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8632 vmx_instruction_info, false, &gva))
8633 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008634 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008635 kvm_inject_page_fault(vcpu, &e);
8636 return 1;
8637 }
Jim Mattson40352602017-06-28 09:37:37 -07008638 if (operand.vpid >> 16) {
8639 nested_vmx_failValid(vcpu,
8640 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8641 return kvm_skip_emulated_instruction(vcpu);
8642 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008643
8644 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008645 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008646 if (!operand.vpid ||
8647 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008648 nested_vmx_failValid(vcpu,
8649 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8650 return kvm_skip_emulated_instruction(vcpu);
8651 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008652 if (cpu_has_vmx_invvpid_individual_addr() &&
8653 vmx->nested.vpid02) {
8654 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8655 vmx->nested.vpid02, operand.gla);
8656 } else
8657 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8658 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008659 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008660 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008661 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008662 nested_vmx_failValid(vcpu,
8663 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008664 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008665 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008666 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008667 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008668 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008669 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008670 break;
8671 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008672 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008673 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008674 }
8675
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008676 nested_vmx_succeed(vcpu);
8677
Kyle Huey6affcbe2016-11-29 12:40:40 -08008678 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008679}
8680
Kai Huang843e4332015-01-28 10:54:28 +08008681static int handle_pml_full(struct kvm_vcpu *vcpu)
8682{
8683 unsigned long exit_qualification;
8684
8685 trace_kvm_pml_full(vcpu->vcpu_id);
8686
8687 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8688
8689 /*
8690 * PML buffer FULL happened while executing iret from NMI,
8691 * "blocked by NMI" bit has to be set before next VM entry.
8692 */
8693 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008694 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008695 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8696 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8697 GUEST_INTR_STATE_NMI);
8698
8699 /*
8700 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8701 * here.., and there's no userspace involvement needed for PML.
8702 */
8703 return 1;
8704}
8705
Yunhong Jiang64672c92016-06-13 14:19:59 -07008706static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8707{
8708 kvm_lapic_expired_hv_timer(vcpu);
8709 return 1;
8710}
8711
Bandan Das41ab9372017-08-03 15:54:43 -04008712static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8713{
8714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008715 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8716
8717 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008718 switch (address & VMX_EPTP_MT_MASK) {
8719 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008720 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008721 return false;
8722 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008723 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008724 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008725 return false;
8726 break;
8727 default:
8728 return false;
8729 }
8730
David Hildenbrandbb97a012017-08-10 23:15:28 +02008731 /* only 4 levels page-walk length are valid */
8732 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008733 return false;
8734
8735 /* Reserved bits should not be set */
8736 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8737 return false;
8738
8739 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008740 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008741 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008742 return false;
8743 }
8744
8745 return true;
8746}
8747
8748static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8749 struct vmcs12 *vmcs12)
8750{
8751 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8752 u64 address;
8753 bool accessed_dirty;
8754 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8755
8756 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8757 !nested_cpu_has_ept(vmcs12))
8758 return 1;
8759
8760 if (index >= VMFUNC_EPTP_ENTRIES)
8761 return 1;
8762
8763
8764 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8765 &address, index * 8, 8))
8766 return 1;
8767
David Hildenbrandbb97a012017-08-10 23:15:28 +02008768 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008769
8770 /*
8771 * If the (L2) guest does a vmfunc to the currently
8772 * active ept pointer, we don't have to do anything else
8773 */
8774 if (vmcs12->ept_pointer != address) {
8775 if (!valid_ept_address(vcpu, address))
8776 return 1;
8777
8778 kvm_mmu_unload(vcpu);
8779 mmu->ept_ad = accessed_dirty;
8780 mmu->base_role.ad_disabled = !accessed_dirty;
8781 vmcs12->ept_pointer = address;
8782 /*
8783 * TODO: Check what's the correct approach in case
8784 * mmu reload fails. Currently, we just let the next
8785 * reload potentially fail
8786 */
8787 kvm_mmu_reload(vcpu);
8788 }
8789
8790 return 0;
8791}
8792
Bandan Das2a499e42017-08-03 15:54:41 -04008793static int handle_vmfunc(struct kvm_vcpu *vcpu)
8794{
Bandan Das27c42a12017-08-03 15:54:42 -04008795 struct vcpu_vmx *vmx = to_vmx(vcpu);
8796 struct vmcs12 *vmcs12;
8797 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8798
8799 /*
8800 * VMFUNC is only supported for nested guests, but we always enable the
8801 * secondary control for simplicity; for non-nested mode, fake that we
8802 * didn't by injecting #UD.
8803 */
8804 if (!is_guest_mode(vcpu)) {
8805 kvm_queue_exception(vcpu, UD_VECTOR);
8806 return 1;
8807 }
8808
8809 vmcs12 = get_vmcs12(vcpu);
8810 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8811 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008812
8813 switch (function) {
8814 case 0:
8815 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8816 goto fail;
8817 break;
8818 default:
8819 goto fail;
8820 }
8821 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008822
8823fail:
8824 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8825 vmcs_read32(VM_EXIT_INTR_INFO),
8826 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008827 return 1;
8828}
8829
Nadav Har'El0140cae2011-05-25 23:06:28 +03008830/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008831 * The exit handlers return 1 if the exit was handled fully and guest execution
8832 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8833 * to be done to userspace and return 0.
8834 */
Mathias Krause772e0312012-08-30 01:30:19 +02008835static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008836 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8837 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008838 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008839 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008840 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008841 [EXIT_REASON_CR_ACCESS] = handle_cr,
8842 [EXIT_REASON_DR_ACCESS] = handle_dr,
8843 [EXIT_REASON_CPUID] = handle_cpuid,
8844 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8845 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8846 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8847 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008848 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008849 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008850 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008851 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008852 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008853 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008854 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008855 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008856 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008857 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008858 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008859 [EXIT_REASON_VMOFF] = handle_vmoff,
8860 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008861 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8862 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008863 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008864 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008865 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008866 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008867 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008868 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008869 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8870 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008871 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8872 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008873 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008874 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008875 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008876 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008877 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008878 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008879 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008880 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008881 [EXIT_REASON_XSAVES] = handle_xsaves,
8882 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008883 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008884 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008885 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008886};
8887
8888static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008889 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008890
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008891static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8892 struct vmcs12 *vmcs12)
8893{
8894 unsigned long exit_qualification;
8895 gpa_t bitmap, last_bitmap;
8896 unsigned int port;
8897 int size;
8898 u8 b;
8899
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008900 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008901 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008902
8903 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8904
8905 port = exit_qualification >> 16;
8906 size = (exit_qualification & 7) + 1;
8907
8908 last_bitmap = (gpa_t)-1;
8909 b = -1;
8910
8911 while (size > 0) {
8912 if (port < 0x8000)
8913 bitmap = vmcs12->io_bitmap_a;
8914 else if (port < 0x10000)
8915 bitmap = vmcs12->io_bitmap_b;
8916 else
Joe Perches1d804d02015-03-30 16:46:09 -07008917 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008918 bitmap += (port & 0x7fff) / 8;
8919
8920 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008921 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008922 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008923 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008924 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008925
8926 port++;
8927 size--;
8928 last_bitmap = bitmap;
8929 }
8930
Joe Perches1d804d02015-03-30 16:46:09 -07008931 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008932}
8933
Nadav Har'El644d7112011-05-25 23:12:35 +03008934/*
8935 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8936 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8937 * disinterest in the current event (read or write a specific MSR) by using an
8938 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8939 */
8940static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8941 struct vmcs12 *vmcs12, u32 exit_reason)
8942{
8943 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8944 gpa_t bitmap;
8945
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008946 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008947 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008948
8949 /*
8950 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8951 * for the four combinations of read/write and low/high MSR numbers.
8952 * First we need to figure out which of the four to use:
8953 */
8954 bitmap = vmcs12->msr_bitmap;
8955 if (exit_reason == EXIT_REASON_MSR_WRITE)
8956 bitmap += 2048;
8957 if (msr_index >= 0xc0000000) {
8958 msr_index -= 0xc0000000;
8959 bitmap += 1024;
8960 }
8961
8962 /* Then read the msr_index'th bit from this bitmap: */
8963 if (msr_index < 1024*8) {
8964 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008965 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008966 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008967 return 1 & (b >> (msr_index & 7));
8968 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008969 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008970}
8971
8972/*
8973 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8974 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8975 * intercept (via guest_host_mask etc.) the current event.
8976 */
8977static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8978 struct vmcs12 *vmcs12)
8979{
8980 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8981 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008982 int reg;
8983 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008984
8985 switch ((exit_qualification >> 4) & 3) {
8986 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008987 reg = (exit_qualification >> 8) & 15;
8988 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008989 switch (cr) {
8990 case 0:
8991 if (vmcs12->cr0_guest_host_mask &
8992 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008993 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008994 break;
8995 case 3:
8996 if ((vmcs12->cr3_target_count >= 1 &&
8997 vmcs12->cr3_target_value0 == val) ||
8998 (vmcs12->cr3_target_count >= 2 &&
8999 vmcs12->cr3_target_value1 == val) ||
9000 (vmcs12->cr3_target_count >= 3 &&
9001 vmcs12->cr3_target_value2 == val) ||
9002 (vmcs12->cr3_target_count >= 4 &&
9003 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009004 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009005 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009006 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009007 break;
9008 case 4:
9009 if (vmcs12->cr4_guest_host_mask &
9010 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009011 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009012 break;
9013 case 8:
9014 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009015 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009016 break;
9017 }
9018 break;
9019 case 2: /* clts */
9020 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9021 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009022 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009023 break;
9024 case 1: /* mov from cr */
9025 switch (cr) {
9026 case 3:
9027 if (vmcs12->cpu_based_vm_exec_control &
9028 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009029 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009030 break;
9031 case 8:
9032 if (vmcs12->cpu_based_vm_exec_control &
9033 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009034 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009035 break;
9036 }
9037 break;
9038 case 3: /* lmsw */
9039 /*
9040 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9041 * cr0. Other attempted changes are ignored, with no exit.
9042 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009043 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009044 if (vmcs12->cr0_guest_host_mask & 0xe &
9045 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009046 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009047 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9048 !(vmcs12->cr0_read_shadow & 0x1) &&
9049 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009050 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009051 break;
9052 }
Joe Perches1d804d02015-03-30 16:46:09 -07009053 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009054}
9055
9056/*
9057 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9058 * should handle it ourselves in L0 (and then continue L2). Only call this
9059 * when in is_guest_mode (L2).
9060 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009061static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009062{
Nadav Har'El644d7112011-05-25 23:12:35 +03009063 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9064 struct vcpu_vmx *vmx = to_vmx(vcpu);
9065 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9066
Jim Mattson4f350c62017-09-14 16:31:44 -07009067 if (vmx->nested.nested_run_pending)
9068 return false;
9069
9070 if (unlikely(vmx->fail)) {
9071 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9072 vmcs_read32(VM_INSTRUCTION_ERROR));
9073 return true;
9074 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009075
David Matlackc9f04402017-08-01 14:00:40 -07009076 /*
9077 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009078 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9079 * Page). The CPU may write to these pages via their host
9080 * physical address while L2 is running, bypassing any
9081 * address-translation-based dirty tracking (e.g. EPT write
9082 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009083 *
9084 * Mark them dirty on every exit from L2 to prevent them from
9085 * getting out of sync with dirty tracking.
9086 */
9087 nested_mark_vmcs12_pages_dirty(vcpu);
9088
Jim Mattson4f350c62017-09-14 16:31:44 -07009089 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9090 vmcs_readl(EXIT_QUALIFICATION),
9091 vmx->idt_vectoring_info,
9092 intr_info,
9093 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9094 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009095
9096 switch (exit_reason) {
9097 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009098 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009099 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009100 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009101 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009102 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009103 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009104 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009105 else if (is_debug(intr_info) &&
9106 vcpu->guest_debug &
9107 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9108 return false;
9109 else if (is_breakpoint(intr_info) &&
9110 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9111 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009112 return vmcs12->exception_bitmap &
9113 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9114 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009115 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009116 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009117 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009118 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009119 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009120 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009121 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009122 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009123 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009124 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009125 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009126 case EXIT_REASON_HLT:
9127 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9128 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009129 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009130 case EXIT_REASON_INVLPG:
9131 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9132 case EXIT_REASON_RDPMC:
9133 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009134 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009135 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009136 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009137 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009138 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009139 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9140 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9141 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9142 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9143 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9144 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009145 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009146 /*
9147 * VMX instructions trap unconditionally. This allows L1 to
9148 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9149 */
Joe Perches1d804d02015-03-30 16:46:09 -07009150 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009151 case EXIT_REASON_CR_ACCESS:
9152 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9153 case EXIT_REASON_DR_ACCESS:
9154 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9155 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009156 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009157 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9158 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009159 case EXIT_REASON_MSR_READ:
9160 case EXIT_REASON_MSR_WRITE:
9161 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9162 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009163 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009164 case EXIT_REASON_MWAIT_INSTRUCTION:
9165 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009166 case EXIT_REASON_MONITOR_TRAP_FLAG:
9167 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 case EXIT_REASON_MONITOR_INSTRUCTION:
9169 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9170 case EXIT_REASON_PAUSE_INSTRUCTION:
9171 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9172 nested_cpu_has2(vmcs12,
9173 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9174 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009175 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009176 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009177 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009178 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009179 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009180 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009181 /*
9182 * The controls for "virtualize APIC accesses," "APIC-
9183 * register virtualization," and "virtual-interrupt
9184 * delivery" only come from vmcs12.
9185 */
Joe Perches1d804d02015-03-30 16:46:09 -07009186 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009188 /*
9189 * L0 always deals with the EPT violation. If nested EPT is
9190 * used, and the nested mmu code discovers that the address is
9191 * missing in the guest EPT table (EPT12), the EPT violation
9192 * will be injected with nested_ept_inject_page_fault()
9193 */
Joe Perches1d804d02015-03-30 16:46:09 -07009194 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009195 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009196 /*
9197 * L2 never uses directly L1's EPT, but rather L0's own EPT
9198 * table (shadow on EPT) or a merged EPT table that L0 built
9199 * (EPT on EPT). So any problems with the structure of the
9200 * table is L0's fault.
9201 */
Joe Perches1d804d02015-03-30 16:46:09 -07009202 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009203 case EXIT_REASON_INVPCID:
9204 return
9205 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9206 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009207 case EXIT_REASON_WBINVD:
9208 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9209 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009210 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009211 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9212 /*
9213 * This should never happen, since it is not possible to
9214 * set XSS to a non-zero value---neither in L1 nor in L2.
9215 * If if it were, XSS would have to be checked against
9216 * the XSS exit bitmap in vmcs12.
9217 */
9218 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009219 case EXIT_REASON_PREEMPTION_TIMER:
9220 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009221 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009222 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009223 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009224 case EXIT_REASON_VMFUNC:
9225 /* VM functions are emulated through L2->L0 vmexits. */
9226 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009227 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009228 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009229 }
9230}
9231
Paolo Bonzini7313c692017-07-27 10:31:25 +02009232static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9233{
9234 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9235
9236 /*
9237 * At this point, the exit interruption info in exit_intr_info
9238 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9239 * we need to query the in-kernel LAPIC.
9240 */
9241 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9242 if ((exit_intr_info &
9243 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9244 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9245 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9246 vmcs12->vm_exit_intr_error_code =
9247 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9248 }
9249
9250 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9251 vmcs_readl(EXIT_QUALIFICATION));
9252 return 1;
9253}
9254
Avi Kivity586f9602010-11-18 13:09:54 +02009255static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9256{
9257 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9258 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9259}
9260
Kai Huanga3eaa862015-11-04 13:46:05 +08009261static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009262{
Kai Huanga3eaa862015-11-04 13:46:05 +08009263 if (vmx->pml_pg) {
9264 __free_page(vmx->pml_pg);
9265 vmx->pml_pg = NULL;
9266 }
Kai Huang843e4332015-01-28 10:54:28 +08009267}
9268
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009269static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009270{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009271 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009272 u64 *pml_buf;
9273 u16 pml_idx;
9274
9275 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9276
9277 /* Do nothing if PML buffer is empty */
9278 if (pml_idx == (PML_ENTITY_NUM - 1))
9279 return;
9280
9281 /* PML index always points to next available PML buffer entity */
9282 if (pml_idx >= PML_ENTITY_NUM)
9283 pml_idx = 0;
9284 else
9285 pml_idx++;
9286
9287 pml_buf = page_address(vmx->pml_pg);
9288 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9289 u64 gpa;
9290
9291 gpa = pml_buf[pml_idx];
9292 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009293 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009294 }
9295
9296 /* reset PML index */
9297 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9298}
9299
9300/*
9301 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9302 * Called before reporting dirty_bitmap to userspace.
9303 */
9304static void kvm_flush_pml_buffers(struct kvm *kvm)
9305{
9306 int i;
9307 struct kvm_vcpu *vcpu;
9308 /*
9309 * We only need to kick vcpu out of guest mode here, as PML buffer
9310 * is flushed at beginning of all VMEXITs, and it's obvious that only
9311 * vcpus running in guest are possible to have unflushed GPAs in PML
9312 * buffer.
9313 */
9314 kvm_for_each_vcpu(i, vcpu, kvm)
9315 kvm_vcpu_kick(vcpu);
9316}
9317
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009318static void vmx_dump_sel(char *name, uint32_t sel)
9319{
9320 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009321 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009322 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9323 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9324 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9325}
9326
9327static void vmx_dump_dtsel(char *name, uint32_t limit)
9328{
9329 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9330 name, vmcs_read32(limit),
9331 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9332}
9333
9334static void dump_vmcs(void)
9335{
9336 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9337 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9338 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9339 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9340 u32 secondary_exec_control = 0;
9341 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009342 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009343 int i, n;
9344
9345 if (cpu_has_secondary_exec_ctrls())
9346 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9347
9348 pr_err("*** Guest State ***\n");
9349 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9350 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9351 vmcs_readl(CR0_GUEST_HOST_MASK));
9352 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9353 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9354 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9355 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9356 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9357 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009358 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9359 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9360 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9361 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009362 }
9363 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9364 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9365 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9366 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9367 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9368 vmcs_readl(GUEST_SYSENTER_ESP),
9369 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9370 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9371 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9372 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9373 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9374 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9375 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9376 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9377 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9378 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9379 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9380 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9381 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009382 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9383 efer, vmcs_read64(GUEST_IA32_PAT));
9384 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9385 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009386 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009387 if (cpu_has_load_perf_global_ctrl &&
9388 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009389 pr_err("PerfGlobCtl = 0x%016llx\n",
9390 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009391 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009392 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009393 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9394 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9395 vmcs_read32(GUEST_ACTIVITY_STATE));
9396 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9397 pr_err("InterruptStatus = %04x\n",
9398 vmcs_read16(GUEST_INTR_STATUS));
9399
9400 pr_err("*** Host State ***\n");
9401 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9402 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9403 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9404 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9405 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9406 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9407 vmcs_read16(HOST_TR_SELECTOR));
9408 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9409 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9410 vmcs_readl(HOST_TR_BASE));
9411 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9412 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9413 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9414 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9415 vmcs_readl(HOST_CR4));
9416 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9417 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9418 vmcs_read32(HOST_IA32_SYSENTER_CS),
9419 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9420 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009421 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9422 vmcs_read64(HOST_IA32_EFER),
9423 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009424 if (cpu_has_load_perf_global_ctrl &&
9425 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009426 pr_err("PerfGlobCtl = 0x%016llx\n",
9427 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009428
9429 pr_err("*** Control State ***\n");
9430 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9431 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9432 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9433 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9434 vmcs_read32(EXCEPTION_BITMAP),
9435 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9436 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9437 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9438 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9439 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9440 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9441 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9442 vmcs_read32(VM_EXIT_INTR_INFO),
9443 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9444 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9445 pr_err(" reason=%08x qualification=%016lx\n",
9446 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9447 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9448 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9449 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009450 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009451 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009452 pr_err("TSC Multiplier = 0x%016llx\n",
9453 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009454 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9455 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9456 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9457 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9458 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009459 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009460 n = vmcs_read32(CR3_TARGET_COUNT);
9461 for (i = 0; i + 1 < n; i += 4)
9462 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9463 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9464 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9465 if (i < n)
9466 pr_err("CR3 target%u=%016lx\n",
9467 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9468 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9469 pr_err("PLE Gap=%08x Window=%08x\n",
9470 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9471 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9472 pr_err("Virtual processor ID = 0x%04x\n",
9473 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9474}
9475
Avi Kivity6aa8b732006-12-10 02:21:36 -08009476/*
9477 * The guest has exited. See if we can fix it or if we need userspace
9478 * assistance.
9479 */
Avi Kivity851ba692009-08-24 11:10:17 +03009480static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009481{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009482 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009483 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009484 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009485
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009486 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9487
Kai Huang843e4332015-01-28 10:54:28 +08009488 /*
9489 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9490 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9491 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9492 * mode as if vcpus is in root mode, the PML buffer must has been
9493 * flushed already.
9494 */
9495 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009496 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009497
Mohammed Gamal80ced182009-09-01 12:48:18 +02009498 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009499 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009500 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009501
Paolo Bonzini7313c692017-07-27 10:31:25 +02009502 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9503 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009504
Mohammed Gamal51207022010-05-31 22:40:54 +03009505 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009506 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009507 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9508 vcpu->run->fail_entry.hardware_entry_failure_reason
9509 = exit_reason;
9510 return 0;
9511 }
9512
Avi Kivity29bd8a72007-09-10 17:27:03 +03009513 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009514 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9515 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009516 = vmcs_read32(VM_INSTRUCTION_ERROR);
9517 return 0;
9518 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009519
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009520 /*
9521 * Note:
9522 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9523 * delivery event since it indicates guest is accessing MMIO.
9524 * The vm-exit can be triggered again after return to guest that
9525 * will cause infinite loop.
9526 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009527 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009528 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009529 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009530 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009531 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9532 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9533 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009534 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009535 vcpu->run->internal.data[0] = vectoring_info;
9536 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009537 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9538 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9539 vcpu->run->internal.ndata++;
9540 vcpu->run->internal.data[3] =
9541 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9542 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009543 return 0;
9544 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009545
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009546 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009547 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9548 if (vmx_interrupt_allowed(vcpu)) {
9549 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9550 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9551 vcpu->arch.nmi_pending) {
9552 /*
9553 * This CPU don't support us in finding the end of an
9554 * NMI-blocked window if the guest runs with IRQs
9555 * disabled. So we pull the trigger after 1 s of
9556 * futile waiting, but inform the user about this.
9557 */
9558 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9559 "state on VCPU %d after 1 s timeout\n",
9560 __func__, vcpu->vcpu_id);
9561 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9562 }
9563 }
9564
Avi Kivity6aa8b732006-12-10 02:21:36 -08009565 if (exit_reason < kvm_vmx_max_exit_handlers
9566 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009567 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009568 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009569 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9570 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009571 kvm_queue_exception(vcpu, UD_VECTOR);
9572 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009573 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009574}
9575
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009576/*
9577 * Software based L1D cache flush which is used when microcode providing
9578 * the cache control MSR is not loaded.
9579 *
9580 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9581 * flush it is required to read in 64 KiB because the replacement algorithm
9582 * is not exactly LRU. This could be sized at runtime via topology
9583 * information but as all relevant affected CPUs have 32KiB L1D cache size
9584 * there is no point in doing so.
9585 */
9586#define L1D_CACHE_ORDER 4
9587static void *vmx_l1d_flush_pages;
9588
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009589static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009590{
9591 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009592 bool always;
9593
9594 /*
9595 * If the mitigation mode is 'flush always', keep the flush bit
9596 * set, otherwise clear it. It gets set again either from
9597 * vcpu_run() or from one of the unsafe VMEXIT handlers.
9598 */
9599 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
9600 vcpu->arch.l1tf_flush_l1d = always;
9601
9602 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009603
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009604 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9605 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9606 return;
9607 }
9608
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009609 asm volatile(
9610 /* First ensure the pages are in the TLB */
9611 "xorl %%eax, %%eax\n"
9612 ".Lpopulate_tlb:\n\t"
9613 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9614 "addl $4096, %%eax\n\t"
9615 "cmpl %%eax, %[size]\n\t"
9616 "jne .Lpopulate_tlb\n\t"
9617 "xorl %%eax, %%eax\n\t"
9618 "cpuid\n\t"
9619 /* Now fill the cache */
9620 "xorl %%eax, %%eax\n"
9621 ".Lfill_cache:\n"
9622 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9623 "addl $64, %%eax\n\t"
9624 "cmpl %%eax, %[size]\n\t"
9625 "jne .Lfill_cache\n\t"
9626 "lfence\n"
9627 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9628 [size] "r" (size)
9629 : "eax", "ebx", "ecx", "edx");
9630}
9631
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009632static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009633{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009634 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9635
9636 if (is_guest_mode(vcpu) &&
9637 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9638 return;
9639
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009640 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009641 vmcs_write32(TPR_THRESHOLD, 0);
9642 return;
9643 }
9644
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009645 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009646}
9647
Jim Mattson8d860bb2018-05-09 16:56:05 -04009648static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009649{
9650 u32 sec_exec_control;
9651
Jim Mattson8d860bb2018-05-09 16:56:05 -04009652 if (!lapic_in_kernel(vcpu))
9653 return;
9654
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009655 /* Postpone execution until vmcs01 is the current VMCS. */
9656 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009657 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009658 return;
9659 }
9660
Paolo Bonzini35754c92015-07-29 12:05:37 +02009661 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009662 return;
9663
9664 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009665 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9666 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009667
Jim Mattson8d860bb2018-05-09 16:56:05 -04009668 switch (kvm_get_apic_mode(vcpu)) {
9669 case LAPIC_MODE_INVALID:
9670 WARN_ONCE(true, "Invalid local APIC state");
9671 case LAPIC_MODE_DISABLED:
9672 break;
9673 case LAPIC_MODE_XAPIC:
9674 if (flexpriority_enabled) {
9675 sec_exec_control |=
9676 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9677 vmx_flush_tlb(vcpu, true);
9678 }
9679 break;
9680 case LAPIC_MODE_X2APIC:
9681 if (cpu_has_vmx_virtualize_x2apic_mode())
9682 sec_exec_control |=
9683 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9684 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009685 }
9686 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9687
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009688 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009689}
9690
Tang Chen38b99172014-09-24 15:57:54 +08009691static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9692{
Jim Mattsonab5df312018-05-09 17:02:03 -04009693 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009694 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009695 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009696 }
Tang Chen38b99172014-09-24 15:57:54 +08009697}
9698
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009699static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009700{
9701 u16 status;
9702 u8 old;
9703
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009704 if (max_isr == -1)
9705 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009706
9707 status = vmcs_read16(GUEST_INTR_STATUS);
9708 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009709 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009710 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009711 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009712 vmcs_write16(GUEST_INTR_STATUS, status);
9713 }
9714}
9715
9716static void vmx_set_rvi(int vector)
9717{
9718 u16 status;
9719 u8 old;
9720
Wei Wang4114c272014-11-05 10:53:43 +08009721 if (vector == -1)
9722 vector = 0;
9723
Yang Zhangc7c9c562013-01-25 10:18:51 +08009724 status = vmcs_read16(GUEST_INTR_STATUS);
9725 old = (u8)status & 0xff;
9726 if ((u8)vector != old) {
9727 status &= ~0xff;
9728 status |= (u8)vector;
9729 vmcs_write16(GUEST_INTR_STATUS, status);
9730 }
9731}
9732
9733static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9734{
Liran Alon851c1a182017-12-24 18:12:56 +02009735 /*
9736 * When running L2, updating RVI is only relevant when
9737 * vmcs12 virtual-interrupt-delivery enabled.
9738 * However, it can be enabled only when L1 also
9739 * intercepts external-interrupts and in that case
9740 * we should not update vmcs02 RVI but instead intercept
9741 * interrupt. Therefore, do nothing when running L2.
9742 */
9743 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009744 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009745}
9746
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009747static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009748{
9749 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009750 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009751 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009752
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009753 WARN_ON(!vcpu->arch.apicv_active);
9754 if (pi_test_on(&vmx->pi_desc)) {
9755 pi_clear_on(&vmx->pi_desc);
9756 /*
9757 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9758 * But on x86 this is just a compiler barrier anyway.
9759 */
9760 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009761 max_irr_updated =
9762 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9763
9764 /*
9765 * If we are running L2 and L1 has a new pending interrupt
9766 * which can be injected, we should re-evaluate
9767 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009768 * If L1 intercepts external-interrupts, we should
9769 * exit from L2 to L1. Otherwise, interrupt should be
9770 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009771 */
Liran Alon851c1a182017-12-24 18:12:56 +02009772 if (is_guest_mode(vcpu) && max_irr_updated) {
9773 if (nested_exit_on_intr(vcpu))
9774 kvm_vcpu_exiting_guest_mode(vcpu);
9775 else
9776 kvm_make_request(KVM_REQ_EVENT, vcpu);
9777 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009778 } else {
9779 max_irr = kvm_lapic_find_highest_irr(vcpu);
9780 }
9781 vmx_hwapic_irr_update(vcpu, max_irr);
9782 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009783}
9784
Andrey Smetanin63086302015-11-10 15:36:32 +03009785static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009786{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009787 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009788 return;
9789
Yang Zhangc7c9c562013-01-25 10:18:51 +08009790 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9791 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9792 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9793 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9794}
9795
Paolo Bonzini967235d2016-12-19 14:03:45 +01009796static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9797{
9798 struct vcpu_vmx *vmx = to_vmx(vcpu);
9799
9800 pi_clear_on(&vmx->pi_desc);
9801 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9802}
9803
Avi Kivity51aa01d2010-07-20 14:31:20 +03009804static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009805{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009806 u32 exit_intr_info = 0;
9807 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009808
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009809 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9810 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009811 return;
9812
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009813 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9814 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9815 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009816
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009817 /* if exit due to PF check for async PF */
9818 if (is_page_fault(exit_intr_info))
9819 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9820
Andi Kleena0861c02009-06-08 17:37:09 +08009821 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009822 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9823 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009824 kvm_machine_check();
9825
Gleb Natapov20f65982009-05-11 13:35:55 +03009826 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009827 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009828 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009829 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009830 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009831 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009832}
Gleb Natapov20f65982009-05-11 13:35:55 +03009833
Yang Zhanga547c6d2013-04-11 19:25:10 +08009834static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9835{
9836 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9837
Yang Zhanga547c6d2013-04-11 19:25:10 +08009838 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9839 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9840 unsigned int vector;
9841 unsigned long entry;
9842 gate_desc *desc;
9843 struct vcpu_vmx *vmx = to_vmx(vcpu);
9844#ifdef CONFIG_X86_64
9845 unsigned long tmp;
9846#endif
9847
9848 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9849 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009850 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009851 asm volatile(
9852#ifdef CONFIG_X86_64
9853 "mov %%" _ASM_SP ", %[sp]\n\t"
9854 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9855 "push $%c[ss]\n\t"
9856 "push %[sp]\n\t"
9857#endif
9858 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009859 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009860 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009861 :
9862#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009863 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009864#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009865 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009866 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009867 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009868 [ss]"i"(__KERNEL_DS),
9869 [cs]"i"(__KERNEL_CS)
9870 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009871 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009872 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009873}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009874STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009875
Tom Lendackybc226f02018-05-10 22:06:39 +02009876static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009877{
Tom Lendackybc226f02018-05-10 22:06:39 +02009878 switch (index) {
9879 case MSR_IA32_SMBASE:
9880 /*
9881 * We cannot do SMM unless we can run the guest in big
9882 * real mode.
9883 */
9884 return enable_unrestricted_guest || emulate_invalid_guest_state;
9885 case MSR_AMD64_VIRT_SPEC_CTRL:
9886 /* This is AMD only. */
9887 return false;
9888 default:
9889 return true;
9890 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009891}
9892
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009893static bool vmx_mpx_supported(void)
9894{
9895 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9896 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9897}
9898
Wanpeng Li55412b22014-12-02 19:21:30 +08009899static bool vmx_xsaves_supported(void)
9900{
9901 return vmcs_config.cpu_based_2nd_exec_ctrl &
9902 SECONDARY_EXEC_XSAVES;
9903}
9904
Avi Kivity51aa01d2010-07-20 14:31:20 +03009905static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9906{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009907 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009908 bool unblock_nmi;
9909 u8 vector;
9910 bool idtv_info_valid;
9911
9912 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009913
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009914 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009915 if (vmx->loaded_vmcs->nmi_known_unmasked)
9916 return;
9917 /*
9918 * Can't use vmx->exit_intr_info since we're not sure what
9919 * the exit reason is.
9920 */
9921 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9922 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9923 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9924 /*
9925 * SDM 3: 27.7.1.2 (September 2008)
9926 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9927 * a guest IRET fault.
9928 * SDM 3: 23.2.2 (September 2008)
9929 * Bit 12 is undefined in any of the following cases:
9930 * If the VM exit sets the valid bit in the IDT-vectoring
9931 * information field.
9932 * If the VM exit is due to a double fault.
9933 */
9934 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9935 vector != DF_VECTOR && !idtv_info_valid)
9936 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9937 GUEST_INTR_STATE_NMI);
9938 else
9939 vmx->loaded_vmcs->nmi_known_unmasked =
9940 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9941 & GUEST_INTR_STATE_NMI);
9942 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9943 vmx->loaded_vmcs->vnmi_blocked_time +=
9944 ktime_to_ns(ktime_sub(ktime_get(),
9945 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009946}
9947
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009948static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009949 u32 idt_vectoring_info,
9950 int instr_len_field,
9951 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009952{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009953 u8 vector;
9954 int type;
9955 bool idtv_info_valid;
9956
9957 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009958
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009959 vcpu->arch.nmi_injected = false;
9960 kvm_clear_exception_queue(vcpu);
9961 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009962
9963 if (!idtv_info_valid)
9964 return;
9965
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009966 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009967
Avi Kivity668f6122008-07-02 09:28:55 +03009968 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9969 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009970
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009971 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009972 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009973 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009974 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009975 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009976 * Clear bit "block by NMI" before VM entry if a NMI
9977 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009978 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009979 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009980 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009981 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009982 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009983 /* fall through */
9984 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009985 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009986 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009987 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009988 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009989 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009990 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009991 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009992 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009993 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009994 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009995 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009996 break;
9997 default:
9998 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009999 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010000}
10001
Avi Kivity83422e12010-07-20 14:43:23 +030010002static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10003{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010004 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010005 VM_EXIT_INSTRUCTION_LEN,
10006 IDT_VECTORING_ERROR_CODE);
10007}
10008
Avi Kivityb463a6f2010-07-20 15:06:17 +030010009static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10010{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010011 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010012 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10013 VM_ENTRY_INSTRUCTION_LEN,
10014 VM_ENTRY_EXCEPTION_ERROR_CODE);
10015
10016 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10017}
10018
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010019static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10020{
10021 int i, nr_msrs;
10022 struct perf_guest_switch_msr *msrs;
10023
10024 msrs = perf_guest_get_msrs(&nr_msrs);
10025
10026 if (!msrs)
10027 return;
10028
10029 for (i = 0; i < nr_msrs; i++)
10030 if (msrs[i].host == msrs[i].guest)
10031 clear_atomic_switch_msr(vmx, msrs[i].msr);
10032 else
10033 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10034 msrs[i].host);
10035}
10036
Jiang Biao33365e72016-11-03 15:03:37 +080010037static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010038{
10039 struct vcpu_vmx *vmx = to_vmx(vcpu);
10040 u64 tscl;
10041 u32 delta_tsc;
10042
10043 if (vmx->hv_deadline_tsc == -1)
10044 return;
10045
10046 tscl = rdtsc();
10047 if (vmx->hv_deadline_tsc > tscl)
10048 /* sure to be 32 bit only because checked on set_hv_timer */
10049 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10050 cpu_preemption_timer_multi);
10051 else
10052 delta_tsc = 0;
10053
10054 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10055}
10056
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010057static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010058{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010059 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010060 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010061
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010062 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010063 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010064 vmx->loaded_vmcs->soft_vnmi_blocked))
10065 vmx->loaded_vmcs->entry_time = ktime_get();
10066
Avi Kivity104f2262010-11-18 13:12:52 +020010067 /* Don't enter VMX if guest state is invalid, let the exit handler
10068 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010069 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010070 return;
10071
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010072 if (vmx->ple_window_dirty) {
10073 vmx->ple_window_dirty = false;
10074 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10075 }
10076
Abel Gordon012f83c2013-04-18 14:39:25 +030010077 if (vmx->nested.sync_shadow_vmcs) {
10078 copy_vmcs12_to_shadow(vmx);
10079 vmx->nested.sync_shadow_vmcs = false;
10080 }
10081
Avi Kivity104f2262010-11-18 13:12:52 +020010082 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10083 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10084 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10085 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10086
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010087 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010088 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010089 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010090 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010091 }
10092
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010093 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010094 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010095 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010096 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010097 }
10098
Avi Kivity104f2262010-11-18 13:12:52 +020010099 /* When single-stepping over STI and MOV SS, we must clear the
10100 * corresponding interruptibility bits in the guest state. Otherwise
10101 * vmentry fails as it then expects bit 14 (BS) in pending debug
10102 * exceptions being set, but that's not correct for the guest debugging
10103 * case. */
10104 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10105 vmx_set_interrupt_shadow(vcpu, 0);
10106
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010107 if (static_cpu_has(X86_FEATURE_PKU) &&
10108 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10109 vcpu->arch.pkru != vmx->host_pkru)
10110 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010111
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010112 atomic_switch_perf_msrs(vmx);
10113
Yunhong Jiang64672c92016-06-13 14:19:59 -070010114 vmx_arm_hv_timer(vcpu);
10115
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010116 /*
10117 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10118 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10119 * is no need to worry about the conditional branch over the wrmsr
10120 * being speculatively taken.
10121 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010122 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010123
Nadav Har'Eld462b812011-05-24 15:26:10 +030010124 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010125
10126 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10127 (unsigned long)&current_evmcs->host_rsp : 0;
10128
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010129 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10130 if (vcpu->arch.l1tf_flush_l1d)
10131 vmx_l1d_flush(vcpu);
10132 }
10133
Avi Kivity104f2262010-11-18 13:12:52 +020010134 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010135 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010136 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10137 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10138 "push %%" _ASM_CX " \n\t"
10139 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010140 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010141 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010142 /* Avoid VMWRITE when Enlightened VMCS is in use */
10143 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10144 "jz 2f \n\t"
10145 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10146 "jmp 1f \n\t"
10147 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010148 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010149 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010150 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010151 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10152 "mov %%cr2, %%" _ASM_DX " \n\t"
10153 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010154 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010155 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010156 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010157 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010158 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010159 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010160 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10161 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10162 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10163 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10164 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10165 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010166#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010167 "mov %c[r8](%0), %%r8 \n\t"
10168 "mov %c[r9](%0), %%r9 \n\t"
10169 "mov %c[r10](%0), %%r10 \n\t"
10170 "mov %c[r11](%0), %%r11 \n\t"
10171 "mov %c[r12](%0), %%r12 \n\t"
10172 "mov %c[r13](%0), %%r13 \n\t"
10173 "mov %c[r14](%0), %%r14 \n\t"
10174 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010175#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010176 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010177
Avi Kivity6aa8b732006-12-10 02:21:36 -080010178 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010179 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010180 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010181 "jmp 2f \n\t"
10182 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10183 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010184 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010185 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010186 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010187 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010188 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10189 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10190 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10191 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10192 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10193 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10194 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010195#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010196 "mov %%r8, %c[r8](%0) \n\t"
10197 "mov %%r9, %c[r9](%0) \n\t"
10198 "mov %%r10, %c[r10](%0) \n\t"
10199 "mov %%r11, %c[r11](%0) \n\t"
10200 "mov %%r12, %c[r12](%0) \n\t"
10201 "mov %%r13, %c[r13](%0) \n\t"
10202 "mov %%r14, %c[r14](%0) \n\t"
10203 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010204 "xor %%r8d, %%r8d \n\t"
10205 "xor %%r9d, %%r9d \n\t"
10206 "xor %%r10d, %%r10d \n\t"
10207 "xor %%r11d, %%r11d \n\t"
10208 "xor %%r12d, %%r12d \n\t"
10209 "xor %%r13d, %%r13d \n\t"
10210 "xor %%r14d, %%r14d \n\t"
10211 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010212#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010213 "mov %%cr2, %%" _ASM_AX " \n\t"
10214 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010215
Jim Mattson0cb5b302018-01-03 14:31:38 -080010216 "xor %%eax, %%eax \n\t"
10217 "xor %%ebx, %%ebx \n\t"
10218 "xor %%esi, %%esi \n\t"
10219 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010220 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010221 ".pushsection .rodata \n\t"
10222 ".global vmx_return \n\t"
10223 "vmx_return: " _ASM_PTR " 2b \n\t"
10224 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010225 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010226 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010227 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010228 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010229 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10230 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10231 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10232 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10233 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10234 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10235 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010236#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010237 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10238 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10239 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10240 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10241 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10242 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10243 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10244 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010245#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010246 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10247 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010248 : "cc", "memory"
10249#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010250 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010251 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010252#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010253 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010254#endif
10255 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010256
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010257 /*
10258 * We do not use IBRS in the kernel. If this vCPU has used the
10259 * SPEC_CTRL MSR it may have left it on; save the value and
10260 * turn it off. This is much more efficient than blindly adding
10261 * it to the atomic save/restore list. Especially as the former
10262 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10263 *
10264 * For non-nested case:
10265 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10266 * save it.
10267 *
10268 * For nested case:
10269 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10270 * save it.
10271 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010272 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010273 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010274
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010275 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010276
David Woodhouse117cc7a2018-01-12 11:11:27 +000010277 /* Eliminate branch target predictions from guest mode */
10278 vmexit_fill_RSB();
10279
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010280 /* All fields are clean at this point */
10281 if (static_branch_unlikely(&enable_evmcs))
10282 current_evmcs->hv_clean_fields |=
10283 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10284
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010285 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010286 if (vmx->host_debugctlmsr)
10287 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010288
Avi Kivityaa67f602012-08-01 16:48:03 +030010289#ifndef CONFIG_X86_64
10290 /*
10291 * The sysexit path does not restore ds/es, so we must set them to
10292 * a reasonable value ourselves.
10293 *
10294 * We can't defer this to vmx_load_host_state() since that function
10295 * may be executed in interrupt context, which saves and restore segments
10296 * around it, nullifying its effect.
10297 */
10298 loadsegment(ds, __USER_DS);
10299 loadsegment(es, __USER_DS);
10300#endif
10301
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010302 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010303 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010304 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010305 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010306 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010307 vcpu->arch.regs_dirty = 0;
10308
Gleb Natapove0b890d2013-09-25 12:51:33 +030010309 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010310 * eager fpu is enabled if PKEY is supported and CR4 is switched
10311 * back on host, so it is safe to read guest PKRU from current
10312 * XSAVE.
10313 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010314 if (static_cpu_has(X86_FEATURE_PKU) &&
10315 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10316 vcpu->arch.pkru = __read_pkru();
10317 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010318 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010319 }
10320
Gleb Natapove0b890d2013-09-25 12:51:33 +030010321 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010322 vmx->idt_vectoring_info = 0;
10323
10324 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10325 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10326 return;
10327
10328 vmx->loaded_vmcs->launched = 1;
10329 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010330
Avi Kivity51aa01d2010-07-20 14:31:20 +030010331 vmx_complete_atomic_exit(vmx);
10332 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010333 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010334}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010335STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010336
Sean Christopherson434a1e92018-03-20 12:17:18 -070010337static struct kvm *vmx_vm_alloc(void)
10338{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010339 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010340 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010341}
10342
10343static void vmx_vm_free(struct kvm *kvm)
10344{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010345 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010346}
10347
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010348static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010349{
10350 struct vcpu_vmx *vmx = to_vmx(vcpu);
10351 int cpu;
10352
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010353 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010354 return;
10355
10356 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010357 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010358 vmx_vcpu_put(vcpu);
10359 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010360 put_cpu();
10361}
10362
Jim Mattson2f1fe812016-07-08 15:36:06 -070010363/*
10364 * Ensure that the current vmcs of the logical processor is the
10365 * vmcs01 of the vcpu before calling free_nested().
10366 */
10367static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10368{
10369 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010370
Christoffer Dallec7660c2017-12-04 21:35:23 +010010371 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010372 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010373 free_nested(vmx);
10374 vcpu_put(vcpu);
10375}
10376
Avi Kivity6aa8b732006-12-10 02:21:36 -080010377static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10378{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010379 struct vcpu_vmx *vmx = to_vmx(vcpu);
10380
Kai Huang843e4332015-01-28 10:54:28 +080010381 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010382 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010383 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010384 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010385 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010386 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010387 kfree(vmx->guest_msrs);
10388 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010389 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010390}
10391
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010392static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010393{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010394 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010395 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010396 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010397 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010398
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010399 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010400 return ERR_PTR(-ENOMEM);
10401
Wanpeng Li991e7a02015-09-16 17:30:05 +080010402 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010403
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010404 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10405 if (err)
10406 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010407
Peter Feiner4e595162016-07-07 14:49:58 -070010408 err = -ENOMEM;
10409
10410 /*
10411 * If PML is turned on, failure on enabling PML just results in failure
10412 * of creating the vcpu, therefore we can simplify PML logic (by
10413 * avoiding dealing with cases, such as enabling PML partially on vcpus
10414 * for the guest, etc.
10415 */
10416 if (enable_pml) {
10417 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10418 if (!vmx->pml_pg)
10419 goto uninit_vcpu;
10420 }
10421
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010422 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010423 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10424 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010425
Peter Feiner4e595162016-07-07 14:49:58 -070010426 if (!vmx->guest_msrs)
10427 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010428
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010429 err = alloc_loaded_vmcs(&vmx->vmcs01);
10430 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010431 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010432
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010433 msr_bitmap = vmx->vmcs01.msr_bitmap;
10434 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10435 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10436 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10437 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10438 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10439 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10440 vmx->msr_bitmap_mode = 0;
10441
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010442 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010443 cpu = get_cpu();
10444 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010445 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010446 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010447 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010448 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010449 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010450 err = alloc_apic_access_page(kvm);
10451 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010452 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010453 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010454
Sean Christophersone90008d2018-03-05 12:04:37 -080010455 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010456 err = init_rmode_identity_map(kvm);
10457 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010458 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010459 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010460
Wanpeng Li5c614b32015-10-13 09:18:36 -070010461 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010462 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10463 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010464 vmx->nested.vpid02 = allocate_vpid();
10465 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010466
Wincy Van705699a2015-02-03 23:58:17 +080010467 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010468 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010469
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010470 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10471
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010472 /*
10473 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10474 * or POSTED_INTR_WAKEUP_VECTOR.
10475 */
10476 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10477 vmx->pi_desc.sn = 1;
10478
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010479 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010480
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010481free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010482 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010483 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010484free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010485 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010486free_pml:
10487 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010488uninit_vcpu:
10489 kvm_vcpu_uninit(&vmx->vcpu);
10490free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010491 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010492 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010493 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010494}
10495
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010496#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10497
Wanpeng Lib31c1142018-03-12 04:53:04 -070010498static int vmx_vm_init(struct kvm *kvm)
10499{
10500 if (!ple_gap)
10501 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010502
10503 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10504 if (nosmt) {
10505 pr_err(L1TF_MSG);
10506 return -EOPNOTSUPP;
10507 }
10508 pr_warn(L1TF_MSG);
10509 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010510 return 0;
10511}
10512
Yang, Sheng002c7f72007-07-31 14:23:01 +030010513static void __init vmx_check_processor_compat(void *rtn)
10514{
10515 struct vmcs_config vmcs_conf;
10516
10517 *(int *)rtn = 0;
10518 if (setup_vmcs_config(&vmcs_conf) < 0)
10519 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010520 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010521 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10522 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10523 smp_processor_id());
10524 *(int *)rtn = -EIO;
10525 }
10526}
10527
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010528static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010529{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010530 u8 cache;
10531 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010532
Sheng Yang522c68c2009-04-27 20:35:43 +080010533 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010534 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010535 * 2. EPT with VT-d:
10536 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010537 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010538 * b. VT-d with snooping control feature: snooping control feature of
10539 * VT-d engine can guarantee the cache correctness. Just set it
10540 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010541 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010542 * consistent with host MTRR
10543 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010544 if (is_mmio) {
10545 cache = MTRR_TYPE_UNCACHABLE;
10546 goto exit;
10547 }
10548
10549 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010550 ipat = VMX_EPT_IPAT_BIT;
10551 cache = MTRR_TYPE_WRBACK;
10552 goto exit;
10553 }
10554
10555 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10556 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010557 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010558 cache = MTRR_TYPE_WRBACK;
10559 else
10560 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010561 goto exit;
10562 }
10563
Xiao Guangrongff536042015-06-15 16:55:22 +080010564 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010565
10566exit:
10567 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010568}
10569
Sheng Yang17cc3932010-01-05 19:02:27 +080010570static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010571{
Sheng Yang878403b2010-01-05 19:02:29 +080010572 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10573 return PT_DIRECTORY_LEVEL;
10574 else
10575 /* For shadow and EPT supported 1GB page */
10576 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010577}
10578
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010579static void vmcs_set_secondary_exec_control(u32 new_ctl)
10580{
10581 /*
10582 * These bits in the secondary execution controls field
10583 * are dynamic, the others are mostly based on the hypervisor
10584 * architecture and the guest's CPUID. Do not touch the
10585 * dynamic bits.
10586 */
10587 u32 mask =
10588 SECONDARY_EXEC_SHADOW_VMCS |
10589 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010590 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10591 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010592
10593 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10594
10595 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10596 (new_ctl & ~mask) | (cur_ctl & mask));
10597}
10598
David Matlack8322ebb2016-11-29 18:14:09 -080010599/*
10600 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10601 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10602 */
10603static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10604{
10605 struct vcpu_vmx *vmx = to_vmx(vcpu);
10606 struct kvm_cpuid_entry2 *entry;
10607
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010608 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10609 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010610
10611#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10612 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010613 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010614} while (0)
10615
10616 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10617 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10618 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10619 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10620 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10621 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10622 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10623 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10624 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10625 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10626 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10627 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10628 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10629 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10630 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10631
10632 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10633 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10634 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10635 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10636 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010637 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010638
10639#undef cr4_fixed1_update
10640}
10641
Sheng Yang0e851882009-12-18 16:48:46 +080010642static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10643{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010644 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010645
Paolo Bonzini80154d72017-08-24 13:55:35 +020010646 if (cpu_has_secondary_exec_ctrls()) {
10647 vmx_compute_secondary_exec_control(vmx);
10648 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010649 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010650
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010651 if (nested_vmx_allowed(vcpu))
10652 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10653 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10654 else
10655 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10656 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010657
10658 if (nested_vmx_allowed(vcpu))
10659 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010660}
10661
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010662static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10663{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010664 if (func == 1 && nested)
10665 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010666}
10667
Yang Zhang25d92082013-08-06 12:00:32 +030010668static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10669 struct x86_exception *fault)
10670{
Jan Kiszka533558b2014-01-04 18:47:20 +010010671 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010672 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010673 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010674 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010675
Bandan Dasc5f983f2017-05-05 15:25:14 -040010676 if (vmx->nested.pml_full) {
10677 exit_reason = EXIT_REASON_PML_FULL;
10678 vmx->nested.pml_full = false;
10679 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10680 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010681 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010682 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010683 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010684
10685 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010686 vmcs12->guest_physical_address = fault->address;
10687}
10688
Peter Feiner995f00a2017-06-30 17:26:32 -070010689static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10690{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010691 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010692}
10693
Nadav Har'El155a97a2013-08-05 11:07:16 +030010694/* Callbacks for nested_ept_init_mmu_context: */
10695
10696static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10697{
10698 /* return the page table to be shadowed - in our case, EPT12 */
10699 return get_vmcs12(vcpu)->ept_pointer;
10700}
10701
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010702static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010703{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010704 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010705 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010706 return 1;
10707
10708 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010709 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010710 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010711 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010712 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010713 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10714 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10715 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10716
10717 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010718 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010719}
10720
10721static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10722{
10723 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10724}
10725
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010726static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10727 u16 error_code)
10728{
10729 bool inequality, bit;
10730
10731 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10732 inequality =
10733 (error_code & vmcs12->page_fault_error_code_mask) !=
10734 vmcs12->page_fault_error_code_match;
10735 return inequality ^ bit;
10736}
10737
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010738static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10739 struct x86_exception *fault)
10740{
10741 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10742
10743 WARN_ON(!is_guest_mode(vcpu));
10744
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010745 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10746 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010747 vmcs12->vm_exit_intr_error_code = fault->error_code;
10748 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10749 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10750 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10751 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010752 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010753 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010754 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010755}
10756
Paolo Bonzinic9923842017-12-13 14:16:30 +010010757static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10758 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010759
10760static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010761 struct vmcs12 *vmcs12)
10762{
10763 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010764 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010765 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010766
10767 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010768 /*
10769 * Translate L1 physical address to host physical
10770 * address for vmcs02. Keep the page pinned, so this
10771 * physical address remains valid. We keep a reference
10772 * to it so we can release it later.
10773 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010774 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010775 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010776 vmx->nested.apic_access_page = NULL;
10777 }
10778 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010779 /*
10780 * If translation failed, no matter: This feature asks
10781 * to exit when accessing the given address, and if it
10782 * can never be accessed, this feature won't do
10783 * anything anyway.
10784 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010785 if (!is_error_page(page)) {
10786 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010787 hpa = page_to_phys(vmx->nested.apic_access_page);
10788 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10789 } else {
10790 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10791 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10792 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010793 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010794
10795 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010796 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010797 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010798 vmx->nested.virtual_apic_page = NULL;
10799 }
10800 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010801
10802 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010803 * If translation failed, VM entry will fail because
10804 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10805 * Failing the vm entry is _not_ what the processor
10806 * does but it's basically the only possibility we
10807 * have. We could still enter the guest if CR8 load
10808 * exits are enabled, CR8 store exits are enabled, and
10809 * virtualize APIC access is disabled; in this case
10810 * the processor would never use the TPR shadow and we
10811 * could simply clear the bit from the execution
10812 * control. But such a configuration is useless, so
10813 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010814 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010815 if (!is_error_page(page)) {
10816 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010817 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10818 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10819 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010820 }
10821
Wincy Van705699a2015-02-03 23:58:17 +080010822 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010823 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10824 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010825 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010826 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010827 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010828 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10829 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010830 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010831 vmx->nested.pi_desc_page = page;
10832 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010833 vmx->nested.pi_desc =
10834 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10835 (unsigned long)(vmcs12->posted_intr_desc_addr &
10836 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010837 vmcs_write64(POSTED_INTR_DESC_ADDR,
10838 page_to_phys(vmx->nested.pi_desc_page) +
10839 (unsigned long)(vmcs12->posted_intr_desc_addr &
10840 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010841 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010842 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010843 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10844 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010845 else
10846 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10847 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010848}
10849
Jan Kiszkaf4124502014-03-07 20:03:13 +010010850static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10851{
10852 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10853 struct vcpu_vmx *vmx = to_vmx(vcpu);
10854
10855 if (vcpu->arch.virtual_tsc_khz == 0)
10856 return;
10857
10858 /* Make sure short timeouts reliably trigger an immediate vmexit.
10859 * hrtimer_start does not guarantee this. */
10860 if (preemption_timeout <= 1) {
10861 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10862 return;
10863 }
10864
10865 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10866 preemption_timeout *= 1000000;
10867 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10868 hrtimer_start(&vmx->nested.preemption_timer,
10869 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10870}
10871
Jim Mattson56a20512017-07-06 16:33:06 -070010872static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10873 struct vmcs12 *vmcs12)
10874{
10875 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10876 return 0;
10877
10878 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10879 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10880 return -EINVAL;
10881
10882 return 0;
10883}
10884
Wincy Van3af18d92015-02-03 23:49:31 +080010885static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10886 struct vmcs12 *vmcs12)
10887{
Wincy Van3af18d92015-02-03 23:49:31 +080010888 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10889 return 0;
10890
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010891 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010892 return -EINVAL;
10893
10894 return 0;
10895}
10896
Jim Mattson712b12d2017-08-24 13:24:47 -070010897static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10898 struct vmcs12 *vmcs12)
10899{
10900 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10901 return 0;
10902
10903 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10904 return -EINVAL;
10905
10906 return 0;
10907}
10908
Wincy Van3af18d92015-02-03 23:49:31 +080010909/*
10910 * Merge L0's and L1's MSR bitmap, return false to indicate that
10911 * we do not use the hardware.
10912 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010913static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10914 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010915{
Wincy Van82f0dd42015-02-03 23:57:18 +080010916 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010917 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010918 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010919 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010920 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010921 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010922 *
10923 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10924 * ensures that we do not accidentally generate an L02 MSR bitmap
10925 * from the L12 MSR bitmap that is too permissive.
10926 * 2. That L1 or L2s have actually used the MSR. This avoids
10927 * unnecessarily merging of the bitmap if the MSR is unused. This
10928 * works properly because we only update the L01 MSR bitmap lazily.
10929 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10930 * updated to reflect this when L1 (or its L2s) actually write to
10931 * the MSR.
10932 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010933 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10934 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010935
Paolo Bonzinic9923842017-12-13 14:16:30 +010010936 /* Nothing to do if the MSR bitmap is not in use. */
10937 if (!cpu_has_vmx_msr_bitmap() ||
10938 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10939 return false;
10940
Ashok Raj15d45072018-02-01 22:59:43 +010010941 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010942 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010943 return false;
10944
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010945 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10946 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010947 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010948
Radim Krčmářd048c092016-08-08 20:16:22 +020010949 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010950 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10951 /*
10952 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10953 * just lets the processor take the value from the virtual-APIC page;
10954 * take those 256 bits directly from the L1 bitmap.
10955 */
10956 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10957 unsigned word = msr / BITS_PER_LONG;
10958 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10959 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010960 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010961 } else {
10962 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10963 unsigned word = msr / BITS_PER_LONG;
10964 msr_bitmap_l0[word] = ~0;
10965 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10966 }
10967 }
10968
10969 nested_vmx_disable_intercept_for_msr(
10970 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010971 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010972 MSR_TYPE_W);
10973
10974 if (nested_cpu_has_vid(vmcs12)) {
10975 nested_vmx_disable_intercept_for_msr(
10976 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010977 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010978 MSR_TYPE_W);
10979 nested_vmx_disable_intercept_for_msr(
10980 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010981 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010982 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010983 }
Ashok Raj15d45072018-02-01 22:59:43 +010010984
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010985 if (spec_ctrl)
10986 nested_vmx_disable_intercept_for_msr(
10987 msr_bitmap_l1, msr_bitmap_l0,
10988 MSR_IA32_SPEC_CTRL,
10989 MSR_TYPE_R | MSR_TYPE_W);
10990
Ashok Raj15d45072018-02-01 22:59:43 +010010991 if (pred_cmd)
10992 nested_vmx_disable_intercept_for_msr(
10993 msr_bitmap_l1, msr_bitmap_l0,
10994 MSR_IA32_PRED_CMD,
10995 MSR_TYPE_W);
10996
Wincy Vanf2b93282015-02-03 23:56:03 +080010997 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010998 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010999
11000 return true;
11001}
11002
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011003static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11004 struct vmcs12 *vmcs12)
11005{
11006 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11007 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11008 return -EINVAL;
11009 else
11010 return 0;
11011}
11012
Wincy Vanf2b93282015-02-03 23:56:03 +080011013static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11014 struct vmcs12 *vmcs12)
11015{
Wincy Van82f0dd42015-02-03 23:57:18 +080011016 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011017 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011018 !nested_cpu_has_vid(vmcs12) &&
11019 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011020 return 0;
11021
11022 /*
11023 * If virtualize x2apic mode is enabled,
11024 * virtualize apic access must be disabled.
11025 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011026 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11027 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011028 return -EINVAL;
11029
Wincy Van608406e2015-02-03 23:57:51 +080011030 /*
11031 * If virtual interrupt delivery is enabled,
11032 * we must exit on external interrupts.
11033 */
11034 if (nested_cpu_has_vid(vmcs12) &&
11035 !nested_exit_on_intr(vcpu))
11036 return -EINVAL;
11037
Wincy Van705699a2015-02-03 23:58:17 +080011038 /*
11039 * bits 15:8 should be zero in posted_intr_nv,
11040 * the descriptor address has been already checked
11041 * in nested_get_vmcs12_pages.
11042 */
11043 if (nested_cpu_has_posted_intr(vmcs12) &&
11044 (!nested_cpu_has_vid(vmcs12) ||
11045 !nested_exit_intr_ack_set(vcpu) ||
11046 vmcs12->posted_intr_nv & 0xff00))
11047 return -EINVAL;
11048
Wincy Vanf2b93282015-02-03 23:56:03 +080011049 /* tpr shadow is needed by all apicv features. */
11050 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11051 return -EINVAL;
11052
11053 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011054}
11055
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011056static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11057 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011058 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011059{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011060 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011061 u64 count, addr;
11062
11063 if (vmcs12_read_any(vcpu, count_field, &count) ||
11064 vmcs12_read_any(vcpu, addr_field, &addr)) {
11065 WARN_ON(1);
11066 return -EINVAL;
11067 }
11068 if (count == 0)
11069 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011070 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011071 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11072 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011073 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011074 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11075 addr_field, maxphyaddr, count, addr);
11076 return -EINVAL;
11077 }
11078 return 0;
11079}
11080
11081static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11082 struct vmcs12 *vmcs12)
11083{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011084 if (vmcs12->vm_exit_msr_load_count == 0 &&
11085 vmcs12->vm_exit_msr_store_count == 0 &&
11086 vmcs12->vm_entry_msr_load_count == 0)
11087 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011088 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011089 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011090 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011091 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011092 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011093 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011094 return -EINVAL;
11095 return 0;
11096}
11097
Bandan Dasc5f983f2017-05-05 15:25:14 -040011098static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11099 struct vmcs12 *vmcs12)
11100{
11101 u64 address = vmcs12->pml_address;
11102 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11103
11104 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11105 if (!nested_cpu_has_ept(vmcs12) ||
11106 !IS_ALIGNED(address, 4096) ||
11107 address >> maxphyaddr)
11108 return -EINVAL;
11109 }
11110
11111 return 0;
11112}
11113
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011114static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11115 struct vmx_msr_entry *e)
11116{
11117 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011118 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011119 return -EINVAL;
11120 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11121 e->index == MSR_IA32_UCODE_REV)
11122 return -EINVAL;
11123 if (e->reserved != 0)
11124 return -EINVAL;
11125 return 0;
11126}
11127
11128static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11129 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011130{
11131 if (e->index == MSR_FS_BASE ||
11132 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011133 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11134 nested_vmx_msr_check_common(vcpu, e))
11135 return -EINVAL;
11136 return 0;
11137}
11138
11139static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11140 struct vmx_msr_entry *e)
11141{
11142 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11143 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011144 return -EINVAL;
11145 return 0;
11146}
11147
11148/*
11149 * Load guest's/host's msr at nested entry/exit.
11150 * return 0 for success, entry index for failure.
11151 */
11152static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11153{
11154 u32 i;
11155 struct vmx_msr_entry e;
11156 struct msr_data msr;
11157
11158 msr.host_initiated = false;
11159 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011160 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11161 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011162 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011163 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11164 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011165 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011166 }
11167 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011168 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011169 "%s check failed (%u, 0x%x, 0x%x)\n",
11170 __func__, i, e.index, e.reserved);
11171 goto fail;
11172 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011173 msr.index = e.index;
11174 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011175 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011176 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011177 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11178 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011179 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011180 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011181 }
11182 return 0;
11183fail:
11184 return i + 1;
11185}
11186
11187static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11188{
11189 u32 i;
11190 struct vmx_msr_entry e;
11191
11192 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011193 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011194 if (kvm_vcpu_read_guest(vcpu,
11195 gpa + i * sizeof(e),
11196 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011197 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011198 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11199 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011200 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011201 }
11202 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011203 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011204 "%s check failed (%u, 0x%x, 0x%x)\n",
11205 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011206 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011207 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011208 msr_info.host_initiated = false;
11209 msr_info.index = e.index;
11210 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011211 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011212 "%s cannot read MSR (%u, 0x%x)\n",
11213 __func__, i, e.index);
11214 return -EINVAL;
11215 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011216 if (kvm_vcpu_write_guest(vcpu,
11217 gpa + i * sizeof(e) +
11218 offsetof(struct vmx_msr_entry, value),
11219 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011220 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011221 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011222 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011223 return -EINVAL;
11224 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011225 }
11226 return 0;
11227}
11228
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011229static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11230{
11231 unsigned long invalid_mask;
11232
11233 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11234 return (val & invalid_mask) == 0;
11235}
11236
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011237/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011238 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11239 * emulating VM entry into a guest with EPT enabled.
11240 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11241 * is assigned to entry_failure_code on failure.
11242 */
11243static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011244 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011245{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011246 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011247 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011248 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11249 return 1;
11250 }
11251
11252 /*
11253 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11254 * must not be dereferenced.
11255 */
11256 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11257 !nested_ept) {
11258 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11259 *entry_failure_code = ENTRY_FAIL_PDPTE;
11260 return 1;
11261 }
11262 }
11263
11264 vcpu->arch.cr3 = cr3;
11265 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11266 }
11267
11268 kvm_mmu_reset_context(vcpu);
11269 return 0;
11270}
11271
Jim Mattson6514dc32018-04-26 16:09:12 -070011272static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011273{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011274 struct vcpu_vmx *vmx = to_vmx(vcpu);
11275
11276 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11277 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11278 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11279 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11280 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11281 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11282 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11283 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11284 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11285 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11286 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11287 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11288 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11289 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11290 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11291 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11292 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11293 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11294 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11295 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11296 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11297 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11298 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11299 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11300 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11301 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11302 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11303 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11304 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11305 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11306 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011307
11308 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11309 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11310 vmcs12->guest_pending_dbg_exceptions);
11311 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11312 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11313
11314 if (nested_cpu_has_xsaves(vmcs12))
11315 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11316 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11317
11318 if (cpu_has_vmx_posted_intr())
11319 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11320
11321 /*
11322 * Whether page-faults are trapped is determined by a combination of
11323 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11324 * If enable_ept, L0 doesn't care about page faults and we should
11325 * set all of these to L1's desires. However, if !enable_ept, L0 does
11326 * care about (at least some) page faults, and because it is not easy
11327 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11328 * to exit on each and every L2 page fault. This is done by setting
11329 * MASK=MATCH=0 and (see below) EB.PF=1.
11330 * Note that below we don't need special code to set EB.PF beyond the
11331 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11332 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11333 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11334 */
11335 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11336 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11337 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11338 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11339
11340 /* All VMFUNCs are currently emulated through L0 vmexits. */
11341 if (cpu_has_vmx_vmfunc())
11342 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11343
11344 if (cpu_has_vmx_apicv()) {
11345 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11346 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11347 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11348 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11349 }
11350
11351 /*
11352 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11353 * Some constant fields are set here by vmx_set_constant_host_state().
11354 * Other fields are different per CPU, and will be set later when
11355 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11356 */
11357 vmx_set_constant_host_state(vmx);
11358
11359 /*
11360 * Set the MSR load/store lists to match L0's settings.
11361 */
11362 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011363 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11364 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11365 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11366 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011367
11368 set_cr4_guest_host_mask(vmx);
11369
11370 if (vmx_mpx_supported())
11371 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11372
11373 if (enable_vpid) {
11374 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11375 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11376 else
11377 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11378 }
11379
11380 /*
11381 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11382 */
11383 if (enable_ept) {
11384 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11385 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11386 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11387 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11388 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011389
11390 if (cpu_has_vmx_msr_bitmap())
11391 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011392}
11393
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011394/*
11395 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11396 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011397 * 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 +030011398 * guest in a way that will both be appropriate to L1's requests, and our
11399 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11400 * function also has additional necessary side-effects, like setting various
11401 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011402 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11403 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011404 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011405static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011406 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011407{
11408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011409 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011410
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011411 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011412 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011413 vmx->nested.dirty_vmcs12 = false;
11414 }
11415
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011416 /*
11417 * First, the fields that are shadowed. This must be kept in sync
11418 * with vmx_shadow_fields.h.
11419 */
11420
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011421 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011422 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011423 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011424 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11425 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011426
11427 /*
11428 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11429 * HOST_FS_BASE, HOST_GS_BASE.
11430 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011431
Jim Mattson6514dc32018-04-26 16:09:12 -070011432 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011433 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011434 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11435 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11436 } else {
11437 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11438 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11439 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011440 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011441 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11442 vmcs12->vm_entry_intr_info_field);
11443 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11444 vmcs12->vm_entry_exception_error_code);
11445 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11446 vmcs12->vm_entry_instruction_len);
11447 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11448 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011449 vmx->loaded_vmcs->nmi_known_unmasked =
11450 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011451 } else {
11452 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11453 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011454 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011455
Jan Kiszkaf4124502014-03-07 20:03:13 +010011456 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011457
Paolo Bonzini93140062016-07-06 13:23:51 +020011458 /* Preemption timer setting is only taken from vmcs01. */
11459 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11460 exec_control |= vmcs_config.pin_based_exec_ctrl;
11461 if (vmx->hv_deadline_tsc == -1)
11462 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11463
11464 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011465 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011466 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11467 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011468 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011469 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011470 }
Wincy Van705699a2015-02-03 23:58:17 +080011471
Jan Kiszkaf4124502014-03-07 20:03:13 +010011472 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011473
Jan Kiszkaf4124502014-03-07 20:03:13 +010011474 vmx->nested.preemption_timer_expired = false;
11475 if (nested_cpu_has_preemption_timer(vmcs12))
11476 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011477
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011478 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011479 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011480
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011481 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011482 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011483 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011484 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011485 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011486 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011487 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11488 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011489 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011490 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11491 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11492 ~SECONDARY_EXEC_ENABLE_PML;
11493 exec_control |= vmcs12_exec_ctrl;
11494 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011495
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011496 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011497 vmcs_write16(GUEST_INTR_STATUS,
11498 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011499
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011500 /*
11501 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11502 * nested_get_vmcs12_pages will either fix it up or
11503 * remove the VM execution control.
11504 */
11505 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11506 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11507
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011508 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11509 }
11510
Jim Mattson83bafef2016-10-04 10:48:38 -070011511 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011512 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11513 * entry, but only if the current (host) sp changed from the value
11514 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11515 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11516 * here we just force the write to happen on entry.
11517 */
11518 vmx->host_rsp = 0;
11519
11520 exec_control = vmx_exec_control(vmx); /* L0's desires */
11521 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11522 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11523 exec_control &= ~CPU_BASED_TPR_SHADOW;
11524 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011525
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011526 /*
11527 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11528 * nested_get_vmcs12_pages can't fix it up, the illegal value
11529 * will result in a VM entry failure.
11530 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011531 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011532 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011533 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011534 } else {
11535#ifdef CONFIG_X86_64
11536 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11537 CPU_BASED_CR8_STORE_EXITING;
11538#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011539 }
11540
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011541 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011542 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11543 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011544 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011545 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11546 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11547
11548 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11549
11550 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11551 * bitwise-or of what L1 wants to trap for L2, and what we want to
11552 * trap. Note that CR0.TS also needs updating - we do this later.
11553 */
11554 update_exception_bitmap(vcpu);
11555 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11556 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11557
Nadav Har'El8049d652013-08-05 11:07:06 +030011558 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11559 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11560 * bits are further modified by vmx_set_efer() below.
11561 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011562 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011563
11564 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11565 * emulated by vmx_set_efer(), below.
11566 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011567 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011568 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11569 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011570 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11571
Jim Mattson6514dc32018-04-26 16:09:12 -070011572 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011573 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011574 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011575 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011576 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011577 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011578 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011579
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011580 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11581
Peter Feinerc95ba922016-08-17 09:36:47 -070011582 if (kvm_has_tsc_control)
11583 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011584
11585 if (enable_vpid) {
11586 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011587 * There is no direct mapping between vpid02 and vpid12, the
11588 * vpid02 is per-vCPU for L0 and reused while the value of
11589 * vpid12 is changed w/ one invvpid during nested vmentry.
11590 * The vpid12 is allocated by L1 for L2, so it will not
11591 * influence global bitmap(for vpid01 and vpid02 allocation)
11592 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011593 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011594 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011595 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11596 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011597 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011598 }
11599 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011600 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011601 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011602 }
11603
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011604 if (enable_pml) {
11605 /*
11606 * Conceptually we want to copy the PML address and index from
11607 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11608 * since we always flush the log on each vmexit, this happens
11609 * to be equivalent to simply resetting the fields in vmcs02.
11610 */
11611 ASSERT(vmx->pml_pg);
11612 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11613 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11614 }
11615
Nadav Har'El155a97a2013-08-05 11:07:16 +030011616 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011617 if (nested_ept_init_mmu_context(vcpu)) {
11618 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11619 return 1;
11620 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011621 } else if (nested_cpu_has2(vmcs12,
11622 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011623 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011624 }
11625
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011626 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011627 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11628 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011629 * The CR0_READ_SHADOW is what L2 should have expected to read given
11630 * the specifications by L1; It's not enough to take
11631 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11632 * have more bits than L1 expected.
11633 */
11634 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11635 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11636
11637 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11638 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11639
Jim Mattson6514dc32018-04-26 16:09:12 -070011640 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011641 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011642 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11643 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11644 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11645 else
11646 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11647 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11648 vmx_set_efer(vcpu, vcpu->arch.efer);
11649
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011650 /*
11651 * Guest state is invalid and unrestricted guest is disabled,
11652 * which means L1 attempted VMEntry to L2 with invalid state.
11653 * Fail the VMEntry.
11654 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011655 if (vmx->emulation_required) {
11656 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011657 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011658 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011659
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011660 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011661 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011662 entry_failure_code))
11663 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011664
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011665 if (!enable_ept)
11666 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11667
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011668 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11669 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011670 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011671}
11672
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011673static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11674{
11675 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11676 nested_cpu_has_virtual_nmis(vmcs12))
11677 return -EINVAL;
11678
11679 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11680 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11681 return -EINVAL;
11682
11683 return 0;
11684}
11685
Jim Mattsonca0bde22016-11-30 12:03:46 -080011686static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11687{
11688 struct vcpu_vmx *vmx = to_vmx(vcpu);
11689
11690 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11691 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11692 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11693
Jim Mattson56a20512017-07-06 16:33:06 -070011694 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11695 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11696
Jim Mattsonca0bde22016-11-30 12:03:46 -080011697 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11698 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11699
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011700 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11701 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11702
Jim Mattson712b12d2017-08-24 13:24:47 -070011703 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11704 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11705
Jim Mattsonca0bde22016-11-30 12:03:46 -080011706 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11707 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11708
11709 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11710 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11711
Bandan Dasc5f983f2017-05-05 15:25:14 -040011712 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11713 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11714
Jim Mattsonca0bde22016-11-30 12:03:46 -080011715 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011716 vmx->nested.msrs.procbased_ctls_low,
11717 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011718 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11719 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011720 vmx->nested.msrs.secondary_ctls_low,
11721 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011722 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011723 vmx->nested.msrs.pinbased_ctls_low,
11724 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011725 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011726 vmx->nested.msrs.exit_ctls_low,
11727 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011728 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011729 vmx->nested.msrs.entry_ctls_low,
11730 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011731 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11732
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011733 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011734 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11735
Bandan Das41ab9372017-08-03 15:54:43 -040011736 if (nested_cpu_has_vmfunc(vmcs12)) {
11737 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011738 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011739 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11740
11741 if (nested_cpu_has_eptp_switching(vmcs12)) {
11742 if (!nested_cpu_has_ept(vmcs12) ||
11743 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11744 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11745 }
11746 }
Bandan Das27c42a12017-08-03 15:54:42 -040011747
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011748 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11749 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11750
Jim Mattsonca0bde22016-11-30 12:03:46 -080011751 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11752 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11753 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11754 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11755
11756 return 0;
11757}
11758
11759static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11760 u32 *exit_qual)
11761{
11762 bool ia32e;
11763
11764 *exit_qual = ENTRY_FAIL_DEFAULT;
11765
11766 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11767 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11768 return 1;
11769
11770 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11771 vmcs12->vmcs_link_pointer != -1ull) {
11772 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11773 return 1;
11774 }
11775
11776 /*
11777 * If the load IA32_EFER VM-entry control is 1, the following checks
11778 * are performed on the field for the IA32_EFER MSR:
11779 * - Bits reserved in the IA32_EFER MSR must be 0.
11780 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11781 * the IA-32e mode guest VM-exit control. It must also be identical
11782 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11783 * CR0.PG) is 1.
11784 */
11785 if (to_vmx(vcpu)->nested.nested_run_pending &&
11786 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11787 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11788 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11789 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11790 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11791 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11792 return 1;
11793 }
11794
11795 /*
11796 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11797 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11798 * the values of the LMA and LME bits in the field must each be that of
11799 * the host address-space size VM-exit control.
11800 */
11801 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11802 ia32e = (vmcs12->vm_exit_controls &
11803 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11804 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11805 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11806 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11807 return 1;
11808 }
11809
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011810 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11811 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11812 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11813 return 1;
11814
Jim Mattsonca0bde22016-11-30 12:03:46 -080011815 return 0;
11816}
11817
Jim Mattson6514dc32018-04-26 16:09:12 -070011818static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011819{
11820 struct vcpu_vmx *vmx = to_vmx(vcpu);
11821 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011822 u32 msr_entry_idx;
11823 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011824 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011825
Jim Mattson858e25c2016-11-30 12:03:47 -080011826 enter_guest_mode(vcpu);
11827
11828 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11829 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11830
Jim Mattsonde3a0022017-11-27 17:22:25 -060011831 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011832 vmx_segment_cache_clear(vmx);
11833
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011834 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11835 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11836
11837 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011838 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011839 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011840
11841 nested_get_vmcs12_pages(vcpu, vmcs12);
11842
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011843 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011844 msr_entry_idx = nested_vmx_load_msr(vcpu,
11845 vmcs12->vm_entry_msr_load_addr,
11846 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011847 if (msr_entry_idx)
11848 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011849
Jim Mattson858e25c2016-11-30 12:03:47 -080011850 /*
11851 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11852 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11853 * returned as far as L1 is concerned. It will only return (and set
11854 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11855 */
11856 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011857
11858fail:
11859 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11860 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11861 leave_guest_mode(vcpu);
11862 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11863 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11864 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011865}
11866
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011867/*
11868 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11869 * for running an L2 nested guest.
11870 */
11871static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11872{
11873 struct vmcs12 *vmcs12;
11874 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011875 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011876 u32 exit_qual;
11877 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011878
Kyle Hueyeb277562016-11-29 12:40:39 -080011879 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011880 return 1;
11881
Kyle Hueyeb277562016-11-29 12:40:39 -080011882 if (!nested_vmx_check_vmcs12(vcpu))
11883 goto out;
11884
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011885 vmcs12 = get_vmcs12(vcpu);
11886
Abel Gordon012f83c2013-04-18 14:39:25 +030011887 if (enable_shadow_vmcs)
11888 copy_shadow_to_vmcs12(vmx);
11889
Nadav Har'El7c177932011-05-25 23:12:04 +030011890 /*
11891 * The nested entry process starts with enforcing various prerequisites
11892 * on vmcs12 as required by the Intel SDM, and act appropriately when
11893 * they fail: As the SDM explains, some conditions should cause the
11894 * instruction to fail, while others will cause the instruction to seem
11895 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11896 * To speed up the normal (success) code path, we should avoid checking
11897 * for misconfigurations which will anyway be caught by the processor
11898 * when using the merged vmcs02.
11899 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011900 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11901 nested_vmx_failValid(vcpu,
11902 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11903 goto out;
11904 }
11905
Nadav Har'El7c177932011-05-25 23:12:04 +030011906 if (vmcs12->launch_state == launch) {
11907 nested_vmx_failValid(vcpu,
11908 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11909 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011910 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011911 }
11912
Jim Mattsonca0bde22016-11-30 12:03:46 -080011913 ret = check_vmentry_prereqs(vcpu, vmcs12);
11914 if (ret) {
11915 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011916 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011917 }
11918
Nadav Har'El7c177932011-05-25 23:12:04 +030011919 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011920 * After this point, the trap flag no longer triggers a singlestep trap
11921 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11922 * This is not 100% correct; for performance reasons, we delegate most
11923 * of the checks on host state to the processor. If those fail,
11924 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011925 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011926 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011927
Jim Mattsonca0bde22016-11-30 12:03:46 -080011928 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11929 if (ret) {
11930 nested_vmx_entry_failure(vcpu, vmcs12,
11931 EXIT_REASON_INVALID_STATE, exit_qual);
11932 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011933 }
11934
11935 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011936 * We're finally done with prerequisite checking, and can start with
11937 * the nested entry.
11938 */
11939
Jim Mattson6514dc32018-04-26 16:09:12 -070011940 vmx->nested.nested_run_pending = 1;
11941 ret = enter_vmx_non_root_mode(vcpu);
11942 if (ret) {
11943 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011944 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011945 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011946
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011947 /* Hide L1D cache contents from the nested guest. */
11948 vmx->vcpu.arch.l1tf_flush_l1d = true;
11949
Chao Gao135a06c2018-02-11 10:06:30 +080011950 /*
11951 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11952 * by event injection, halt vcpu.
11953 */
11954 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011955 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11956 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011957 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011958 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011959 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011960
11961out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011962 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011963}
11964
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011965/*
11966 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11967 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11968 * This function returns the new value we should put in vmcs12.guest_cr0.
11969 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11970 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11971 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11972 * didn't trap the bit, because if L1 did, so would L0).
11973 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11974 * been modified by L2, and L1 knows it. So just leave the old value of
11975 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11976 * isn't relevant, because if L0 traps this bit it can set it to anything.
11977 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11978 * changed these bits, and therefore they need to be updated, but L0
11979 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11980 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11981 */
11982static inline unsigned long
11983vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11984{
11985 return
11986 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11987 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11988 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11989 vcpu->arch.cr0_guest_owned_bits));
11990}
11991
11992static inline unsigned long
11993vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11994{
11995 return
11996 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11997 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11998 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11999 vcpu->arch.cr4_guest_owned_bits));
12000}
12001
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012002static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12003 struct vmcs12 *vmcs12)
12004{
12005 u32 idt_vectoring;
12006 unsigned int nr;
12007
Wanpeng Li664f8e22017-08-24 03:35:09 -070012008 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012009 nr = vcpu->arch.exception.nr;
12010 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12011
12012 if (kvm_exception_is_soft(nr)) {
12013 vmcs12->vm_exit_instruction_len =
12014 vcpu->arch.event_exit_inst_len;
12015 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12016 } else
12017 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12018
12019 if (vcpu->arch.exception.has_error_code) {
12020 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12021 vmcs12->idt_vectoring_error_code =
12022 vcpu->arch.exception.error_code;
12023 }
12024
12025 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012026 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012027 vmcs12->idt_vectoring_info_field =
12028 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012029 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012030 nr = vcpu->arch.interrupt.nr;
12031 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12032
12033 if (vcpu->arch.interrupt.soft) {
12034 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12035 vmcs12->vm_entry_instruction_len =
12036 vcpu->arch.event_exit_inst_len;
12037 } else
12038 idt_vectoring |= INTR_TYPE_EXT_INTR;
12039
12040 vmcs12->idt_vectoring_info_field = idt_vectoring;
12041 }
12042}
12043
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012044static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12045{
12046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012047 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012048 bool block_nested_events =
12049 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012050
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012051 if (vcpu->arch.exception.pending &&
12052 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012053 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012054 return -EBUSY;
12055 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012056 return 0;
12057 }
12058
Jan Kiszkaf4124502014-03-07 20:03:13 +010012059 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12060 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012061 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012062 return -EBUSY;
12063 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12064 return 0;
12065 }
12066
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012067 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012068 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012069 return -EBUSY;
12070 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12071 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12072 INTR_INFO_VALID_MASK, 0);
12073 /*
12074 * The NMI-triggered VM exit counts as injection:
12075 * clear this one and block further NMIs.
12076 */
12077 vcpu->arch.nmi_pending = 0;
12078 vmx_set_nmi_mask(vcpu, true);
12079 return 0;
12080 }
12081
12082 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12083 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012084 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012085 return -EBUSY;
12086 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012087 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012088 }
12089
David Hildenbrand6342c502017-01-25 11:58:58 +010012090 vmx_complete_nested_posted_interrupt(vcpu);
12091 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012092}
12093
Jan Kiszkaf4124502014-03-07 20:03:13 +010012094static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12095{
12096 ktime_t remaining =
12097 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12098 u64 value;
12099
12100 if (ktime_to_ns(remaining) <= 0)
12101 return 0;
12102
12103 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12104 do_div(value, 1000000);
12105 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12106}
12107
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012108/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012109 * Update the guest state fields of vmcs12 to reflect changes that
12110 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12111 * VM-entry controls is also updated, since this is really a guest
12112 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012113 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012114static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012115{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012116 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12117 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12118
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012119 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12120 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12121 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12122
12123 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12124 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12125 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12126 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12127 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12128 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12129 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12130 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12131 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12132 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12133 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12134 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12135 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12136 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12137 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12138 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12139 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12140 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12141 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12142 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12143 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12144 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12145 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12146 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12147 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12148 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12149 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12150 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12151 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12152 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12153 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12154 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12155 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12156 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12157 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12158 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12159
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012160 vmcs12->guest_interruptibility_info =
12161 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12162 vmcs12->guest_pending_dbg_exceptions =
12163 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012164 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12165 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12166 else
12167 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012168
Jan Kiszkaf4124502014-03-07 20:03:13 +010012169 if (nested_cpu_has_preemption_timer(vmcs12)) {
12170 if (vmcs12->vm_exit_controls &
12171 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12172 vmcs12->vmx_preemption_timer_value =
12173 vmx_get_preemption_timer_value(vcpu);
12174 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12175 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012176
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012177 /*
12178 * In some cases (usually, nested EPT), L2 is allowed to change its
12179 * own CR3 without exiting. If it has changed it, we must keep it.
12180 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12181 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12182 *
12183 * Additionally, restore L2's PDPTR to vmcs12.
12184 */
12185 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012186 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012187 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12188 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12189 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12190 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12191 }
12192
Jim Mattsond281e132017-06-01 12:44:46 -070012193 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012194
Wincy Van608406e2015-02-03 23:57:51 +080012195 if (nested_cpu_has_vid(vmcs12))
12196 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12197
Jan Kiszkac18911a2013-03-13 16:06:41 +010012198 vmcs12->vm_entry_controls =
12199 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012200 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012201
Jan Kiszka2996fca2014-06-16 13:59:43 +020012202 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12203 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12204 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12205 }
12206
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012207 /* TODO: These cannot have changed unless we have MSR bitmaps and
12208 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012209 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012210 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012211 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12212 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012213 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12214 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12215 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012216 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012217 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012218}
12219
12220/*
12221 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12222 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12223 * and this function updates it to reflect the changes to the guest state while
12224 * L2 was running (and perhaps made some exits which were handled directly by L0
12225 * without going back to L1), and to reflect the exit reason.
12226 * Note that we do not have to copy here all VMCS fields, just those that
12227 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12228 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12229 * which already writes to vmcs12 directly.
12230 */
12231static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12232 u32 exit_reason, u32 exit_intr_info,
12233 unsigned long exit_qualification)
12234{
12235 /* update guest state fields: */
12236 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012237
12238 /* update exit information fields: */
12239
Jan Kiszka533558b2014-01-04 18:47:20 +010012240 vmcs12->vm_exit_reason = exit_reason;
12241 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012242 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012243
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012244 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012245 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12246 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12247
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012248 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012249 vmcs12->launch_state = 1;
12250
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012251 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12252 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012253 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012254
12255 /*
12256 * Transfer the event that L0 or L1 may wanted to inject into
12257 * L2 to IDT_VECTORING_INFO_FIELD.
12258 */
12259 vmcs12_save_pending_event(vcpu, vmcs12);
12260 }
12261
12262 /*
12263 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12264 * preserved above and would only end up incorrectly in L1.
12265 */
12266 vcpu->arch.nmi_injected = false;
12267 kvm_clear_exception_queue(vcpu);
12268 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012269}
12270
Wanpeng Li5af41572017-11-05 16:54:49 -080012271static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12272 struct vmcs12 *vmcs12)
12273{
12274 u32 entry_failure_code;
12275
12276 nested_ept_uninit_mmu_context(vcpu);
12277
12278 /*
12279 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12280 * couldn't have changed.
12281 */
12282 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12283 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12284
12285 if (!enable_ept)
12286 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12287}
12288
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012289/*
12290 * A part of what we need to when the nested L2 guest exits and we want to
12291 * run its L1 parent, is to reset L1's guest state to the host state specified
12292 * in vmcs12.
12293 * This function is to be called not only on normal nested exit, but also on
12294 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12295 * Failures During or After Loading Guest State").
12296 * This function should be called when the active VMCS is L1's (vmcs01).
12297 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012298static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12299 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012300{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012301 struct kvm_segment seg;
12302
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012303 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12304 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012305 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012306 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12307 else
12308 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12309 vmx_set_efer(vcpu, vcpu->arch.efer);
12310
12311 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12312 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012313 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012314 /*
12315 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012316 * actually changed, because vmx_set_cr0 refers to efer set above.
12317 *
12318 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12319 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012320 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012321 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012322 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012323
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012324 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012325 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012326 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012327
Wanpeng Li5af41572017-11-05 16:54:49 -080012328 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012329
Liran Alon6f1e03b2018-05-22 17:16:14 +030012330 /*
12331 * If vmcs01 don't use VPID, CPU flushes TLB on every
12332 * VMEntry/VMExit. Thus, no need to flush TLB.
12333 *
12334 * If vmcs12 uses VPID, TLB entries populated by L2 are
12335 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12336 * with vmx->vpid. Thus, no need to flush TLB.
12337 *
12338 * Therefore, flush TLB only in case vmcs01 uses VPID and
12339 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12340 * are both tagged with vmx->vpid.
12341 */
12342 if (enable_vpid &&
12343 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012344 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012345 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012346
12347 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12348 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12349 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12350 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12351 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012352 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12353 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012354
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012355 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12356 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12357 vmcs_write64(GUEST_BNDCFGS, 0);
12358
Jan Kiszka44811c02013-08-04 17:17:27 +020012359 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012360 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012361 vcpu->arch.pat = vmcs12->host_ia32_pat;
12362 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012363 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12364 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12365 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012366
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012367 /* Set L1 segment info according to Intel SDM
12368 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12369 seg = (struct kvm_segment) {
12370 .base = 0,
12371 .limit = 0xFFFFFFFF,
12372 .selector = vmcs12->host_cs_selector,
12373 .type = 11,
12374 .present = 1,
12375 .s = 1,
12376 .g = 1
12377 };
12378 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12379 seg.l = 1;
12380 else
12381 seg.db = 1;
12382 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12383 seg = (struct kvm_segment) {
12384 .base = 0,
12385 .limit = 0xFFFFFFFF,
12386 .type = 3,
12387 .present = 1,
12388 .s = 1,
12389 .db = 1,
12390 .g = 1
12391 };
12392 seg.selector = vmcs12->host_ds_selector;
12393 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12394 seg.selector = vmcs12->host_es_selector;
12395 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12396 seg.selector = vmcs12->host_ss_selector;
12397 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12398 seg.selector = vmcs12->host_fs_selector;
12399 seg.base = vmcs12->host_fs_base;
12400 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12401 seg.selector = vmcs12->host_gs_selector;
12402 seg.base = vmcs12->host_gs_base;
12403 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12404 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012405 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012406 .limit = 0x67,
12407 .selector = vmcs12->host_tr_selector,
12408 .type = 11,
12409 .present = 1
12410 };
12411 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12412
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012413 kvm_set_dr(vcpu, 7, 0x400);
12414 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012415
Wincy Van3af18d92015-02-03 23:49:31 +080012416 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012417 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012418
Wincy Vanff651cb2014-12-11 08:52:58 +030012419 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12420 vmcs12->vm_exit_msr_load_count))
12421 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012422}
12423
12424/*
12425 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12426 * and modify vmcs12 to make it see what it would expect to see there if
12427 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12428 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012429static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12430 u32 exit_intr_info,
12431 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012432{
12433 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012434 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12435
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012436 /* trying to cancel vmlaunch/vmresume is a bug */
12437 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12438
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012439 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012440 * The only expected VM-instruction error is "VM entry with
12441 * invalid control field(s)." Anything else indicates a
12442 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012443 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012444 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12445 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12446
12447 leave_guest_mode(vcpu);
12448
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012449 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12450 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12451
Jim Mattson4f350c62017-09-14 16:31:44 -070012452 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012453 if (exit_reason == -1)
12454 sync_vmcs12(vcpu, vmcs12);
12455 else
12456 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12457 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012458
12459 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12460 vmcs12->vm_exit_msr_store_count))
12461 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012462 }
12463
Jim Mattson4f350c62017-09-14 16:31:44 -070012464 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012465 vm_entry_controls_reset_shadow(vmx);
12466 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012467 vmx_segment_cache_clear(vmx);
12468
Paolo Bonzini93140062016-07-06 13:23:51 +020012469 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012470 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12471 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012472 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012473 if (vmx->hv_deadline_tsc == -1)
12474 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12475 PIN_BASED_VMX_PREEMPTION_TIMER);
12476 else
12477 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12478 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012479 if (kvm_has_tsc_control)
12480 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012481
Jim Mattson8d860bb2018-05-09 16:56:05 -040012482 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12483 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12484 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012485 } else if (!nested_cpu_has_ept(vmcs12) &&
12486 nested_cpu_has2(vmcs12,
12487 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012488 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012489 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012490
12491 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12492 vmx->host_rsp = 0;
12493
12494 /* Unpin physical memory we referred to in vmcs02 */
12495 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012496 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012497 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012498 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012499 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012500 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012501 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012502 }
Wincy Van705699a2015-02-03 23:58:17 +080012503 if (vmx->nested.pi_desc_page) {
12504 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012505 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012506 vmx->nested.pi_desc_page = NULL;
12507 vmx->nested.pi_desc = NULL;
12508 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012509
12510 /*
Tang Chen38b99172014-09-24 15:57:54 +080012511 * We are now running in L2, mmu_notifier will force to reload the
12512 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12513 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012514 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012515
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012516 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012517 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012518
12519 /* in case we halted in L2 */
12520 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012521
12522 if (likely(!vmx->fail)) {
12523 /*
12524 * TODO: SDM says that with acknowledge interrupt on
12525 * exit, bit 31 of the VM-exit interrupt information
12526 * (valid interrupt) is always set to 1 on
12527 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12528 * need kvm_cpu_has_interrupt(). See the commit
12529 * message for details.
12530 */
12531 if (nested_exit_intr_ack_set(vcpu) &&
12532 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12533 kvm_cpu_has_interrupt(vcpu)) {
12534 int irq = kvm_cpu_get_interrupt(vcpu);
12535 WARN_ON(irq < 0);
12536 vmcs12->vm_exit_intr_info = irq |
12537 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12538 }
12539
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012540 if (exit_reason != -1)
12541 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12542 vmcs12->exit_qualification,
12543 vmcs12->idt_vectoring_info_field,
12544 vmcs12->vm_exit_intr_info,
12545 vmcs12->vm_exit_intr_error_code,
12546 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012547
12548 load_vmcs12_host_state(vcpu, vmcs12);
12549
12550 return;
12551 }
12552
12553 /*
12554 * After an early L2 VM-entry failure, we're now back
12555 * in L1 which thinks it just finished a VMLAUNCH or
12556 * VMRESUME instruction, so we need to set the failure
12557 * flag and the VM-instruction error field of the VMCS
12558 * accordingly.
12559 */
12560 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012561
12562 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12563
Jim Mattson4f350c62017-09-14 16:31:44 -070012564 /*
12565 * The emulated instruction was already skipped in
12566 * nested_vmx_run, but the updated RIP was never
12567 * written back to the vmcs01.
12568 */
12569 skip_emulated_instruction(vcpu);
12570 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012571}
12572
Nadav Har'El7c177932011-05-25 23:12:04 +030012573/*
Jan Kiszka42124922014-01-04 18:47:19 +010012574 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12575 */
12576static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12577{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012578 if (is_guest_mode(vcpu)) {
12579 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012580 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012581 }
Jan Kiszka42124922014-01-04 18:47:19 +010012582 free_nested(to_vmx(vcpu));
12583}
12584
12585/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012586 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12587 * 23.7 "VM-entry failures during or after loading guest state" (this also
12588 * lists the acceptable exit-reason and exit-qualification parameters).
12589 * It should only be called before L2 actually succeeded to run, and when
12590 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12591 */
12592static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12593 struct vmcs12 *vmcs12,
12594 u32 reason, unsigned long qualification)
12595{
12596 load_vmcs12_host_state(vcpu, vmcs12);
12597 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12598 vmcs12->exit_qualification = qualification;
12599 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012600 if (enable_shadow_vmcs)
12601 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012602}
12603
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012604static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12605 struct x86_instruction_info *info,
12606 enum x86_intercept_stage stage)
12607{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012608 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12609 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12610
12611 /*
12612 * RDPID causes #UD if disabled through secondary execution controls.
12613 * Because it is marked as EmulateOnUD, we need to intercept it here.
12614 */
12615 if (info->intercept == x86_intercept_rdtscp &&
12616 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12617 ctxt->exception.vector = UD_VECTOR;
12618 ctxt->exception.error_code_valid = false;
12619 return X86EMUL_PROPAGATE_FAULT;
12620 }
12621
12622 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012623 return X86EMUL_CONTINUE;
12624}
12625
Yunhong Jiang64672c92016-06-13 14:19:59 -070012626#ifdef CONFIG_X86_64
12627/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12628static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12629 u64 divisor, u64 *result)
12630{
12631 u64 low = a << shift, high = a >> (64 - shift);
12632
12633 /* To avoid the overflow on divq */
12634 if (high >= divisor)
12635 return 1;
12636
12637 /* Low hold the result, high hold rem which is discarded */
12638 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12639 "rm" (divisor), "0" (low), "1" (high));
12640 *result = low;
12641
12642 return 0;
12643}
12644
12645static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12646{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012647 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012648 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012649
12650 if (kvm_mwait_in_guest(vcpu->kvm))
12651 return -EOPNOTSUPP;
12652
12653 vmx = to_vmx(vcpu);
12654 tscl = rdtsc();
12655 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12656 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012657 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12658
12659 if (delta_tsc > lapic_timer_advance_cycles)
12660 delta_tsc -= lapic_timer_advance_cycles;
12661 else
12662 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012663
12664 /* Convert to host delta tsc if tsc scaling is enabled */
12665 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12666 u64_shl_div_u64(delta_tsc,
12667 kvm_tsc_scaling_ratio_frac_bits,
12668 vcpu->arch.tsc_scaling_ratio,
12669 &delta_tsc))
12670 return -ERANGE;
12671
12672 /*
12673 * If the delta tsc can't fit in the 32 bit after the multi shift,
12674 * we can't use the preemption timer.
12675 * It's possible that it fits on later vmentries, but checking
12676 * on every vmentry is costly so we just use an hrtimer.
12677 */
12678 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12679 return -ERANGE;
12680
12681 vmx->hv_deadline_tsc = tscl + delta_tsc;
12682 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12683 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012684
12685 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012686}
12687
12688static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12689{
12690 struct vcpu_vmx *vmx = to_vmx(vcpu);
12691 vmx->hv_deadline_tsc = -1;
12692 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12693 PIN_BASED_VMX_PREEMPTION_TIMER);
12694}
12695#endif
12696
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012697static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012698{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012699 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012700 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012701}
12702
Kai Huang843e4332015-01-28 10:54:28 +080012703static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12704 struct kvm_memory_slot *slot)
12705{
12706 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12707 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12708}
12709
12710static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12711 struct kvm_memory_slot *slot)
12712{
12713 kvm_mmu_slot_set_dirty(kvm, slot);
12714}
12715
12716static void vmx_flush_log_dirty(struct kvm *kvm)
12717{
12718 kvm_flush_pml_buffers(kvm);
12719}
12720
Bandan Dasc5f983f2017-05-05 15:25:14 -040012721static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12722{
12723 struct vmcs12 *vmcs12;
12724 struct vcpu_vmx *vmx = to_vmx(vcpu);
12725 gpa_t gpa;
12726 struct page *page = NULL;
12727 u64 *pml_address;
12728
12729 if (is_guest_mode(vcpu)) {
12730 WARN_ON_ONCE(vmx->nested.pml_full);
12731
12732 /*
12733 * Check if PML is enabled for the nested guest.
12734 * Whether eptp bit 6 is set is already checked
12735 * as part of A/D emulation.
12736 */
12737 vmcs12 = get_vmcs12(vcpu);
12738 if (!nested_cpu_has_pml(vmcs12))
12739 return 0;
12740
Dan Carpenter47698862017-05-10 22:43:17 +030012741 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012742 vmx->nested.pml_full = true;
12743 return 1;
12744 }
12745
12746 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12747
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012748 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12749 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012750 return 0;
12751
12752 pml_address = kmap(page);
12753 pml_address[vmcs12->guest_pml_index--] = gpa;
12754 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012755 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012756 }
12757
12758 return 0;
12759}
12760
Kai Huang843e4332015-01-28 10:54:28 +080012761static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12762 struct kvm_memory_slot *memslot,
12763 gfn_t offset, unsigned long mask)
12764{
12765 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12766}
12767
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012768static void __pi_post_block(struct kvm_vcpu *vcpu)
12769{
12770 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12771 struct pi_desc old, new;
12772 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012773
12774 do {
12775 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012776 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12777 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012778
12779 dest = cpu_physical_id(vcpu->cpu);
12780
12781 if (x2apic_enabled())
12782 new.ndst = dest;
12783 else
12784 new.ndst = (dest << 8) & 0xFF00;
12785
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012786 /* set 'NV' to 'notification vector' */
12787 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012788 } while (cmpxchg64(&pi_desc->control, old.control,
12789 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012790
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012791 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12792 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012793 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012794 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012795 vcpu->pre_pcpu = -1;
12796 }
12797}
12798
Feng Wuefc64402015-09-18 22:29:51 +080012799/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012800 * This routine does the following things for vCPU which is going
12801 * to be blocked if VT-d PI is enabled.
12802 * - Store the vCPU to the wakeup list, so when interrupts happen
12803 * we can find the right vCPU to wake up.
12804 * - Change the Posted-interrupt descriptor as below:
12805 * 'NDST' <-- vcpu->pre_pcpu
12806 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12807 * - If 'ON' is set during this process, which means at least one
12808 * interrupt is posted for this vCPU, we cannot block it, in
12809 * this case, return 1, otherwise, return 0.
12810 *
12811 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012812static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012813{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012814 unsigned int dest;
12815 struct pi_desc old, new;
12816 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12817
12818 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012819 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12820 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012821 return 0;
12822
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012823 WARN_ON(irqs_disabled());
12824 local_irq_disable();
12825 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12826 vcpu->pre_pcpu = vcpu->cpu;
12827 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12828 list_add_tail(&vcpu->blocked_vcpu_list,
12829 &per_cpu(blocked_vcpu_on_cpu,
12830 vcpu->pre_pcpu));
12831 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12832 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012833
12834 do {
12835 old.control = new.control = pi_desc->control;
12836
Feng Wubf9f6ac2015-09-18 22:29:55 +080012837 WARN((pi_desc->sn == 1),
12838 "Warning: SN field of posted-interrupts "
12839 "is set before blocking\n");
12840
12841 /*
12842 * Since vCPU can be preempted during this process,
12843 * vcpu->cpu could be different with pre_pcpu, we
12844 * need to set pre_pcpu as the destination of wakeup
12845 * notification event, then we can find the right vCPU
12846 * to wakeup in wakeup handler if interrupts happen
12847 * when the vCPU is in blocked state.
12848 */
12849 dest = cpu_physical_id(vcpu->pre_pcpu);
12850
12851 if (x2apic_enabled())
12852 new.ndst = dest;
12853 else
12854 new.ndst = (dest << 8) & 0xFF00;
12855
12856 /* set 'NV' to 'wakeup vector' */
12857 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012858 } while (cmpxchg64(&pi_desc->control, old.control,
12859 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012860
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012861 /* We should not block the vCPU if an interrupt is posted for it. */
12862 if (pi_test_on(pi_desc) == 1)
12863 __pi_post_block(vcpu);
12864
12865 local_irq_enable();
12866 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012867}
12868
Yunhong Jiangbc225122016-06-13 14:19:58 -070012869static int vmx_pre_block(struct kvm_vcpu *vcpu)
12870{
12871 if (pi_pre_block(vcpu))
12872 return 1;
12873
Yunhong Jiang64672c92016-06-13 14:19:59 -070012874 if (kvm_lapic_hv_timer_in_use(vcpu))
12875 kvm_lapic_switch_to_sw_timer(vcpu);
12876
Yunhong Jiangbc225122016-06-13 14:19:58 -070012877 return 0;
12878}
12879
12880static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012881{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012882 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012883 return;
12884
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012885 WARN_ON(irqs_disabled());
12886 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012887 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012888 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012889}
12890
Yunhong Jiangbc225122016-06-13 14:19:58 -070012891static void vmx_post_block(struct kvm_vcpu *vcpu)
12892{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012893 if (kvm_x86_ops->set_hv_timer)
12894 kvm_lapic_switch_to_hv_timer(vcpu);
12895
Yunhong Jiangbc225122016-06-13 14:19:58 -070012896 pi_post_block(vcpu);
12897}
12898
Feng Wubf9f6ac2015-09-18 22:29:55 +080012899/*
Feng Wuefc64402015-09-18 22:29:51 +080012900 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12901 *
12902 * @kvm: kvm
12903 * @host_irq: host irq of the interrupt
12904 * @guest_irq: gsi of the interrupt
12905 * @set: set or unset PI
12906 * returns 0 on success, < 0 on failure
12907 */
12908static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12909 uint32_t guest_irq, bool set)
12910{
12911 struct kvm_kernel_irq_routing_entry *e;
12912 struct kvm_irq_routing_table *irq_rt;
12913 struct kvm_lapic_irq irq;
12914 struct kvm_vcpu *vcpu;
12915 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012916 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012917
12918 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012919 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12920 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012921 return 0;
12922
12923 idx = srcu_read_lock(&kvm->irq_srcu);
12924 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012925 if (guest_irq >= irq_rt->nr_rt_entries ||
12926 hlist_empty(&irq_rt->map[guest_irq])) {
12927 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12928 guest_irq, irq_rt->nr_rt_entries);
12929 goto out;
12930 }
Feng Wuefc64402015-09-18 22:29:51 +080012931
12932 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12933 if (e->type != KVM_IRQ_ROUTING_MSI)
12934 continue;
12935 /*
12936 * VT-d PI cannot support posting multicast/broadcast
12937 * interrupts to a vCPU, we still use interrupt remapping
12938 * for these kind of interrupts.
12939 *
12940 * For lowest-priority interrupts, we only support
12941 * those with single CPU as the destination, e.g. user
12942 * configures the interrupts via /proc/irq or uses
12943 * irqbalance to make the interrupts single-CPU.
12944 *
12945 * We will support full lowest-priority interrupt later.
12946 */
12947
Radim Krčmář371313132016-07-12 22:09:27 +020012948 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012949 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12950 /*
12951 * Make sure the IRTE is in remapped mode if
12952 * we don't handle it in posted mode.
12953 */
12954 ret = irq_set_vcpu_affinity(host_irq, NULL);
12955 if (ret < 0) {
12956 printk(KERN_INFO
12957 "failed to back to remapped mode, irq: %u\n",
12958 host_irq);
12959 goto out;
12960 }
12961
Feng Wuefc64402015-09-18 22:29:51 +080012962 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012963 }
Feng Wuefc64402015-09-18 22:29:51 +080012964
12965 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12966 vcpu_info.vector = irq.vector;
12967
hu huajun2698d822018-04-11 15:16:40 +080012968 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012969 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12970
12971 if (set)
12972 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012973 else
Feng Wuefc64402015-09-18 22:29:51 +080012974 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012975
12976 if (ret < 0) {
12977 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12978 __func__);
12979 goto out;
12980 }
12981 }
12982
12983 ret = 0;
12984out:
12985 srcu_read_unlock(&kvm->irq_srcu, idx);
12986 return ret;
12987}
12988
Ashok Rajc45dcc72016-06-22 14:59:56 +080012989static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12990{
12991 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12992 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12993 FEATURE_CONTROL_LMCE;
12994 else
12995 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12996 ~FEATURE_CONTROL_LMCE;
12997}
12998
Ladi Prosek72d7b372017-10-11 16:54:41 +020012999static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13000{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013001 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13002 if (to_vmx(vcpu)->nested.nested_run_pending)
13003 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013004 return 1;
13005}
13006
Ladi Prosek0234bf82017-10-11 16:54:40 +020013007static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13008{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013009 struct vcpu_vmx *vmx = to_vmx(vcpu);
13010
13011 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13012 if (vmx->nested.smm.guest_mode)
13013 nested_vmx_vmexit(vcpu, -1, 0, 0);
13014
13015 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13016 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013017 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013018 return 0;
13019}
13020
13021static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13022{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013023 struct vcpu_vmx *vmx = to_vmx(vcpu);
13024 int ret;
13025
13026 if (vmx->nested.smm.vmxon) {
13027 vmx->nested.vmxon = true;
13028 vmx->nested.smm.vmxon = false;
13029 }
13030
13031 if (vmx->nested.smm.guest_mode) {
13032 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013033 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013034 vcpu->arch.hflags |= HF_SMM_MASK;
13035 if (ret)
13036 return ret;
13037
13038 vmx->nested.smm.guest_mode = false;
13039 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013040 return 0;
13041}
13042
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013043static int enable_smi_window(struct kvm_vcpu *vcpu)
13044{
13045 return 0;
13046}
13047
Kees Cook404f6aa2016-08-08 16:29:06 -070013048static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013049 .cpu_has_kvm_support = cpu_has_kvm_support,
13050 .disabled_by_bios = vmx_disabled_by_bios,
13051 .hardware_setup = hardware_setup,
13052 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013053 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013054 .hardware_enable = hardware_enable,
13055 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013056 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013057 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013058
Wanpeng Lib31c1142018-03-12 04:53:04 -070013059 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013060 .vm_alloc = vmx_vm_alloc,
13061 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013062
Avi Kivity6aa8b732006-12-10 02:21:36 -080013063 .vcpu_create = vmx_create_vcpu,
13064 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013065 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013066
Avi Kivity04d2cc72007-09-10 18:10:54 +030013067 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013068 .vcpu_load = vmx_vcpu_load,
13069 .vcpu_put = vmx_vcpu_put,
13070
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013071 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013072 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013073 .get_msr = vmx_get_msr,
13074 .set_msr = vmx_set_msr,
13075 .get_segment_base = vmx_get_segment_base,
13076 .get_segment = vmx_get_segment,
13077 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013078 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013079 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013080 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013081 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013082 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013083 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013084 .set_cr3 = vmx_set_cr3,
13085 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013086 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013087 .get_idt = vmx_get_idt,
13088 .set_idt = vmx_set_idt,
13089 .get_gdt = vmx_get_gdt,
13090 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013091 .get_dr6 = vmx_get_dr6,
13092 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013093 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013094 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013095 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013096 .get_rflags = vmx_get_rflags,
13097 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013098
Avi Kivity6aa8b732006-12-10 02:21:36 -080013099 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013100
Avi Kivity6aa8b732006-12-10 02:21:36 -080013101 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013102 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013103 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013104 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13105 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013106 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013107 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013108 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013109 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013110 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013111 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013112 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013113 .get_nmi_mask = vmx_get_nmi_mask,
13114 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013115 .enable_nmi_window = enable_nmi_window,
13116 .enable_irq_window = enable_irq_window,
13117 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013118 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013119 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013120 .get_enable_apicv = vmx_get_enable_apicv,
13121 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013122 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013123 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013124 .hwapic_irr_update = vmx_hwapic_irr_update,
13125 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013126 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13127 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013128
Izik Eiduscbc94022007-10-25 00:29:55 +020013129 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013130 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013131 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013132 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013133
Avi Kivity586f9602010-11-18 13:09:54 +020013134 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013135
Sheng Yang17cc3932010-01-05 19:02:27 +080013136 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013137
13138 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013139
13140 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013141 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013142
13143 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013144
13145 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013146
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013147 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013148 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013149
13150 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013151
13152 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013153 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013154 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013155 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013156 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013157
13158 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013159
13160 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013161
13162 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13163 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13164 .flush_log_dirty = vmx_flush_log_dirty,
13165 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013166 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013167
Feng Wubf9f6ac2015-09-18 22:29:55 +080013168 .pre_block = vmx_pre_block,
13169 .post_block = vmx_post_block,
13170
Wei Huang25462f72015-06-19 15:45:05 +020013171 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013172
13173 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013174
13175#ifdef CONFIG_X86_64
13176 .set_hv_timer = vmx_set_hv_timer,
13177 .cancel_hv_timer = vmx_cancel_hv_timer,
13178#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013179
13180 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013181
Ladi Prosek72d7b372017-10-11 16:54:41 +020013182 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013183 .pre_enter_smm = vmx_pre_enter_smm,
13184 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013185 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013186};
13187
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013188static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013189{
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013190 struct page *page;
13191
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013192 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
13193 !boot_cpu_has_bug(X86_BUG_L1TF))
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013194 return 0;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013195
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020013196 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
13197 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
13198 if (!page)
13199 return -ENOMEM;
13200 vmx_l1d_flush_pages = page_address(page);
13201 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013202
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013203 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013204 return 0;
13205}
13206
13207static void vmx_free_l1d_flush_pages(void)
13208{
13209 if (vmx_l1d_flush_pages) {
13210 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13211 vmx_l1d_flush_pages = NULL;
13212 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013213}
13214
Avi Kivity6aa8b732006-12-10 02:21:36 -080013215static int __init vmx_init(void)
13216{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013217 int r;
13218
13219#if IS_ENABLED(CONFIG_HYPERV)
13220 /*
13221 * Enlightened VMCS usage should be recommended and the host needs
13222 * to support eVMCS v1 or above. We can also disable eVMCS support
13223 * with module parameter.
13224 */
13225 if (enlightened_vmcs &&
13226 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13227 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13228 KVM_EVMCS_VERSION) {
13229 int cpu;
13230
13231 /* Check that we have assist pages on all online CPUs */
13232 for_each_online_cpu(cpu) {
13233 if (!hv_get_vp_assist_page(cpu)) {
13234 enlightened_vmcs = false;
13235 break;
13236 }
13237 }
13238
13239 if (enlightened_vmcs) {
13240 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13241 static_branch_enable(&enable_evmcs);
13242 }
13243 } else {
13244 enlightened_vmcs = false;
13245 }
13246#endif
13247
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013248 r = vmx_setup_l1d_flush();
He, Qingfdef3ad2007-04-30 09:45:24 +030013249 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013250 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013251
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013252 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13253 __alignof__(struct vcpu_vmx), THIS_MODULE);
13254 if (r) {
13255 vmx_free_l1d_flush_pages();
13256 return r;
13257 }
13258
Dave Young2965faa2015-09-09 15:38:55 -070013259#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013260 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13261 crash_vmclear_local_loaded_vmcss);
13262#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013263 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013264
He, Qingfdef3ad2007-04-30 09:45:24 +030013265 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013266}
13267
13268static void __exit vmx_exit(void)
13269{
Dave Young2965faa2015-09-09 15:38:55 -070013270#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013271 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013272 synchronize_rcu();
13273#endif
13274
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013275 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013276
13277#if IS_ENABLED(CONFIG_HYPERV)
13278 if (static_branch_unlikely(&enable_evmcs)) {
13279 int cpu;
13280 struct hv_vp_assist_page *vp_ap;
13281 /*
13282 * Reset everything to support using non-enlightened VMCS
13283 * access later (e.g. when we reload the module with
13284 * enlightened_vmcs=0)
13285 */
13286 for_each_online_cpu(cpu) {
13287 vp_ap = hv_get_vp_assist_page(cpu);
13288
13289 if (!vp_ap)
13290 continue;
13291
13292 vp_ap->current_nested_vmcs = 0;
13293 vp_ap->enlighten_vmentry = 0;
13294 }
13295
13296 static_branch_disable(&enable_evmcs);
13297 }
13298#endif
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013299 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080013300}
13301
13302module_init(vmx_init)
13303module_exit(vmx_exit)