blob: 6151418cec323e2f6de0079b9b75119973f0bc10 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf41245002014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700191struct kvm_vmx {
192 struct kvm kvm;
193
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
197};
198
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200199#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300200
Liran Alon392b2f22018-06-23 02:35:01 +0300201struct vmcs_hdr {
202 u32 revision_id:31;
203 u32 shadow_vmcs:1;
204};
205
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400206struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300207 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400208 u32 abort;
209 char data[0];
210};
211
Nadav Har'Eld462b812011-05-24 15:26:10 +0300212/*
213 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
214 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
215 * loaded on this CPU (so we can clear them if the CPU goes down).
216 */
217struct loaded_vmcs {
218 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700219 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300220 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200221 bool launched;
222 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200223 unsigned long vmcs_host_cr3; /* May not match real cr3 */
224 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100225 /* Support for vnmi-less CPUs */
226 int soft_vnmi_blocked;
227 ktime_t entry_time;
228 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100229 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300230 struct list_head loaded_vmcss_on_cpu_link;
231};
232
Avi Kivity26bb0982009-09-07 11:14:12 +0300233struct shared_msr_entry {
234 unsigned index;
235 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200236 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300237};
238
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300239/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300240 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
241 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
242 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
243 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
244 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
245 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600246 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300247 * underlying hardware which will be used to run L2.
248 * This structure is packed to ensure that its layout is identical across
249 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700250 *
251 * IMPORTANT: Changing the layout of existing fields in this structure
252 * will break save/restore compatibility with older kvm releases. When
253 * adding new fields, either use space in the reserved padding* arrays
254 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300255 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300256typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300257struct __packed vmcs12 {
258 /* According to the Intel spec, a VMCS region must start with the
259 * following two fields. Then follow implementation-specific data.
260 */
Liran Alon392b2f22018-06-23 02:35:01 +0300261 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300262 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300263
Nadav Har'El27d6c862011-05-25 23:06:59 +0300264 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
265 u32 padding[7]; /* room for future expansion */
266
Nadav Har'El22bd0352011-05-25 23:05:57 +0300267 u64 io_bitmap_a;
268 u64 io_bitmap_b;
269 u64 msr_bitmap;
270 u64 vm_exit_msr_store_addr;
271 u64 vm_exit_msr_load_addr;
272 u64 vm_entry_msr_load_addr;
273 u64 tsc_offset;
274 u64 virtual_apic_page_addr;
275 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800276 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300277 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800278 u64 eoi_exit_bitmap0;
279 u64 eoi_exit_bitmap1;
280 u64 eoi_exit_bitmap2;
281 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800282 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300283 u64 guest_physical_address;
284 u64 vmcs_link_pointer;
285 u64 guest_ia32_debugctl;
286 u64 guest_ia32_pat;
287 u64 guest_ia32_efer;
288 u64 guest_ia32_perf_global_ctrl;
289 u64 guest_pdptr0;
290 u64 guest_pdptr1;
291 u64 guest_pdptr2;
292 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100293 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300294 u64 host_ia32_pat;
295 u64 host_ia32_efer;
296 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700297 u64 vmread_bitmap;
298 u64 vmwrite_bitmap;
299 u64 vm_function_control;
300 u64 eptp_list_address;
301 u64 pml_address;
302 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 /*
304 * To allow migration of L1 (complete with its L2 guests) between
305 * machines of different natural widths (32 or 64 bit), we cannot have
306 * unsigned long fields with no explict size. We use u64 (aliased
307 * natural_width) instead. Luckily, x86 is little-endian.
308 */
309 natural_width cr0_guest_host_mask;
310 natural_width cr4_guest_host_mask;
311 natural_width cr0_read_shadow;
312 natural_width cr4_read_shadow;
313 natural_width cr3_target_value0;
314 natural_width cr3_target_value1;
315 natural_width cr3_target_value2;
316 natural_width cr3_target_value3;
317 natural_width exit_qualification;
318 natural_width guest_linear_address;
319 natural_width guest_cr0;
320 natural_width guest_cr3;
321 natural_width guest_cr4;
322 natural_width guest_es_base;
323 natural_width guest_cs_base;
324 natural_width guest_ss_base;
325 natural_width guest_ds_base;
326 natural_width guest_fs_base;
327 natural_width guest_gs_base;
328 natural_width guest_ldtr_base;
329 natural_width guest_tr_base;
330 natural_width guest_gdtr_base;
331 natural_width guest_idtr_base;
332 natural_width guest_dr7;
333 natural_width guest_rsp;
334 natural_width guest_rip;
335 natural_width guest_rflags;
336 natural_width guest_pending_dbg_exceptions;
337 natural_width guest_sysenter_esp;
338 natural_width guest_sysenter_eip;
339 natural_width host_cr0;
340 natural_width host_cr3;
341 natural_width host_cr4;
342 natural_width host_fs_base;
343 natural_width host_gs_base;
344 natural_width host_tr_base;
345 natural_width host_gdtr_base;
346 natural_width host_idtr_base;
347 natural_width host_ia32_sysenter_esp;
348 natural_width host_ia32_sysenter_eip;
349 natural_width host_rsp;
350 natural_width host_rip;
351 natural_width paddingl[8]; /* room for future expansion */
352 u32 pin_based_vm_exec_control;
353 u32 cpu_based_vm_exec_control;
354 u32 exception_bitmap;
355 u32 page_fault_error_code_mask;
356 u32 page_fault_error_code_match;
357 u32 cr3_target_count;
358 u32 vm_exit_controls;
359 u32 vm_exit_msr_store_count;
360 u32 vm_exit_msr_load_count;
361 u32 vm_entry_controls;
362 u32 vm_entry_msr_load_count;
363 u32 vm_entry_intr_info_field;
364 u32 vm_entry_exception_error_code;
365 u32 vm_entry_instruction_len;
366 u32 tpr_threshold;
367 u32 secondary_vm_exec_control;
368 u32 vm_instruction_error;
369 u32 vm_exit_reason;
370 u32 vm_exit_intr_info;
371 u32 vm_exit_intr_error_code;
372 u32 idt_vectoring_info_field;
373 u32 idt_vectoring_error_code;
374 u32 vm_exit_instruction_len;
375 u32 vmx_instruction_info;
376 u32 guest_es_limit;
377 u32 guest_cs_limit;
378 u32 guest_ss_limit;
379 u32 guest_ds_limit;
380 u32 guest_fs_limit;
381 u32 guest_gs_limit;
382 u32 guest_ldtr_limit;
383 u32 guest_tr_limit;
384 u32 guest_gdtr_limit;
385 u32 guest_idtr_limit;
386 u32 guest_es_ar_bytes;
387 u32 guest_cs_ar_bytes;
388 u32 guest_ss_ar_bytes;
389 u32 guest_ds_ar_bytes;
390 u32 guest_fs_ar_bytes;
391 u32 guest_gs_ar_bytes;
392 u32 guest_ldtr_ar_bytes;
393 u32 guest_tr_ar_bytes;
394 u32 guest_interruptibility_info;
395 u32 guest_activity_state;
396 u32 guest_sysenter_cs;
397 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100398 u32 vmx_preemption_timer_value;
399 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300400 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800401 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402 u16 guest_es_selector;
403 u16 guest_cs_selector;
404 u16 guest_ss_selector;
405 u16 guest_ds_selector;
406 u16 guest_fs_selector;
407 u16 guest_gs_selector;
408 u16 guest_ldtr_selector;
409 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800410 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 host_es_selector;
412 u16 host_cs_selector;
413 u16 host_ss_selector;
414 u16 host_ds_selector;
415 u16 host_fs_selector;
416 u16 host_gs_selector;
417 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700418 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300419};
420
421/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700422 * For save/restore compatibility, the vmcs12 field offsets must not change.
423 */
424#define CHECK_OFFSET(field, loc) \
425 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
426 "Offset of " #field " in struct vmcs12 has changed.")
427
428static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300429 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700430 CHECK_OFFSET(abort, 4);
431 CHECK_OFFSET(launch_state, 8);
432 CHECK_OFFSET(io_bitmap_a, 40);
433 CHECK_OFFSET(io_bitmap_b, 48);
434 CHECK_OFFSET(msr_bitmap, 56);
435 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
436 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
437 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
438 CHECK_OFFSET(tsc_offset, 88);
439 CHECK_OFFSET(virtual_apic_page_addr, 96);
440 CHECK_OFFSET(apic_access_addr, 104);
441 CHECK_OFFSET(posted_intr_desc_addr, 112);
442 CHECK_OFFSET(ept_pointer, 120);
443 CHECK_OFFSET(eoi_exit_bitmap0, 128);
444 CHECK_OFFSET(eoi_exit_bitmap1, 136);
445 CHECK_OFFSET(eoi_exit_bitmap2, 144);
446 CHECK_OFFSET(eoi_exit_bitmap3, 152);
447 CHECK_OFFSET(xss_exit_bitmap, 160);
448 CHECK_OFFSET(guest_physical_address, 168);
449 CHECK_OFFSET(vmcs_link_pointer, 176);
450 CHECK_OFFSET(guest_ia32_debugctl, 184);
451 CHECK_OFFSET(guest_ia32_pat, 192);
452 CHECK_OFFSET(guest_ia32_efer, 200);
453 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
454 CHECK_OFFSET(guest_pdptr0, 216);
455 CHECK_OFFSET(guest_pdptr1, 224);
456 CHECK_OFFSET(guest_pdptr2, 232);
457 CHECK_OFFSET(guest_pdptr3, 240);
458 CHECK_OFFSET(guest_bndcfgs, 248);
459 CHECK_OFFSET(host_ia32_pat, 256);
460 CHECK_OFFSET(host_ia32_efer, 264);
461 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
462 CHECK_OFFSET(vmread_bitmap, 280);
463 CHECK_OFFSET(vmwrite_bitmap, 288);
464 CHECK_OFFSET(vm_function_control, 296);
465 CHECK_OFFSET(eptp_list_address, 304);
466 CHECK_OFFSET(pml_address, 312);
467 CHECK_OFFSET(cr0_guest_host_mask, 344);
468 CHECK_OFFSET(cr4_guest_host_mask, 352);
469 CHECK_OFFSET(cr0_read_shadow, 360);
470 CHECK_OFFSET(cr4_read_shadow, 368);
471 CHECK_OFFSET(cr3_target_value0, 376);
472 CHECK_OFFSET(cr3_target_value1, 384);
473 CHECK_OFFSET(cr3_target_value2, 392);
474 CHECK_OFFSET(cr3_target_value3, 400);
475 CHECK_OFFSET(exit_qualification, 408);
476 CHECK_OFFSET(guest_linear_address, 416);
477 CHECK_OFFSET(guest_cr0, 424);
478 CHECK_OFFSET(guest_cr3, 432);
479 CHECK_OFFSET(guest_cr4, 440);
480 CHECK_OFFSET(guest_es_base, 448);
481 CHECK_OFFSET(guest_cs_base, 456);
482 CHECK_OFFSET(guest_ss_base, 464);
483 CHECK_OFFSET(guest_ds_base, 472);
484 CHECK_OFFSET(guest_fs_base, 480);
485 CHECK_OFFSET(guest_gs_base, 488);
486 CHECK_OFFSET(guest_ldtr_base, 496);
487 CHECK_OFFSET(guest_tr_base, 504);
488 CHECK_OFFSET(guest_gdtr_base, 512);
489 CHECK_OFFSET(guest_idtr_base, 520);
490 CHECK_OFFSET(guest_dr7, 528);
491 CHECK_OFFSET(guest_rsp, 536);
492 CHECK_OFFSET(guest_rip, 544);
493 CHECK_OFFSET(guest_rflags, 552);
494 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
495 CHECK_OFFSET(guest_sysenter_esp, 568);
496 CHECK_OFFSET(guest_sysenter_eip, 576);
497 CHECK_OFFSET(host_cr0, 584);
498 CHECK_OFFSET(host_cr3, 592);
499 CHECK_OFFSET(host_cr4, 600);
500 CHECK_OFFSET(host_fs_base, 608);
501 CHECK_OFFSET(host_gs_base, 616);
502 CHECK_OFFSET(host_tr_base, 624);
503 CHECK_OFFSET(host_gdtr_base, 632);
504 CHECK_OFFSET(host_idtr_base, 640);
505 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
506 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
507 CHECK_OFFSET(host_rsp, 664);
508 CHECK_OFFSET(host_rip, 672);
509 CHECK_OFFSET(pin_based_vm_exec_control, 744);
510 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
511 CHECK_OFFSET(exception_bitmap, 752);
512 CHECK_OFFSET(page_fault_error_code_mask, 756);
513 CHECK_OFFSET(page_fault_error_code_match, 760);
514 CHECK_OFFSET(cr3_target_count, 764);
515 CHECK_OFFSET(vm_exit_controls, 768);
516 CHECK_OFFSET(vm_exit_msr_store_count, 772);
517 CHECK_OFFSET(vm_exit_msr_load_count, 776);
518 CHECK_OFFSET(vm_entry_controls, 780);
519 CHECK_OFFSET(vm_entry_msr_load_count, 784);
520 CHECK_OFFSET(vm_entry_intr_info_field, 788);
521 CHECK_OFFSET(vm_entry_exception_error_code, 792);
522 CHECK_OFFSET(vm_entry_instruction_len, 796);
523 CHECK_OFFSET(tpr_threshold, 800);
524 CHECK_OFFSET(secondary_vm_exec_control, 804);
525 CHECK_OFFSET(vm_instruction_error, 808);
526 CHECK_OFFSET(vm_exit_reason, 812);
527 CHECK_OFFSET(vm_exit_intr_info, 816);
528 CHECK_OFFSET(vm_exit_intr_error_code, 820);
529 CHECK_OFFSET(idt_vectoring_info_field, 824);
530 CHECK_OFFSET(idt_vectoring_error_code, 828);
531 CHECK_OFFSET(vm_exit_instruction_len, 832);
532 CHECK_OFFSET(vmx_instruction_info, 836);
533 CHECK_OFFSET(guest_es_limit, 840);
534 CHECK_OFFSET(guest_cs_limit, 844);
535 CHECK_OFFSET(guest_ss_limit, 848);
536 CHECK_OFFSET(guest_ds_limit, 852);
537 CHECK_OFFSET(guest_fs_limit, 856);
538 CHECK_OFFSET(guest_gs_limit, 860);
539 CHECK_OFFSET(guest_ldtr_limit, 864);
540 CHECK_OFFSET(guest_tr_limit, 868);
541 CHECK_OFFSET(guest_gdtr_limit, 872);
542 CHECK_OFFSET(guest_idtr_limit, 876);
543 CHECK_OFFSET(guest_es_ar_bytes, 880);
544 CHECK_OFFSET(guest_cs_ar_bytes, 884);
545 CHECK_OFFSET(guest_ss_ar_bytes, 888);
546 CHECK_OFFSET(guest_ds_ar_bytes, 892);
547 CHECK_OFFSET(guest_fs_ar_bytes, 896);
548 CHECK_OFFSET(guest_gs_ar_bytes, 900);
549 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
550 CHECK_OFFSET(guest_tr_ar_bytes, 908);
551 CHECK_OFFSET(guest_interruptibility_info, 912);
552 CHECK_OFFSET(guest_activity_state, 916);
553 CHECK_OFFSET(guest_sysenter_cs, 920);
554 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
555 CHECK_OFFSET(vmx_preemption_timer_value, 928);
556 CHECK_OFFSET(virtual_processor_id, 960);
557 CHECK_OFFSET(posted_intr_nv, 962);
558 CHECK_OFFSET(guest_es_selector, 964);
559 CHECK_OFFSET(guest_cs_selector, 966);
560 CHECK_OFFSET(guest_ss_selector, 968);
561 CHECK_OFFSET(guest_ds_selector, 970);
562 CHECK_OFFSET(guest_fs_selector, 972);
563 CHECK_OFFSET(guest_gs_selector, 974);
564 CHECK_OFFSET(guest_ldtr_selector, 976);
565 CHECK_OFFSET(guest_tr_selector, 978);
566 CHECK_OFFSET(guest_intr_status, 980);
567 CHECK_OFFSET(host_es_selector, 982);
568 CHECK_OFFSET(host_cs_selector, 984);
569 CHECK_OFFSET(host_ss_selector, 986);
570 CHECK_OFFSET(host_ds_selector, 988);
571 CHECK_OFFSET(host_fs_selector, 990);
572 CHECK_OFFSET(host_gs_selector, 992);
573 CHECK_OFFSET(host_tr_selector, 994);
574 CHECK_OFFSET(guest_pml_index, 996);
575}
576
577/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300578 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
579 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
580 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700581 *
582 * IMPORTANT: Changing this value will break save/restore compatibility with
583 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300584 */
585#define VMCS12_REVISION 0x11e57ed0
586
587/*
588 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
589 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
590 * current implementation, 4K are reserved to avoid future complications.
591 */
592#define VMCS12_SIZE 0x1000
593
594/*
Jim Mattson5b157062017-12-22 12:11:12 -0800595 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
596 * supported VMCS12 field encoding.
597 */
598#define VMCS12_MAX_FIELD_INDEX 0x17
599
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100600struct nested_vmx_msrs {
601 /*
602 * We only store the "true" versions of the VMX capability MSRs. We
603 * generate the "non-true" versions by setting the must-be-1 bits
604 * according to the SDM.
605 */
606 u32 procbased_ctls_low;
607 u32 procbased_ctls_high;
608 u32 secondary_ctls_low;
609 u32 secondary_ctls_high;
610 u32 pinbased_ctls_low;
611 u32 pinbased_ctls_high;
612 u32 exit_ctls_low;
613 u32 exit_ctls_high;
614 u32 entry_ctls_low;
615 u32 entry_ctls_high;
616 u32 misc_low;
617 u32 misc_high;
618 u32 ept_caps;
619 u32 vpid_caps;
620 u64 basic;
621 u64 cr0_fixed0;
622 u64 cr0_fixed1;
623 u64 cr4_fixed0;
624 u64 cr4_fixed1;
625 u64 vmcs_enum;
626 u64 vmfunc_controls;
627};
628
Jim Mattson5b157062017-12-22 12:11:12 -0800629/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300630 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
631 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
632 */
633struct nested_vmx {
634 /* Has the level1 guest done vmxon? */
635 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400636 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400637 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300638
639 /* The guest-physical address of the current VMCS L1 keeps for L2 */
640 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700641 /*
642 * Cache of the guest's VMCS, existing outside of guest memory.
643 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700644 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700645 */
646 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300647 /*
Liran Alon61ada742018-06-23 02:35:08 +0300648 * Cache of the guest's shadow VMCS, existing outside of guest
649 * memory. Loaded from guest memory during VM entry. Flushed
650 * to guest memory during VM exit.
651 */
652 struct vmcs12 *cached_shadow_vmcs12;
653 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300654 * Indicates if the shadow vmcs must be updated with the
655 * data hold by vmcs12
656 */
657 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100658 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300659
Jim Mattson8d860bb2018-05-09 16:56:05 -0400660 bool change_vmcs01_virtual_apic_mode;
661
Nadav Har'El644d7112011-05-25 23:12:35 +0300662 /* L2 must run next, and mustn't decide to exit to L1. */
663 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600664
665 struct loaded_vmcs vmcs02;
666
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300667 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600668 * Guest pages referred to in the vmcs02 with host-physical
669 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300670 */
671 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800672 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800673 struct page *pi_desc_page;
674 struct pi_desc *pi_desc;
675 bool pi_pending;
676 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100677
678 struct hrtimer preemption_timer;
679 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200680
681 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
682 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800683
Wanpeng Li5c614b32015-10-13 09:18:36 -0700684 u16 vpid02;
685 u16 last_vpid;
686
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100687 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200688
689 /* SMM related state */
690 struct {
691 /* in VMX operation on SMM entry? */
692 bool vmxon;
693 /* in guest mode on SMM entry? */
694 bool guest_mode;
695 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300696};
697
Yang Zhang01e439b2013-04-11 19:25:12 +0800698#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800699#define POSTED_INTR_SN 1
700
Yang Zhang01e439b2013-04-11 19:25:12 +0800701/* Posted-Interrupt Descriptor */
702struct pi_desc {
703 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800704 union {
705 struct {
706 /* bit 256 - Outstanding Notification */
707 u16 on : 1,
708 /* bit 257 - Suppress Notification */
709 sn : 1,
710 /* bit 271:258 - Reserved */
711 rsvd_1 : 14;
712 /* bit 279:272 - Notification Vector */
713 u8 nv;
714 /* bit 287:280 - Reserved */
715 u8 rsvd_2;
716 /* bit 319:288 - Notification Destination */
717 u32 ndst;
718 };
719 u64 control;
720 };
721 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800722} __aligned(64);
723
Yang Zhanga20ed542013-04-11 19:25:15 +0800724static bool pi_test_and_set_on(struct pi_desc *pi_desc)
725{
726 return test_and_set_bit(POSTED_INTR_ON,
727 (unsigned long *)&pi_desc->control);
728}
729
730static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
731{
732 return test_and_clear_bit(POSTED_INTR_ON,
733 (unsigned long *)&pi_desc->control);
734}
735
736static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
737{
738 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
739}
740
Feng Wuebbfc762015-09-18 22:29:46 +0800741static inline void pi_clear_sn(struct pi_desc *pi_desc)
742{
743 return clear_bit(POSTED_INTR_SN,
744 (unsigned long *)&pi_desc->control);
745}
746
747static inline void pi_set_sn(struct pi_desc *pi_desc)
748{
749 return set_bit(POSTED_INTR_SN,
750 (unsigned long *)&pi_desc->control);
751}
752
Paolo Bonziniad361092016-09-20 16:15:05 +0200753static inline void pi_clear_on(struct pi_desc *pi_desc)
754{
755 clear_bit(POSTED_INTR_ON,
756 (unsigned long *)&pi_desc->control);
757}
758
Feng Wuebbfc762015-09-18 22:29:46 +0800759static inline int pi_test_on(struct pi_desc *pi_desc)
760{
761 return test_bit(POSTED_INTR_ON,
762 (unsigned long *)&pi_desc->control);
763}
764
765static inline int pi_test_sn(struct pi_desc *pi_desc)
766{
767 return test_bit(POSTED_INTR_SN,
768 (unsigned long *)&pi_desc->control);
769}
770
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000772 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300773 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300774 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100775 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300776 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200777 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200778 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300779 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780 int nmsrs;
781 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800782 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400783#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300784 u64 msr_host_kernel_gs_base;
785 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400786#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100787
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100788 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100789 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100790
Gleb Natapov2961e8762013-11-25 15:37:13 +0200791 u32 vm_entry_controls_shadow;
792 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200793 u32 secondary_exec_control;
794
Nadav Har'Eld462b812011-05-24 15:26:10 +0300795 /*
796 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
797 * non-nested (L1) guest, it always points to vmcs01. For a nested
798 * guest (L2), it points to a different VMCS.
799 */
800 struct loaded_vmcs vmcs01;
801 struct loaded_vmcs *loaded_vmcs;
802 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300803 struct msr_autoload {
804 unsigned nr;
805 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
806 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
807 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808 struct {
809 int loaded;
810 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300811#ifdef CONFIG_X86_64
812 u16 ds_sel, es_sel;
813#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200814 int gs_ldt_reload_needed;
815 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400816 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200817 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300818 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300819 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300820 struct kvm_segment segs[8];
821 } rmode;
822 struct {
823 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300824 struct kvm_save_segment {
825 u16 selector;
826 unsigned long base;
827 u32 limit;
828 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300829 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300830 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800831 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300832 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200833
Andi Kleena0861c02009-06-08 17:37:09 +0800834 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800835
Yang Zhang01e439b2013-04-11 19:25:12 +0800836 /* Posted interrupt descriptor */
837 struct pi_desc pi_desc;
838
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300839 /* Support for a guest hypervisor (nested VMX) */
840 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200841
842 /* Dynamic PLE window. */
843 int ple_window;
844 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800845
846 /* Support for PML */
847#define PML_ENTITY_NUM 512
848 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800849
Yunhong Jiang64672c92016-06-13 14:19:59 -0700850 /* apic deadline value in host tsc */
851 u64 hv_deadline_tsc;
852
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800853 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800854
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800855 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800856
Wanpeng Li74c55932017-11-29 01:31:20 -0800857 unsigned long host_debugctlmsr;
858
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800859 /*
860 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
861 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
862 * in msr_ia32_feature_control_valid_bits.
863 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800864 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800865 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866};
867
Avi Kivity2fb92db2011-04-27 19:42:18 +0300868enum segment_cache_field {
869 SEG_FIELD_SEL = 0,
870 SEG_FIELD_BASE = 1,
871 SEG_FIELD_LIMIT = 2,
872 SEG_FIELD_AR = 3,
873
874 SEG_FIELD_NR = 4
875};
876
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700877static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
878{
879 return container_of(kvm, struct kvm_vmx, kvm);
880}
881
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400882static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
883{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000884 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885}
886
Feng Wuefc64402015-09-18 22:29:51 +0800887static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
888{
889 return &(to_vmx(vcpu)->pi_desc);
890}
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300893#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800894#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
895#define FIELD64(number, name) \
896 FIELD(number, name), \
897 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300898
Abel Gordon4607c2d2013-04-18 14:35:55 +0300899
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100900static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100901#define SHADOW_FIELD_RO(x) x,
902#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300903};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400904static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300905 ARRAY_SIZE(shadow_read_only_fields);
906
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100907static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100908#define SHADOW_FIELD_RW(x) x,
909#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300910};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400911static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300912 ARRAY_SIZE(shadow_read_write_fields);
913
Mathias Krause772e0312012-08-30 01:30:19 +0200914static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300915 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800916 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300917 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
918 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
919 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
920 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
921 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
922 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
923 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
924 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800925 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400926 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300927 FIELD(HOST_ES_SELECTOR, host_es_selector),
928 FIELD(HOST_CS_SELECTOR, host_cs_selector),
929 FIELD(HOST_SS_SELECTOR, host_ss_selector),
930 FIELD(HOST_DS_SELECTOR, host_ds_selector),
931 FIELD(HOST_FS_SELECTOR, host_fs_selector),
932 FIELD(HOST_GS_SELECTOR, host_gs_selector),
933 FIELD(HOST_TR_SELECTOR, host_tr_selector),
934 FIELD64(IO_BITMAP_A, io_bitmap_a),
935 FIELD64(IO_BITMAP_B, io_bitmap_b),
936 FIELD64(MSR_BITMAP, msr_bitmap),
937 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
938 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
939 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700940 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300941 FIELD64(TSC_OFFSET, tsc_offset),
942 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
943 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800944 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400945 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300946 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800947 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
948 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
949 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
950 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400951 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700952 FIELD64(VMREAD_BITMAP, vmread_bitmap),
953 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800954 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300955 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
956 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
957 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
958 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
959 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
960 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
961 FIELD64(GUEST_PDPTR0, guest_pdptr0),
962 FIELD64(GUEST_PDPTR1, guest_pdptr1),
963 FIELD64(GUEST_PDPTR2, guest_pdptr2),
964 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100965 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300966 FIELD64(HOST_IA32_PAT, host_ia32_pat),
967 FIELD64(HOST_IA32_EFER, host_ia32_efer),
968 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
969 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
970 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
971 FIELD(EXCEPTION_BITMAP, exception_bitmap),
972 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
973 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
974 FIELD(CR3_TARGET_COUNT, cr3_target_count),
975 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
976 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
977 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
978 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
979 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
980 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
981 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
982 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
983 FIELD(TPR_THRESHOLD, tpr_threshold),
984 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
985 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
986 FIELD(VM_EXIT_REASON, vm_exit_reason),
987 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
988 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
989 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
990 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
991 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
992 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
993 FIELD(GUEST_ES_LIMIT, guest_es_limit),
994 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
995 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
996 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
997 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
998 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
999 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1000 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1001 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1002 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1003 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1004 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1005 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1006 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1007 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1008 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1009 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1010 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1011 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1012 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1013 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1014 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001015 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001016 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1017 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1018 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1019 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1020 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1021 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1022 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1023 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1024 FIELD(EXIT_QUALIFICATION, exit_qualification),
1025 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1026 FIELD(GUEST_CR0, guest_cr0),
1027 FIELD(GUEST_CR3, guest_cr3),
1028 FIELD(GUEST_CR4, guest_cr4),
1029 FIELD(GUEST_ES_BASE, guest_es_base),
1030 FIELD(GUEST_CS_BASE, guest_cs_base),
1031 FIELD(GUEST_SS_BASE, guest_ss_base),
1032 FIELD(GUEST_DS_BASE, guest_ds_base),
1033 FIELD(GUEST_FS_BASE, guest_fs_base),
1034 FIELD(GUEST_GS_BASE, guest_gs_base),
1035 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1036 FIELD(GUEST_TR_BASE, guest_tr_base),
1037 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1038 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1039 FIELD(GUEST_DR7, guest_dr7),
1040 FIELD(GUEST_RSP, guest_rsp),
1041 FIELD(GUEST_RIP, guest_rip),
1042 FIELD(GUEST_RFLAGS, guest_rflags),
1043 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1044 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1045 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1046 FIELD(HOST_CR0, host_cr0),
1047 FIELD(HOST_CR3, host_cr3),
1048 FIELD(HOST_CR4, host_cr4),
1049 FIELD(HOST_FS_BASE, host_fs_base),
1050 FIELD(HOST_GS_BASE, host_gs_base),
1051 FIELD(HOST_TR_BASE, host_tr_base),
1052 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1053 FIELD(HOST_IDTR_BASE, host_idtr_base),
1054 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1055 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1056 FIELD(HOST_RSP, host_rsp),
1057 FIELD(HOST_RIP, host_rip),
1058};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001059
1060static inline short vmcs_field_to_offset(unsigned long field)
1061{
Dan Williams085331d2018-01-31 17:47:03 -08001062 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1063 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001064 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001065
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001066 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001067 return -ENOENT;
1068
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001069 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001070 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001071 return -ENOENT;
1072
Linus Torvalds15303ba2018-02-10 13:16:35 -08001073 index = array_index_nospec(index, size);
1074 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001075 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001076 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001077 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078}
1079
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001080static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1081{
David Matlack4f2777b2016-07-13 17:16:37 -07001082 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001083}
1084
Liran Alon61ada742018-06-23 02:35:08 +03001085static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1086{
1087 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1088}
1089
Peter Feiner995f00a2017-06-30 17:26:32 -07001090static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001091static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001092static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001093static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001094static void vmx_set_segment(struct kvm_vcpu *vcpu,
1095 struct kvm_segment *var, int seg);
1096static void vmx_get_segment(struct kvm_vcpu *vcpu,
1097 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001098static bool guest_state_valid(struct kvm_vcpu *vcpu);
1099static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001100static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001101static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1102static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1103static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1104 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001105static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001106static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1107 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001108
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1110static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001111/*
1112 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1113 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1114 */
1115static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001116
Feng Wubf9f6ac2015-09-18 22:29:55 +08001117/*
1118 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1119 * can find which vCPU should be waken up.
1120 */
1121static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1122static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1123
Radim Krčmář23611332016-09-29 22:41:33 +02001124enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001125 VMX_VMREAD_BITMAP,
1126 VMX_VMWRITE_BITMAP,
1127 VMX_BITMAP_NR
1128};
1129
1130static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1131
Radim Krčmář23611332016-09-29 22:41:33 +02001132#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1133#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001134
Avi Kivity110312c2010-12-21 12:54:20 +02001135static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001136static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001137
Sheng Yang2384d2b2008-01-17 15:14:33 +08001138static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1139static DEFINE_SPINLOCK(vmx_vpid_lock);
1140
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001141static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 int size;
1143 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001144 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001146 u32 pin_based_exec_ctrl;
1147 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001148 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001149 u32 vmexit_ctrl;
1150 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001151 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001152} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153
Hannes Ederefff9e52008-11-28 17:02:06 +01001154static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001155 u32 ept;
1156 u32 vpid;
1157} vmx_capability;
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159#define VMX_SEGMENT_FIELD(seg) \
1160 [VCPU_SREG_##seg] = { \
1161 .selector = GUEST_##seg##_SELECTOR, \
1162 .base = GUEST_##seg##_BASE, \
1163 .limit = GUEST_##seg##_LIMIT, \
1164 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1165 }
1166
Mathias Krause772e0312012-08-30 01:30:19 +02001167static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 unsigned selector;
1169 unsigned base;
1170 unsigned limit;
1171 unsigned ar_bytes;
1172} kvm_vmx_segment_fields[] = {
1173 VMX_SEGMENT_FIELD(CS),
1174 VMX_SEGMENT_FIELD(DS),
1175 VMX_SEGMENT_FIELD(ES),
1176 VMX_SEGMENT_FIELD(FS),
1177 VMX_SEGMENT_FIELD(GS),
1178 VMX_SEGMENT_FIELD(SS),
1179 VMX_SEGMENT_FIELD(TR),
1180 VMX_SEGMENT_FIELD(LDTR),
1181};
1182
Avi Kivity26bb0982009-09-07 11:14:12 +03001183static u64 host_efer;
1184
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001185static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1186
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001187/*
Brian Gerst8c065852010-07-17 09:03:26 -04001188 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001189 * away by decrementing the array size.
1190 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001192#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001193 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001195 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001198DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1199
1200#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1201
1202#define KVM_EVMCS_VERSION 1
1203
1204#if IS_ENABLED(CONFIG_HYPERV)
1205static bool __read_mostly enlightened_vmcs = true;
1206module_param(enlightened_vmcs, bool, 0444);
1207
1208static inline void evmcs_write64(unsigned long field, u64 value)
1209{
1210 u16 clean_field;
1211 int offset = get_evmcs_offset(field, &clean_field);
1212
1213 if (offset < 0)
1214 return;
1215
1216 *(u64 *)((char *)current_evmcs + offset) = value;
1217
1218 current_evmcs->hv_clean_fields &= ~clean_field;
1219}
1220
1221static inline void evmcs_write32(unsigned long field, u32 value)
1222{
1223 u16 clean_field;
1224 int offset = get_evmcs_offset(field, &clean_field);
1225
1226 if (offset < 0)
1227 return;
1228
1229 *(u32 *)((char *)current_evmcs + offset) = value;
1230 current_evmcs->hv_clean_fields &= ~clean_field;
1231}
1232
1233static inline void evmcs_write16(unsigned long field, u16 value)
1234{
1235 u16 clean_field;
1236 int offset = get_evmcs_offset(field, &clean_field);
1237
1238 if (offset < 0)
1239 return;
1240
1241 *(u16 *)((char *)current_evmcs + offset) = value;
1242 current_evmcs->hv_clean_fields &= ~clean_field;
1243}
1244
1245static inline u64 evmcs_read64(unsigned long field)
1246{
1247 int offset = get_evmcs_offset(field, NULL);
1248
1249 if (offset < 0)
1250 return 0;
1251
1252 return *(u64 *)((char *)current_evmcs + offset);
1253}
1254
1255static inline u32 evmcs_read32(unsigned long field)
1256{
1257 int offset = get_evmcs_offset(field, NULL);
1258
1259 if (offset < 0)
1260 return 0;
1261
1262 return *(u32 *)((char *)current_evmcs + offset);
1263}
1264
1265static inline u16 evmcs_read16(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u16 *)((char *)current_evmcs + offset);
1273}
1274
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001275static inline void evmcs_touch_msr_bitmap(void)
1276{
1277 if (unlikely(!current_evmcs))
1278 return;
1279
1280 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1281 current_evmcs->hv_clean_fields &=
1282 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1283}
1284
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001285static void evmcs_load(u64 phys_addr)
1286{
1287 struct hv_vp_assist_page *vp_ap =
1288 hv_get_vp_assist_page(smp_processor_id());
1289
1290 vp_ap->current_nested_vmcs = phys_addr;
1291 vp_ap->enlighten_vmentry = 1;
1292}
1293
1294static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1295{
1296 /*
1297 * Enlightened VMCSv1 doesn't support these:
1298 *
1299 * POSTED_INTR_NV = 0x00000002,
1300 * GUEST_INTR_STATUS = 0x00000810,
1301 * APIC_ACCESS_ADDR = 0x00002014,
1302 * POSTED_INTR_DESC_ADDR = 0x00002016,
1303 * EOI_EXIT_BITMAP0 = 0x0000201c,
1304 * EOI_EXIT_BITMAP1 = 0x0000201e,
1305 * EOI_EXIT_BITMAP2 = 0x00002020,
1306 * EOI_EXIT_BITMAP3 = 0x00002022,
1307 */
1308 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1309 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1310 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1311 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1312 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1313 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1314 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1315
1316 /*
1317 * GUEST_PML_INDEX = 0x00000812,
1318 * PML_ADDRESS = 0x0000200e,
1319 */
1320 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1321
1322 /* VM_FUNCTION_CONTROL = 0x00002018, */
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1324
1325 /*
1326 * EPTP_LIST_ADDRESS = 0x00002024,
1327 * VMREAD_BITMAP = 0x00002026,
1328 * VMWRITE_BITMAP = 0x00002028,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1331
1332 /*
1333 * TSC_MULTIPLIER = 0x00002032,
1334 */
1335 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1336
1337 /*
1338 * PLE_GAP = 0x00004020,
1339 * PLE_WINDOW = 0x00004022,
1340 */
1341 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1342
1343 /*
1344 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1345 */
1346 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1347
1348 /*
1349 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1350 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1351 */
1352 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1353 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1354
1355 /*
1356 * Currently unsupported in KVM:
1357 * GUEST_IA32_RTIT_CTL = 0x00002814,
1358 */
1359}
1360#else /* !IS_ENABLED(CONFIG_HYPERV) */
1361static inline void evmcs_write64(unsigned long field, u64 value) {}
1362static inline void evmcs_write32(unsigned long field, u32 value) {}
1363static inline void evmcs_write16(unsigned long field, u16 value) {}
1364static inline u64 evmcs_read64(unsigned long field) { return 0; }
1365static inline u32 evmcs_read32(unsigned long field) { return 0; }
1366static inline u16 evmcs_read16(unsigned long field) { return 0; }
1367static inline void evmcs_load(u64 phys_addr) {}
1368static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001369static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001370#endif /* IS_ENABLED(CONFIG_HYPERV) */
1371
Jan Kiszka5bb16012016-02-09 20:14:21 +01001372static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373{
1374 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1375 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001376 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1377}
1378
Jan Kiszka6f054852016-02-09 20:15:18 +01001379static inline bool is_debug(u32 intr_info)
1380{
1381 return is_exception_n(intr_info, DB_VECTOR);
1382}
1383
1384static inline bool is_breakpoint(u32 intr_info)
1385{
1386 return is_exception_n(intr_info, BP_VECTOR);
1387}
1388
Jan Kiszka5bb16012016-02-09 20:14:21 +01001389static inline bool is_page_fault(u32 intr_info)
1390{
1391 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392}
1393
Gui Jianfeng31299942010-03-15 17:29:09 +08001394static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001395{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001396 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001397}
1398
Gui Jianfeng31299942010-03-15 17:29:09 +08001399static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001400{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001401 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001402}
1403
Liran Alon9e869482018-03-12 13:12:51 +02001404static inline bool is_gp_fault(u32 intr_info)
1405{
1406 return is_exception_n(intr_info, GP_VECTOR);
1407}
1408
Gui Jianfeng31299942010-03-15 17:29:09 +08001409static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
1411 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1412 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1413}
1414
Gui Jianfeng31299942010-03-15 17:29:09 +08001415static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001416{
1417 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1418 INTR_INFO_VALID_MASK)) ==
1419 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1420}
1421
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001422/* Undocumented: icebp/int1 */
1423static inline bool is_icebp(u32 intr_info)
1424{
1425 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1426 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001430{
Sheng Yang04547152009-04-01 15:52:31 +08001431 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001432}
1433
Gui Jianfeng31299942010-03-15 17:29:09 +08001434static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001435{
Sheng Yang04547152009-04-01 15:52:31 +08001436 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001437}
1438
Paolo Bonzini35754c92015-07-29 12:05:37 +02001439static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001440{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001441 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001442}
1443
Gui Jianfeng31299942010-03-15 17:29:09 +08001444static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001445{
Sheng Yang04547152009-04-01 15:52:31 +08001446 return vmcs_config.cpu_based_exec_ctrl &
1447 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001448}
1449
Avi Kivity774ead32007-12-26 13:57:04 +02001450static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001451{
Sheng Yang04547152009-04-01 15:52:31 +08001452 return vmcs_config.cpu_based_2nd_exec_ctrl &
1453 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1454}
1455
Yang Zhang8d146952013-01-25 10:18:50 +08001456static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1457{
1458 return vmcs_config.cpu_based_2nd_exec_ctrl &
1459 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1460}
1461
Yang Zhang83d4c282013-01-25 10:18:49 +08001462static inline bool cpu_has_vmx_apic_register_virt(void)
1463{
1464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1466}
1467
Yang Zhangc7c9c562013-01-25 10:18:51 +08001468static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1469{
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1472}
1473
Yunhong Jiang64672c92016-06-13 14:19:59 -07001474/*
1475 * Comment's format: document - errata name - stepping - processor name.
1476 * Refer from
1477 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1478 */
1479static u32 vmx_preemption_cpu_tfms[] = {
1480/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
14810x000206E6,
1482/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1483/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1484/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
14850x00020652,
1486/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
14870x00020655,
1488/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1489/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1490/*
1491 * 320767.pdf - AAP86 - B1 -
1492 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1493 */
14940x000106E5,
1495/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
14960x000106A0,
1497/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
14980x000106A1,
1499/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15000x000106A4,
1501 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1502 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1503 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15040x000106A5,
1505};
1506
1507static inline bool cpu_has_broken_vmx_preemption_timer(void)
1508{
1509 u32 eax = cpuid_eax(0x00000001), i;
1510
1511 /* Clear the reserved bits */
1512 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001513 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001514 if (eax == vmx_preemption_cpu_tfms[i])
1515 return true;
1516
1517 return false;
1518}
1519
1520static inline bool cpu_has_vmx_preemption_timer(void)
1521{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001522 return vmcs_config.pin_based_exec_ctrl &
1523 PIN_BASED_VMX_PREEMPTION_TIMER;
1524}
1525
Yang Zhang01e439b2013-04-11 19:25:12 +08001526static inline bool cpu_has_vmx_posted_intr(void)
1527{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001528 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1529 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001530}
1531
1532static inline bool cpu_has_vmx_apicv(void)
1533{
1534 return cpu_has_vmx_apic_register_virt() &&
1535 cpu_has_vmx_virtual_intr_delivery() &&
1536 cpu_has_vmx_posted_intr();
1537}
1538
Sheng Yang04547152009-04-01 15:52:31 +08001539static inline bool cpu_has_vmx_flexpriority(void)
1540{
1541 return cpu_has_vmx_tpr_shadow() &&
1542 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001543}
1544
Marcelo Tosattie7997942009-06-11 12:07:40 -03001545static inline bool cpu_has_vmx_ept_execute_only(void)
1546{
Gui Jianfeng31299942010-03-15 17:29:09 +08001547 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001548}
1549
Marcelo Tosattie7997942009-06-11 12:07:40 -03001550static inline bool cpu_has_vmx_ept_2m_page(void)
1551{
Gui Jianfeng31299942010-03-15 17:29:09 +08001552 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001553}
1554
Sheng Yang878403b2010-01-05 19:02:29 +08001555static inline bool cpu_has_vmx_ept_1g_page(void)
1556{
Gui Jianfeng31299942010-03-15 17:29:09 +08001557 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001558}
1559
Sheng Yang4bc9b982010-06-02 14:05:24 +08001560static inline bool cpu_has_vmx_ept_4levels(void)
1561{
1562 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1563}
1564
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001565static inline bool cpu_has_vmx_ept_mt_wb(void)
1566{
1567 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1568}
1569
Yu Zhang855feb62017-08-24 20:27:55 +08001570static inline bool cpu_has_vmx_ept_5levels(void)
1571{
1572 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1573}
1574
Xudong Hao83c3a332012-05-28 19:33:35 +08001575static inline bool cpu_has_vmx_ept_ad_bits(void)
1576{
1577 return vmx_capability.ept & VMX_EPT_AD_BIT;
1578}
1579
Gui Jianfeng31299942010-03-15 17:29:09 +08001580static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001581{
Gui Jianfeng31299942010-03-15 17:29:09 +08001582 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001583}
1584
Gui Jianfeng31299942010-03-15 17:29:09 +08001585static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001586{
Gui Jianfeng31299942010-03-15 17:29:09 +08001587 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001588}
1589
Liran Aloncd9a4912018-05-22 17:16:15 +03001590static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1591{
1592 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1593}
1594
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001595static inline bool cpu_has_vmx_invvpid_single(void)
1596{
1597 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1598}
1599
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001600static inline bool cpu_has_vmx_invvpid_global(void)
1601{
1602 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1603}
1604
Wanpeng Li08d839c2017-03-23 05:30:08 -07001605static inline bool cpu_has_vmx_invvpid(void)
1606{
1607 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1608}
1609
Gui Jianfeng31299942010-03-15 17:29:09 +08001610static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001611{
Sheng Yang04547152009-04-01 15:52:31 +08001612 return vmcs_config.cpu_based_2nd_exec_ctrl &
1613 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001617{
1618 return vmcs_config.cpu_based_2nd_exec_ctrl &
1619 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1620}
1621
Gui Jianfeng31299942010-03-15 17:29:09 +08001622static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001623{
1624 return vmcs_config.cpu_based_2nd_exec_ctrl &
1625 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1626}
1627
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001628static inline bool cpu_has_vmx_basic_inout(void)
1629{
1630 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1631}
1632
Paolo Bonzini35754c92015-07-29 12:05:37 +02001633static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001634{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001635 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001636}
1637
Gui Jianfeng31299942010-03-15 17:29:09 +08001638static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001639{
Sheng Yang04547152009-04-01 15:52:31 +08001640 return vmcs_config.cpu_based_2nd_exec_ctrl &
1641 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001642}
1643
Gui Jianfeng31299942010-03-15 17:29:09 +08001644static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001645{
1646 return vmcs_config.cpu_based_2nd_exec_ctrl &
1647 SECONDARY_EXEC_RDTSCP;
1648}
1649
Mao, Junjiead756a12012-07-02 01:18:48 +00001650static inline bool cpu_has_vmx_invpcid(void)
1651{
1652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_ENABLE_INVPCID;
1654}
1655
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001656static inline bool cpu_has_virtual_nmis(void)
1657{
1658 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1659}
1660
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001661static inline bool cpu_has_vmx_wbinvd_exit(void)
1662{
1663 return vmcs_config.cpu_based_2nd_exec_ctrl &
1664 SECONDARY_EXEC_WBINVD_EXITING;
1665}
1666
Abel Gordonabc4fc52013-04-18 14:35:25 +03001667static inline bool cpu_has_vmx_shadow_vmcs(void)
1668{
1669 u64 vmx_msr;
1670 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1671 /* check if the cpu supports writing r/o exit information fields */
1672 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1673 return false;
1674
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_SHADOW_VMCS;
1677}
1678
Kai Huang843e4332015-01-28 10:54:28 +08001679static inline bool cpu_has_vmx_pml(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1682}
1683
Haozhong Zhang64903d62015-10-20 15:39:09 +08001684static inline bool cpu_has_vmx_tsc_scaling(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_TSC_SCALING;
1688}
1689
Bandan Das2a499e42017-08-03 15:54:41 -04001690static inline bool cpu_has_vmx_vmfunc(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_VMFUNC;
1694}
1695
Sean Christopherson64f7a112018-04-30 10:01:06 -07001696static bool vmx_umip_emulated(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_DESC;
1700}
1701
Sheng Yang04547152009-04-01 15:52:31 +08001702static inline bool report_flexpriority(void)
1703{
1704 return flexpriority_enabled;
1705}
1706
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001707static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1708{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001709 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001710}
1711
Jim Mattsonf4160e42018-05-29 09:11:33 -07001712/*
1713 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1714 * to modify any valid field of the VMCS, or are the VM-exit
1715 * information fields read-only?
1716 */
1717static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1718{
1719 return to_vmx(vcpu)->nested.msrs.misc_low &
1720 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1721}
1722
Marc Orr04473782018-06-20 17:21:29 -07001723static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1724{
1725 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1726}
1727
1728static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1729{
1730 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1731 CPU_BASED_MONITOR_TRAP_FLAG;
1732}
1733
Liran Alonfa97d7d2018-07-18 14:07:59 +02001734static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1735{
1736 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1737 SECONDARY_EXEC_SHADOW_VMCS;
1738}
1739
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001740static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1741{
1742 return vmcs12->cpu_based_vm_exec_control & bit;
1743}
1744
1745static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1746{
1747 return (vmcs12->cpu_based_vm_exec_control &
1748 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1749 (vmcs12->secondary_vm_exec_control & bit);
1750}
1751
Jan Kiszkaf41245002014-03-07 20:03:13 +01001752static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1753{
1754 return vmcs12->pin_based_vm_exec_control &
1755 PIN_BASED_VMX_PREEMPTION_TIMER;
1756}
1757
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001758static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1759{
1760 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1761}
1762
1763static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1764{
1765 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1766}
1767
Nadav Har'El155a97a2013-08-05 11:07:16 +03001768static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1769{
1770 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1771}
1772
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001773static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1774{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001775 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001776}
1777
Bandan Dasc5f983f2017-05-05 15:25:14 -04001778static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1779{
1780 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1781}
1782
Wincy Vanf2b93282015-02-03 23:56:03 +08001783static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1784{
1785 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1786}
1787
Wanpeng Li5c614b32015-10-13 09:18:36 -07001788static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1789{
1790 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1791}
1792
Wincy Van82f0dd42015-02-03 23:57:18 +08001793static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1794{
1795 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1796}
1797
Wincy Van608406e2015-02-03 23:57:51 +08001798static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1799{
1800 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1801}
1802
Wincy Van705699a2015-02-03 23:58:17 +08001803static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1804{
1805 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1806}
1807
Bandan Das27c42a12017-08-03 15:54:42 -04001808static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1809{
1810 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1811}
1812
Bandan Das41ab9372017-08-03 15:54:43 -04001813static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1814{
1815 return nested_cpu_has_vmfunc(vmcs12) &&
1816 (vmcs12->vm_function_control &
1817 VMX_VMFUNC_EPTP_SWITCHING);
1818}
1819
Liran Alonf792d272018-06-23 02:35:05 +03001820static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1821{
1822 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1823}
1824
Jim Mattsonef85b672016-12-12 11:01:37 -08001825static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001826{
1827 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001828 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001829}
1830
Jan Kiszka533558b2014-01-04 18:47:20 +01001831static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1832 u32 exit_intr_info,
1833 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001834static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1835 struct vmcs12 *vmcs12,
1836 u32 reason, unsigned long qualification);
1837
Rusty Russell8b9cf982007-07-30 16:31:43 +10001838static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001839{
1840 int i;
1841
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001842 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001843 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001844 return i;
1845 return -1;
1846}
1847
Sheng Yang2384d2b2008-01-17 15:14:33 +08001848static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1849{
1850 struct {
1851 u64 vpid : 16;
1852 u64 rsvd : 48;
1853 u64 gva;
1854 } operand = { vpid, 0, gva };
1855
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001856 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001857 /* CF==1 or ZF==1 --> rc = -1 */
1858 "; ja 1f ; ud2 ; 1:"
1859 : : "a"(&operand), "c"(ext) : "cc", "memory");
1860}
1861
Sheng Yang14394422008-04-28 12:24:45 +08001862static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1863{
1864 struct {
1865 u64 eptp, gpa;
1866 } operand = {eptp, gpa};
1867
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001868 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001869 /* CF==1 or ZF==1 --> rc = -1 */
1870 "; ja 1f ; ud2 ; 1:\n"
1871 : : "a" (&operand), "c" (ext) : "cc", "memory");
1872}
1873
Avi Kivity26bb0982009-09-07 11:14:12 +03001874static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001875{
1876 int i;
1877
Rusty Russell8b9cf982007-07-30 16:31:43 +10001878 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001879 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001880 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001881 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001882}
1883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884static void vmcs_clear(struct vmcs *vmcs)
1885{
1886 u64 phys_addr = __pa(vmcs);
1887 u8 error;
1888
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001889 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001890 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 : "cc", "memory");
1892 if (error)
1893 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1894 vmcs, phys_addr);
1895}
1896
Nadav Har'Eld462b812011-05-24 15:26:10 +03001897static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1898{
1899 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001900 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1901 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001902 loaded_vmcs->cpu = -1;
1903 loaded_vmcs->launched = 0;
1904}
1905
Dongxiao Xu7725b892010-05-11 18:29:38 +08001906static void vmcs_load(struct vmcs *vmcs)
1907{
1908 u64 phys_addr = __pa(vmcs);
1909 u8 error;
1910
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001911 if (static_branch_unlikely(&enable_evmcs))
1912 return evmcs_load(phys_addr);
1913
Dongxiao Xu7725b892010-05-11 18:29:38 +08001914 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001915 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001916 : "cc", "memory");
1917 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001918 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001919 vmcs, phys_addr);
1920}
1921
Dave Young2965faa2015-09-09 15:38:55 -07001922#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001923/*
1924 * This bitmap is used to indicate whether the vmclear
1925 * operation is enabled on all cpus. All disabled by
1926 * default.
1927 */
1928static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1929
1930static inline void crash_enable_local_vmclear(int cpu)
1931{
1932 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1933}
1934
1935static inline void crash_disable_local_vmclear(int cpu)
1936{
1937 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1938}
1939
1940static inline int crash_local_vmclear_enabled(int cpu)
1941{
1942 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1943}
1944
1945static void crash_vmclear_local_loaded_vmcss(void)
1946{
1947 int cpu = raw_smp_processor_id();
1948 struct loaded_vmcs *v;
1949
1950 if (!crash_local_vmclear_enabled(cpu))
1951 return;
1952
1953 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1954 loaded_vmcss_on_cpu_link)
1955 vmcs_clear(v->vmcs);
1956}
1957#else
1958static inline void crash_enable_local_vmclear(int cpu) { }
1959static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001960#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001961
Nadav Har'Eld462b812011-05-24 15:26:10 +03001962static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001964 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001965 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966
Nadav Har'Eld462b812011-05-24 15:26:10 +03001967 if (loaded_vmcs->cpu != cpu)
1968 return; /* vcpu migration can race with cpu offline */
1969 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001971 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001972 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001973
1974 /*
1975 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1976 * is before setting loaded_vmcs->vcpu to -1 which is done in
1977 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1978 * then adds the vmcs into percpu list before it is deleted.
1979 */
1980 smp_wmb();
1981
Nadav Har'Eld462b812011-05-24 15:26:10 +03001982 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001983 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984}
1985
Nadav Har'Eld462b812011-05-24 15:26:10 +03001986static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001987{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001988 int cpu = loaded_vmcs->cpu;
1989
1990 if (cpu != -1)
1991 smp_call_function_single(cpu,
1992 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001993}
1994
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001995static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001996{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001997 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001998 return;
1999
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002000 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002001 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002002}
2003
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002004static inline void vpid_sync_vcpu_global(void)
2005{
2006 if (cpu_has_vmx_invvpid_global())
2007 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2008}
2009
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002010static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002011{
2012 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002013 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002014 else
2015 vpid_sync_vcpu_global();
2016}
2017
Sheng Yang14394422008-04-28 12:24:45 +08002018static inline void ept_sync_global(void)
2019{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002020 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002021}
2022
2023static inline void ept_sync_context(u64 eptp)
2024{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002025 if (cpu_has_vmx_invept_context())
2026 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2027 else
2028 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002029}
2030
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002031static __always_inline void vmcs_check16(unsigned long field)
2032{
2033 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2034 "16-bit accessor invalid for 64-bit field");
2035 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2036 "16-bit accessor invalid for 64-bit high field");
2037 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2038 "16-bit accessor invalid for 32-bit high field");
2039 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2040 "16-bit accessor invalid for natural width field");
2041}
2042
2043static __always_inline void vmcs_check32(unsigned long field)
2044{
2045 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2046 "32-bit accessor invalid for 16-bit field");
2047 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2048 "32-bit accessor invalid for natural width field");
2049}
2050
2051static __always_inline void vmcs_check64(unsigned long field)
2052{
2053 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2054 "64-bit accessor invalid for 16-bit field");
2055 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2056 "64-bit accessor invalid for 64-bit high field");
2057 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2058 "64-bit accessor invalid for 32-bit field");
2059 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2060 "64-bit accessor invalid for natural width field");
2061}
2062
2063static __always_inline void vmcs_checkl(unsigned long field)
2064{
2065 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2066 "Natural width accessor invalid for 16-bit field");
2067 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2068 "Natural width accessor invalid for 64-bit field");
2069 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2070 "Natural width accessor invalid for 64-bit high field");
2071 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2072 "Natural width accessor invalid for 32-bit field");
2073}
2074
2075static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076{
Avi Kivity5e520e62011-05-15 10:13:12 -04002077 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Avi Kivity5e520e62011-05-15 10:13:12 -04002079 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2080 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002081 return value;
2082}
2083
Avi Kivity96304212011-05-15 10:13:13 -04002084static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002086 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002087 if (static_branch_unlikely(&enable_evmcs))
2088 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002089 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090}
2091
Avi Kivity96304212011-05-15 10:13:13 -04002092static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002094 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002095 if (static_branch_unlikely(&enable_evmcs))
2096 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002097 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098}
2099
Avi Kivity96304212011-05-15 10:13:13 -04002100static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002102 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002103 if (static_branch_unlikely(&enable_evmcs))
2104 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002105#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002108 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109#endif
2110}
2111
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002112static __always_inline unsigned long vmcs_readl(unsigned long field)
2113{
2114 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002115 if (static_branch_unlikely(&enable_evmcs))
2116 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002117 return __vmcs_readl(field);
2118}
2119
Avi Kivitye52de1b2007-01-05 16:36:56 -08002120static noinline void vmwrite_error(unsigned long field, unsigned long value)
2121{
2122 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2123 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2124 dump_stack();
2125}
2126
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002127static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128{
2129 u8 error;
2130
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002131 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002132 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002133 if (unlikely(error))
2134 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002137static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002139 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002140 if (static_branch_unlikely(&enable_evmcs))
2141 return evmcs_write16(field, value);
2142
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002143 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144}
2145
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002146static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002148 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002149 if (static_branch_unlikely(&enable_evmcs))
2150 return evmcs_write32(field, value);
2151
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002152 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002157 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002158 if (static_branch_unlikely(&enable_evmcs))
2159 return evmcs_write64(field, value);
2160
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002161 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002162#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165#endif
2166}
2167
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002168static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002169{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002170 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002171 if (static_branch_unlikely(&enable_evmcs))
2172 return evmcs_write64(field, value);
2173
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002174 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002175}
2176
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002178{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002179 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2180 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002181 if (static_branch_unlikely(&enable_evmcs))
2182 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2183
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002184 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2185}
2186
2187static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2188{
2189 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2190 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002191 if (static_branch_unlikely(&enable_evmcs))
2192 return evmcs_write32(field, evmcs_read32(field) | mask);
2193
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002194 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002195}
2196
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002197static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2198{
2199 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2200}
2201
Gleb Natapov2961e8762013-11-25 15:37:13 +02002202static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2203{
2204 vmcs_write32(VM_ENTRY_CONTROLS, val);
2205 vmx->vm_entry_controls_shadow = val;
2206}
2207
2208static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2209{
2210 if (vmx->vm_entry_controls_shadow != val)
2211 vm_entry_controls_init(vmx, val);
2212}
2213
2214static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2215{
2216 return vmx->vm_entry_controls_shadow;
2217}
2218
2219
2220static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2221{
2222 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2223}
2224
2225static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2226{
2227 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2228}
2229
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002230static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2231{
2232 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2233}
2234
Gleb Natapov2961e8762013-11-25 15:37:13 +02002235static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2236{
2237 vmcs_write32(VM_EXIT_CONTROLS, val);
2238 vmx->vm_exit_controls_shadow = val;
2239}
2240
2241static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2242{
2243 if (vmx->vm_exit_controls_shadow != val)
2244 vm_exit_controls_init(vmx, val);
2245}
2246
2247static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2248{
2249 return vmx->vm_exit_controls_shadow;
2250}
2251
2252
2253static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2254{
2255 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2256}
2257
2258static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2259{
2260 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2261}
2262
Avi Kivity2fb92db2011-04-27 19:42:18 +03002263static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2264{
2265 vmx->segment_cache.bitmask = 0;
2266}
2267
2268static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2269 unsigned field)
2270{
2271 bool ret;
2272 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2273
2274 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2275 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2276 vmx->segment_cache.bitmask = 0;
2277 }
2278 ret = vmx->segment_cache.bitmask & mask;
2279 vmx->segment_cache.bitmask |= mask;
2280 return ret;
2281}
2282
2283static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2284{
2285 u16 *p = &vmx->segment_cache.seg[seg].selector;
2286
2287 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2288 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2289 return *p;
2290}
2291
2292static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2293{
2294 ulong *p = &vmx->segment_cache.seg[seg].base;
2295
2296 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2297 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2298 return *p;
2299}
2300
2301static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2302{
2303 u32 *p = &vmx->segment_cache.seg[seg].limit;
2304
2305 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2306 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2307 return *p;
2308}
2309
2310static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2311{
2312 u32 *p = &vmx->segment_cache.seg[seg].ar;
2313
2314 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2315 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2316 return *p;
2317}
2318
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002319static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2320{
2321 u32 eb;
2322
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002323 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002324 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002325 /*
2326 * Guest access to VMware backdoor ports could legitimately
2327 * trigger #GP because of TSS I/O permission bitmap.
2328 * We intercept those #GP and allow access to them anyway
2329 * as VMware does.
2330 */
2331 if (enable_vmware_backdoor)
2332 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002333 if ((vcpu->guest_debug &
2334 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2335 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2336 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002337 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002338 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002339 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002340 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002341
2342 /* When we are running a nested L2 guest and L1 specified for it a
2343 * certain exception bitmap, we must trap the same exceptions and pass
2344 * them to L1. When running L2, we will only handle the exceptions
2345 * specified above if L1 did not want them.
2346 */
2347 if (is_guest_mode(vcpu))
2348 eb |= get_vmcs12(vcpu)->exception_bitmap;
2349
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002350 vmcs_write32(EXCEPTION_BITMAP, eb);
2351}
2352
Ashok Raj15d45072018-02-01 22:59:43 +01002353/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002354 * Check if MSR is intercepted for currently loaded MSR bitmap.
2355 */
2356static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2357{
2358 unsigned long *msr_bitmap;
2359 int f = sizeof(unsigned long);
2360
2361 if (!cpu_has_vmx_msr_bitmap())
2362 return true;
2363
2364 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2365
2366 if (msr <= 0x1fff) {
2367 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2368 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2369 msr &= 0x1fff;
2370 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2371 }
2372
2373 return true;
2374}
2375
2376/*
Ashok Raj15d45072018-02-01 22:59:43 +01002377 * Check if MSR is intercepted for L01 MSR bitmap.
2378 */
2379static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2380{
2381 unsigned long *msr_bitmap;
2382 int f = sizeof(unsigned long);
2383
2384 if (!cpu_has_vmx_msr_bitmap())
2385 return true;
2386
2387 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2388
2389 if (msr <= 0x1fff) {
2390 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2391 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2392 msr &= 0x1fff;
2393 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2394 }
2395
2396 return true;
2397}
2398
Gleb Natapov2961e8762013-11-25 15:37:13 +02002399static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2400 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002401{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002402 vm_entry_controls_clearbit(vmx, entry);
2403 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002404}
2405
Avi Kivity61d2ef22010-04-28 16:40:38 +03002406static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2407{
2408 unsigned i;
2409 struct msr_autoload *m = &vmx->msr_autoload;
2410
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002411 switch (msr) {
2412 case MSR_EFER:
2413 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002414 clear_atomic_switch_msr_special(vmx,
2415 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002416 VM_EXIT_LOAD_IA32_EFER);
2417 return;
2418 }
2419 break;
2420 case MSR_CORE_PERF_GLOBAL_CTRL:
2421 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002422 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002423 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2424 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2425 return;
2426 }
2427 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002428 }
2429
Avi Kivity61d2ef22010-04-28 16:40:38 +03002430 for (i = 0; i < m->nr; ++i)
2431 if (m->guest[i].index == msr)
2432 break;
2433
2434 if (i == m->nr)
2435 return;
2436 --m->nr;
2437 m->guest[i] = m->guest[m->nr];
2438 m->host[i] = m->host[m->nr];
2439 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2440 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2441}
2442
Gleb Natapov2961e8762013-11-25 15:37:13 +02002443static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2444 unsigned long entry, unsigned long exit,
2445 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2446 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002447{
2448 vmcs_write64(guest_val_vmcs, guest_val);
2449 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002450 vm_entry_controls_setbit(vmx, entry);
2451 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002452}
2453
Avi Kivity61d2ef22010-04-28 16:40:38 +03002454static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2455 u64 guest_val, u64 host_val)
2456{
2457 unsigned i;
2458 struct msr_autoload *m = &vmx->msr_autoload;
2459
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002460 switch (msr) {
2461 case MSR_EFER:
2462 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002463 add_atomic_switch_msr_special(vmx,
2464 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002465 VM_EXIT_LOAD_IA32_EFER,
2466 GUEST_IA32_EFER,
2467 HOST_IA32_EFER,
2468 guest_val, host_val);
2469 return;
2470 }
2471 break;
2472 case MSR_CORE_PERF_GLOBAL_CTRL:
2473 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002474 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002475 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2476 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2477 GUEST_IA32_PERF_GLOBAL_CTRL,
2478 HOST_IA32_PERF_GLOBAL_CTRL,
2479 guest_val, host_val);
2480 return;
2481 }
2482 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002483 case MSR_IA32_PEBS_ENABLE:
2484 /* PEBS needs a quiescent period after being disabled (to write
2485 * a record). Disabling PEBS through VMX MSR swapping doesn't
2486 * provide that period, so a CPU could write host's record into
2487 * guest's memory.
2488 */
2489 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002490 }
2491
Avi Kivity61d2ef22010-04-28 16:40:38 +03002492 for (i = 0; i < m->nr; ++i)
2493 if (m->guest[i].index == msr)
2494 break;
2495
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002496 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002497 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002498 "Can't add msr %x\n", msr);
2499 return;
2500 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002501 ++m->nr;
2502 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2503 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2504 }
2505
2506 m->guest[i].index = msr;
2507 m->guest[i].value = guest_val;
2508 m->host[i].index = msr;
2509 m->host[i].value = host_val;
2510}
2511
Avi Kivity92c0d902009-10-29 11:00:16 +02002512static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002513{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002514 u64 guest_efer = vmx->vcpu.arch.efer;
2515 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002516
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002517 if (!enable_ept) {
2518 /*
2519 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2520 * host CPUID is more efficient than testing guest CPUID
2521 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2522 */
2523 if (boot_cpu_has(X86_FEATURE_SMEP))
2524 guest_efer |= EFER_NX;
2525 else if (!(guest_efer & EFER_NX))
2526 ignore_bits |= EFER_NX;
2527 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002528
Avi Kivity51c6cf62007-08-29 03:48:05 +03002529 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002530 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002531 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002532 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002533#ifdef CONFIG_X86_64
2534 ignore_bits |= EFER_LMA | EFER_LME;
2535 /* SCE is meaningful only in long mode on Intel */
2536 if (guest_efer & EFER_LMA)
2537 ignore_bits &= ~(u64)EFER_SCE;
2538#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002539
2540 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002541
2542 /*
2543 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2544 * On CPUs that support "load IA32_EFER", always switch EFER
2545 * atomically, since it's faster than switching it manually.
2546 */
2547 if (cpu_has_load_ia32_efer ||
2548 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002549 if (!(guest_efer & EFER_LMA))
2550 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002551 if (guest_efer != host_efer)
2552 add_atomic_switch_msr(vmx, MSR_EFER,
2553 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002554 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002555 } else {
2556 guest_efer &= ~ignore_bits;
2557 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002558
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002559 vmx->guest_msrs[efer_offset].data = guest_efer;
2560 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2561
2562 return true;
2563 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002564}
2565
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002566#ifdef CONFIG_X86_32
2567/*
2568 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2569 * VMCS rather than the segment table. KVM uses this helper to figure
2570 * out the current bases to poke them into the VMCS before entry.
2571 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002572static unsigned long segment_base(u16 selector)
2573{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002574 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002575 unsigned long v;
2576
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002577 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002578 return 0;
2579
Thomas Garnier45fc8752017-03-14 10:05:08 -07002580 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002581
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002582 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002583 u16 ldt_selector = kvm_read_ldt();
2584
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002585 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002586 return 0;
2587
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002588 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002589 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002590 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002591 return v;
2592}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002593#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002594
Avi Kivity04d2cc72007-09-10 18:10:54 +03002595static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002596{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002598#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002599 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002600 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002601#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002602 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002604 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002605 return;
2606
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002607 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002608 /*
2609 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2610 * allow segment selectors with cpl > 0 or ti == 1.
2611 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002612 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002613 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002614
2615#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002616 if (likely(is_64bit_mm(current->mm))) {
2617 save_fsgs_for_kvm();
2618 vmx->host_state.fs_sel = current->thread.fsindex;
2619 vmx->host_state.gs_sel = current->thread.gsindex;
2620 fs_base = current->thread.fsbase;
2621 kernel_gs_base = current->thread.gsbase;
2622 } else {
2623#endif
2624 savesegment(fs, vmx->host_state.fs_sel);
2625 savesegment(gs, vmx->host_state.gs_sel);
2626#ifdef CONFIG_X86_64
2627 fs_base = read_msr(MSR_FS_BASE);
2628 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2629 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002630#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002631 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002633 vmx->host_state.fs_reload_needed = 0;
2634 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002635 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002636 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002637 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002638 if (!(vmx->host_state.gs_sel & 7))
2639 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002640 else {
2641 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002642 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002643 }
2644
2645#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002646 savesegment(ds, vmx->host_state.ds_sel);
2647 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002648
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002649 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002650 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002651
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002652 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002653 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002654 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002655#else
2656 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2657 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2658#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002659 for (i = 0; i < vmx->save_nmsrs; ++i)
2660 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002661 vmx->guest_msrs[i].data,
2662 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002663}
2664
Avi Kivitya9b21b62008-06-24 11:48:49 +03002665static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002666{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002667 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002668 return;
2669
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002670 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002671 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002672#ifdef CONFIG_X86_64
2673 if (is_long_mode(&vmx->vcpu))
2674 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2675#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002676 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002677 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002678#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002679 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002680#else
2681 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002682#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002683 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002684 if (vmx->host_state.fs_reload_needed)
2685 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002686#ifdef CONFIG_X86_64
2687 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2688 loadsegment(ds, vmx->host_state.ds_sel);
2689 loadsegment(es, vmx->host_state.es_sel);
2690 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002691#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002692 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002693#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002694 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002695#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002696 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002697}
2698
Avi Kivitya9b21b62008-06-24 11:48:49 +03002699static void vmx_load_host_state(struct vcpu_vmx *vmx)
2700{
2701 preempt_disable();
2702 __vmx_load_host_state(vmx);
2703 preempt_enable();
2704}
2705
Feng Wu28b835d2015-09-18 22:29:54 +08002706static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2707{
2708 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2709 struct pi_desc old, new;
2710 unsigned int dest;
2711
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002712 /*
2713 * In case of hot-plug or hot-unplug, we may have to undo
2714 * vmx_vcpu_pi_put even if there is no assigned device. And we
2715 * always keep PI.NDST up to date for simplicity: it makes the
2716 * code easier, and CPU migration is not a fast path.
2717 */
2718 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002719 return;
2720
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002721 /*
2722 * First handle the simple case where no cmpxchg is necessary; just
2723 * allow posting non-urgent interrupts.
2724 *
2725 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2726 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2727 * expects the VCPU to be on the blocked_vcpu_list that matches
2728 * PI.NDST.
2729 */
2730 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2731 vcpu->cpu == cpu) {
2732 pi_clear_sn(pi_desc);
2733 return;
2734 }
2735
2736 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002737 do {
2738 old.control = new.control = pi_desc->control;
2739
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002740 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002741
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002742 if (x2apic_enabled())
2743 new.ndst = dest;
2744 else
2745 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002746
Feng Wu28b835d2015-09-18 22:29:54 +08002747 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002748 } while (cmpxchg64(&pi_desc->control, old.control,
2749 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002750}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002751
Peter Feinerc95ba922016-08-17 09:36:47 -07002752static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2753{
2754 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2755 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2756}
2757
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758/*
2759 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2760 * vcpu mutex is already taken.
2761 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002762static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002764 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002765 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002767 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002768 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002769 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002770 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002771
2772 /*
2773 * Read loaded_vmcs->cpu should be before fetching
2774 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2775 * See the comments in __loaded_vmcs_clear().
2776 */
2777 smp_rmb();
2778
Nadav Har'Eld462b812011-05-24 15:26:10 +03002779 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2780 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002781 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002782 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002783 }
2784
2785 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2786 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2787 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002788 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002789 }
2790
2791 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002792 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002793 unsigned long sysenter_esp;
2794
2795 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002796
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 /*
2798 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002799 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002801 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002802 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002803 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002805 /*
2806 * VM exits change the host TR limit to 0x67 after a VM
2807 * exit. This is okay, since 0x67 covers everything except
2808 * the IO bitmap and have have code to handle the IO bitmap
2809 * being lost after a VM exit.
2810 */
2811 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2812
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2814 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002815
Nadav Har'Eld462b812011-05-24 15:26:10 +03002816 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 }
Feng Wu28b835d2015-09-18 22:29:54 +08002818
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002819 /* Setup TSC multiplier */
2820 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002821 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2822 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002823
Feng Wu28b835d2015-09-18 22:29:54 +08002824 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002825 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002826 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002827}
2828
2829static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2830{
2831 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2832
2833 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002834 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2835 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002836 return;
2837
2838 /* Set SN when the vCPU is preempted */
2839 if (vcpu->preempted)
2840 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841}
2842
2843static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2844{
Feng Wu28b835d2015-09-18 22:29:54 +08002845 vmx_vcpu_pi_put(vcpu);
2846
Avi Kivitya9b21b62008-06-24 11:48:49 +03002847 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848}
2849
Wanpeng Lif244dee2017-07-20 01:11:54 -07002850static bool emulation_required(struct kvm_vcpu *vcpu)
2851{
2852 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2853}
2854
Avi Kivityedcafe32009-12-30 18:07:40 +02002855static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2856
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002857/*
2858 * Return the cr0 value that a nested guest would read. This is a combination
2859 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2860 * its hypervisor (cr0_read_shadow).
2861 */
2862static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2863{
2864 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2865 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2866}
2867static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2868{
2869 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2870 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2871}
2872
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2874{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002875 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002876
Avi Kivity6de12732011-03-07 12:51:22 +02002877 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2878 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2879 rflags = vmcs_readl(GUEST_RFLAGS);
2880 if (to_vmx(vcpu)->rmode.vm86_active) {
2881 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2882 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2883 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2884 }
2885 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002886 }
Avi Kivity6de12732011-03-07 12:51:22 +02002887 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888}
2889
2890static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2891{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002892 unsigned long old_rflags = vmx_get_rflags(vcpu);
2893
Avi Kivity6de12732011-03-07 12:51:22 +02002894 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2895 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002896 if (to_vmx(vcpu)->rmode.vm86_active) {
2897 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002898 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002899 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002901
2902 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2903 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904}
2905
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002906static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002907{
2908 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2909 int ret = 0;
2910
2911 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002912 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002913 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002914 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002915
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002916 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002917}
2918
2919static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2920{
2921 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2922 u32 interruptibility = interruptibility_old;
2923
2924 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2925
Jan Kiszka48005f62010-02-19 19:38:07 +01002926 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002927 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002928 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002929 interruptibility |= GUEST_INTR_STATE_STI;
2930
2931 if ((interruptibility != interruptibility_old))
2932 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2933}
2934
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2936{
2937 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002939 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002941 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942
Glauber Costa2809f5d2009-05-12 16:21:05 -04002943 /* skipping an emulated instruction also counts */
2944 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945}
2946
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002947static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2948 unsigned long exit_qual)
2949{
2950 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2951 unsigned int nr = vcpu->arch.exception.nr;
2952 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2953
2954 if (vcpu->arch.exception.has_error_code) {
2955 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2956 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2957 }
2958
2959 if (kvm_exception_is_soft(nr))
2960 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2961 else
2962 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2963
2964 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2965 vmx_get_nmi_mask(vcpu))
2966 intr_info |= INTR_INFO_UNBLOCK_NMI;
2967
2968 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2969}
2970
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002971/*
2972 * KVM wants to inject page-faults which it got to the guest. This function
2973 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002974 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002975static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002976{
2977 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002978 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002979
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002980 if (nr == PF_VECTOR) {
2981 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002982 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002983 return 1;
2984 }
2985 /*
2986 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2987 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2988 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2989 * can be written only when inject_pending_event runs. This should be
2990 * conditional on a new capability---if the capability is disabled,
2991 * kvm_multiple_exception would write the ancillary information to
2992 * CR2 or DR6, for backwards ABI-compatibility.
2993 */
2994 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2995 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002996 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002997 return 1;
2998 }
2999 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003000 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003001 if (nr == DB_VECTOR)
3002 *exit_qual = vcpu->arch.dr6;
3003 else
3004 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003005 return 1;
3006 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003007 }
3008
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003009 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003010}
3011
Wanpeng Licaa057a2018-03-12 04:53:03 -07003012static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3013{
3014 /*
3015 * Ensure that we clear the HLT state in the VMCS. We don't need to
3016 * explicitly skip the instruction because if the HLT state is set,
3017 * then the instruction is already executing and RIP has already been
3018 * advanced.
3019 */
3020 if (kvm_hlt_in_guest(vcpu->kvm) &&
3021 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3022 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3023}
3024
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003025static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003026{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003028 unsigned nr = vcpu->arch.exception.nr;
3029 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003030 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003031 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003032
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003033 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003034 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003035 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3036 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003037
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003038 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003039 int inc_eip = 0;
3040 if (kvm_exception_is_soft(nr))
3041 inc_eip = vcpu->arch.event_exit_inst_len;
3042 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003043 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003044 return;
3045 }
3046
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003047 WARN_ON_ONCE(vmx->emulation_required);
3048
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003049 if (kvm_exception_is_soft(nr)) {
3050 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3051 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003052 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3053 } else
3054 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3055
3056 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003057
3058 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003059}
3060
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003061static bool vmx_rdtscp_supported(void)
3062{
3063 return cpu_has_vmx_rdtscp();
3064}
3065
Mao, Junjiead756a12012-07-02 01:18:48 +00003066static bool vmx_invpcid_supported(void)
3067{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003068 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003069}
3070
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071/*
Eddie Donga75beee2007-05-17 18:55:15 +03003072 * Swap MSR entry in host/guest MSR entry array.
3073 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003074static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003075{
Avi Kivity26bb0982009-09-07 11:14:12 +03003076 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003077
3078 tmp = vmx->guest_msrs[to];
3079 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3080 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003081}
3082
3083/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003084 * Set up the vmcs to automatically save and restore system
3085 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3086 * mode, as fiddling with msrs is very expensive.
3087 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003088static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003089{
Avi Kivity26bb0982009-09-07 11:14:12 +03003090 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003091
Eddie Donga75beee2007-05-17 18:55:15 +03003092 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003093#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003094 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003095 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003096 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003097 move_msr_up(vmx, index, save_nmsrs++);
3098 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003099 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003100 move_msr_up(vmx, index, save_nmsrs++);
3101 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003102 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003103 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003104 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003105 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003106 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003107 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003108 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003109 * if efer.sce is enabled.
3110 */
Brian Gerst8c065852010-07-17 09:03:26 -04003111 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003112 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003113 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003114 }
Eddie Donga75beee2007-05-17 18:55:15 +03003115#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003116 index = __find_msr_index(vmx, MSR_EFER);
3117 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003118 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003119
Avi Kivity26bb0982009-09-07 11:14:12 +03003120 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003121
Yang Zhang8d146952013-01-25 10:18:50 +08003122 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003123 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003124}
3125
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003126static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003128 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003130 if (is_guest_mode(vcpu) &&
3131 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3132 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3133
3134 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135}
3136
3137/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003138 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003140static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003142 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003143 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003144 * We're here if L1 chose not to trap WRMSR to TSC. According
3145 * to the spec, this should set L1's TSC; The offset that L1
3146 * set for L2 remains unchanged, and still needs to be added
3147 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003148 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003149 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003150 /* recalculate vmcs02.TSC_OFFSET: */
3151 vmcs12 = get_vmcs12(vcpu);
3152 vmcs_write64(TSC_OFFSET, offset +
3153 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3154 vmcs12->tsc_offset : 0));
3155 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003156 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3157 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003158 vmcs_write64(TSC_OFFSET, offset);
3159 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160}
3161
Nadav Har'El801d3422011-05-25 23:02:23 +03003162/*
3163 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3164 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3165 * all guests if the "nested" module option is off, and can also be disabled
3166 * for a single guest by disabling its VMX cpuid bit.
3167 */
3168static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3169{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003170 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003171}
3172
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003174 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3175 * returned for the various VMX controls MSRs when nested VMX is enabled.
3176 * The same values should also be used to verify that vmcs12 control fields are
3177 * valid during nested entry from L1 to L2.
3178 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3179 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3180 * bit in the high half is on if the corresponding bit in the control field
3181 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003182 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003183static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003184{
Paolo Bonzini13893092018-02-26 13:40:09 +01003185 if (!nested) {
3186 memset(msrs, 0, sizeof(*msrs));
3187 return;
3188 }
3189
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003190 /*
3191 * Note that as a general rule, the high half of the MSRs (bits in
3192 * the control fields which may be 1) should be initialized by the
3193 * intersection of the underlying hardware's MSR (i.e., features which
3194 * can be supported) and the list of features we want to expose -
3195 * because they are known to be properly supported in our code.
3196 * Also, usually, the low half of the MSRs (bits which must be 1) can
3197 * be set to 0, meaning that L1 may turn off any of these bits. The
3198 * reason is that if one of these bits is necessary, it will appear
3199 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3200 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003201 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 * These rules have exceptions below.
3203 */
3204
3205 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003206 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003207 msrs->pinbased_ctls_low,
3208 msrs->pinbased_ctls_high);
3209 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003210 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003211 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003212 PIN_BASED_EXT_INTR_MASK |
3213 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003214 PIN_BASED_VIRTUAL_NMIS |
3215 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003217 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003218 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003219
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003220 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003221 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003222 msrs->exit_ctls_low,
3223 msrs->exit_ctls_high);
3224 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003225 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003226
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003229 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003231 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003232 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003233 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003234 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003235 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3236
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003237 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239
Jan Kiszka2996fca2014-06-16 13:59:43 +02003240 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003242
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003243 /* entry controls */
3244 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 msrs->entry_ctls_low,
3246 msrs->entry_ctls_high);
3247 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003248 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003249 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003250#ifdef CONFIG_X86_64
3251 VM_ENTRY_IA32E_MODE |
3252#endif
3253 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003254 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003255 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003256 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003257 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003258
Jan Kiszka2996fca2014-06-16 13:59:43 +02003259 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003260 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003261
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003262 /* cpu-based controls */
3263 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003264 msrs->procbased_ctls_low,
3265 msrs->procbased_ctls_high);
3266 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003267 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003268 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003269 CPU_BASED_VIRTUAL_INTR_PENDING |
3270 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3272 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3273 CPU_BASED_CR3_STORE_EXITING |
3274#ifdef CONFIG_X86_64
3275 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3276#endif
3277 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003278 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3279 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3280 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3281 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003282 /*
3283 * We can allow some features even when not supported by the
3284 * hardware. For example, L1 can specify an MSR bitmap - and we
3285 * can use it to avoid exits to L1 - even when L0 runs L2
3286 * without MSR bitmaps.
3287 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003288 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003289 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003290 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003291
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003292 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003293 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003294 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3295
Paolo Bonzini80154d72017-08-24 13:55:35 +02003296 /*
3297 * secondary cpu-based controls. Do not include those that
3298 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3299 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003300 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003301 msrs->secondary_ctls_low,
3302 msrs->secondary_ctls_high);
3303 msrs->secondary_ctls_low = 0;
3304 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003305 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003306 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003307 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003308 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003309 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003310 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003311 /*
3312 * We can emulate "VMCS shadowing," even if the hardware
3313 * doesn't support it.
3314 */
3315 msrs->secondary_ctls_high |=
3316 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003317
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003318 if (enable_ept) {
3319 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003320 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003321 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003323 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003324 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003326 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003327 msrs->ept_caps &= vmx_capability.ept;
3328 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003329 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3330 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003331 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003332 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003333 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003334 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003335 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003336 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003337
Bandan Das27c42a12017-08-03 15:54:42 -04003338 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003339 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003340 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003341 /*
3342 * Advertise EPTP switching unconditionally
3343 * since we emulate it
3344 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003345 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003346 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003347 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003348 }
3349
Paolo Bonzinief697a72016-03-18 16:58:38 +01003350 /*
3351 * Old versions of KVM use the single-context version without
3352 * checking for support, so declare that it is supported even
3353 * though it is treated as global context. The alternative is
3354 * not failing the single-context invvpid, and it is worse.
3355 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003356 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003357 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003358 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003359 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003360 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003361 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003362
Radim Krčmář0790ec12015-03-17 14:02:32 +01003363 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003364 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003365 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3366
Jan Kiszkac18911a2013-03-13 16:06:41 +01003367 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003368 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->misc_low,
3370 msrs->misc_high);
3371 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3372 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003373 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003374 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003375 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003376 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003377
3378 /*
3379 * This MSR reports some information about VMX support. We
3380 * should return information about the VMX we emulate for the
3381 * guest, and the VMCS structure we give it - not about the
3382 * VMX support of the underlying hardware.
3383 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003384 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003385 VMCS12_REVISION |
3386 VMX_BASIC_TRUE_CTLS |
3387 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3388 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3389
3390 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003392
3393 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003394 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003395 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3396 * We picked the standard core2 setting.
3397 */
3398#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3399#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003400 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3401 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003402
3403 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3405 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003406
3407 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003408 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003409}
3410
David Matlack38991522016-11-29 18:14:08 -08003411/*
3412 * if fixed0[i] == 1: val[i] must be 1
3413 * if fixed1[i] == 0: val[i] must be 0
3414 */
3415static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3416{
3417 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003418}
3419
3420static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3421{
David Matlack38991522016-11-29 18:14:08 -08003422 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003423}
3424
3425static inline u64 vmx_control_msr(u32 low, u32 high)
3426{
3427 return low | ((u64)high << 32);
3428}
3429
David Matlack62cc6b9d2016-11-29 18:14:07 -08003430static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3431{
3432 superset &= mask;
3433 subset &= mask;
3434
3435 return (superset | subset) == superset;
3436}
3437
3438static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3439{
3440 const u64 feature_and_reserved =
3441 /* feature (except bit 48; see below) */
3442 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3443 /* reserved */
3444 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003445 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003446
3447 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3448 return -EINVAL;
3449
3450 /*
3451 * KVM does not emulate a version of VMX that constrains physical
3452 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3453 */
3454 if (data & BIT_ULL(48))
3455 return -EINVAL;
3456
3457 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3458 vmx_basic_vmcs_revision_id(data))
3459 return -EINVAL;
3460
3461 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3462 return -EINVAL;
3463
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003464 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003465 return 0;
3466}
3467
3468static int
3469vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3470{
3471 u64 supported;
3472 u32 *lowp, *highp;
3473
3474 switch (msr_index) {
3475 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3477 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003478 break;
3479 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003480 lowp = &vmx->nested.msrs.procbased_ctls_low;
3481 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003482 break;
3483 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003484 lowp = &vmx->nested.msrs.exit_ctls_low;
3485 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003486 break;
3487 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003488 lowp = &vmx->nested.msrs.entry_ctls_low;
3489 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003490 break;
3491 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003492 lowp = &vmx->nested.msrs.secondary_ctls_low;
3493 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003494 break;
3495 default:
3496 BUG();
3497 }
3498
3499 supported = vmx_control_msr(*lowp, *highp);
3500
3501 /* Check must-be-1 bits are still 1. */
3502 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3503 return -EINVAL;
3504
3505 /* Check must-be-0 bits are still 0. */
3506 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3507 return -EINVAL;
3508
3509 *lowp = data;
3510 *highp = data >> 32;
3511 return 0;
3512}
3513
3514static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3515{
3516 const u64 feature_and_reserved_bits =
3517 /* feature */
3518 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3519 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3520 /* reserved */
3521 GENMASK_ULL(13, 9) | BIT_ULL(31);
3522 u64 vmx_misc;
3523
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3525 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003526
3527 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3528 return -EINVAL;
3529
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003531 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3532 vmx_misc_preemption_timer_rate(data) !=
3533 vmx_misc_preemption_timer_rate(vmx_misc))
3534 return -EINVAL;
3535
3536 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3537 return -EINVAL;
3538
3539 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3540 return -EINVAL;
3541
3542 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3543 return -EINVAL;
3544
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 vmx->nested.msrs.misc_low = data;
3546 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003547
3548 /*
3549 * If L1 has read-only VM-exit information fields, use the
3550 * less permissive vmx_vmwrite_bitmap to specify write
3551 * permissions for the shadow VMCS.
3552 */
3553 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3554 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3555
David Matlack62cc6b9d2016-11-29 18:14:07 -08003556 return 0;
3557}
3558
3559static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3560{
3561 u64 vmx_ept_vpid_cap;
3562
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003563 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3564 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003565
3566 /* Every bit is either reserved or a feature bit. */
3567 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3568 return -EINVAL;
3569
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003570 vmx->nested.msrs.ept_caps = data;
3571 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003572 return 0;
3573}
3574
3575static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3576{
3577 u64 *msr;
3578
3579 switch (msr_index) {
3580 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003581 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003582 break;
3583 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003584 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003585 break;
3586 default:
3587 BUG();
3588 }
3589
3590 /*
3591 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3592 * must be 1 in the restored value.
3593 */
3594 if (!is_bitwise_subset(data, *msr, -1ULL))
3595 return -EINVAL;
3596
3597 *msr = data;
3598 return 0;
3599}
3600
3601/*
3602 * Called when userspace is restoring VMX MSRs.
3603 *
3604 * Returns 0 on success, non-0 otherwise.
3605 */
3606static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3607{
3608 struct vcpu_vmx *vmx = to_vmx(vcpu);
3609
Jim Mattsona943ac52018-05-29 09:11:32 -07003610 /*
3611 * Don't allow changes to the VMX capability MSRs while the vCPU
3612 * is in VMX operation.
3613 */
3614 if (vmx->nested.vmxon)
3615 return -EBUSY;
3616
David Matlack62cc6b9d2016-11-29 18:14:07 -08003617 switch (msr_index) {
3618 case MSR_IA32_VMX_BASIC:
3619 return vmx_restore_vmx_basic(vmx, data);
3620 case MSR_IA32_VMX_PINBASED_CTLS:
3621 case MSR_IA32_VMX_PROCBASED_CTLS:
3622 case MSR_IA32_VMX_EXIT_CTLS:
3623 case MSR_IA32_VMX_ENTRY_CTLS:
3624 /*
3625 * The "non-true" VMX capability MSRs are generated from the
3626 * "true" MSRs, so we do not support restoring them directly.
3627 *
3628 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3629 * should restore the "true" MSRs with the must-be-1 bits
3630 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3631 * DEFAULT SETTINGS".
3632 */
3633 return -EINVAL;
3634 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3635 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3636 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3637 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3638 case MSR_IA32_VMX_PROCBASED_CTLS2:
3639 return vmx_restore_control_msr(vmx, msr_index, data);
3640 case MSR_IA32_VMX_MISC:
3641 return vmx_restore_vmx_misc(vmx, data);
3642 case MSR_IA32_VMX_CR0_FIXED0:
3643 case MSR_IA32_VMX_CR4_FIXED0:
3644 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3645 case MSR_IA32_VMX_CR0_FIXED1:
3646 case MSR_IA32_VMX_CR4_FIXED1:
3647 /*
3648 * These MSRs are generated based on the vCPU's CPUID, so we
3649 * do not support restoring them directly.
3650 */
3651 return -EINVAL;
3652 case MSR_IA32_VMX_EPT_VPID_CAP:
3653 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3654 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003655 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003656 return 0;
3657 default:
3658 /*
3659 * The rest of the VMX capability MSRs do not support restore.
3660 */
3661 return -EINVAL;
3662 }
3663}
3664
Jan Kiszkacae50132014-01-04 18:47:22 +01003665/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003667{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003668 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003669 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003671 break;
3672 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3673 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003674 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003675 msrs->pinbased_ctls_low,
3676 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003677 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3678 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003679 break;
3680 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3681 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003682 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 msrs->procbased_ctls_low,
3684 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003685 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3686 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003687 break;
3688 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3689 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003690 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 msrs->exit_ctls_low,
3692 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003693 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3694 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695 break;
3696 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3697 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003698 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003699 msrs->entry_ctls_low,
3700 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003701 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3702 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703 break;
3704 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003705 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003706 msrs->misc_low,
3707 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003710 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711 break;
3712 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714 break;
3715 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003716 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717 break;
3718 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003719 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003720 break;
3721 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003723 break;
3724 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003725 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003726 msrs->secondary_ctls_low,
3727 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003728 break;
3729 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003730 *pdata = msrs->ept_caps |
3731 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003732 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003733 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003734 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003735 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003737 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003738 }
3739
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003740 return 0;
3741}
3742
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003743static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3744 uint64_t val)
3745{
3746 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3747
3748 return !(val & ~valid_bits);
3749}
3750
Tom Lendacky801e4592018-02-21 13:39:51 -06003751static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3752{
Paolo Bonzini13893092018-02-26 13:40:09 +01003753 switch (msr->index) {
3754 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3755 if (!nested)
3756 return 1;
3757 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3758 default:
3759 return 1;
3760 }
3761
3762 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003763}
3764
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003765/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766 * Reads an msr value (of 'msr_index') into 'pdata'.
3767 * Returns 0 on success, non-0 otherwise.
3768 * Assumes vcpu_load() was already called.
3769 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003770static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003772 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003773 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003775 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003776#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003778 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 break;
3780 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003781 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003783 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003784 vmx_load_host_state(vmx);
3785 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003786 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003787#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003789 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003790 case MSR_IA32_SPEC_CTRL:
3791 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003792 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3793 return 1;
3794
3795 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3796 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003797 case MSR_IA32_ARCH_CAPABILITIES:
3798 if (!msr_info->host_initiated &&
3799 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3800 return 1;
3801 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3802 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003804 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 break;
3806 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003807 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808 break;
3809 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003810 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003812 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003813 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003814 (!msr_info->host_initiated &&
3815 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003816 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003817 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003818 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003819 case MSR_IA32_MCG_EXT_CTL:
3820 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003821 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003822 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003823 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003824 msr_info->data = vcpu->arch.mcg_ext_ctl;
3825 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003826 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003827 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003828 break;
3829 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3830 if (!nested_vmx_allowed(vcpu))
3831 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003832 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3833 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003834 case MSR_IA32_XSS:
3835 if (!vmx_xsaves_supported())
3836 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003837 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003838 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003839 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003840 if (!msr_info->host_initiated &&
3841 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003842 return 1;
3843 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003845 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003846 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003847 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003848 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851 }
3852
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853 return 0;
3854}
3855
Jan Kiszkacae50132014-01-04 18:47:22 +01003856static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3857
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858/*
3859 * Writes msr value into into the appropriate "register".
3860 * Returns 0 on success, non-0 otherwise.
3861 * Assumes vcpu_load() was already called.
3862 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003863static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003865 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003866 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003867 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003868 u32 msr_index = msr_info->index;
3869 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003870
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003872 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003873 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003874 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003875#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003877 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 vmcs_writel(GUEST_FS_BASE, data);
3879 break;
3880 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003881 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 vmcs_writel(GUEST_GS_BASE, data);
3883 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003884 case MSR_KERNEL_GS_BASE:
3885 vmx_load_host_state(vmx);
3886 vmx->msr_guest_kernel_gs_base = data;
3887 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888#endif
3889 case MSR_IA32_SYSENTER_CS:
3890 vmcs_write32(GUEST_SYSENTER_CS, data);
3891 break;
3892 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003893 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894 break;
3895 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003896 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003898 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003899 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003900 (!msr_info->host_initiated &&
3901 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003902 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003903 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003904 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003906 vmcs_write64(GUEST_BNDCFGS, data);
3907 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003908 case MSR_IA32_SPEC_CTRL:
3909 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003910 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3911 return 1;
3912
3913 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003914 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003915 return 1;
3916
3917 vmx->spec_ctrl = data;
3918
3919 if (!data)
3920 break;
3921
3922 /*
3923 * For non-nested:
3924 * When it's written (to non-zero) for the first time, pass
3925 * it through.
3926 *
3927 * For nested:
3928 * The handling of the MSR bitmap for L2 guests is done in
3929 * nested_vmx_merge_msr_bitmap. We should not touch the
3930 * vmcs02.msr_bitmap here since it gets completely overwritten
3931 * in the merging. We update the vmcs01 here for L1 as well
3932 * since it will end up touching the MSR anyway now.
3933 */
3934 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3935 MSR_IA32_SPEC_CTRL,
3936 MSR_TYPE_RW);
3937 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003938 case MSR_IA32_PRED_CMD:
3939 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003940 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3941 return 1;
3942
3943 if (data & ~PRED_CMD_IBPB)
3944 return 1;
3945
3946 if (!data)
3947 break;
3948
3949 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3950
3951 /*
3952 * For non-nested:
3953 * When it's written (to non-zero) for the first time, pass
3954 * it through.
3955 *
3956 * For nested:
3957 * The handling of the MSR bitmap for L2 guests is done in
3958 * nested_vmx_merge_msr_bitmap. We should not touch the
3959 * vmcs02.msr_bitmap here since it gets completely overwritten
3960 * in the merging.
3961 */
3962 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3963 MSR_TYPE_W);
3964 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003965 case MSR_IA32_ARCH_CAPABILITIES:
3966 if (!msr_info->host_initiated)
3967 return 1;
3968 vmx->arch_capabilities = data;
3969 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003970 case MSR_IA32_CR_PAT:
3971 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003972 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3973 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003974 vmcs_write64(GUEST_IA32_PAT, data);
3975 vcpu->arch.pat = data;
3976 break;
3977 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003978 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003979 break;
Will Auldba904632012-11-29 12:42:50 -08003980 case MSR_IA32_TSC_ADJUST:
3981 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003982 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003983 case MSR_IA32_MCG_EXT_CTL:
3984 if ((!msr_info->host_initiated &&
3985 !(to_vmx(vcpu)->msr_ia32_feature_control &
3986 FEATURE_CONTROL_LMCE)) ||
3987 (data & ~MCG_EXT_CTL_LMCE_EN))
3988 return 1;
3989 vcpu->arch.mcg_ext_ctl = data;
3990 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003991 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003992 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003993 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003994 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3995 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003996 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003997 if (msr_info->host_initiated && data == 0)
3998 vmx_leave_nested(vcpu);
3999 break;
4000 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004001 if (!msr_info->host_initiated)
4002 return 1; /* they are read-only */
4003 if (!nested_vmx_allowed(vcpu))
4004 return 1;
4005 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004006 case MSR_IA32_XSS:
4007 if (!vmx_xsaves_supported())
4008 return 1;
4009 /*
4010 * The only supported bit as of Skylake is bit 8, but
4011 * it is not supported on KVM.
4012 */
4013 if (data != 0)
4014 return 1;
4015 vcpu->arch.ia32_xss = data;
4016 if (vcpu->arch.ia32_xss != host_xss)
4017 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4018 vcpu->arch.ia32_xss, host_xss);
4019 else
4020 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4021 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004022 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004023 if (!msr_info->host_initiated &&
4024 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004025 return 1;
4026 /* Check reserved bit, higher 32 bits should be zero */
4027 if ((data >> 32) != 0)
4028 return 1;
4029 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004030 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004031 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004032 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004033 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004034 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004035 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4036 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004037 ret = kvm_set_shared_msr(msr->index, msr->data,
4038 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004039 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004040 if (ret)
4041 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004042 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004043 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004045 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046 }
4047
Eddie Dong2cc51562007-05-21 07:28:09 +03004048 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049}
4050
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004051static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004053 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4054 switch (reg) {
4055 case VCPU_REGS_RSP:
4056 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4057 break;
4058 case VCPU_REGS_RIP:
4059 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4060 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004061 case VCPU_EXREG_PDPTR:
4062 if (enable_ept)
4063 ept_save_pdptrs(vcpu);
4064 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004065 default:
4066 break;
4067 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068}
4069
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070static __init int cpu_has_kvm_support(void)
4071{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004072 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073}
4074
4075static __init int vmx_disabled_by_bios(void)
4076{
4077 u64 msr;
4078
4079 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004080 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004081 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004082 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4083 && tboot_enabled())
4084 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004085 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004086 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004087 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004088 && !tboot_enabled()) {
4089 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004090 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004091 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004092 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004093 /* launched w/o TXT and VMX disabled */
4094 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4095 && !tboot_enabled())
4096 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004097 }
4098
4099 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100}
4101
Dongxiao Xu7725b892010-05-11 18:29:38 +08004102static void kvm_cpu_vmxon(u64 addr)
4103{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004104 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004105 intel_pt_handle_vmx(1);
4106
Dongxiao Xu7725b892010-05-11 18:29:38 +08004107 asm volatile (ASM_VMX_VMXON_RAX
4108 : : "a"(&addr), "m"(addr)
4109 : "memory", "cc");
4110}
4111
Radim Krčmář13a34e02014-08-28 15:13:03 +02004112static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113{
4114 int cpu = raw_smp_processor_id();
4115 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004116 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004118 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004119 return -EBUSY;
4120
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004121 /*
4122 * This can happen if we hot-added a CPU but failed to allocate
4123 * VP assist page for it.
4124 */
4125 if (static_branch_unlikely(&enable_evmcs) &&
4126 !hv_get_vp_assist_page(cpu))
4127 return -EFAULT;
4128
Nadav Har'Eld462b812011-05-24 15:26:10 +03004129 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004130 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4131 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004132
4133 /*
4134 * Now we can enable the vmclear operation in kdump
4135 * since the loaded_vmcss_on_cpu list on this cpu
4136 * has been initialized.
4137 *
4138 * Though the cpu is not in VMX operation now, there
4139 * is no problem to enable the vmclear operation
4140 * for the loaded_vmcss_on_cpu list is empty!
4141 */
4142 crash_enable_local_vmclear(cpu);
4143
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004145
4146 test_bits = FEATURE_CONTROL_LOCKED;
4147 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4148 if (tboot_enabled())
4149 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4150
4151 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004153 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4154 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004155 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004156 if (enable_ept)
4157 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004158
4159 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160}
4161
Nadav Har'Eld462b812011-05-24 15:26:10 +03004162static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004163{
4164 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004165 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004166
Nadav Har'Eld462b812011-05-24 15:26:10 +03004167 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4168 loaded_vmcss_on_cpu_link)
4169 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004170}
4171
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004172
4173/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4174 * tricks.
4175 */
4176static void kvm_cpu_vmxoff(void)
4177{
4178 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004179
4180 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004181 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004182}
4183
Radim Krčmář13a34e02014-08-28 15:13:03 +02004184static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004186 vmclear_local_loaded_vmcss();
4187 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188}
4189
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004190static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004191 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192{
4193 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004194 u32 ctl = ctl_min | ctl_opt;
4195
4196 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4197
4198 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4199 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4200
4201 /* Ensure minimum (required) set of control bits are supported. */
4202 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004203 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004204
4205 *result = ctl;
4206 return 0;
4207}
4208
Avi Kivity110312c2010-12-21 12:54:20 +02004209static __init bool allow_1_setting(u32 msr, u32 ctl)
4210{
4211 u32 vmx_msr_low, vmx_msr_high;
4212
4213 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4214 return vmx_msr_high & ctl;
4215}
4216
Yang, Sheng002c7f72007-07-31 14:23:01 +03004217static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004218{
4219 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004220 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004221 u32 _pin_based_exec_control = 0;
4222 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004223 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004224 u32 _vmexit_control = 0;
4225 u32 _vmentry_control = 0;
4226
Paolo Bonzini13893092018-02-26 13:40:09 +01004227 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304228 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004229#ifdef CONFIG_X86_64
4230 CPU_BASED_CR8_LOAD_EXITING |
4231 CPU_BASED_CR8_STORE_EXITING |
4232#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004233 CPU_BASED_CR3_LOAD_EXITING |
4234 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004235 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004236 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004237 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004238 CPU_BASED_MWAIT_EXITING |
4239 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004240 CPU_BASED_INVLPG_EXITING |
4241 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004242
Sheng Yangf78e0e22007-10-29 09:40:42 +08004243 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004244 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004245 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004246 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4247 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004248 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004249#ifdef CONFIG_X86_64
4250 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4251 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4252 ~CPU_BASED_CR8_STORE_EXITING;
4253#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004254 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004255 min2 = 0;
4256 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004257 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004258 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004259 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004260 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004261 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004262 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004263 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004264 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004265 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004266 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004267 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004268 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004269 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004270 SECONDARY_EXEC_RDSEED_EXITING |
4271 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004272 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004273 SECONDARY_EXEC_TSC_SCALING |
4274 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004275 if (adjust_vmx_controls(min2, opt2,
4276 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004277 &_cpu_based_2nd_exec_control) < 0)
4278 return -EIO;
4279 }
4280#ifndef CONFIG_X86_64
4281 if (!(_cpu_based_2nd_exec_control &
4282 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4283 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4284#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004285
4286 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4287 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004288 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004289 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4290 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004291
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004292 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4293 &vmx_capability.ept, &vmx_capability.vpid);
4294
Sheng Yangd56f5462008-04-25 10:13:16 +08004295 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004296 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4297 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004298 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4299 CPU_BASED_CR3_STORE_EXITING |
4300 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004301 } else if (vmx_capability.ept) {
4302 vmx_capability.ept = 0;
4303 pr_warn_once("EPT CAP should not exist if not support "
4304 "1-setting enable EPT VM-execution control\n");
4305 }
4306 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4307 vmx_capability.vpid) {
4308 vmx_capability.vpid = 0;
4309 pr_warn_once("VPID CAP should not exist if not support "
4310 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004311 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004312
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004313 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004314#ifdef CONFIG_X86_64
4315 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4316#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004317 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004318 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004319 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4320 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004321 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004322
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004323 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4324 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4325 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004326 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4327 &_pin_based_exec_control) < 0)
4328 return -EIO;
4329
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004330 if (cpu_has_broken_vmx_preemption_timer())
4331 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004332 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004333 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004334 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4335
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004336 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004337 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004338 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4339 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004340 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004342 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004343
4344 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4345 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004346 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004347
4348#ifdef CONFIG_X86_64
4349 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4350 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004351 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004352#endif
4353
4354 /* Require Write-Back (WB) memory type for VMCS accesses. */
4355 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004356 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004357
Yang, Sheng002c7f72007-07-31 14:23:01 +03004358 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004359 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004360 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004361
Liran Alon2307af12018-06-29 22:59:04 +03004362 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004363
Yang, Sheng002c7f72007-07-31 14:23:01 +03004364 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4365 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004366 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004367 vmcs_conf->vmexit_ctrl = _vmexit_control;
4368 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004369
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004370 if (static_branch_unlikely(&enable_evmcs))
4371 evmcs_sanitize_exec_ctrls(vmcs_conf);
4372
Avi Kivity110312c2010-12-21 12:54:20 +02004373 cpu_has_load_ia32_efer =
4374 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4375 VM_ENTRY_LOAD_IA32_EFER)
4376 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4377 VM_EXIT_LOAD_IA32_EFER);
4378
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004379 cpu_has_load_perf_global_ctrl =
4380 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4381 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4382 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4383 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4384
4385 /*
4386 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004387 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004388 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4389 *
4390 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4391 *
4392 * AAK155 (model 26)
4393 * AAP115 (model 30)
4394 * AAT100 (model 37)
4395 * BC86,AAY89,BD102 (model 44)
4396 * BA97 (model 46)
4397 *
4398 */
4399 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4400 switch (boot_cpu_data.x86_model) {
4401 case 26:
4402 case 30:
4403 case 37:
4404 case 44:
4405 case 46:
4406 cpu_has_load_perf_global_ctrl = false;
4407 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4408 "does not work properly. Using workaround\n");
4409 break;
4410 default:
4411 break;
4412 }
4413 }
4414
Borislav Petkov782511b2016-04-04 22:25:03 +02004415 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004416 rdmsrl(MSR_IA32_XSS, host_xss);
4417
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004418 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004419}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420
Liran Alon491a6032018-06-23 02:35:12 +03004421static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422{
4423 int node = cpu_to_node(cpu);
4424 struct page *pages;
4425 struct vmcs *vmcs;
4426
Vlastimil Babka96db8002015-09-08 15:03:50 -07004427 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428 if (!pages)
4429 return NULL;
4430 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004431 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004432
4433 /* KVM supports Enlightened VMCS v1 only */
4434 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004435 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004436 else
Liran Alon392b2f22018-06-23 02:35:01 +03004437 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004438
Liran Alon491a6032018-06-23 02:35:12 +03004439 if (shadow)
4440 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441 return vmcs;
4442}
4443
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444static void free_vmcs(struct vmcs *vmcs)
4445{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004446 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004447}
4448
Nadav Har'Eld462b812011-05-24 15:26:10 +03004449/*
4450 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4451 */
4452static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4453{
4454 if (!loaded_vmcs->vmcs)
4455 return;
4456 loaded_vmcs_clear(loaded_vmcs);
4457 free_vmcs(loaded_vmcs->vmcs);
4458 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004459 if (loaded_vmcs->msr_bitmap)
4460 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004461 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004462}
4463
Liran Alon491a6032018-06-23 02:35:12 +03004464static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004465{
Liran Alon491a6032018-06-23 02:35:12 +03004466 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004467}
4468
4469static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4470{
Liran Alon491a6032018-06-23 02:35:12 +03004471 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004472 if (!loaded_vmcs->vmcs)
4473 return -ENOMEM;
4474
4475 loaded_vmcs->shadow_vmcs = NULL;
4476 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004477
4478 if (cpu_has_vmx_msr_bitmap()) {
4479 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4480 if (!loaded_vmcs->msr_bitmap)
4481 goto out_vmcs;
4482 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004483
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004484 if (IS_ENABLED(CONFIG_HYPERV) &&
4485 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004486 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4487 struct hv_enlightened_vmcs *evmcs =
4488 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4489
4490 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4491 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004492 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004493 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004494
4495out_vmcs:
4496 free_loaded_vmcs(loaded_vmcs);
4497 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004498}
4499
Sam Ravnborg39959582007-06-01 00:47:13 -07004500static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501{
4502 int cpu;
4503
Zachary Amsden3230bb42009-09-29 11:38:37 -10004504 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004506 per_cpu(vmxarea, cpu) = NULL;
4507 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004508}
4509
Jim Mattsond37f4262017-12-22 12:12:16 -08004510enum vmcs_field_width {
4511 VMCS_FIELD_WIDTH_U16 = 0,
4512 VMCS_FIELD_WIDTH_U64 = 1,
4513 VMCS_FIELD_WIDTH_U32 = 2,
4514 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004515};
4516
Jim Mattsond37f4262017-12-22 12:12:16 -08004517static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004518{
4519 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004520 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004521 return (field >> 13) & 0x3 ;
4522}
4523
4524static inline int vmcs_field_readonly(unsigned long field)
4525{
4526 return (((field >> 10) & 0x3) == 1);
4527}
4528
Bandan Dasfe2b2012014-04-21 15:20:14 -04004529static void init_vmcs_shadow_fields(void)
4530{
4531 int i, j;
4532
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004533 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4534 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004535 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004536 (i + 1 == max_shadow_read_only_fields ||
4537 shadow_read_only_fields[i + 1] != field + 1))
4538 pr_err("Missing field from shadow_read_only_field %x\n",
4539 field + 1);
4540
4541 clear_bit(field, vmx_vmread_bitmap);
4542#ifdef CONFIG_X86_64
4543 if (field & 1)
4544 continue;
4545#endif
4546 if (j < i)
4547 shadow_read_only_fields[j] = field;
4548 j++;
4549 }
4550 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004551
4552 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004553 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004554 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004555 (i + 1 == max_shadow_read_write_fields ||
4556 shadow_read_write_fields[i + 1] != field + 1))
4557 pr_err("Missing field from shadow_read_write_field %x\n",
4558 field + 1);
4559
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004560 /*
4561 * PML and the preemption timer can be emulated, but the
4562 * processor cannot vmwrite to fields that don't exist
4563 * on bare metal.
4564 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004565 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004566 case GUEST_PML_INDEX:
4567 if (!cpu_has_vmx_pml())
4568 continue;
4569 break;
4570 case VMX_PREEMPTION_TIMER_VALUE:
4571 if (!cpu_has_vmx_preemption_timer())
4572 continue;
4573 break;
4574 case GUEST_INTR_STATUS:
4575 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004576 continue;
4577 break;
4578 default:
4579 break;
4580 }
4581
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004582 clear_bit(field, vmx_vmwrite_bitmap);
4583 clear_bit(field, vmx_vmread_bitmap);
4584#ifdef CONFIG_X86_64
4585 if (field & 1)
4586 continue;
4587#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004588 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004589 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004590 j++;
4591 }
4592 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004593}
4594
Avi Kivity6aa8b732006-12-10 02:21:36 -08004595static __init int alloc_kvm_area(void)
4596{
4597 int cpu;
4598
Zachary Amsden3230bb42009-09-29 11:38:37 -10004599 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004600 struct vmcs *vmcs;
4601
Liran Alon491a6032018-06-23 02:35:12 +03004602 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603 if (!vmcs) {
4604 free_kvm_area();
4605 return -ENOMEM;
4606 }
4607
Liran Alon2307af12018-06-29 22:59:04 +03004608 /*
4609 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4610 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4611 * revision_id reported by MSR_IA32_VMX_BASIC.
4612 *
4613 * However, even though not explictly documented by
4614 * TLFS, VMXArea passed as VMXON argument should
4615 * still be marked with revision_id reported by
4616 * physical CPU.
4617 */
4618 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004619 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004620
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621 per_cpu(vmxarea, cpu) = vmcs;
4622 }
4623 return 0;
4624}
4625
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004626static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004627 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004628{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004629 if (!emulate_invalid_guest_state) {
4630 /*
4631 * CS and SS RPL should be equal during guest entry according
4632 * to VMX spec, but in reality it is not always so. Since vcpu
4633 * is in the middle of the transition from real mode to
4634 * protected mode it is safe to assume that RPL 0 is a good
4635 * default value.
4636 */
4637 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004638 save->selector &= ~SEGMENT_RPL_MASK;
4639 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004640 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004642 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004643}
4644
4645static void enter_pmode(struct kvm_vcpu *vcpu)
4646{
4647 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004648 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004649
Gleb Natapovd99e4152012-12-20 16:57:45 +02004650 /*
4651 * Update real mode segment cache. It may be not up-to-date if sement
4652 * register was written while vcpu was in a guest mode.
4653 */
4654 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4655 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4656 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4657 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4658 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4659 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4660
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004661 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662
Avi Kivity2fb92db2011-04-27 19:42:18 +03004663 vmx_segment_cache_clear(vmx);
4664
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004665 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666
4667 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004668 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4669 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670 vmcs_writel(GUEST_RFLAGS, flags);
4671
Rusty Russell66aee912007-07-17 23:34:16 +10004672 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4673 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674
4675 update_exception_bitmap(vcpu);
4676
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004677 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4678 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4679 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4680 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4681 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4682 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683}
4684
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004685static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686{
Mathias Krause772e0312012-08-30 01:30:19 +02004687 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004688 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689
Gleb Natapovd99e4152012-12-20 16:57:45 +02004690 var.dpl = 0x3;
4691 if (seg == VCPU_SREG_CS)
4692 var.type = 0x3;
4693
4694 if (!emulate_invalid_guest_state) {
4695 var.selector = var.base >> 4;
4696 var.base = var.base & 0xffff0;
4697 var.limit = 0xffff;
4698 var.g = 0;
4699 var.db = 0;
4700 var.present = 1;
4701 var.s = 1;
4702 var.l = 0;
4703 var.unusable = 0;
4704 var.type = 0x3;
4705 var.avl = 0;
4706 if (save->base & 0xf)
4707 printk_once(KERN_WARNING "kvm: segment base is not "
4708 "paragraph aligned when entering "
4709 "protected mode (seg=%d)", seg);
4710 }
4711
4712 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004713 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004714 vmcs_write32(sf->limit, var.limit);
4715 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716}
4717
4718static void enter_rmode(struct kvm_vcpu *vcpu)
4719{
4720 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004721 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004722 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4727 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4728 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004729 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4730 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004731
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004732 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733
Gleb Natapov776e58e2011-03-13 12:34:27 +02004734 /*
4735 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004736 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004737 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004738 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004739 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4740 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004741
Avi Kivity2fb92db2011-04-27 19:42:18 +03004742 vmx_segment_cache_clear(vmx);
4743
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004744 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4747
4748 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004749 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004751 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752
4753 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004754 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755 update_exception_bitmap(vcpu);
4756
Gleb Natapovd99e4152012-12-20 16:57:45 +02004757 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4758 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4759 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4760 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4761 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4762 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004763
Eddie Dong8668a3c2007-10-10 14:26:45 +08004764 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765}
4766
Amit Shah401d10d2009-02-20 22:53:37 +05304767static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4768{
4769 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004770 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4771
4772 if (!msr)
4773 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304774
Avi Kivity44ea2b12009-09-06 15:55:37 +03004775 /*
4776 * Force kernel_gs_base reloading before EFER changes, as control
4777 * of this msr depends on is_long_mode().
4778 */
4779 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004780 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304781 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004782 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304783 msr->data = efer;
4784 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004785 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304786
4787 msr->data = efer & ~EFER_LME;
4788 }
4789 setup_msrs(vmx);
4790}
4791
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004792#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793
4794static void enter_lmode(struct kvm_vcpu *vcpu)
4795{
4796 u32 guest_tr_ar;
4797
Avi Kivity2fb92db2011-04-27 19:42:18 +03004798 vmx_segment_cache_clear(to_vmx(vcpu));
4799
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004801 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004802 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4803 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004805 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4806 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004807 }
Avi Kivityda38f432010-07-06 11:30:49 +03004808 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809}
4810
4811static void exit_lmode(struct kvm_vcpu *vcpu)
4812{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004813 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004814 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815}
4816
4817#endif
4818
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004819static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4820 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004821{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004822 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004823 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4824 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004825 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004826 } else {
4827 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004828 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004829}
4830
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004831static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004832{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004833 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004834}
4835
Avi Kivitye8467fd2009-12-29 18:43:06 +02004836static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4837{
4838 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4839
4840 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4841 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4842}
4843
Avi Kivityaff48ba2010-12-05 18:56:11 +02004844static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4845{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004846 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004847 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4848 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4849}
4850
Anthony Liguori25c4c272007-04-27 09:29:21 +03004851static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004852{
Avi Kivityfc78f512009-12-07 12:16:48 +02004853 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4854
4855 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4856 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004857}
4858
Sheng Yang14394422008-04-28 12:24:45 +08004859static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4860{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004861 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4862
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004863 if (!test_bit(VCPU_EXREG_PDPTR,
4864 (unsigned long *)&vcpu->arch.regs_dirty))
4865 return;
4866
Sheng Yang14394422008-04-28 12:24:45 +08004867 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004868 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4869 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4870 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4871 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004872 }
4873}
4874
Avi Kivity8f5d5492009-05-31 18:41:29 +03004875static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4876{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004877 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4878
Avi Kivity8f5d5492009-05-31 18:41:29 +03004879 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004880 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4881 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4882 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4883 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004884 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004885
4886 __set_bit(VCPU_EXREG_PDPTR,
4887 (unsigned long *)&vcpu->arch.regs_avail);
4888 __set_bit(VCPU_EXREG_PDPTR,
4889 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004890}
4891
David Matlack38991522016-11-29 18:14:08 -08004892static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4893{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004894 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4895 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4897
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004898 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004899 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4900 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4901 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4902
4903 return fixed_bits_valid(val, fixed0, fixed1);
4904}
4905
4906static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4907{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004908 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4909 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004910
4911 return fixed_bits_valid(val, fixed0, fixed1);
4912}
4913
4914static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4915{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004916 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4917 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004918
4919 return fixed_bits_valid(val, fixed0, fixed1);
4920}
4921
4922/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4923#define nested_guest_cr4_valid nested_cr4_valid
4924#define nested_host_cr4_valid nested_cr4_valid
4925
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004926static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004927
4928static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4929 unsigned long cr0,
4930 struct kvm_vcpu *vcpu)
4931{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004932 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4933 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004934 if (!(cr0 & X86_CR0_PG)) {
4935 /* From paging/starting to nonpaging */
4936 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004937 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004938 (CPU_BASED_CR3_LOAD_EXITING |
4939 CPU_BASED_CR3_STORE_EXITING));
4940 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004941 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004942 } else if (!is_paging(vcpu)) {
4943 /* From nonpaging to paging */
4944 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004945 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004946 ~(CPU_BASED_CR3_LOAD_EXITING |
4947 CPU_BASED_CR3_STORE_EXITING));
4948 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004949 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004950 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004951
4952 if (!(cr0 & X86_CR0_WP))
4953 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004954}
4955
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4957{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004958 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004959 unsigned long hw_cr0;
4960
Gleb Natapov50378782013-02-04 16:00:28 +02004961 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004962 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004963 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004964 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004965 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004966
Gleb Natapov218e7632013-01-21 15:36:45 +02004967 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4968 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969
Gleb Natapov218e7632013-01-21 15:36:45 +02004970 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4971 enter_rmode(vcpu);
4972 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004974#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004975 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004976 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004978 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979 exit_lmode(vcpu);
4980 }
4981#endif
4982
Sean Christophersonb4d18512018-03-05 12:04:40 -08004983 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004984 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4985
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004987 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004988 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004989
4990 /* depends on vcpu->arch.cr0 to be set to a new value */
4991 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992}
4993
Yu Zhang855feb62017-08-24 20:27:55 +08004994static int get_ept_level(struct kvm_vcpu *vcpu)
4995{
4996 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4997 return 5;
4998 return 4;
4999}
5000
Peter Feiner995f00a2017-06-30 17:26:32 -07005001static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005002{
Yu Zhang855feb62017-08-24 20:27:55 +08005003 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005004
Yu Zhang855feb62017-08-24 20:27:55 +08005005 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005006
Peter Feiner995f00a2017-06-30 17:26:32 -07005007 if (enable_ept_ad_bits &&
5008 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005009 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005010 eptp |= (root_hpa & PAGE_MASK);
5011
5012 return eptp;
5013}
5014
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5016{
Sheng Yang14394422008-04-28 12:24:45 +08005017 unsigned long guest_cr3;
5018 u64 eptp;
5019
5020 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005021 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005022 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005023 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005024 if (enable_unrestricted_guest || is_paging(vcpu) ||
5025 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005026 guest_cr3 = kvm_read_cr3(vcpu);
5027 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005028 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005029 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005030 }
5031
Sheng Yang14394422008-04-28 12:24:45 +08005032 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033}
5034
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005035static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005037 /*
5038 * Pass through host's Machine Check Enable value to hw_cr4, which
5039 * is in force while we are in guest mode. Do not let guests control
5040 * this bit, even if host CR4.MCE == 0.
5041 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005042 unsigned long hw_cr4;
5043
5044 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5045 if (enable_unrestricted_guest)
5046 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5047 else if (to_vmx(vcpu)->rmode.vm86_active)
5048 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5049 else
5050 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005051
Sean Christopherson64f7a112018-04-30 10:01:06 -07005052 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5053 if (cr4 & X86_CR4_UMIP) {
5054 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005055 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005056 hw_cr4 &= ~X86_CR4_UMIP;
5057 } else if (!is_guest_mode(vcpu) ||
5058 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5059 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5060 SECONDARY_EXEC_DESC);
5061 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005062
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005063 if (cr4 & X86_CR4_VMXE) {
5064 /*
5065 * To use VMXON (and later other VMX instructions), a guest
5066 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5067 * So basically the check on whether to allow nested VMX
5068 * is here.
5069 */
5070 if (!nested_vmx_allowed(vcpu))
5071 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005072 }
David Matlack38991522016-11-29 18:14:08 -08005073
5074 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005075 return 1;
5076
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005077 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005078
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005079 if (!enable_unrestricted_guest) {
5080 if (enable_ept) {
5081 if (!is_paging(vcpu)) {
5082 hw_cr4 &= ~X86_CR4_PAE;
5083 hw_cr4 |= X86_CR4_PSE;
5084 } else if (!(cr4 & X86_CR4_PAE)) {
5085 hw_cr4 &= ~X86_CR4_PAE;
5086 }
5087 }
5088
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005089 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005090 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5091 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5092 * to be manually disabled when guest switches to non-paging
5093 * mode.
5094 *
5095 * If !enable_unrestricted_guest, the CPU is always running
5096 * with CR0.PG=1 and CR4 needs to be modified.
5097 * If enable_unrestricted_guest, the CPU automatically
5098 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005099 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005100 if (!is_paging(vcpu))
5101 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5102 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005103
Sheng Yang14394422008-04-28 12:24:45 +08005104 vmcs_writel(CR4_READ_SHADOW, cr4);
5105 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005106 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005107}
5108
Avi Kivity6aa8b732006-12-10 02:21:36 -08005109static void vmx_get_segment(struct kvm_vcpu *vcpu,
5110 struct kvm_segment *var, int seg)
5111{
Avi Kivitya9179492011-01-03 14:28:52 +02005112 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113 u32 ar;
5114
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005115 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005116 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005117 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005118 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005119 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005120 var->base = vmx_read_guest_seg_base(vmx, seg);
5121 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5122 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005123 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005124 var->base = vmx_read_guest_seg_base(vmx, seg);
5125 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5126 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5127 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005128 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005129 var->type = ar & 15;
5130 var->s = (ar >> 4) & 1;
5131 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005132 /*
5133 * Some userspaces do not preserve unusable property. Since usable
5134 * segment has to be present according to VMX spec we can use present
5135 * property to amend userspace bug by making unusable segment always
5136 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5137 * segment as unusable.
5138 */
5139 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 var->avl = (ar >> 12) & 1;
5141 var->l = (ar >> 13) & 1;
5142 var->db = (ar >> 14) & 1;
5143 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144}
5145
Avi Kivitya9179492011-01-03 14:28:52 +02005146static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5147{
Avi Kivitya9179492011-01-03 14:28:52 +02005148 struct kvm_segment s;
5149
5150 if (to_vmx(vcpu)->rmode.vm86_active) {
5151 vmx_get_segment(vcpu, &s, seg);
5152 return s.base;
5153 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005154 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005155}
5156
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005157static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005158{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005159 struct vcpu_vmx *vmx = to_vmx(vcpu);
5160
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005161 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005162 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005163 else {
5164 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005165 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005166 }
Avi Kivity69c73022011-03-07 15:26:44 +02005167}
5168
Avi Kivity653e3102007-05-07 10:55:37 +03005169static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005170{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171 u32 ar;
5172
Avi Kivityf0495f92012-06-07 17:06:10 +03005173 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174 ar = 1 << 16;
5175 else {
5176 ar = var->type & 15;
5177 ar |= (var->s & 1) << 4;
5178 ar |= (var->dpl & 3) << 5;
5179 ar |= (var->present & 1) << 7;
5180 ar |= (var->avl & 1) << 12;
5181 ar |= (var->l & 1) << 13;
5182 ar |= (var->db & 1) << 14;
5183 ar |= (var->g & 1) << 15;
5184 }
Avi Kivity653e3102007-05-07 10:55:37 +03005185
5186 return ar;
5187}
5188
5189static void vmx_set_segment(struct kvm_vcpu *vcpu,
5190 struct kvm_segment *var, int seg)
5191{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005192 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005193 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005194
Avi Kivity2fb92db2011-04-27 19:42:18 +03005195 vmx_segment_cache_clear(vmx);
5196
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005197 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5198 vmx->rmode.segs[seg] = *var;
5199 if (seg == VCPU_SREG_TR)
5200 vmcs_write16(sf->selector, var->selector);
5201 else if (var->s)
5202 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005203 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005204 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005205
Avi Kivity653e3102007-05-07 10:55:37 +03005206 vmcs_writel(sf->base, var->base);
5207 vmcs_write32(sf->limit, var->limit);
5208 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005209
5210 /*
5211 * Fix the "Accessed" bit in AR field of segment registers for older
5212 * qemu binaries.
5213 * IA32 arch specifies that at the time of processor reset the
5214 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005215 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005216 * state vmexit when "unrestricted guest" mode is turned on.
5217 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5218 * tree. Newer qemu binaries with that qemu fix would not need this
5219 * kvm hack.
5220 */
5221 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005222 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005223
Gleb Natapovf924d662012-12-12 19:10:55 +02005224 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005225
5226out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005227 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228}
5229
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5231{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005232 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233
5234 *db = (ar >> 14) & 1;
5235 *l = (ar >> 13) & 1;
5236}
5237
Gleb Natapov89a27f42010-02-16 10:51:48 +02005238static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005240 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5241 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242}
5243
Gleb Natapov89a27f42010-02-16 10:51:48 +02005244static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005246 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5247 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248}
5249
Gleb Natapov89a27f42010-02-16 10:51:48 +02005250static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005252 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5253 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254}
5255
Gleb Natapov89a27f42010-02-16 10:51:48 +02005256static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005258 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5259 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260}
5261
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005262static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5263{
5264 struct kvm_segment var;
5265 u32 ar;
5266
5267 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005268 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005269 if (seg == VCPU_SREG_CS)
5270 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005271 ar = vmx_segment_access_rights(&var);
5272
5273 if (var.base != (var.selector << 4))
5274 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005275 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005276 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005277 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005278 return false;
5279
5280 return true;
5281}
5282
5283static bool code_segment_valid(struct kvm_vcpu *vcpu)
5284{
5285 struct kvm_segment cs;
5286 unsigned int cs_rpl;
5287
5288 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005289 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005290
Avi Kivity1872a3f2009-01-04 23:26:52 +02005291 if (cs.unusable)
5292 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005293 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005294 return false;
5295 if (!cs.s)
5296 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005297 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005298 if (cs.dpl > cs_rpl)
5299 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005300 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005301 if (cs.dpl != cs_rpl)
5302 return false;
5303 }
5304 if (!cs.present)
5305 return false;
5306
5307 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5308 return true;
5309}
5310
5311static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5312{
5313 struct kvm_segment ss;
5314 unsigned int ss_rpl;
5315
5316 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005317 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005318
Avi Kivity1872a3f2009-01-04 23:26:52 +02005319 if (ss.unusable)
5320 return true;
5321 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005322 return false;
5323 if (!ss.s)
5324 return false;
5325 if (ss.dpl != ss_rpl) /* DPL != RPL */
5326 return false;
5327 if (!ss.present)
5328 return false;
5329
5330 return true;
5331}
5332
5333static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5334{
5335 struct kvm_segment var;
5336 unsigned int rpl;
5337
5338 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005339 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005340
Avi Kivity1872a3f2009-01-04 23:26:52 +02005341 if (var.unusable)
5342 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005343 if (!var.s)
5344 return false;
5345 if (!var.present)
5346 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005347 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005348 if (var.dpl < rpl) /* DPL < RPL */
5349 return false;
5350 }
5351
5352 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5353 * rights flags
5354 */
5355 return true;
5356}
5357
5358static bool tr_valid(struct kvm_vcpu *vcpu)
5359{
5360 struct kvm_segment tr;
5361
5362 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5363
Avi Kivity1872a3f2009-01-04 23:26:52 +02005364 if (tr.unusable)
5365 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005366 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005367 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005368 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005369 return false;
5370 if (!tr.present)
5371 return false;
5372
5373 return true;
5374}
5375
5376static bool ldtr_valid(struct kvm_vcpu *vcpu)
5377{
5378 struct kvm_segment ldtr;
5379
5380 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5381
Avi Kivity1872a3f2009-01-04 23:26:52 +02005382 if (ldtr.unusable)
5383 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005384 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005385 return false;
5386 if (ldtr.type != 2)
5387 return false;
5388 if (!ldtr.present)
5389 return false;
5390
5391 return true;
5392}
5393
5394static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5395{
5396 struct kvm_segment cs, ss;
5397
5398 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5399 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5400
Nadav Amitb32a9912015-03-29 16:33:04 +03005401 return ((cs.selector & SEGMENT_RPL_MASK) ==
5402 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005403}
5404
5405/*
5406 * Check if guest state is valid. Returns true if valid, false if
5407 * not.
5408 * We assume that registers are always usable
5409 */
5410static bool guest_state_valid(struct kvm_vcpu *vcpu)
5411{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005412 if (enable_unrestricted_guest)
5413 return true;
5414
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005415 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005416 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005417 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5418 return false;
5419 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5420 return false;
5421 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5422 return false;
5423 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5424 return false;
5425 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5426 return false;
5427 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5428 return false;
5429 } else {
5430 /* protected mode guest state checks */
5431 if (!cs_ss_rpl_check(vcpu))
5432 return false;
5433 if (!code_segment_valid(vcpu))
5434 return false;
5435 if (!stack_segment_valid(vcpu))
5436 return false;
5437 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5438 return false;
5439 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5440 return false;
5441 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5442 return false;
5443 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5444 return false;
5445 if (!tr_valid(vcpu))
5446 return false;
5447 if (!ldtr_valid(vcpu))
5448 return false;
5449 }
5450 /* TODO:
5451 * - Add checks on RIP
5452 * - Add checks on RFLAGS
5453 */
5454
5455 return true;
5456}
5457
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005458static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5459{
5460 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5461}
5462
Mike Dayd77c26f2007-10-08 09:02:08 -04005463static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005465 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005466 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005467 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005469 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005470 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005471 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5472 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005473 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005474 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005475 r = kvm_write_guest_page(kvm, fn++, &data,
5476 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005477 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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5483 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005484 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005485 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005486 r = kvm_write_guest_page(kvm, fn, &data,
5487 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5488 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005489out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005490 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005491 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005492}
5493
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005494static int init_rmode_identity_map(struct kvm *kvm)
5495{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005496 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005497 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005498 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005499 u32 tmp;
5500
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005501 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005502 mutex_lock(&kvm->slots_lock);
5503
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005504 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005505 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005506
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005507 if (!kvm_vmx->ept_identity_map_addr)
5508 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5509 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005510
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005511 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005512 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005513 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005514 goto out2;
5515
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005516 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005517 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5518 if (r < 0)
5519 goto out;
5520 /* Set up identity-mapping pagetable for EPT in real mode */
5521 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5522 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5523 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5524 r = kvm_write_guest_page(kvm, identity_map_pfn,
5525 &tmp, i * sizeof(tmp), sizeof(tmp));
5526 if (r < 0)
5527 goto out;
5528 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005529 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005530
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005531out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005532 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005533
5534out2:
5535 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005536 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005537}
5538
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539static void seg_setup(int seg)
5540{
Mathias Krause772e0312012-08-30 01:30:19 +02005541 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005542 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543
5544 vmcs_write16(sf->selector, 0);
5545 vmcs_writel(sf->base, 0);
5546 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005547 ar = 0x93;
5548 if (seg == VCPU_SREG_CS)
5549 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005550
5551 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552}
5553
Sheng Yangf78e0e22007-10-29 09:40:42 +08005554static int alloc_apic_access_page(struct kvm *kvm)
5555{
Xiao Guangrong44841412012-09-07 14:14:20 +08005556 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005557 int r = 0;
5558
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005559 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005560 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005561 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005562 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5563 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005564 if (r)
5565 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005566
Tang Chen73a6d942014-09-11 13:38:00 +08005567 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005568 if (is_error_page(page)) {
5569 r = -EFAULT;
5570 goto out;
5571 }
5572
Tang Chenc24ae0d2014-09-24 15:57:58 +08005573 /*
5574 * Do not pin the page in memory, so that memory hot-unplug
5575 * is able to migrate it.
5576 */
5577 put_page(page);
5578 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005579out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005580 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005581 return r;
5582}
5583
Wanpeng Li991e7a02015-09-16 17:30:05 +08005584static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005585{
5586 int vpid;
5587
Avi Kivity919818a2009-03-23 18:01:29 +02005588 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005589 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005590 spin_lock(&vmx_vpid_lock);
5591 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005592 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005593 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005594 else
5595 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005596 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005597 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005598}
5599
Wanpeng Li991e7a02015-09-16 17:30:05 +08005600static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005601{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005602 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005603 return;
5604 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005605 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005606 spin_unlock(&vmx_vpid_lock);
5607}
5608
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005609static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5610 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005611{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005612 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005613
5614 if (!cpu_has_vmx_msr_bitmap())
5615 return;
5616
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005617 if (static_branch_unlikely(&enable_evmcs))
5618 evmcs_touch_msr_bitmap();
5619
Sheng Yang25c5f222008-03-28 13:18:56 +08005620 /*
5621 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5622 * have the write-low and read-high bitmap offsets the wrong way round.
5623 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5624 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005625 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005626 if (type & MSR_TYPE_R)
5627 /* read-low */
5628 __clear_bit(msr, msr_bitmap + 0x000 / f);
5629
5630 if (type & MSR_TYPE_W)
5631 /* write-low */
5632 __clear_bit(msr, msr_bitmap + 0x800 / f);
5633
Sheng Yang25c5f222008-03-28 13:18:56 +08005634 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5635 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005636 if (type & MSR_TYPE_R)
5637 /* read-high */
5638 __clear_bit(msr, msr_bitmap + 0x400 / f);
5639
5640 if (type & MSR_TYPE_W)
5641 /* write-high */
5642 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5643
5644 }
5645}
5646
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005647static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5648 u32 msr, int type)
5649{
5650 int f = sizeof(unsigned long);
5651
5652 if (!cpu_has_vmx_msr_bitmap())
5653 return;
5654
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005655 if (static_branch_unlikely(&enable_evmcs))
5656 evmcs_touch_msr_bitmap();
5657
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005658 /*
5659 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5660 * have the write-low and read-high bitmap offsets the wrong way round.
5661 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5662 */
5663 if (msr <= 0x1fff) {
5664 if (type & MSR_TYPE_R)
5665 /* read-low */
5666 __set_bit(msr, msr_bitmap + 0x000 / f);
5667
5668 if (type & MSR_TYPE_W)
5669 /* write-low */
5670 __set_bit(msr, msr_bitmap + 0x800 / f);
5671
5672 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5673 msr &= 0x1fff;
5674 if (type & MSR_TYPE_R)
5675 /* read-high */
5676 __set_bit(msr, msr_bitmap + 0x400 / f);
5677
5678 if (type & MSR_TYPE_W)
5679 /* write-high */
5680 __set_bit(msr, msr_bitmap + 0xc00 / f);
5681
5682 }
5683}
5684
5685static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5686 u32 msr, int type, bool value)
5687{
5688 if (value)
5689 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5690 else
5691 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5692}
5693
Wincy Vanf2b93282015-02-03 23:56:03 +08005694/*
5695 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5696 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5697 */
5698static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5699 unsigned long *msr_bitmap_nested,
5700 u32 msr, int type)
5701{
5702 int f = sizeof(unsigned long);
5703
Wincy Vanf2b93282015-02-03 23:56:03 +08005704 /*
5705 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5706 * have the write-low and read-high bitmap offsets the wrong way round.
5707 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5708 */
5709 if (msr <= 0x1fff) {
5710 if (type & MSR_TYPE_R &&
5711 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5712 /* read-low */
5713 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5714
5715 if (type & MSR_TYPE_W &&
5716 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5717 /* write-low */
5718 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5719
5720 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5721 msr &= 0x1fff;
5722 if (type & MSR_TYPE_R &&
5723 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5724 /* read-high */
5725 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5726
5727 if (type & MSR_TYPE_W &&
5728 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5729 /* write-high */
5730 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5731
5732 }
5733}
5734
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005735static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005736{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005737 u8 mode = 0;
5738
5739 if (cpu_has_secondary_exec_ctrls() &&
5740 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5741 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5742 mode |= MSR_BITMAP_MODE_X2APIC;
5743 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5744 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5745 }
5746
5747 if (is_long_mode(vcpu))
5748 mode |= MSR_BITMAP_MODE_LM;
5749
5750 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005751}
5752
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005753#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5754
5755static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5756 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005757{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005758 int msr;
5759
5760 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5761 unsigned word = msr / BITS_PER_LONG;
5762 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5763 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005764 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005765
5766 if (mode & MSR_BITMAP_MODE_X2APIC) {
5767 /*
5768 * TPR reads and writes can be virtualized even if virtual interrupt
5769 * delivery is not in use.
5770 */
5771 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5772 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5773 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5774 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5775 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5776 }
5777 }
5778}
5779
5780static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5781{
5782 struct vcpu_vmx *vmx = to_vmx(vcpu);
5783 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5784 u8 mode = vmx_msr_bitmap_mode(vcpu);
5785 u8 changed = mode ^ vmx->msr_bitmap_mode;
5786
5787 if (!changed)
5788 return;
5789
5790 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5791 !(mode & MSR_BITMAP_MODE_LM));
5792
5793 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5794 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5795
5796 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005797}
5798
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005799static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005800{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005801 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005802}
5803
David Matlackc9f04402017-08-01 14:00:40 -07005804static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5805{
5806 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5807 gfn_t gfn;
5808
5809 /*
5810 * Don't need to mark the APIC access page dirty; it is never
5811 * written to by the CPU during APIC virtualization.
5812 */
5813
5814 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5815 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5816 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5817 }
5818
5819 if (nested_cpu_has_posted_intr(vmcs12)) {
5820 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5821 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5822 }
5823}
5824
5825
David Hildenbrand6342c502017-01-25 11:58:58 +01005826static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005827{
5828 struct vcpu_vmx *vmx = to_vmx(vcpu);
5829 int max_irr;
5830 void *vapic_page;
5831 u16 status;
5832
David Matlackc9f04402017-08-01 14:00:40 -07005833 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5834 return;
Wincy Van705699a2015-02-03 23:58:17 +08005835
David Matlackc9f04402017-08-01 14:00:40 -07005836 vmx->nested.pi_pending = false;
5837 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5838 return;
Wincy Van705699a2015-02-03 23:58:17 +08005839
David Matlackc9f04402017-08-01 14:00:40 -07005840 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5841 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005842 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005843 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5844 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005845 kunmap(vmx->nested.virtual_apic_page);
5846
5847 status = vmcs_read16(GUEST_INTR_STATUS);
5848 if ((u8)max_irr > ((u8)status & 0xff)) {
5849 status &= ~0xff;
5850 status |= (u8)max_irr;
5851 vmcs_write16(GUEST_INTR_STATUS, status);
5852 }
5853 }
David Matlackc9f04402017-08-01 14:00:40 -07005854
5855 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005856}
5857
Wincy Van06a55242017-04-28 13:13:59 +08005858static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5859 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005860{
5861#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005862 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5863
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005864 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005865 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005866 * The vector of interrupt to be delivered to vcpu had
5867 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005868 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005869 * Following cases will be reached in this block, and
5870 * we always send a notification event in all cases as
5871 * explained below.
5872 *
5873 * Case 1: vcpu keeps in non-root mode. Sending a
5874 * notification event posts the interrupt to vcpu.
5875 *
5876 * Case 2: vcpu exits to root mode and is still
5877 * runnable. PIR will be synced to vIRR before the
5878 * next vcpu entry. Sending a notification event in
5879 * this case has no effect, as vcpu is not in root
5880 * mode.
5881 *
5882 * Case 3: vcpu exits to root mode and is blocked.
5883 * vcpu_block() has already synced PIR to vIRR and
5884 * never blocks vcpu if vIRR is not cleared. Therefore,
5885 * a blocked vcpu here does not wait for any requested
5886 * interrupts in PIR, and sending a notification event
5887 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005888 */
Feng Wu28b835d2015-09-18 22:29:54 +08005889
Wincy Van06a55242017-04-28 13:13:59 +08005890 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005891 return true;
5892 }
5893#endif
5894 return false;
5895}
5896
Wincy Van705699a2015-02-03 23:58:17 +08005897static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5898 int vector)
5899{
5900 struct vcpu_vmx *vmx = to_vmx(vcpu);
5901
5902 if (is_guest_mode(vcpu) &&
5903 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005904 /*
5905 * If a posted intr is not recognized by hardware,
5906 * we will accomplish it in the next vmentry.
5907 */
5908 vmx->nested.pi_pending = true;
5909 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005910 /* the PIR and ON have been set by L1. */
5911 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5912 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005913 return 0;
5914 }
5915 return -1;
5916}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005917/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005918 * Send interrupt to vcpu via posted interrupt way.
5919 * 1. If target vcpu is running(non-root mode), send posted interrupt
5920 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5921 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5922 * interrupt from PIR in next vmentry.
5923 */
5924static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5925{
5926 struct vcpu_vmx *vmx = to_vmx(vcpu);
5927 int r;
5928
Wincy Van705699a2015-02-03 23:58:17 +08005929 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5930 if (!r)
5931 return;
5932
Yang Zhanga20ed542013-04-11 19:25:15 +08005933 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5934 return;
5935
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005936 /* If a previous notification has sent the IPI, nothing to do. */
5937 if (pi_test_and_set_on(&vmx->pi_desc))
5938 return;
5939
Wincy Van06a55242017-04-28 13:13:59 +08005940 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005941 kvm_vcpu_kick(vcpu);
5942}
5943
Avi Kivity6aa8b732006-12-10 02:21:36 -08005944/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005945 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5946 * will not change in the lifetime of the guest.
5947 * Note that host-state that does change is set elsewhere. E.g., host-state
5948 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5949 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005950static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005951{
5952 u32 low32, high32;
5953 unsigned long tmpl;
5954 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005955 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005956
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005957 cr0 = read_cr0();
5958 WARN_ON(cr0 & X86_CR0_TS);
5959 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005960
5961 /*
5962 * Save the most likely value for this task's CR3 in the VMCS.
5963 * We can't use __get_current_cr3_fast() because we're not atomic.
5964 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005965 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005966 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005967 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005968
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005969 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005970 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005971 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005972 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005973
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005974 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005975#ifdef CONFIG_X86_64
5976 /*
5977 * Load null selectors, so we can avoid reloading them in
5978 * __vmx_load_host_state(), in case userspace uses the null selectors
5979 * too (the expected case).
5980 */
5981 vmcs_write16(HOST_DS_SELECTOR, 0);
5982 vmcs_write16(HOST_ES_SELECTOR, 0);
5983#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005984 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5985 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005986#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005987 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5988 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5989
Juergen Gross87930012017-09-04 12:25:27 +02005990 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005991 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005992 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005993
Avi Kivity83287ea422012-09-16 15:10:57 +03005994 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005995
5996 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5997 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5998 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5999 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6000
6001 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6002 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6003 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6004 }
6005}
6006
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006007static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6008{
6009 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6010 if (enable_ept)
6011 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006012 if (is_guest_mode(&vmx->vcpu))
6013 vmx->vcpu.arch.cr4_guest_owned_bits &=
6014 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006015 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6016}
6017
Yang Zhang01e439b2013-04-11 19:25:12 +08006018static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6019{
6020 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6021
Andrey Smetanind62caab2015-11-10 15:36:33 +03006022 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006023 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006024
6025 if (!enable_vnmi)
6026 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6027
Yunhong Jiang64672c92016-06-13 14:19:59 -07006028 /* Enable the preemption timer dynamically */
6029 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006030 return pin_based_exec_ctrl;
6031}
6032
Andrey Smetanind62caab2015-11-10 15:36:33 +03006033static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6034{
6035 struct vcpu_vmx *vmx = to_vmx(vcpu);
6036
6037 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006038 if (cpu_has_secondary_exec_ctrls()) {
6039 if (kvm_vcpu_apicv_active(vcpu))
6040 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6041 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6042 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6043 else
6044 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6045 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6046 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6047 }
6048
6049 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006050 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006051}
6052
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006053static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6054{
6055 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006056
6057 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6058 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6059
Paolo Bonzini35754c92015-07-29 12:05:37 +02006060 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006061 exec_control &= ~CPU_BASED_TPR_SHADOW;
6062#ifdef CONFIG_X86_64
6063 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6064 CPU_BASED_CR8_LOAD_EXITING;
6065#endif
6066 }
6067 if (!enable_ept)
6068 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6069 CPU_BASED_CR3_LOAD_EXITING |
6070 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006071 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6072 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6073 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006074 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6075 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006076 return exec_control;
6077}
6078
Jim Mattson45ec3682017-08-23 16:32:04 -07006079static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006080{
Jim Mattson45ec3682017-08-23 16:32:04 -07006081 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006082 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006083}
6084
Jim Mattson75f4fc82017-08-23 16:32:03 -07006085static bool vmx_rdseed_supported(void)
6086{
6087 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006088 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006089}
6090
Paolo Bonzini80154d72017-08-24 13:55:35 +02006091static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006092{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006093 struct kvm_vcpu *vcpu = &vmx->vcpu;
6094
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006095 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006096
Paolo Bonzini80154d72017-08-24 13:55:35 +02006097 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006098 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6099 if (vmx->vpid == 0)
6100 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6101 if (!enable_ept) {
6102 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6103 enable_unrestricted_guest = 0;
6104 }
6105 if (!enable_unrestricted_guest)
6106 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006107 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006108 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006109 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006110 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6111 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006112 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006113
6114 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6115 * in vmx_set_cr4. */
6116 exec_control &= ~SECONDARY_EXEC_DESC;
6117
Abel Gordonabc4fc52013-04-18 14:35:25 +03006118 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6119 (handle_vmptrld).
6120 We can NOT enable shadow_vmcs here because we don't have yet
6121 a current VMCS12
6122 */
6123 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006124
6125 if (!enable_pml)
6126 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006127
Paolo Bonzini3db13482017-08-24 14:48:03 +02006128 if (vmx_xsaves_supported()) {
6129 /* Exposing XSAVES only when XSAVE is exposed */
6130 bool xsaves_enabled =
6131 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6132 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6133
6134 if (!xsaves_enabled)
6135 exec_control &= ~SECONDARY_EXEC_XSAVES;
6136
6137 if (nested) {
6138 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006139 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006140 SECONDARY_EXEC_XSAVES;
6141 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006142 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006143 ~SECONDARY_EXEC_XSAVES;
6144 }
6145 }
6146
Paolo Bonzini80154d72017-08-24 13:55:35 +02006147 if (vmx_rdtscp_supported()) {
6148 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6149 if (!rdtscp_enabled)
6150 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6151
6152 if (nested) {
6153 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006154 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006155 SECONDARY_EXEC_RDTSCP;
6156 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006157 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006158 ~SECONDARY_EXEC_RDTSCP;
6159 }
6160 }
6161
6162 if (vmx_invpcid_supported()) {
6163 /* Exposing INVPCID only when PCID is exposed */
6164 bool invpcid_enabled =
6165 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6166 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6167
6168 if (!invpcid_enabled) {
6169 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6170 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6171 }
6172
6173 if (nested) {
6174 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006175 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006176 SECONDARY_EXEC_ENABLE_INVPCID;
6177 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006178 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006179 ~SECONDARY_EXEC_ENABLE_INVPCID;
6180 }
6181 }
6182
Jim Mattson45ec3682017-08-23 16:32:04 -07006183 if (vmx_rdrand_supported()) {
6184 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6185 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006186 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006187
6188 if (nested) {
6189 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006190 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006191 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006192 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006193 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006194 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006195 }
6196 }
6197
Jim Mattson75f4fc82017-08-23 16:32:03 -07006198 if (vmx_rdseed_supported()) {
6199 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6200 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006201 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006202
6203 if (nested) {
6204 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006205 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006206 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006207 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006208 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006209 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006210 }
6211 }
6212
Paolo Bonzini80154d72017-08-24 13:55:35 +02006213 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006214}
6215
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006216static void ept_set_mmio_spte_mask(void)
6217{
6218 /*
6219 * EPT Misconfigurations can be generated if the value of bits 2:0
6220 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006221 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006222 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6223 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006224}
6225
Wanpeng Lif53cd632014-12-02 19:14:58 +08006226#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006227/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006228 * Sets up the vmcs for emulated real mode.
6229 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006230static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006232#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006233 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006234#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006235 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236
Abel Gordon4607c2d2013-04-18 14:35:55 +03006237 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006238 /*
6239 * At vCPU creation, "VMWRITE to any supported field
6240 * in the VMCS" is supported, so use the more
6241 * permissive vmx_vmread_bitmap to specify both read
6242 * and write permissions for the shadow VMCS.
6243 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006244 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006245 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006246 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006247 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006248 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006249
Avi Kivity6aa8b732006-12-10 02:21:36 -08006250 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6251
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006253 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006254 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006255
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006256 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006257
Dan Williamsdfa169b2016-06-02 11:17:24 -07006258 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006259 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006260 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006261 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006262 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006263
Andrey Smetanind62caab2015-11-10 15:36:33 +03006264 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006265 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6266 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6267 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6268 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6269
6270 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006271
Li RongQing0bcf2612015-12-03 13:29:34 +08006272 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006273 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006274 }
6275
Wanpeng Lib31c1142018-03-12 04:53:04 -07006276 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006277 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006278 vmx->ple_window = ple_window;
6279 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006280 }
6281
Xiao Guangrongc3707952011-07-12 03:28:04 +08006282 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6283 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006284 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6285
Avi Kivity9581d442010-10-19 16:46:55 +02006286 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6287 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006288 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006289#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006290 rdmsrl(MSR_FS_BASE, a);
6291 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6292 rdmsrl(MSR_GS_BASE, a);
6293 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6294#else
6295 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6296 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6297#endif
6298
Bandan Das2a499e42017-08-03 15:54:41 -04006299 if (cpu_has_vmx_vmfunc())
6300 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6301
Eddie Dong2cc51562007-05-21 07:28:09 +03006302 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6303 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006304 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006305 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006306 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006307
Radim Krčmář74545702015-04-27 15:11:25 +02006308 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6309 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006310
Paolo Bonzini03916db2014-07-24 14:21:57 +02006311 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006312 u32 index = vmx_msr_index[i];
6313 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006314 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006315
6316 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6317 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006318 if (wrmsr_safe(index, data_low, data_high) < 0)
6319 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006320 vmx->guest_msrs[j].index = i;
6321 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006322 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006323 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006326 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6327 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006328
6329 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006330
6331 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006332 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006333
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006334 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6335 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6336
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006337 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006338
Wanpeng Lif53cd632014-12-02 19:14:58 +08006339 if (vmx_xsaves_supported())
6340 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6341
Peter Feiner4e595162016-07-07 14:49:58 -07006342 if (enable_pml) {
6343 ASSERT(vmx->pml_pg);
6344 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6345 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6346 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006347}
6348
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006349static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006350{
6351 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006352 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006353 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006354
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006355 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006356 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006357
Wanpeng Li518e7b92018-02-28 14:03:31 +08006358 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006359 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006360 kvm_set_cr8(vcpu, 0);
6361
6362 if (!init_event) {
6363 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6364 MSR_IA32_APICBASE_ENABLE;
6365 if (kvm_vcpu_is_reset_bsp(vcpu))
6366 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6367 apic_base_msr.host_initiated = true;
6368 kvm_set_apic_base(vcpu, &apic_base_msr);
6369 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006370
Avi Kivity2fb92db2011-04-27 19:42:18 +03006371 vmx_segment_cache_clear(vmx);
6372
Avi Kivity5706be02008-08-20 15:07:31 +03006373 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006374 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006375 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006376
6377 seg_setup(VCPU_SREG_DS);
6378 seg_setup(VCPU_SREG_ES);
6379 seg_setup(VCPU_SREG_FS);
6380 seg_setup(VCPU_SREG_GS);
6381 seg_setup(VCPU_SREG_SS);
6382
6383 vmcs_write16(GUEST_TR_SELECTOR, 0);
6384 vmcs_writel(GUEST_TR_BASE, 0);
6385 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6386 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6387
6388 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6389 vmcs_writel(GUEST_LDTR_BASE, 0);
6390 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6391 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6392
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006393 if (!init_event) {
6394 vmcs_write32(GUEST_SYSENTER_CS, 0);
6395 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6396 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6397 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6398 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006399
Wanpeng Lic37c2872017-11-20 14:52:21 -08006400 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006401 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006402
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006403 vmcs_writel(GUEST_GDTR_BASE, 0);
6404 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6405
6406 vmcs_writel(GUEST_IDTR_BASE, 0);
6407 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6408
Anthony Liguori443381a2010-12-06 10:53:38 -06006409 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006410 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006411 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006412 if (kvm_mpx_supported())
6413 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006414
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006415 setup_msrs(vmx);
6416
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6418
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006419 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006420 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006421 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006422 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006423 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006424 vmcs_write32(TPR_THRESHOLD, 0);
6425 }
6426
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006427 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006428
Sheng Yang2384d2b2008-01-17 15:14:33 +08006429 if (vmx->vpid != 0)
6430 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6431
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006432 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006433 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006434 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006435 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006436 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006437
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006438 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006439
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006440 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006441 if (init_event)
6442 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006443}
6444
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006445/*
6446 * In nested virtualization, check if L1 asked to exit on external interrupts.
6447 * For most existing hypervisors, this will always return true.
6448 */
6449static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6450{
6451 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6452 PIN_BASED_EXT_INTR_MASK;
6453}
6454
Bandan Das77b0f5d2014-04-19 18:17:45 -04006455/*
6456 * In nested virtualization, check if L1 has set
6457 * VM_EXIT_ACK_INTR_ON_EXIT
6458 */
6459static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6460{
6461 return get_vmcs12(vcpu)->vm_exit_controls &
6462 VM_EXIT_ACK_INTR_ON_EXIT;
6463}
6464
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006465static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6466{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006467 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006468}
6469
Jan Kiszkac9a79532014-03-07 20:03:15 +01006470static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006471{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006472 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6473 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006474}
6475
Jan Kiszkac9a79532014-03-07 20:03:15 +01006476static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006477{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006478 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006479 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006480 enable_irq_window(vcpu);
6481 return;
6482 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006483
Paolo Bonzini47c01522016-12-19 11:44:07 +01006484 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6485 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006486}
6487
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006488static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006489{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006491 uint32_t intr;
6492 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006493
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006494 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006495
Avi Kivityfa89a812008-09-01 15:57:51 +03006496 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006497 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006498 int inc_eip = 0;
6499 if (vcpu->arch.interrupt.soft)
6500 inc_eip = vcpu->arch.event_exit_inst_len;
6501 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006502 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006503 return;
6504 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006505 intr = irq | INTR_INFO_VALID_MASK;
6506 if (vcpu->arch.interrupt.soft) {
6507 intr |= INTR_TYPE_SOFT_INTR;
6508 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6509 vmx->vcpu.arch.event_exit_inst_len);
6510 } else
6511 intr |= INTR_TYPE_EXT_INTR;
6512 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006513
6514 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006515}
6516
Sheng Yangf08864b2008-05-15 18:23:25 +08006517static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6518{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006519 struct vcpu_vmx *vmx = to_vmx(vcpu);
6520
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006521 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006522 /*
6523 * Tracking the NMI-blocked state in software is built upon
6524 * finding the next open IRQ window. This, in turn, depends on
6525 * well-behaving guests: They have to keep IRQs disabled at
6526 * least as long as the NMI handler runs. Otherwise we may
6527 * cause NMI nesting, maybe breaking the guest. But as this is
6528 * highly unlikely, we can live with the residual risk.
6529 */
6530 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6531 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6532 }
6533
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006534 ++vcpu->stat.nmi_injections;
6535 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006536
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006537 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006538 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006539 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006540 return;
6541 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006542
Sheng Yangf08864b2008-05-15 18:23:25 +08006543 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6544 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006545
6546 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006547}
6548
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006549static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6550{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006551 struct vcpu_vmx *vmx = to_vmx(vcpu);
6552 bool masked;
6553
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006554 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006555 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006556 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006557 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006558 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6559 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6560 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006561}
6562
6563static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6564{
6565 struct vcpu_vmx *vmx = to_vmx(vcpu);
6566
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006567 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006568 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6569 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6570 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6571 }
6572 } else {
6573 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6574 if (masked)
6575 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6576 GUEST_INTR_STATE_NMI);
6577 else
6578 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6579 GUEST_INTR_STATE_NMI);
6580 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006581}
6582
Jan Kiszka2505dc92013-04-14 12:12:47 +02006583static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6584{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006585 if (to_vmx(vcpu)->nested.nested_run_pending)
6586 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006587
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006588 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006589 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6590 return 0;
6591
Jan Kiszka2505dc92013-04-14 12:12:47 +02006592 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6593 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6594 | GUEST_INTR_STATE_NMI));
6595}
6596
Gleb Natapov78646122009-03-23 12:12:11 +02006597static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6598{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006599 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6600 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006601 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6602 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006603}
6604
Izik Eiduscbc94022007-10-25 00:29:55 +02006605static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6606{
6607 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006608
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006609 if (enable_unrestricted_guest)
6610 return 0;
6611
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006612 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6613 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006614 if (ret)
6615 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006616 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006617 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006618}
6619
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006620static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6621{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006622 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006623 return 0;
6624}
6625
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006626static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006628 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006629 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006630 /*
6631 * Update instruction length as we may reinject the exception
6632 * from user space while in guest debugging mode.
6633 */
6634 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6635 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006636 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006637 return false;
6638 /* fall through */
6639 case DB_VECTOR:
6640 if (vcpu->guest_debug &
6641 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6642 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006643 /* fall through */
6644 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006645 case OF_VECTOR:
6646 case BR_VECTOR:
6647 case UD_VECTOR:
6648 case DF_VECTOR:
6649 case SS_VECTOR:
6650 case GP_VECTOR:
6651 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006652 return true;
6653 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006654 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006655 return false;
6656}
6657
6658static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6659 int vec, u32 err_code)
6660{
6661 /*
6662 * Instruction with address size override prefix opcode 0x67
6663 * Cause the #SS fault with 0 error code in VM86 mode.
6664 */
6665 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6666 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6667 if (vcpu->arch.halt_request) {
6668 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006669 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006670 }
6671 return 1;
6672 }
6673 return 0;
6674 }
6675
6676 /*
6677 * Forward all other exceptions that are valid in real mode.
6678 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6679 * the required debugging infrastructure rework.
6680 */
6681 kvm_queue_exception(vcpu, vec);
6682 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006683}
6684
Andi Kleena0861c02009-06-08 17:37:09 +08006685/*
6686 * Trigger machine check on the host. We assume all the MSRs are already set up
6687 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6688 * We pass a fake environment to the machine check handler because we want
6689 * the guest to be always treated like user space, no matter what context
6690 * it used internally.
6691 */
6692static void kvm_machine_check(void)
6693{
6694#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6695 struct pt_regs regs = {
6696 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6697 .flags = X86_EFLAGS_IF,
6698 };
6699
6700 do_machine_check(&regs, 0);
6701#endif
6702}
6703
Avi Kivity851ba692009-08-24 11:10:17 +03006704static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006705{
6706 /* already handled by vcpu_run */
6707 return 1;
6708}
6709
Avi Kivity851ba692009-08-24 11:10:17 +03006710static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006711{
Avi Kivity1155f762007-11-22 11:30:47 +02006712 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006713 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006714 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006715 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006716 u32 vect_info;
6717 enum emulation_result er;
6718
Avi Kivity1155f762007-11-22 11:30:47 +02006719 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006720 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006721
Andi Kleena0861c02009-06-08 17:37:09 +08006722 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006723 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006724
Jim Mattsonef85b672016-12-12 11:01:37 -08006725 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006726 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006727
Wanpeng Li082d06e2018-04-03 16:28:48 -07006728 if (is_invalid_opcode(intr_info))
6729 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006730
Avi Kivity6aa8b732006-12-10 02:21:36 -08006731 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006732 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006733 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006734
Liran Alon9e869482018-03-12 13:12:51 +02006735 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6736 WARN_ON_ONCE(!enable_vmware_backdoor);
6737 er = emulate_instruction(vcpu,
6738 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6739 if (er == EMULATE_USER_EXIT)
6740 return 0;
6741 else if (er != EMULATE_DONE)
6742 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6743 return 1;
6744 }
6745
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006746 /*
6747 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6748 * MMIO, it is better to report an internal error.
6749 * See the comments in vmx_handle_exit.
6750 */
6751 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6752 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6753 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6754 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006755 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006756 vcpu->run->internal.data[0] = vect_info;
6757 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006758 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006759 return 0;
6760 }
6761
Avi Kivity6aa8b732006-12-10 02:21:36 -08006762 if (is_page_fault(intr_info)) {
6763 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006764 /* EPT won't cause page fault directly */
6765 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006766 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006767 }
6768
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006769 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006770
6771 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6772 return handle_rmode_exception(vcpu, ex_no, error_code);
6773
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006774 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006775 case AC_VECTOR:
6776 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6777 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006778 case DB_VECTOR:
6779 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6780 if (!(vcpu->guest_debug &
6781 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006782 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006783 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006784 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006785 skip_emulated_instruction(vcpu);
6786
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006787 kvm_queue_exception(vcpu, DB_VECTOR);
6788 return 1;
6789 }
6790 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6791 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6792 /* fall through */
6793 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006794 /*
6795 * Update instruction length as we may reinject #BP from
6796 * user space while in guest debugging mode. Reading it for
6797 * #DB as well causes no harm, it is not used in that case.
6798 */
6799 vmx->vcpu.arch.event_exit_inst_len =
6800 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006801 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006802 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006803 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6804 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006805 break;
6806 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006807 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6808 kvm_run->ex.exception = ex_no;
6809 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006810 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006812 return 0;
6813}
6814
Avi Kivity851ba692009-08-24 11:10:17 +03006815static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006817 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006818 return 1;
6819}
6820
Avi Kivity851ba692009-08-24 11:10:17 +03006821static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006822{
Avi Kivity851ba692009-08-24 11:10:17 +03006823 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006824 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006825 return 0;
6826}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006827
Avi Kivity851ba692009-08-24 11:10:17 +03006828static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006829{
He, Qingbfdaab02007-09-12 14:18:28 +08006830 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006831 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006832 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006833
He, Qingbfdaab02007-09-12 14:18:28 +08006834 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006835 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006836
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006837 ++vcpu->stat.io_exits;
6838
Sean Christopherson432baf62018-03-08 08:57:26 -08006839 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006840 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006841
6842 port = exit_qualification >> 16;
6843 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006844 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006845
Sean Christophersondca7f122018-03-08 08:57:27 -08006846 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006847}
6848
Ingo Molnar102d8322007-02-19 14:37:47 +02006849static void
6850vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6851{
6852 /*
6853 * Patch in the VMCALL instruction:
6854 */
6855 hypercall[0] = 0x0f;
6856 hypercall[1] = 0x01;
6857 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006858}
6859
Guo Chao0fa06072012-06-28 15:16:19 +08006860/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006861static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6862{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006863 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006864 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6865 unsigned long orig_val = val;
6866
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006867 /*
6868 * We get here when L2 changed cr0 in a way that did not change
6869 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006870 * but did change L0 shadowed bits. So we first calculate the
6871 * effective cr0 value that L1 would like to write into the
6872 * hardware. It consists of the L2-owned bits from the new
6873 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006875 val = (val & ~vmcs12->cr0_guest_host_mask) |
6876 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6877
David Matlack38991522016-11-29 18:14:08 -08006878 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006879 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006880
6881 if (kvm_set_cr0(vcpu, val))
6882 return 1;
6883 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006884 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006885 } else {
6886 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006887 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006888 return 1;
David Matlack38991522016-11-29 18:14:08 -08006889
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006890 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006891 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006892}
6893
6894static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6895{
6896 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006897 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6898 unsigned long orig_val = val;
6899
6900 /* analogously to handle_set_cr0 */
6901 val = (val & ~vmcs12->cr4_guest_host_mask) |
6902 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6903 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006904 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006905 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006906 return 0;
6907 } else
6908 return kvm_set_cr4(vcpu, val);
6909}
6910
Paolo Bonzini0367f202016-07-12 10:44:55 +02006911static int handle_desc(struct kvm_vcpu *vcpu)
6912{
6913 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6914 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6915}
6916
Avi Kivity851ba692009-08-24 11:10:17 +03006917static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006918{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006919 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006920 int cr;
6921 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006922 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006923 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006924
He, Qingbfdaab02007-09-12 14:18:28 +08006925 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006926 cr = exit_qualification & 15;
6927 reg = (exit_qualification >> 8) & 15;
6928 switch ((exit_qualification >> 4) & 3) {
6929 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006930 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006931 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006932 switch (cr) {
6933 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006934 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006935 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006936 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006937 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006938 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006939 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006940 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006941 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006942 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006943 case 8: {
6944 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006945 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006946 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006947 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006948 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006949 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006950 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006951 return ret;
6952 /*
6953 * TODO: we might be squashing a
6954 * KVM_GUESTDBG_SINGLESTEP-triggered
6955 * KVM_EXIT_DEBUG here.
6956 */
Avi Kivity851ba692009-08-24 11:10:17 +03006957 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006958 return 0;
6959 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006960 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006961 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006962 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006963 WARN_ONCE(1, "Guest should always own CR0.TS");
6964 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006965 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006966 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006967 case 1: /*mov from cr*/
6968 switch (cr) {
6969 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006970 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006971 val = kvm_read_cr3(vcpu);
6972 kvm_register_write(vcpu, reg, val);
6973 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006976 val = kvm_get_cr8(vcpu);
6977 kvm_register_write(vcpu, reg, val);
6978 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006979 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006980 }
6981 break;
6982 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006983 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006984 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006985 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006986
Kyle Huey6affcbe2016-11-29 12:40:40 -08006987 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988 default:
6989 break;
6990 }
Avi Kivity851ba692009-08-24 11:10:17 +03006991 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006992 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006993 (int)(exit_qualification >> 4) & 3, cr);
6994 return 0;
6995}
6996
Avi Kivity851ba692009-08-24 11:10:17 +03006997static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998{
He, Qingbfdaab02007-09-12 14:18:28 +08006999 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007000 int dr, dr7, reg;
7001
7002 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7003 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7004
7005 /* First, if DR does not exist, trigger UD */
7006 if (!kvm_require_dr(vcpu, dr))
7007 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007008
Jan Kiszkaf2483412010-01-20 18:20:20 +01007009 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007010 if (!kvm_require_cpl(vcpu, 0))
7011 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007012 dr7 = vmcs_readl(GUEST_DR7);
7013 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007014 /*
7015 * As the vm-exit takes precedence over the debug trap, we
7016 * need to emulate the latter, either for the host or the
7017 * guest debugging itself.
7018 */
7019 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007020 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007021 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007022 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007023 vcpu->run->debug.arch.exception = DB_VECTOR;
7024 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007025 return 0;
7026 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007027 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007028 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007029 kvm_queue_exception(vcpu, DB_VECTOR);
7030 return 1;
7031 }
7032 }
7033
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007034 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007035 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7036 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007037
7038 /*
7039 * No more DR vmexits; force a reload of the debug registers
7040 * and reenter on this instruction. The next vmexit will
7041 * retrieve the full state of the debug registers.
7042 */
7043 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7044 return 1;
7045 }
7046
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007047 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7048 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007049 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007050
7051 if (kvm_get_dr(vcpu, dr, &val))
7052 return 1;
7053 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007054 } else
Nadav Amit57773922014-06-18 17:19:23 +03007055 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007056 return 1;
7057
Kyle Huey6affcbe2016-11-29 12:40:40 -08007058 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059}
7060
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007061static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7062{
7063 return vcpu->arch.dr6;
7064}
7065
7066static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7067{
7068}
7069
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007070static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7071{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007072 get_debugreg(vcpu->arch.db[0], 0);
7073 get_debugreg(vcpu->arch.db[1], 1);
7074 get_debugreg(vcpu->arch.db[2], 2);
7075 get_debugreg(vcpu->arch.db[3], 3);
7076 get_debugreg(vcpu->arch.dr6, 6);
7077 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7078
7079 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007080 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007081}
7082
Gleb Natapov020df072010-04-13 10:05:23 +03007083static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7084{
7085 vmcs_writel(GUEST_DR7, val);
7086}
7087
Avi Kivity851ba692009-08-24 11:10:17 +03007088static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007089{
Kyle Huey6a908b62016-11-29 12:40:37 -08007090 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007091}
7092
Avi Kivity851ba692009-08-24 11:10:17 +03007093static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007094{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007095 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007096 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007098 msr_info.index = ecx;
7099 msr_info.host_initiated = false;
7100 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007101 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007102 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007103 return 1;
7104 }
7105
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007106 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007107
Avi Kivity6aa8b732006-12-10 02:21:36 -08007108 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007109 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7110 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007111 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007112}
7113
Avi Kivity851ba692009-08-24 11:10:17 +03007114static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115{
Will Auld8fe8ab42012-11-29 12:42:12 -08007116 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007117 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7118 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7119 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120
Will Auld8fe8ab42012-11-29 12:42:12 -08007121 msr.data = data;
7122 msr.index = ecx;
7123 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007124 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007125 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007126 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127 return 1;
7128 }
7129
Avi Kivity59200272010-01-25 19:47:02 +02007130 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007131 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007132}
7133
Avi Kivity851ba692009-08-24 11:10:17 +03007134static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007135{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007136 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007137 return 1;
7138}
7139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007141{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007142 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7143 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007144
Avi Kivity3842d132010-07-27 12:30:24 +03007145 kvm_make_request(KVM_REQ_EVENT, vcpu);
7146
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007147 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148 return 1;
7149}
7150
Avi Kivity851ba692009-08-24 11:10:17 +03007151static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007152{
Avi Kivityd3bef152007-06-05 15:53:05 +03007153 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007154}
7155
Avi Kivity851ba692009-08-24 11:10:17 +03007156static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007157{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007158 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007159}
7160
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007161static int handle_invd(struct kvm_vcpu *vcpu)
7162{
Andre Przywara51d8b662010-12-21 11:12:02 +01007163 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007164}
7165
Avi Kivity851ba692009-08-24 11:10:17 +03007166static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007167{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007168 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007169
7170 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007171 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007172}
7173
Avi Kivityfee84b02011-11-10 14:57:25 +02007174static int handle_rdpmc(struct kvm_vcpu *vcpu)
7175{
7176 int err;
7177
7178 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007179 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007180}
7181
Avi Kivity851ba692009-08-24 11:10:17 +03007182static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007183{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007184 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007185}
7186
Dexuan Cui2acf9232010-06-10 11:27:12 +08007187static int handle_xsetbv(struct kvm_vcpu *vcpu)
7188{
7189 u64 new_bv = kvm_read_edx_eax(vcpu);
7190 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7191
7192 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007193 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007194 return 1;
7195}
7196
Wanpeng Lif53cd632014-12-02 19:14:58 +08007197static int handle_xsaves(struct kvm_vcpu *vcpu)
7198{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007199 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007200 WARN(1, "this should never happen\n");
7201 return 1;
7202}
7203
7204static int handle_xrstors(struct kvm_vcpu *vcpu)
7205{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007206 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007207 WARN(1, "this should never happen\n");
7208 return 1;
7209}
7210
Avi Kivity851ba692009-08-24 11:10:17 +03007211static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007212{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007213 if (likely(fasteoi)) {
7214 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7215 int access_type, offset;
7216
7217 access_type = exit_qualification & APIC_ACCESS_TYPE;
7218 offset = exit_qualification & APIC_ACCESS_OFFSET;
7219 /*
7220 * Sane guest uses MOV to write EOI, with written value
7221 * not cared. So make a short-circuit here by avoiding
7222 * heavy instruction emulation.
7223 */
7224 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7225 (offset == APIC_EOI)) {
7226 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007227 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007228 }
7229 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007230 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007231}
7232
Yang Zhangc7c9c562013-01-25 10:18:51 +08007233static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7234{
7235 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7236 int vector = exit_qualification & 0xff;
7237
7238 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7239 kvm_apic_set_eoi_accelerated(vcpu, vector);
7240 return 1;
7241}
7242
Yang Zhang83d4c282013-01-25 10:18:49 +08007243static int handle_apic_write(struct kvm_vcpu *vcpu)
7244{
7245 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7246 u32 offset = exit_qualification & 0xfff;
7247
7248 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7249 kvm_apic_write_nodecode(vcpu, offset);
7250 return 1;
7251}
7252
Avi Kivity851ba692009-08-24 11:10:17 +03007253static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007254{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007255 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007256 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007257 bool has_error_code = false;
7258 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007259 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007260 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007261
7262 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007263 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007264 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007265
7266 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7267
7268 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007269 if (reason == TASK_SWITCH_GATE && idt_v) {
7270 switch (type) {
7271 case INTR_TYPE_NMI_INTR:
7272 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007273 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007274 break;
7275 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007276 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007277 kvm_clear_interrupt_queue(vcpu);
7278 break;
7279 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007280 if (vmx->idt_vectoring_info &
7281 VECTORING_INFO_DELIVER_CODE_MASK) {
7282 has_error_code = true;
7283 error_code =
7284 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7285 }
7286 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007287 case INTR_TYPE_SOFT_EXCEPTION:
7288 kvm_clear_exception_queue(vcpu);
7289 break;
7290 default:
7291 break;
7292 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007293 }
Izik Eidus37817f22008-03-24 23:14:53 +02007294 tss_selector = exit_qualification;
7295
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007296 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7297 type != INTR_TYPE_EXT_INTR &&
7298 type != INTR_TYPE_NMI_INTR))
7299 skip_emulated_instruction(vcpu);
7300
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007301 if (kvm_task_switch(vcpu, tss_selector,
7302 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7303 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007304 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7305 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7306 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007307 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007308 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007309
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007310 /*
7311 * TODO: What about debug traps on tss switch?
7312 * Are we supposed to inject them and update dr6?
7313 */
7314
7315 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007316}
7317
Avi Kivity851ba692009-08-24 11:10:17 +03007318static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007319{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007320 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007321 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007322 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007323
Sheng Yangf9c617f2009-03-25 10:08:52 +08007324 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007325
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007326 /*
7327 * EPT violation happened while executing iret from NMI,
7328 * "blocked by NMI" bit has to be set before next VM entry.
7329 * There are errata that may cause this bit to not be set:
7330 * AAK134, BY25.
7331 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007332 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007333 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007334 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007335 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7336
Sheng Yang14394422008-04-28 12:24:45 +08007337 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007338 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007339
Junaid Shahid27959a42016-12-06 16:46:10 -08007340 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007341 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007342 ? PFERR_USER_MASK : 0;
7343 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007344 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007345 ? PFERR_WRITE_MASK : 0;
7346 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007347 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007348 ? PFERR_FETCH_MASK : 0;
7349 /* ept page table entry is present? */
7350 error_code |= (exit_qualification &
7351 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7352 EPT_VIOLATION_EXECUTABLE))
7353 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007354
Paolo Bonzinieebed242016-11-28 14:39:58 +01007355 error_code |= (exit_qualification & 0x100) != 0 ?
7356 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007357
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007358 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007359 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007360}
7361
Avi Kivity851ba692009-08-24 11:10:17 +03007362static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007363{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007364 gpa_t gpa;
7365
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007366 /*
7367 * A nested guest cannot optimize MMIO vmexits, because we have an
7368 * nGPA here instead of the required GPA.
7369 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007370 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007371 if (!is_guest_mode(vcpu) &&
7372 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007373 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007374 /*
7375 * Doing kvm_skip_emulated_instruction() depends on undefined
7376 * behavior: Intel's manual doesn't mandate
7377 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7378 * occurs and while on real hardware it was observed to be set,
7379 * other hypervisors (namely Hyper-V) don't set it, we end up
7380 * advancing IP with some random value. Disable fast mmio when
7381 * running nested and keep it for real hardware in hope that
7382 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7383 */
7384 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7385 return kvm_skip_emulated_instruction(vcpu);
7386 else
7387 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7388 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007389 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007390
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007391 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007392}
7393
Avi Kivity851ba692009-08-24 11:10:17 +03007394static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007395{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007396 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007397 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7398 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007399 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007400 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007401
7402 return 1;
7403}
7404
Mohammed Gamal80ced182009-09-01 12:48:18 +02007405static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007406{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007407 struct vcpu_vmx *vmx = to_vmx(vcpu);
7408 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007409 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007410 u32 cpu_exec_ctrl;
7411 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007412 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007413
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007414 /*
7415 * We should never reach the point where we are emulating L2
7416 * due to invalid guest state as that means we incorrectly
7417 * allowed a nested VMEntry with an invalid vmcs12.
7418 */
7419 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7420
Avi Kivity49e9d552010-09-19 14:34:08 +02007421 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7422 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007423
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007424 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007425 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007426 return handle_interrupt_window(&vmx->vcpu);
7427
Radim Krčmář72875d82017-04-26 22:32:19 +02007428 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007429 return 1;
7430
Liran Alon9b8ae632017-11-05 16:56:34 +02007431 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007432
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007433 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007434 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007435 ret = 0;
7436 goto out;
7437 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007438
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007439 if (err != EMULATE_DONE)
7440 goto emulation_error;
7441
7442 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7443 vcpu->arch.exception.pending)
7444 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007445
Gleb Natapov8d76c492013-05-08 18:38:44 +03007446 if (vcpu->arch.halt_request) {
7447 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007448 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007449 goto out;
7450 }
7451
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007452 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007453 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007454 if (need_resched())
7455 schedule();
7456 }
7457
Mohammed Gamal80ced182009-09-01 12:48:18 +02007458out:
7459 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007460
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007461emulation_error:
7462 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7463 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7464 vcpu->run->internal.ndata = 0;
7465 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007466}
7467
7468static void grow_ple_window(struct kvm_vcpu *vcpu)
7469{
7470 struct vcpu_vmx *vmx = to_vmx(vcpu);
7471 int old = vmx->ple_window;
7472
Babu Mogerc8e88712018-03-16 16:37:24 -04007473 vmx->ple_window = __grow_ple_window(old, ple_window,
7474 ple_window_grow,
7475 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007476
7477 if (vmx->ple_window != old)
7478 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007479
7480 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007481}
7482
7483static void shrink_ple_window(struct kvm_vcpu *vcpu)
7484{
7485 struct vcpu_vmx *vmx = to_vmx(vcpu);
7486 int old = vmx->ple_window;
7487
Babu Mogerc8e88712018-03-16 16:37:24 -04007488 vmx->ple_window = __shrink_ple_window(old, ple_window,
7489 ple_window_shrink,
7490 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007491
7492 if (vmx->ple_window != old)
7493 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007494
7495 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007496}
7497
7498/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007499 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7500 */
7501static void wakeup_handler(void)
7502{
7503 struct kvm_vcpu *vcpu;
7504 int cpu = smp_processor_id();
7505
7506 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7507 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7508 blocked_vcpu_list) {
7509 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7510
7511 if (pi_test_on(pi_desc) == 1)
7512 kvm_vcpu_kick(vcpu);
7513 }
7514 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7515}
7516
Peng Haoe01bca22018-04-07 05:47:32 +08007517static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007518{
7519 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7520 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7521 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7522 0ull, VMX_EPT_EXECUTABLE_MASK,
7523 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007524 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007525
7526 ept_set_mmio_spte_mask();
7527 kvm_enable_tdp();
7528}
7529
Tiejun Chenf2c76482014-10-28 10:14:47 +08007530static __init int hardware_setup(void)
7531{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007532 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007533 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007534
7535 rdmsrl_safe(MSR_EFER, &host_efer);
7536
7537 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7538 kvm_define_shared_msr(i, vmx_msr_index[i]);
7539
Radim Krčmář23611332016-09-29 22:41:33 +02007540 for (i = 0; i < VMX_BITMAP_NR; i++) {
7541 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7542 if (!vmx_bitmap[i])
7543 goto out;
7544 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007545
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007546 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7547 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7548
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007549 if (setup_vmcs_config(&vmcs_config) < 0) {
7550 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007551 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007552 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007553
7554 if (boot_cpu_has(X86_FEATURE_NX))
7555 kvm_enable_efer_bits(EFER_NX);
7556
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007557 if (boot_cpu_has(X86_FEATURE_MPX)) {
7558 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7559 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7560 }
7561
Wanpeng Li08d839c2017-03-23 05:30:08 -07007562 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7563 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007564 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007565
Tiejun Chenf2c76482014-10-28 10:14:47 +08007566 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007567 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007568 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007569 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007570 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007571
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007572 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007573 enable_ept_ad_bits = 0;
7574
Wanpeng Li8ad81822017-10-09 15:51:53 -07007575 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007576 enable_unrestricted_guest = 0;
7577
Paolo Bonziniad15a292015-01-30 16:18:49 +01007578 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007579 flexpriority_enabled = 0;
7580
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007581 if (!cpu_has_virtual_nmis())
7582 enable_vnmi = 0;
7583
Paolo Bonziniad15a292015-01-30 16:18:49 +01007584 /*
7585 * set_apic_access_page_addr() is used to reload apic access
7586 * page upon invalidation. No need to do anything if not
7587 * using the APIC_ACCESS_ADDR VMCS field.
7588 */
7589 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007590 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007591
7592 if (!cpu_has_vmx_tpr_shadow())
7593 kvm_x86_ops->update_cr8_intercept = NULL;
7594
7595 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7596 kvm_disable_largepages();
7597
Wanpeng Li0f107682017-09-28 18:06:24 -07007598 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007599 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007600 ple_window = 0;
7601 ple_window_grow = 0;
7602 ple_window_max = 0;
7603 ple_window_shrink = 0;
7604 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007605
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007606 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007607 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007608 kvm_x86_ops->sync_pir_to_irr = NULL;
7609 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007610
Haozhong Zhang64903d62015-10-20 15:39:09 +08007611 if (cpu_has_vmx_tsc_scaling()) {
7612 kvm_has_tsc_control = true;
7613 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7614 kvm_tsc_scaling_ratio_frac_bits = 48;
7615 }
7616
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007617 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7618
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007619 if (enable_ept)
7620 vmx_enable_tdp();
7621 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007622 kvm_disable_tdp();
7623
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007624 if (!nested) {
7625 kvm_x86_ops->get_nested_state = NULL;
7626 kvm_x86_ops->set_nested_state = NULL;
7627 }
7628
Kai Huang843e4332015-01-28 10:54:28 +08007629 /*
7630 * Only enable PML when hardware supports PML feature, and both EPT
7631 * and EPT A/D bit features are enabled -- PML depends on them to work.
7632 */
7633 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7634 enable_pml = 0;
7635
7636 if (!enable_pml) {
7637 kvm_x86_ops->slot_enable_log_dirty = NULL;
7638 kvm_x86_ops->slot_disable_log_dirty = NULL;
7639 kvm_x86_ops->flush_log_dirty = NULL;
7640 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7641 }
7642
Yunhong Jiang64672c92016-06-13 14:19:59 -07007643 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7644 u64 vmx_msr;
7645
7646 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7647 cpu_preemption_timer_multi =
7648 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7649 } else {
7650 kvm_x86_ops->set_hv_timer = NULL;
7651 kvm_x86_ops->cancel_hv_timer = NULL;
7652 }
7653
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007654 if (!cpu_has_vmx_shadow_vmcs())
7655 enable_shadow_vmcs = 0;
7656 if (enable_shadow_vmcs)
7657 init_vmcs_shadow_fields();
7658
Feng Wubf9f6ac2015-09-18 22:29:55 +08007659 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007660 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007661
Ashok Rajc45dcc72016-06-22 14:59:56 +08007662 kvm_mce_cap_supported |= MCG_LMCE_P;
7663
Tiejun Chenf2c76482014-10-28 10:14:47 +08007664 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007665
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007666out:
Radim Krčmář23611332016-09-29 22:41:33 +02007667 for (i = 0; i < VMX_BITMAP_NR; i++)
7668 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007669
7670 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007671}
7672
7673static __exit void hardware_unsetup(void)
7674{
Radim Krčmář23611332016-09-29 22:41:33 +02007675 int i;
7676
7677 for (i = 0; i < VMX_BITMAP_NR; i++)
7678 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007679
Tiejun Chenf2c76482014-10-28 10:14:47 +08007680 free_kvm_area();
7681}
7682
Avi Kivity6aa8b732006-12-10 02:21:36 -08007683/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007684 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7685 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7686 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007687static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007688{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007689 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007690 grow_ple_window(vcpu);
7691
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007692 /*
7693 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7694 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7695 * never set PAUSE_EXITING and just set PLE if supported,
7696 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7697 */
7698 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007699 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007700}
7701
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007702static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007703{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007704 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007705}
7706
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007707static int handle_mwait(struct kvm_vcpu *vcpu)
7708{
7709 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7710 return handle_nop(vcpu);
7711}
7712
Jim Mattson45ec3682017-08-23 16:32:04 -07007713static int handle_invalid_op(struct kvm_vcpu *vcpu)
7714{
7715 kvm_queue_exception(vcpu, UD_VECTOR);
7716 return 1;
7717}
7718
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007719static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7720{
7721 return 1;
7722}
7723
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007724static int handle_monitor(struct kvm_vcpu *vcpu)
7725{
7726 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7727 return handle_nop(vcpu);
7728}
7729
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007730/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007731 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7732 * set the success or error code of an emulated VMX instruction, as specified
7733 * by Vol 2B, VMX Instruction Reference, "Conventions".
7734 */
7735static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7736{
7737 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7738 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7739 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7740}
7741
7742static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7743{
7744 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7745 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7746 X86_EFLAGS_SF | X86_EFLAGS_OF))
7747 | X86_EFLAGS_CF);
7748}
7749
Abel Gordon145c28d2013-04-18 14:36:55 +03007750static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007751 u32 vm_instruction_error)
7752{
7753 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7754 /*
7755 * failValid writes the error number to the current VMCS, which
7756 * can't be done there isn't a current VMCS.
7757 */
7758 nested_vmx_failInvalid(vcpu);
7759 return;
7760 }
7761 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7762 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7763 X86_EFLAGS_SF | X86_EFLAGS_OF))
7764 | X86_EFLAGS_ZF);
7765 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7766 /*
7767 * We don't need to force a shadow sync because
7768 * VM_INSTRUCTION_ERROR is not shadowed
7769 */
7770}
Abel Gordon145c28d2013-04-18 14:36:55 +03007771
Wincy Vanff651cb2014-12-11 08:52:58 +03007772static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7773{
7774 /* TODO: not to reset guest simply here. */
7775 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007776 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007777}
7778
Jan Kiszkaf41245002014-03-07 20:03:13 +01007779static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7780{
7781 struct vcpu_vmx *vmx =
7782 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7783
7784 vmx->nested.preemption_timer_expired = true;
7785 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7786 kvm_vcpu_kick(&vmx->vcpu);
7787
7788 return HRTIMER_NORESTART;
7789}
7790
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007791/*
Bandan Das19677e32014-05-06 02:19:15 -04007792 * Decode the memory-address operand of a vmx instruction, as recorded on an
7793 * exit caused by such an instruction (run by a guest hypervisor).
7794 * On success, returns 0. When the operand is invalid, returns 1 and throws
7795 * #UD or #GP.
7796 */
7797static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7798 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007799 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007800{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007801 gva_t off;
7802 bool exn;
7803 struct kvm_segment s;
7804
Bandan Das19677e32014-05-06 02:19:15 -04007805 /*
7806 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7807 * Execution", on an exit, vmx_instruction_info holds most of the
7808 * addressing components of the operand. Only the displacement part
7809 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7810 * For how an actual address is calculated from all these components,
7811 * refer to Vol. 1, "Operand Addressing".
7812 */
7813 int scaling = vmx_instruction_info & 3;
7814 int addr_size = (vmx_instruction_info >> 7) & 7;
7815 bool is_reg = vmx_instruction_info & (1u << 10);
7816 int seg_reg = (vmx_instruction_info >> 15) & 7;
7817 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7818 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7819 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7820 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7821
7822 if (is_reg) {
7823 kvm_queue_exception(vcpu, UD_VECTOR);
7824 return 1;
7825 }
7826
7827 /* Addr = segment_base + offset */
7828 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007829 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007830 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007831 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007832 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007833 off += kvm_register_read(vcpu, index_reg)<<scaling;
7834 vmx_get_segment(vcpu, &s, seg_reg);
7835 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007836
7837 if (addr_size == 1) /* 32 bit */
7838 *ret &= 0xffffffff;
7839
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007840 /* Checks for #GP/#SS exceptions. */
7841 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007842 if (is_long_mode(vcpu)) {
7843 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7844 * non-canonical form. This is the only check on the memory
7845 * destination for long mode!
7846 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007847 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007848 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007849 /* Protected mode: apply checks for segment validity in the
7850 * following order:
7851 * - segment type check (#GP(0) may be thrown)
7852 * - usability check (#GP(0)/#SS(0))
7853 * - limit check (#GP(0)/#SS(0))
7854 */
7855 if (wr)
7856 /* #GP(0) if the destination operand is located in a
7857 * read-only data segment or any code segment.
7858 */
7859 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7860 else
7861 /* #GP(0) if the source operand is located in an
7862 * execute-only code segment
7863 */
7864 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007865 if (exn) {
7866 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7867 return 1;
7868 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007869 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7870 */
7871 exn = (s.unusable != 0);
7872 /* Protected mode: #GP(0)/#SS(0) if the memory
7873 * operand is outside the segment limit.
7874 */
7875 exn = exn || (off + sizeof(u64) > s.limit);
7876 }
7877 if (exn) {
7878 kvm_queue_exception_e(vcpu,
7879 seg_reg == VCPU_SREG_SS ?
7880 SS_VECTOR : GP_VECTOR,
7881 0);
7882 return 1;
7883 }
7884
Bandan Das19677e32014-05-06 02:19:15 -04007885 return 0;
7886}
7887
Radim Krčmářcbf71272017-05-19 15:48:51 +02007888static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007889{
7890 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007891 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007892
7893 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007894 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007895 return 1;
7896
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007897 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007898 kvm_inject_page_fault(vcpu, &e);
7899 return 1;
7900 }
7901
Bandan Das3573e222014-05-06 02:19:16 -04007902 return 0;
7903}
7904
Liran Alonabfc52c2018-06-23 02:35:13 +03007905/*
7906 * Allocate a shadow VMCS and associate it with the currently loaded
7907 * VMCS, unless such a shadow VMCS already exists. The newly allocated
7908 * VMCS is also VMCLEARed, so that it is ready for use.
7909 */
7910static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
7911{
7912 struct vcpu_vmx *vmx = to_vmx(vcpu);
7913 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
7914
7915 /*
7916 * We should allocate a shadow vmcs for vmcs01 only when L1
7917 * executes VMXON and free it when L1 executes VMXOFF.
7918 * As it is invalid to execute VMXON twice, we shouldn't reach
7919 * here when vmcs01 already have an allocated shadow vmcs.
7920 */
7921 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
7922
7923 if (!loaded_vmcs->shadow_vmcs) {
7924 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
7925 if (loaded_vmcs->shadow_vmcs)
7926 vmcs_clear(loaded_vmcs->shadow_vmcs);
7927 }
7928 return loaded_vmcs->shadow_vmcs;
7929}
7930
Jim Mattsone29acc52016-11-30 12:03:43 -08007931static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7932{
7933 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007934 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007935
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007936 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7937 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007938 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007939
7940 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7941 if (!vmx->nested.cached_vmcs12)
7942 goto out_cached_vmcs12;
7943
Liran Alon61ada742018-06-23 02:35:08 +03007944 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7945 if (!vmx->nested.cached_shadow_vmcs12)
7946 goto out_cached_shadow_vmcs12;
7947
Liran Alonabfc52c2018-06-23 02:35:13 +03007948 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
7949 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08007950
Jim Mattsone29acc52016-11-30 12:03:43 -08007951 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7952 HRTIMER_MODE_REL_PINNED);
7953 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7954
7955 vmx->nested.vmxon = true;
7956 return 0;
7957
7958out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03007959 kfree(vmx->nested.cached_shadow_vmcs12);
7960
7961out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08007962 kfree(vmx->nested.cached_vmcs12);
7963
7964out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007965 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007966
Jim Mattsonde3a0022017-11-27 17:22:25 -06007967out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007968 return -ENOMEM;
7969}
7970
Bandan Das3573e222014-05-06 02:19:16 -04007971/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007972 * Emulate the VMXON instruction.
7973 * Currently, we just remember that VMX is active, and do not save or even
7974 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7975 * do not currently need to store anything in that guest-allocated memory
7976 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7977 * argument is different from the VMXON pointer (which the spec says they do).
7978 */
7979static int handle_vmon(struct kvm_vcpu *vcpu)
7980{
Jim Mattsone29acc52016-11-30 12:03:43 -08007981 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007982 gpa_t vmptr;
7983 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007985 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7986 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007987
Jim Mattson70f3aac2017-04-26 08:53:46 -07007988 /*
7989 * The Intel VMX Instruction Reference lists a bunch of bits that are
7990 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7991 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7992 * Otherwise, we should fail with #UD. But most faulting conditions
7993 * have already been checked by hardware, prior to the VM-exit for
7994 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7995 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007996 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007997 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007998 kvm_queue_exception(vcpu, UD_VECTOR);
7999 return 1;
8000 }
8001
Felix Wilhelm727ba742018-06-11 09:43:44 +02008002 /* CPL=0 must be checked manually. */
8003 if (vmx_get_cpl(vcpu)) {
8004 kvm_queue_exception(vcpu, UD_VECTOR);
8005 return 1;
8006 }
8007
Abel Gordon145c28d2013-04-18 14:36:55 +03008008 if (vmx->nested.vmxon) {
8009 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008010 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008011 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008012
Haozhong Zhang3b840802016-06-22 14:59:54 +08008013 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008014 != VMXON_NEEDED_FEATURES) {
8015 kvm_inject_gp(vcpu, 0);
8016 return 1;
8017 }
8018
Radim Krčmářcbf71272017-05-19 15:48:51 +02008019 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008020 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008021
8022 /*
8023 * SDM 3: 24.11.5
8024 * The first 4 bytes of VMXON region contain the supported
8025 * VMCS revision identifier
8026 *
8027 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8028 * which replaces physical address width with 32
8029 */
8030 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8031 nested_vmx_failInvalid(vcpu);
8032 return kvm_skip_emulated_instruction(vcpu);
8033 }
8034
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008035 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8036 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008037 nested_vmx_failInvalid(vcpu);
8038 return kvm_skip_emulated_instruction(vcpu);
8039 }
8040 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8041 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008042 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008043 nested_vmx_failInvalid(vcpu);
8044 return kvm_skip_emulated_instruction(vcpu);
8045 }
8046 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008047 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008048
8049 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008050 ret = enter_vmx_operation(vcpu);
8051 if (ret)
8052 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008053
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008054 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008055 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008056}
8057
8058/*
8059 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8060 * for running VMX instructions (except VMXON, whose prerequisites are
8061 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008062 * Note that many of these exceptions have priority over VM exits, so they
8063 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008064 */
8065static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8066{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008067 if (vmx_get_cpl(vcpu)) {
8068 kvm_queue_exception(vcpu, UD_VECTOR);
8069 return 0;
8070 }
8071
Jim Mattson70f3aac2017-04-26 08:53:46 -07008072 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008073 kvm_queue_exception(vcpu, UD_VECTOR);
8074 return 0;
8075 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008076 return 1;
8077}
8078
David Matlack8ca44e82017-08-01 14:00:39 -07008079static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8080{
8081 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8082 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8083}
8084
Abel Gordone7953d72013-04-18 14:37:55 +03008085static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8086{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008087 if (vmx->nested.current_vmptr == -1ull)
8088 return;
8089
Abel Gordon012f83c2013-04-18 14:39:25 +03008090 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008091 /* copy to memory all shadowed fields in case
8092 they were modified */
8093 copy_shadow_to_vmcs12(vmx);
8094 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008095 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008096 }
Wincy Van705699a2015-02-03 23:58:17 +08008097 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008098
8099 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008100 kvm_vcpu_write_guest_page(&vmx->vcpu,
8101 vmx->nested.current_vmptr >> PAGE_SHIFT,
8102 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008103
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008104 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008105}
8106
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008107/*
8108 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8109 * just stops using VMX.
8110 */
8111static void free_nested(struct vcpu_vmx *vmx)
8112{
Wanpeng Lib7455822017-11-22 14:04:00 -08008113 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008114 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008115
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008116 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008117 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008118 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008119 vmx->nested.posted_intr_nv = -1;
8120 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008121 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008122 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008123 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8124 free_vmcs(vmx->vmcs01.shadow_vmcs);
8125 vmx->vmcs01.shadow_vmcs = NULL;
8126 }
David Matlack4f2777b2016-07-13 17:16:37 -07008127 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008128 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008129 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008130 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008131 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008132 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008133 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008134 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008135 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008136 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008137 }
Wincy Van705699a2015-02-03 23:58:17 +08008138 if (vmx->nested.pi_desc_page) {
8139 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008140 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008141 vmx->nested.pi_desc_page = NULL;
8142 vmx->nested.pi_desc = NULL;
8143 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008144
Jim Mattsonde3a0022017-11-27 17:22:25 -06008145 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008146}
8147
8148/* Emulate the VMXOFF instruction */
8149static int handle_vmoff(struct kvm_vcpu *vcpu)
8150{
8151 if (!nested_vmx_check_permission(vcpu))
8152 return 1;
8153 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008154 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008155 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008156}
8157
Nadav Har'El27d6c862011-05-25 23:06:59 +03008158/* Emulate the VMCLEAR instruction */
8159static int handle_vmclear(struct kvm_vcpu *vcpu)
8160{
8161 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008162 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008163 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008164
8165 if (!nested_vmx_check_permission(vcpu))
8166 return 1;
8167
Radim Krčmářcbf71272017-05-19 15:48:51 +02008168 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008169 return 1;
8170
Radim Krčmářcbf71272017-05-19 15:48:51 +02008171 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8172 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8173 return kvm_skip_emulated_instruction(vcpu);
8174 }
8175
8176 if (vmptr == vmx->nested.vmxon_ptr) {
8177 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8178 return kvm_skip_emulated_instruction(vcpu);
8179 }
8180
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008181 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008182 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008183
Jim Mattson587d7e722017-03-02 12:41:48 -08008184 kvm_vcpu_write_guest(vcpu,
8185 vmptr + offsetof(struct vmcs12, launch_state),
8186 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008187
Nadav Har'El27d6c862011-05-25 23:06:59 +03008188 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008189 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008190}
8191
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008192static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8193
8194/* Emulate the VMLAUNCH instruction */
8195static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8196{
8197 return nested_vmx_run(vcpu, true);
8198}
8199
8200/* Emulate the VMRESUME instruction */
8201static int handle_vmresume(struct kvm_vcpu *vcpu)
8202{
8203
8204 return nested_vmx_run(vcpu, false);
8205}
8206
Nadav Har'El49f705c2011-05-25 23:08:30 +03008207/*
8208 * Read a vmcs12 field. Since these can have varying lengths and we return
8209 * one type, we chose the biggest type (u64) and zero-extend the return value
8210 * to that size. Note that the caller, handle_vmread, might need to use only
8211 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8212 * 64-bit fields are to be returned).
8213 */
Liran Alone2536742018-06-23 02:35:02 +03008214static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008215 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008216{
8217 short offset = vmcs_field_to_offset(field);
8218 char *p;
8219
8220 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008221 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008222
Liran Alone2536742018-06-23 02:35:02 +03008223 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008224
Jim Mattsond37f4262017-12-22 12:12:16 -08008225 switch (vmcs_field_width(field)) {
8226 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008227 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008228 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008229 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008230 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008231 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008232 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008233 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008234 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008235 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008236 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008237 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008238 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008239 WARN_ON(1);
8240 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008241 }
8242}
8243
Abel Gordon20b97fe2013-04-18 14:36:25 +03008244
Liran Alone2536742018-06-23 02:35:02 +03008245static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008246 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008247 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008248 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008249 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008250 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008251
Jim Mattsond37f4262017-12-22 12:12:16 -08008252 switch (vmcs_field_width(field)) {
8253 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008254 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008255 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008256 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008257 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008258 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008259 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008260 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008261 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008262 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008263 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008265 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008266 WARN_ON(1);
8267 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008268 }
8269
8270}
8271
Jim Mattsonf4160e42018-05-29 09:11:33 -07008272/*
8273 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8274 * they have been modified by the L1 guest. Note that the "read-only"
8275 * VM-exit information fields are actually writable if the vCPU is
8276 * configured to support "VMWRITE to any supported field in the VMCS."
8277 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008278static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8279{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008280 const u16 *fields[] = {
8281 shadow_read_write_fields,
8282 shadow_read_only_fields
8283 };
8284 const int max_fields[] = {
8285 max_shadow_read_write_fields,
8286 max_shadow_read_only_fields
8287 };
8288 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008289 unsigned long field;
8290 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008291 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008292
Jan Kiszka282da872014-10-08 18:05:39 +02008293 preempt_disable();
8294
Abel Gordon16f5b902013-04-18 14:38:25 +03008295 vmcs_load(shadow_vmcs);
8296
Jim Mattsonf4160e42018-05-29 09:11:33 -07008297 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8298 for (i = 0; i < max_fields[q]; i++) {
8299 field = fields[q][i];
8300 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008301 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008302 }
8303 /*
8304 * Skip the VM-exit information fields if they are read-only.
8305 */
8306 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8307 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008308 }
8309
8310 vmcs_clear(shadow_vmcs);
8311 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008312
8313 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008314}
8315
Abel Gordonc3114422013-04-18 14:38:55 +03008316static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8317{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008318 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008319 shadow_read_write_fields,
8320 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008321 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008322 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008323 max_shadow_read_write_fields,
8324 max_shadow_read_only_fields
8325 };
8326 int i, q;
8327 unsigned long field;
8328 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008329 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008330
8331 vmcs_load(shadow_vmcs);
8332
Mathias Krausec2bae892013-06-26 20:36:21 +02008333 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008334 for (i = 0; i < max_fields[q]; i++) {
8335 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008336 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008337 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008338 }
8339 }
8340
8341 vmcs_clear(shadow_vmcs);
8342 vmcs_load(vmx->loaded_vmcs->vmcs);
8343}
8344
Nadav Har'El49f705c2011-05-25 23:08:30 +03008345/*
8346 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8347 * used before) all generate the same failure when it is missing.
8348 */
8349static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8350{
8351 struct vcpu_vmx *vmx = to_vmx(vcpu);
8352 if (vmx->nested.current_vmptr == -1ull) {
8353 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008354 return 0;
8355 }
8356 return 1;
8357}
8358
8359static int handle_vmread(struct kvm_vcpu *vcpu)
8360{
8361 unsigned long field;
8362 u64 field_value;
8363 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8364 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8365 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008366 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008367
Kyle Hueyeb277562016-11-29 12:40:39 -08008368 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008369 return 1;
8370
Kyle Huey6affcbe2016-11-29 12:40:40 -08008371 if (!nested_vmx_check_vmcs12(vcpu))
8372 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008373
Liran Alon6d894f42018-06-23 02:35:09 +03008374 if (!is_guest_mode(vcpu))
8375 vmcs12 = get_vmcs12(vcpu);
8376 else {
8377 /*
8378 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8379 * to shadowed-field sets the ALU flags for VMfailInvalid.
8380 */
8381 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8382 nested_vmx_failInvalid(vcpu);
8383 return kvm_skip_emulated_instruction(vcpu);
8384 }
8385 vmcs12 = get_shadow_vmcs12(vcpu);
8386 }
8387
Nadav Har'El49f705c2011-05-25 23:08:30 +03008388 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008389 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008390 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008391 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008392 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008393 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008394 }
8395 /*
8396 * Now copy part of this value to register or memory, as requested.
8397 * Note that the number of bits actually copied is 32 or 64 depending
8398 * on the guest's mode (32 or 64 bit), not on the given field's length.
8399 */
8400 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008401 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008402 field_value);
8403 } else {
8404 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008405 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008406 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008407 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008408 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8409 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008410 }
8411
8412 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008413 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008414}
8415
8416
8417static int handle_vmwrite(struct kvm_vcpu *vcpu)
8418{
8419 unsigned long field;
8420 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008421 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008422 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8423 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008424
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 /* The value to write might be 32 or 64 bits, depending on L1's long
8426 * mode, and eventually we need to write that into a field of several
8427 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008428 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008429 * bits into the vmcs12 field.
8430 */
8431 u64 field_value = 0;
8432 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008433 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008434
Kyle Hueyeb277562016-11-29 12:40:39 -08008435 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008436 return 1;
8437
Kyle Huey6affcbe2016-11-29 12:40:40 -08008438 if (!nested_vmx_check_vmcs12(vcpu))
8439 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008440
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008442 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008443 (((vmx_instruction_info) >> 3) & 0xf));
8444 else {
8445 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008446 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008447 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008448 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8449 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 kvm_inject_page_fault(vcpu, &e);
8451 return 1;
8452 }
8453 }
8454
8455
Nadav Amit27e6fb52014-06-18 17:19:26 +03008456 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008457 /*
8458 * If the vCPU supports "VMWRITE to any supported field in the
8459 * VMCS," then the "read-only" fields are actually read/write.
8460 */
8461 if (vmcs_field_readonly(field) &&
8462 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008463 nested_vmx_failValid(vcpu,
8464 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008465 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008466 }
8467
Liran Alon6d894f42018-06-23 02:35:09 +03008468 if (!is_guest_mode(vcpu))
8469 vmcs12 = get_vmcs12(vcpu);
8470 else {
8471 /*
8472 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8473 * to shadowed-field sets the ALU flags for VMfailInvalid.
8474 */
8475 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8476 nested_vmx_failInvalid(vcpu);
8477 return kvm_skip_emulated_instruction(vcpu);
8478 }
8479 vmcs12 = get_shadow_vmcs12(vcpu);
8480
8481 }
8482
8483 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008484 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008485 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008486 }
8487
Liran Alon6d894f42018-06-23 02:35:09 +03008488 /*
8489 * Do not track vmcs12 dirty-state if in guest-mode
8490 * as we actually dirty shadow vmcs12 instead of vmcs12.
8491 */
8492 if (!is_guest_mode(vcpu)) {
8493 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008494#define SHADOW_FIELD_RW(x) case x:
8495#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008496 /*
8497 * The fields that can be updated by L1 without a vmexit are
8498 * always updated in the vmcs02, the others go down the slow
8499 * path of prepare_vmcs02.
8500 */
8501 break;
8502 default:
8503 vmx->nested.dirty_vmcs12 = true;
8504 break;
8505 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008506 }
8507
Nadav Har'El49f705c2011-05-25 23:08:30 +03008508 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008509 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008510}
8511
Jim Mattsona8bc2842016-11-30 12:03:44 -08008512static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8513{
8514 vmx->nested.current_vmptr = vmptr;
8515 if (enable_shadow_vmcs) {
8516 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8517 SECONDARY_EXEC_SHADOW_VMCS);
8518 vmcs_write64(VMCS_LINK_POINTER,
8519 __pa(vmx->vmcs01.shadow_vmcs));
8520 vmx->nested.sync_shadow_vmcs = true;
8521 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008522 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008523}
8524
Nadav Har'El63846662011-05-25 23:07:29 +03008525/* Emulate the VMPTRLD instruction */
8526static int handle_vmptrld(struct kvm_vcpu *vcpu)
8527{
8528 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008529 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008530
8531 if (!nested_vmx_check_permission(vcpu))
8532 return 1;
8533
Radim Krčmářcbf71272017-05-19 15:48:51 +02008534 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008535 return 1;
8536
Radim Krčmářcbf71272017-05-19 15:48:51 +02008537 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8538 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8539 return kvm_skip_emulated_instruction(vcpu);
8540 }
8541
8542 if (vmptr == vmx->nested.vmxon_ptr) {
8543 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8544 return kvm_skip_emulated_instruction(vcpu);
8545 }
8546
Nadav Har'El63846662011-05-25 23:07:29 +03008547 if (vmx->nested.current_vmptr != vmptr) {
8548 struct vmcs12 *new_vmcs12;
8549 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008550 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8551 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008552 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008553 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008554 }
8555 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008556 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008557 (new_vmcs12->hdr.shadow_vmcs &&
8558 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008559 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008560 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008561 nested_vmx_failValid(vcpu,
8562 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008563 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008564 }
Nadav Har'El63846662011-05-25 23:07:29 +03008565
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008566 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008567 /*
8568 * Load VMCS12 from guest memory since it is not already
8569 * cached.
8570 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008571 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8572 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008573 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008574
Jim Mattsona8bc2842016-11-30 12:03:44 -08008575 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008576 }
8577
8578 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008579 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008580}
8581
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008582/* Emulate the VMPTRST instruction */
8583static int handle_vmptrst(struct kvm_vcpu *vcpu)
8584{
8585 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8586 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8587 gva_t vmcs_gva;
8588 struct x86_exception e;
8589
8590 if (!nested_vmx_check_permission(vcpu))
8591 return 1;
8592
8593 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008594 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008595 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008596 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008597 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8598 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8599 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008600 kvm_inject_page_fault(vcpu, &e);
8601 return 1;
8602 }
8603 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008604 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008605}
8606
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008607/* Emulate the INVEPT instruction */
8608static int handle_invept(struct kvm_vcpu *vcpu)
8609{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008610 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008611 u32 vmx_instruction_info, types;
8612 unsigned long type;
8613 gva_t gva;
8614 struct x86_exception e;
8615 struct {
8616 u64 eptp, gpa;
8617 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008618
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008619 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008620 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008621 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008622 kvm_queue_exception(vcpu, UD_VECTOR);
8623 return 1;
8624 }
8625
8626 if (!nested_vmx_check_permission(vcpu))
8627 return 1;
8628
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008629 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008630 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008631
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008632 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008633
Jim Mattson85c856b2016-10-26 08:38:38 -07008634 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008635 nested_vmx_failValid(vcpu,
8636 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008637 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 }
8639
8640 /* According to the Intel VMX instruction reference, the memory
8641 * operand is read even if it isn't needed (e.g., for type==global)
8642 */
8643 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008644 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008645 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008646 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008647 kvm_inject_page_fault(vcpu, &e);
8648 return 1;
8649 }
8650
8651 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008652 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008653 /*
8654 * TODO: track mappings and invalidate
8655 * single context requests appropriately
8656 */
8657 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008658 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008659 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008660 nested_vmx_succeed(vcpu);
8661 break;
8662 default:
8663 BUG_ON(1);
8664 break;
8665 }
8666
Kyle Huey6affcbe2016-11-29 12:40:40 -08008667 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008668}
8669
Petr Matouseka642fc32014-09-23 20:22:30 +02008670static int handle_invvpid(struct kvm_vcpu *vcpu)
8671{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008672 struct vcpu_vmx *vmx = to_vmx(vcpu);
8673 u32 vmx_instruction_info;
8674 unsigned long type, types;
8675 gva_t gva;
8676 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008677 struct {
8678 u64 vpid;
8679 u64 gla;
8680 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008681
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008682 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008683 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008684 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008685 kvm_queue_exception(vcpu, UD_VECTOR);
8686 return 1;
8687 }
8688
8689 if (!nested_vmx_check_permission(vcpu))
8690 return 1;
8691
8692 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8693 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8694
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008695 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008696 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008697
Jim Mattson85c856b2016-10-26 08:38:38 -07008698 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008699 nested_vmx_failValid(vcpu,
8700 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008701 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008702 }
8703
8704 /* according to the intel vmx instruction reference, the memory
8705 * operand is read even if it isn't needed (e.g., for type==global)
8706 */
8707 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8708 vmx_instruction_info, false, &gva))
8709 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008710 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008711 kvm_inject_page_fault(vcpu, &e);
8712 return 1;
8713 }
Jim Mattson40352602017-06-28 09:37:37 -07008714 if (operand.vpid >> 16) {
8715 nested_vmx_failValid(vcpu,
8716 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8717 return kvm_skip_emulated_instruction(vcpu);
8718 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008719
8720 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008721 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008722 if (!operand.vpid ||
8723 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008724 nested_vmx_failValid(vcpu,
8725 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8726 return kvm_skip_emulated_instruction(vcpu);
8727 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008728 if (cpu_has_vmx_invvpid_individual_addr() &&
8729 vmx->nested.vpid02) {
8730 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8731 vmx->nested.vpid02, operand.gla);
8732 } else
8733 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8734 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008735 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008736 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008737 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008738 nested_vmx_failValid(vcpu,
8739 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008740 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008741 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008742 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008743 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008744 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008745 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008746 break;
8747 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008748 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008749 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008750 }
8751
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008752 nested_vmx_succeed(vcpu);
8753
Kyle Huey6affcbe2016-11-29 12:40:40 -08008754 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008755}
8756
Junaid Shahideb4b2482018-06-27 14:59:14 -07008757static int handle_invpcid(struct kvm_vcpu *vcpu)
8758{
8759 u32 vmx_instruction_info;
8760 unsigned long type;
8761 bool pcid_enabled;
8762 gva_t gva;
8763 struct x86_exception e;
8764 struct {
8765 u64 pcid;
8766 u64 gla;
8767 } operand;
8768
8769 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8770 kvm_queue_exception(vcpu, UD_VECTOR);
8771 return 1;
8772 }
8773
8774 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8775 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8776
8777 if (type > 3) {
8778 kvm_inject_gp(vcpu, 0);
8779 return 1;
8780 }
8781
8782 /* According to the Intel instruction reference, the memory operand
8783 * is read even if it isn't needed (e.g., for type==all)
8784 */
8785 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8786 vmx_instruction_info, false, &gva))
8787 return 1;
8788
8789 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8790 kvm_inject_page_fault(vcpu, &e);
8791 return 1;
8792 }
8793
8794 if (operand.pcid >> 12 != 0) {
8795 kvm_inject_gp(vcpu, 0);
8796 return 1;
8797 }
8798
8799 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8800
8801 switch (type) {
8802 case INVPCID_TYPE_INDIV_ADDR:
8803 if ((!pcid_enabled && (operand.pcid != 0)) ||
8804 is_noncanonical_address(operand.gla, vcpu)) {
8805 kvm_inject_gp(vcpu, 0);
8806 return 1;
8807 }
8808 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8809 return kvm_skip_emulated_instruction(vcpu);
8810
8811 case INVPCID_TYPE_SINGLE_CTXT:
8812 if (!pcid_enabled && (operand.pcid != 0)) {
8813 kvm_inject_gp(vcpu, 0);
8814 return 1;
8815 }
8816
8817 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8818 kvm_mmu_sync_roots(vcpu);
8819 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8820 }
8821
Junaid Shahidade61e22018-06-27 14:59:15 -07008822 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_root.cr3)
8823 == operand.pcid)
8824 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8825
Junaid Shahideb4b2482018-06-27 14:59:14 -07008826 /*
Junaid Shahidade61e22018-06-27 14:59:15 -07008827 * If neither the current cr3 nor the prev_root.cr3 use the
8828 * given PCID, then nothing needs to be done here because a
8829 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008830 */
8831
8832 return kvm_skip_emulated_instruction(vcpu);
8833
8834 case INVPCID_TYPE_ALL_NON_GLOBAL:
8835 /*
8836 * Currently, KVM doesn't mark global entries in the shadow
8837 * page tables, so a non-global flush just degenerates to a
8838 * global flush. If needed, we could optimize this later by
8839 * keeping track of global entries in shadow page tables.
8840 */
8841
8842 /* fall-through */
8843 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8844 kvm_mmu_unload(vcpu);
8845 return kvm_skip_emulated_instruction(vcpu);
8846
8847 default:
8848 BUG(); /* We have already checked above that type <= 3 */
8849 }
8850}
8851
Kai Huang843e4332015-01-28 10:54:28 +08008852static int handle_pml_full(struct kvm_vcpu *vcpu)
8853{
8854 unsigned long exit_qualification;
8855
8856 trace_kvm_pml_full(vcpu->vcpu_id);
8857
8858 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8859
8860 /*
8861 * PML buffer FULL happened while executing iret from NMI,
8862 * "blocked by NMI" bit has to be set before next VM entry.
8863 */
8864 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008865 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008866 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8867 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8868 GUEST_INTR_STATE_NMI);
8869
8870 /*
8871 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8872 * here.., and there's no userspace involvement needed for PML.
8873 */
8874 return 1;
8875}
8876
Yunhong Jiang64672c92016-06-13 14:19:59 -07008877static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8878{
8879 kvm_lapic_expired_hv_timer(vcpu);
8880 return 1;
8881}
8882
Bandan Das41ab9372017-08-03 15:54:43 -04008883static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8884{
8885 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008886 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8887
8888 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008889 switch (address & VMX_EPTP_MT_MASK) {
8890 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008891 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008892 return false;
8893 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008894 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008895 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008896 return false;
8897 break;
8898 default:
8899 return false;
8900 }
8901
David Hildenbrandbb97a012017-08-10 23:15:28 +02008902 /* only 4 levels page-walk length are valid */
8903 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008904 return false;
8905
8906 /* Reserved bits should not be set */
8907 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8908 return false;
8909
8910 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008911 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008912 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008913 return false;
8914 }
8915
8916 return true;
8917}
8918
8919static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8920 struct vmcs12 *vmcs12)
8921{
8922 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8923 u64 address;
8924 bool accessed_dirty;
8925 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8926
8927 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8928 !nested_cpu_has_ept(vmcs12))
8929 return 1;
8930
8931 if (index >= VMFUNC_EPTP_ENTRIES)
8932 return 1;
8933
8934
8935 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8936 &address, index * 8, 8))
8937 return 1;
8938
David Hildenbrandbb97a012017-08-10 23:15:28 +02008939 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008940
8941 /*
8942 * If the (L2) guest does a vmfunc to the currently
8943 * active ept pointer, we don't have to do anything else
8944 */
8945 if (vmcs12->ept_pointer != address) {
8946 if (!valid_ept_address(vcpu, address))
8947 return 1;
8948
8949 kvm_mmu_unload(vcpu);
8950 mmu->ept_ad = accessed_dirty;
8951 mmu->base_role.ad_disabled = !accessed_dirty;
8952 vmcs12->ept_pointer = address;
8953 /*
8954 * TODO: Check what's the correct approach in case
8955 * mmu reload fails. Currently, we just let the next
8956 * reload potentially fail
8957 */
8958 kvm_mmu_reload(vcpu);
8959 }
8960
8961 return 0;
8962}
8963
Bandan Das2a499e42017-08-03 15:54:41 -04008964static int handle_vmfunc(struct kvm_vcpu *vcpu)
8965{
Bandan Das27c42a12017-08-03 15:54:42 -04008966 struct vcpu_vmx *vmx = to_vmx(vcpu);
8967 struct vmcs12 *vmcs12;
8968 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8969
8970 /*
8971 * VMFUNC is only supported for nested guests, but we always enable the
8972 * secondary control for simplicity; for non-nested mode, fake that we
8973 * didn't by injecting #UD.
8974 */
8975 if (!is_guest_mode(vcpu)) {
8976 kvm_queue_exception(vcpu, UD_VECTOR);
8977 return 1;
8978 }
8979
8980 vmcs12 = get_vmcs12(vcpu);
8981 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8982 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008983
8984 switch (function) {
8985 case 0:
8986 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8987 goto fail;
8988 break;
8989 default:
8990 goto fail;
8991 }
8992 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008993
8994fail:
8995 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8996 vmcs_read32(VM_EXIT_INTR_INFO),
8997 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008998 return 1;
8999}
9000
Nadav Har'El0140cae2011-05-25 23:06:28 +03009001/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009002 * The exit handlers return 1 if the exit was handled fully and guest execution
9003 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9004 * to be done to userspace and return 0.
9005 */
Mathias Krause772e0312012-08-30 01:30:19 +02009006static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009007 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9008 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009009 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009010 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009011 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009012 [EXIT_REASON_CR_ACCESS] = handle_cr,
9013 [EXIT_REASON_DR_ACCESS] = handle_dr,
9014 [EXIT_REASON_CPUID] = handle_cpuid,
9015 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9016 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9017 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9018 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009019 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009020 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009021 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009022 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009023 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009024 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009025 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009026 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009027 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009028 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009029 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009030 [EXIT_REASON_VMOFF] = handle_vmoff,
9031 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009032 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9033 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009034 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009035 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009036 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009037 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009038 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009039 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009040 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9041 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009042 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9043 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009044 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009045 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009046 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009047 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009048 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009049 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009050 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009051 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009052 [EXIT_REASON_XSAVES] = handle_xsaves,
9053 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009054 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009055 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009056 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009057 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009058};
9059
9060static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009061 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009062
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009063static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9064 struct vmcs12 *vmcs12)
9065{
9066 unsigned long exit_qualification;
9067 gpa_t bitmap, last_bitmap;
9068 unsigned int port;
9069 int size;
9070 u8 b;
9071
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009072 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009073 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009074
9075 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9076
9077 port = exit_qualification >> 16;
9078 size = (exit_qualification & 7) + 1;
9079
9080 last_bitmap = (gpa_t)-1;
9081 b = -1;
9082
9083 while (size > 0) {
9084 if (port < 0x8000)
9085 bitmap = vmcs12->io_bitmap_a;
9086 else if (port < 0x10000)
9087 bitmap = vmcs12->io_bitmap_b;
9088 else
Joe Perches1d804d02015-03-30 16:46:09 -07009089 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009090 bitmap += (port & 0x7fff) / 8;
9091
9092 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009093 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009094 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009095 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009096 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009097
9098 port++;
9099 size--;
9100 last_bitmap = bitmap;
9101 }
9102
Joe Perches1d804d02015-03-30 16:46:09 -07009103 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009104}
9105
Nadav Har'El644d7112011-05-25 23:12:35 +03009106/*
9107 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9108 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9109 * disinterest in the current event (read or write a specific MSR) by using an
9110 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9111 */
9112static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9113 struct vmcs12 *vmcs12, u32 exit_reason)
9114{
9115 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9116 gpa_t bitmap;
9117
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009118 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009119 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009120
9121 /*
9122 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9123 * for the four combinations of read/write and low/high MSR numbers.
9124 * First we need to figure out which of the four to use:
9125 */
9126 bitmap = vmcs12->msr_bitmap;
9127 if (exit_reason == EXIT_REASON_MSR_WRITE)
9128 bitmap += 2048;
9129 if (msr_index >= 0xc0000000) {
9130 msr_index -= 0xc0000000;
9131 bitmap += 1024;
9132 }
9133
9134 /* Then read the msr_index'th bit from this bitmap: */
9135 if (msr_index < 1024*8) {
9136 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009137 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009138 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009139 return 1 & (b >> (msr_index & 7));
9140 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009141 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009142}
9143
9144/*
9145 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9146 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9147 * intercept (via guest_host_mask etc.) the current event.
9148 */
9149static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9150 struct vmcs12 *vmcs12)
9151{
9152 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9153 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009154 int reg;
9155 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009156
9157 switch ((exit_qualification >> 4) & 3) {
9158 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009159 reg = (exit_qualification >> 8) & 15;
9160 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009161 switch (cr) {
9162 case 0:
9163 if (vmcs12->cr0_guest_host_mask &
9164 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009165 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 break;
9167 case 3:
9168 if ((vmcs12->cr3_target_count >= 1 &&
9169 vmcs12->cr3_target_value0 == val) ||
9170 (vmcs12->cr3_target_count >= 2 &&
9171 vmcs12->cr3_target_value1 == val) ||
9172 (vmcs12->cr3_target_count >= 3 &&
9173 vmcs12->cr3_target_value2 == val) ||
9174 (vmcs12->cr3_target_count >= 4 &&
9175 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009176 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009177 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009178 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009179 break;
9180 case 4:
9181 if (vmcs12->cr4_guest_host_mask &
9182 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009183 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009184 break;
9185 case 8:
9186 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009187 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009188 break;
9189 }
9190 break;
9191 case 2: /* clts */
9192 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9193 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009194 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009195 break;
9196 case 1: /* mov from cr */
9197 switch (cr) {
9198 case 3:
9199 if (vmcs12->cpu_based_vm_exec_control &
9200 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009201 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009202 break;
9203 case 8:
9204 if (vmcs12->cpu_based_vm_exec_control &
9205 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009206 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009207 break;
9208 }
9209 break;
9210 case 3: /* lmsw */
9211 /*
9212 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9213 * cr0. Other attempted changes are ignored, with no exit.
9214 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009215 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009216 if (vmcs12->cr0_guest_host_mask & 0xe &
9217 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009218 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009219 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9220 !(vmcs12->cr0_read_shadow & 0x1) &&
9221 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009222 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009223 break;
9224 }
Joe Perches1d804d02015-03-30 16:46:09 -07009225 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009226}
9227
Liran Alona7cde482018-06-23 02:35:10 +03009228static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9229 struct vmcs12 *vmcs12, gpa_t bitmap)
9230{
9231 u32 vmx_instruction_info;
9232 unsigned long field;
9233 u8 b;
9234
9235 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9236 return true;
9237
9238 /* Decode instruction info and find the field to access */
9239 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9240 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9241
9242 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9243 if (field >> 15)
9244 return true;
9245
9246 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9247 return true;
9248
9249 return 1 & (b >> (field & 7));
9250}
9251
Nadav Har'El644d7112011-05-25 23:12:35 +03009252/*
9253 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9254 * should handle it ourselves in L0 (and then continue L2). Only call this
9255 * when in is_guest_mode (L2).
9256 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009257static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009258{
Nadav Har'El644d7112011-05-25 23:12:35 +03009259 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9260 struct vcpu_vmx *vmx = to_vmx(vcpu);
9261 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9262
Jim Mattson4f350c62017-09-14 16:31:44 -07009263 if (vmx->nested.nested_run_pending)
9264 return false;
9265
9266 if (unlikely(vmx->fail)) {
9267 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9268 vmcs_read32(VM_INSTRUCTION_ERROR));
9269 return true;
9270 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009271
David Matlackc9f04402017-08-01 14:00:40 -07009272 /*
9273 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009274 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9275 * Page). The CPU may write to these pages via their host
9276 * physical address while L2 is running, bypassing any
9277 * address-translation-based dirty tracking (e.g. EPT write
9278 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009279 *
9280 * Mark them dirty on every exit from L2 to prevent them from
9281 * getting out of sync with dirty tracking.
9282 */
9283 nested_mark_vmcs12_pages_dirty(vcpu);
9284
Jim Mattson4f350c62017-09-14 16:31:44 -07009285 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9286 vmcs_readl(EXIT_QUALIFICATION),
9287 vmx->idt_vectoring_info,
9288 intr_info,
9289 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9290 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009291
9292 switch (exit_reason) {
9293 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009294 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009295 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009296 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009297 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009298 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009299 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009300 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009301 else if (is_debug(intr_info) &&
9302 vcpu->guest_debug &
9303 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9304 return false;
9305 else if (is_breakpoint(intr_info) &&
9306 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9307 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009308 return vmcs12->exception_bitmap &
9309 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9310 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009311 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009312 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009313 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009314 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009315 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009316 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009317 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009318 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009319 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009320 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009321 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009322 case EXIT_REASON_HLT:
9323 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9324 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009325 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009326 case EXIT_REASON_INVLPG:
9327 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9328 case EXIT_REASON_RDPMC:
9329 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009330 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009331 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009332 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009333 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009334 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009335 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009336 case EXIT_REASON_VMREAD:
9337 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9338 vmcs12->vmread_bitmap);
9339 case EXIT_REASON_VMWRITE:
9340 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9341 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009342 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9343 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009344 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009345 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009346 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009347 /*
9348 * VMX instructions trap unconditionally. This allows L1 to
9349 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9350 */
Joe Perches1d804d02015-03-30 16:46:09 -07009351 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009352 case EXIT_REASON_CR_ACCESS:
9353 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9354 case EXIT_REASON_DR_ACCESS:
9355 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9356 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009357 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009358 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009360 case EXIT_REASON_MSR_READ:
9361 case EXIT_REASON_MSR_WRITE:
9362 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9363 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009364 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009365 case EXIT_REASON_MWAIT_INSTRUCTION:
9366 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009367 case EXIT_REASON_MONITOR_TRAP_FLAG:
9368 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009369 case EXIT_REASON_MONITOR_INSTRUCTION:
9370 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9371 case EXIT_REASON_PAUSE_INSTRUCTION:
9372 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9373 nested_cpu_has2(vmcs12,
9374 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9375 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009376 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009377 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009378 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009379 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009380 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009381 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009382 /*
9383 * The controls for "virtualize APIC accesses," "APIC-
9384 * register virtualization," and "virtual-interrupt
9385 * delivery" only come from vmcs12.
9386 */
Joe Perches1d804d02015-03-30 16:46:09 -07009387 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009388 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009389 /*
9390 * L0 always deals with the EPT violation. If nested EPT is
9391 * used, and the nested mmu code discovers that the address is
9392 * missing in the guest EPT table (EPT12), the EPT violation
9393 * will be injected with nested_ept_inject_page_fault()
9394 */
Joe Perches1d804d02015-03-30 16:46:09 -07009395 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009396 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009397 /*
9398 * L2 never uses directly L1's EPT, but rather L0's own EPT
9399 * table (shadow on EPT) or a merged EPT table that L0 built
9400 * (EPT on EPT). So any problems with the structure of the
9401 * table is L0's fault.
9402 */
Joe Perches1d804d02015-03-30 16:46:09 -07009403 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009404 case EXIT_REASON_INVPCID:
9405 return
9406 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9407 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009408 case EXIT_REASON_WBINVD:
9409 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9410 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009411 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009412 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9413 /*
9414 * This should never happen, since it is not possible to
9415 * set XSS to a non-zero value---neither in L1 nor in L2.
9416 * If if it were, XSS would have to be checked against
9417 * the XSS exit bitmap in vmcs12.
9418 */
9419 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009420 case EXIT_REASON_PREEMPTION_TIMER:
9421 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009422 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009423 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009424 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009425 case EXIT_REASON_VMFUNC:
9426 /* VM functions are emulated through L2->L0 vmexits. */
9427 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009428 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009429 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009430 }
9431}
9432
Paolo Bonzini7313c692017-07-27 10:31:25 +02009433static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9434{
9435 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9436
9437 /*
9438 * At this point, the exit interruption info in exit_intr_info
9439 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9440 * we need to query the in-kernel LAPIC.
9441 */
9442 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9443 if ((exit_intr_info &
9444 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9445 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9447 vmcs12->vm_exit_intr_error_code =
9448 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9449 }
9450
9451 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9452 vmcs_readl(EXIT_QUALIFICATION));
9453 return 1;
9454}
9455
Avi Kivity586f9602010-11-18 13:09:54 +02009456static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9457{
9458 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9459 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9460}
9461
Kai Huanga3eaa862015-11-04 13:46:05 +08009462static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009463{
Kai Huanga3eaa862015-11-04 13:46:05 +08009464 if (vmx->pml_pg) {
9465 __free_page(vmx->pml_pg);
9466 vmx->pml_pg = NULL;
9467 }
Kai Huang843e4332015-01-28 10:54:28 +08009468}
9469
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009470static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009471{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009472 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009473 u64 *pml_buf;
9474 u16 pml_idx;
9475
9476 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9477
9478 /* Do nothing if PML buffer is empty */
9479 if (pml_idx == (PML_ENTITY_NUM - 1))
9480 return;
9481
9482 /* PML index always points to next available PML buffer entity */
9483 if (pml_idx >= PML_ENTITY_NUM)
9484 pml_idx = 0;
9485 else
9486 pml_idx++;
9487
9488 pml_buf = page_address(vmx->pml_pg);
9489 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9490 u64 gpa;
9491
9492 gpa = pml_buf[pml_idx];
9493 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009494 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009495 }
9496
9497 /* reset PML index */
9498 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9499}
9500
9501/*
9502 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9503 * Called before reporting dirty_bitmap to userspace.
9504 */
9505static void kvm_flush_pml_buffers(struct kvm *kvm)
9506{
9507 int i;
9508 struct kvm_vcpu *vcpu;
9509 /*
9510 * We only need to kick vcpu out of guest mode here, as PML buffer
9511 * is flushed at beginning of all VMEXITs, and it's obvious that only
9512 * vcpus running in guest are possible to have unflushed GPAs in PML
9513 * buffer.
9514 */
9515 kvm_for_each_vcpu(i, vcpu, kvm)
9516 kvm_vcpu_kick(vcpu);
9517}
9518
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009519static void vmx_dump_sel(char *name, uint32_t sel)
9520{
9521 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009522 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009523 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9524 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9525 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9526}
9527
9528static void vmx_dump_dtsel(char *name, uint32_t limit)
9529{
9530 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9531 name, vmcs_read32(limit),
9532 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9533}
9534
9535static void dump_vmcs(void)
9536{
9537 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9538 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9539 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9540 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9541 u32 secondary_exec_control = 0;
9542 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009543 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009544 int i, n;
9545
9546 if (cpu_has_secondary_exec_ctrls())
9547 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9548
9549 pr_err("*** Guest State ***\n");
9550 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9551 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9552 vmcs_readl(CR0_GUEST_HOST_MASK));
9553 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9554 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9555 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9556 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9557 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9558 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009559 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9560 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9561 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9562 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009563 }
9564 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9565 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9566 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9567 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9568 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9569 vmcs_readl(GUEST_SYSENTER_ESP),
9570 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9571 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9572 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9573 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9574 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9575 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9576 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9577 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9578 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9579 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9580 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9581 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9582 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009583 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9584 efer, vmcs_read64(GUEST_IA32_PAT));
9585 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9586 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009587 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009588 if (cpu_has_load_perf_global_ctrl &&
9589 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009590 pr_err("PerfGlobCtl = 0x%016llx\n",
9591 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009592 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009593 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009594 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9595 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9596 vmcs_read32(GUEST_ACTIVITY_STATE));
9597 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9598 pr_err("InterruptStatus = %04x\n",
9599 vmcs_read16(GUEST_INTR_STATUS));
9600
9601 pr_err("*** Host State ***\n");
9602 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9603 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9604 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9605 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9606 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9607 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9608 vmcs_read16(HOST_TR_SELECTOR));
9609 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9610 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9611 vmcs_readl(HOST_TR_BASE));
9612 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9613 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9614 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9615 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9616 vmcs_readl(HOST_CR4));
9617 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9618 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9619 vmcs_read32(HOST_IA32_SYSENTER_CS),
9620 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9621 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009622 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9623 vmcs_read64(HOST_IA32_EFER),
9624 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009625 if (cpu_has_load_perf_global_ctrl &&
9626 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009627 pr_err("PerfGlobCtl = 0x%016llx\n",
9628 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009629
9630 pr_err("*** Control State ***\n");
9631 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9632 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9633 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9634 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9635 vmcs_read32(EXCEPTION_BITMAP),
9636 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9637 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9638 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9639 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9640 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9641 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9642 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9643 vmcs_read32(VM_EXIT_INTR_INFO),
9644 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9645 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9646 pr_err(" reason=%08x qualification=%016lx\n",
9647 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9648 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9649 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9650 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009651 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009652 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009653 pr_err("TSC Multiplier = 0x%016llx\n",
9654 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009655 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9656 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9657 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9658 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9659 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009660 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009661 n = vmcs_read32(CR3_TARGET_COUNT);
9662 for (i = 0; i + 1 < n; i += 4)
9663 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9664 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9665 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9666 if (i < n)
9667 pr_err("CR3 target%u=%016lx\n",
9668 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9669 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9670 pr_err("PLE Gap=%08x Window=%08x\n",
9671 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9672 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9673 pr_err("Virtual processor ID = 0x%04x\n",
9674 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9675}
9676
Avi Kivity6aa8b732006-12-10 02:21:36 -08009677/*
9678 * The guest has exited. See if we can fix it or if we need userspace
9679 * assistance.
9680 */
Avi Kivity851ba692009-08-24 11:10:17 +03009681static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009682{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009683 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009684 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009685 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009686
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009687 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9688
Kai Huang843e4332015-01-28 10:54:28 +08009689 /*
9690 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9691 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9692 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9693 * mode as if vcpus is in root mode, the PML buffer must has been
9694 * flushed already.
9695 */
9696 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009697 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009698
Mohammed Gamal80ced182009-09-01 12:48:18 +02009699 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009700 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009701 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009702
Paolo Bonzini7313c692017-07-27 10:31:25 +02009703 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9704 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009705
Mohammed Gamal51207022010-05-31 22:40:54 +03009706 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009707 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009708 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9709 vcpu->run->fail_entry.hardware_entry_failure_reason
9710 = exit_reason;
9711 return 0;
9712 }
9713
Avi Kivity29bd8a72007-09-10 17:27:03 +03009714 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009715 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9716 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009717 = vmcs_read32(VM_INSTRUCTION_ERROR);
9718 return 0;
9719 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009720
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009721 /*
9722 * Note:
9723 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9724 * delivery event since it indicates guest is accessing MMIO.
9725 * The vm-exit can be triggered again after return to guest that
9726 * will cause infinite loop.
9727 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009728 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009729 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009730 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009731 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009732 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9733 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9734 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009735 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009736 vcpu->run->internal.data[0] = vectoring_info;
9737 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009738 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9739 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9740 vcpu->run->internal.ndata++;
9741 vcpu->run->internal.data[3] =
9742 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9743 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009744 return 0;
9745 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009746
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009747 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009748 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9749 if (vmx_interrupt_allowed(vcpu)) {
9750 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9751 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9752 vcpu->arch.nmi_pending) {
9753 /*
9754 * This CPU don't support us in finding the end of an
9755 * NMI-blocked window if the guest runs with IRQs
9756 * disabled. So we pull the trigger after 1 s of
9757 * futile waiting, but inform the user about this.
9758 */
9759 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9760 "state on VCPU %d after 1 s timeout\n",
9761 __func__, vcpu->vcpu_id);
9762 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9763 }
9764 }
9765
Avi Kivity6aa8b732006-12-10 02:21:36 -08009766 if (exit_reason < kvm_vmx_max_exit_handlers
9767 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009768 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009769 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009770 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9771 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009772 kvm_queue_exception(vcpu, UD_VECTOR);
9773 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009775}
9776
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009777static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009778{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009779 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9780
9781 if (is_guest_mode(vcpu) &&
9782 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9783 return;
9784
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009785 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009786 vmcs_write32(TPR_THRESHOLD, 0);
9787 return;
9788 }
9789
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009790 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009791}
9792
Jim Mattson8d860bb2018-05-09 16:56:05 -04009793static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009794{
9795 u32 sec_exec_control;
9796
Jim Mattson8d860bb2018-05-09 16:56:05 -04009797 if (!lapic_in_kernel(vcpu))
9798 return;
9799
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009800 /* Postpone execution until vmcs01 is the current VMCS. */
9801 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009802 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009803 return;
9804 }
9805
Paolo Bonzini35754c92015-07-29 12:05:37 +02009806 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009807 return;
9808
9809 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009810 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9811 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009812
Jim Mattson8d860bb2018-05-09 16:56:05 -04009813 switch (kvm_get_apic_mode(vcpu)) {
9814 case LAPIC_MODE_INVALID:
9815 WARN_ONCE(true, "Invalid local APIC state");
9816 case LAPIC_MODE_DISABLED:
9817 break;
9818 case LAPIC_MODE_XAPIC:
9819 if (flexpriority_enabled) {
9820 sec_exec_control |=
9821 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9822 vmx_flush_tlb(vcpu, true);
9823 }
9824 break;
9825 case LAPIC_MODE_X2APIC:
9826 if (cpu_has_vmx_virtualize_x2apic_mode())
9827 sec_exec_control |=
9828 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9829 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009830 }
9831 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9832
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009833 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009834}
9835
Tang Chen38b99172014-09-24 15:57:54 +08009836static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9837{
Jim Mattsonab5df312018-05-09 17:02:03 -04009838 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009839 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009840 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009841 }
Tang Chen38b99172014-09-24 15:57:54 +08009842}
9843
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009844static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009845{
9846 u16 status;
9847 u8 old;
9848
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009849 if (max_isr == -1)
9850 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009851
9852 status = vmcs_read16(GUEST_INTR_STATUS);
9853 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009854 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009855 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009856 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009857 vmcs_write16(GUEST_INTR_STATUS, status);
9858 }
9859}
9860
9861static void vmx_set_rvi(int vector)
9862{
9863 u16 status;
9864 u8 old;
9865
Wei Wang4114c272014-11-05 10:53:43 +08009866 if (vector == -1)
9867 vector = 0;
9868
Yang Zhangc7c9c562013-01-25 10:18:51 +08009869 status = vmcs_read16(GUEST_INTR_STATUS);
9870 old = (u8)status & 0xff;
9871 if ((u8)vector != old) {
9872 status &= ~0xff;
9873 status |= (u8)vector;
9874 vmcs_write16(GUEST_INTR_STATUS, status);
9875 }
9876}
9877
9878static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9879{
Liran Alon851c1a182017-12-24 18:12:56 +02009880 /*
9881 * When running L2, updating RVI is only relevant when
9882 * vmcs12 virtual-interrupt-delivery enabled.
9883 * However, it can be enabled only when L1 also
9884 * intercepts external-interrupts and in that case
9885 * we should not update vmcs02 RVI but instead intercept
9886 * interrupt. Therefore, do nothing when running L2.
9887 */
9888 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009889 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009890}
9891
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009892static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009893{
9894 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009895 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009896 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009897
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009898 WARN_ON(!vcpu->arch.apicv_active);
9899 if (pi_test_on(&vmx->pi_desc)) {
9900 pi_clear_on(&vmx->pi_desc);
9901 /*
9902 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9903 * But on x86 this is just a compiler barrier anyway.
9904 */
9905 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009906 max_irr_updated =
9907 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9908
9909 /*
9910 * If we are running L2 and L1 has a new pending interrupt
9911 * which can be injected, we should re-evaluate
9912 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009913 * If L1 intercepts external-interrupts, we should
9914 * exit from L2 to L1. Otherwise, interrupt should be
9915 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009916 */
Liran Alon851c1a182017-12-24 18:12:56 +02009917 if (is_guest_mode(vcpu) && max_irr_updated) {
9918 if (nested_exit_on_intr(vcpu))
9919 kvm_vcpu_exiting_guest_mode(vcpu);
9920 else
9921 kvm_make_request(KVM_REQ_EVENT, vcpu);
9922 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009923 } else {
9924 max_irr = kvm_lapic_find_highest_irr(vcpu);
9925 }
9926 vmx_hwapic_irr_update(vcpu, max_irr);
9927 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009928}
9929
Andrey Smetanin63086302015-11-10 15:36:32 +03009930static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009931{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009932 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009933 return;
9934
Yang Zhangc7c9c562013-01-25 10:18:51 +08009935 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9936 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9937 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9938 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9939}
9940
Paolo Bonzini967235d2016-12-19 14:03:45 +01009941static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9942{
9943 struct vcpu_vmx *vmx = to_vmx(vcpu);
9944
9945 pi_clear_on(&vmx->pi_desc);
9946 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9947}
9948
Avi Kivity51aa01d2010-07-20 14:31:20 +03009949static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009950{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009951 u32 exit_intr_info = 0;
9952 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009953
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009954 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9955 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009956 return;
9957
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009958 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9959 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9960 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009961
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009962 /* if exit due to PF check for async PF */
9963 if (is_page_fault(exit_intr_info))
9964 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9965
Andi Kleena0861c02009-06-08 17:37:09 +08009966 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009967 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9968 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009969 kvm_machine_check();
9970
Gleb Natapov20f65982009-05-11 13:35:55 +03009971 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009972 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009973 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009974 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009975 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009976 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009977}
Gleb Natapov20f65982009-05-11 13:35:55 +03009978
Yang Zhanga547c6d2013-04-11 19:25:10 +08009979static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9980{
9981 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9982
Yang Zhanga547c6d2013-04-11 19:25:10 +08009983 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9984 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9985 unsigned int vector;
9986 unsigned long entry;
9987 gate_desc *desc;
9988 struct vcpu_vmx *vmx = to_vmx(vcpu);
9989#ifdef CONFIG_X86_64
9990 unsigned long tmp;
9991#endif
9992
9993 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9994 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009995 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009996 asm volatile(
9997#ifdef CONFIG_X86_64
9998 "mov %%" _ASM_SP ", %[sp]\n\t"
9999 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10000 "push $%c[ss]\n\t"
10001 "push %[sp]\n\t"
10002#endif
10003 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010004 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010005 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010006 :
10007#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010008 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010009#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010010 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010011 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010012 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010013 [ss]"i"(__KERNEL_DS),
10014 [cs]"i"(__KERNEL_CS)
10015 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010016 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010017}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010018STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010019
Tom Lendackybc226f02018-05-10 22:06:39 +020010020static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010021{
Tom Lendackybc226f02018-05-10 22:06:39 +020010022 switch (index) {
10023 case MSR_IA32_SMBASE:
10024 /*
10025 * We cannot do SMM unless we can run the guest in big
10026 * real mode.
10027 */
10028 return enable_unrestricted_guest || emulate_invalid_guest_state;
10029 case MSR_AMD64_VIRT_SPEC_CTRL:
10030 /* This is AMD only. */
10031 return false;
10032 default:
10033 return true;
10034 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010035}
10036
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010037static bool vmx_mpx_supported(void)
10038{
10039 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10040 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10041}
10042
Wanpeng Li55412b22014-12-02 19:21:30 +080010043static bool vmx_xsaves_supported(void)
10044{
10045 return vmcs_config.cpu_based_2nd_exec_ctrl &
10046 SECONDARY_EXEC_XSAVES;
10047}
10048
Avi Kivity51aa01d2010-07-20 14:31:20 +030010049static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10050{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010051 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010052 bool unblock_nmi;
10053 u8 vector;
10054 bool idtv_info_valid;
10055
10056 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010057
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010058 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010059 if (vmx->loaded_vmcs->nmi_known_unmasked)
10060 return;
10061 /*
10062 * Can't use vmx->exit_intr_info since we're not sure what
10063 * the exit reason is.
10064 */
10065 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10066 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10067 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10068 /*
10069 * SDM 3: 27.7.1.2 (September 2008)
10070 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10071 * a guest IRET fault.
10072 * SDM 3: 23.2.2 (September 2008)
10073 * Bit 12 is undefined in any of the following cases:
10074 * If the VM exit sets the valid bit in the IDT-vectoring
10075 * information field.
10076 * If the VM exit is due to a double fault.
10077 */
10078 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10079 vector != DF_VECTOR && !idtv_info_valid)
10080 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10081 GUEST_INTR_STATE_NMI);
10082 else
10083 vmx->loaded_vmcs->nmi_known_unmasked =
10084 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10085 & GUEST_INTR_STATE_NMI);
10086 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10087 vmx->loaded_vmcs->vnmi_blocked_time +=
10088 ktime_to_ns(ktime_sub(ktime_get(),
10089 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010090}
10091
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010092static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010093 u32 idt_vectoring_info,
10094 int instr_len_field,
10095 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010096{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010097 u8 vector;
10098 int type;
10099 bool idtv_info_valid;
10100
10101 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010102
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010103 vcpu->arch.nmi_injected = false;
10104 kvm_clear_exception_queue(vcpu);
10105 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010106
10107 if (!idtv_info_valid)
10108 return;
10109
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010110 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010111
Avi Kivity668f6122008-07-02 09:28:55 +030010112 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10113 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010114
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010115 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010116 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010117 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010118 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010119 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010120 * Clear bit "block by NMI" before VM entry if a NMI
10121 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010122 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010123 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010124 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010125 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010126 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010127 /* fall through */
10128 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010129 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010130 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010131 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010132 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010133 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010134 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010135 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010136 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010137 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010138 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010139 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010140 break;
10141 default:
10142 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010143 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010144}
10145
Avi Kivity83422e12010-07-20 14:43:23 +030010146static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10147{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010148 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010149 VM_EXIT_INSTRUCTION_LEN,
10150 IDT_VECTORING_ERROR_CODE);
10151}
10152
Avi Kivityb463a6f2010-07-20 15:06:17 +030010153static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10154{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010155 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010156 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10157 VM_ENTRY_INSTRUCTION_LEN,
10158 VM_ENTRY_EXCEPTION_ERROR_CODE);
10159
10160 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10161}
10162
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010163static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10164{
10165 int i, nr_msrs;
10166 struct perf_guest_switch_msr *msrs;
10167
10168 msrs = perf_guest_get_msrs(&nr_msrs);
10169
10170 if (!msrs)
10171 return;
10172
10173 for (i = 0; i < nr_msrs; i++)
10174 if (msrs[i].host == msrs[i].guest)
10175 clear_atomic_switch_msr(vmx, msrs[i].msr);
10176 else
10177 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10178 msrs[i].host);
10179}
10180
Jiang Biao33365e72016-11-03 15:03:37 +080010181static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010182{
10183 struct vcpu_vmx *vmx = to_vmx(vcpu);
10184 u64 tscl;
10185 u32 delta_tsc;
10186
10187 if (vmx->hv_deadline_tsc == -1)
10188 return;
10189
10190 tscl = rdtsc();
10191 if (vmx->hv_deadline_tsc > tscl)
10192 /* sure to be 32 bit only because checked on set_hv_timer */
10193 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10194 cpu_preemption_timer_multi);
10195 else
10196 delta_tsc = 0;
10197
10198 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10199}
10200
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010201static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010202{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010203 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010204 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010205
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010206 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010207 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010208 vmx->loaded_vmcs->soft_vnmi_blocked))
10209 vmx->loaded_vmcs->entry_time = ktime_get();
10210
Avi Kivity104f2262010-11-18 13:12:52 +020010211 /* Don't enter VMX if guest state is invalid, let the exit handler
10212 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010213 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010214 return;
10215
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010216 if (vmx->ple_window_dirty) {
10217 vmx->ple_window_dirty = false;
10218 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10219 }
10220
Abel Gordon012f83c2013-04-18 14:39:25 +030010221 if (vmx->nested.sync_shadow_vmcs) {
10222 copy_vmcs12_to_shadow(vmx);
10223 vmx->nested.sync_shadow_vmcs = false;
10224 }
10225
Avi Kivity104f2262010-11-18 13:12:52 +020010226 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10227 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10228 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10229 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10230
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010231 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010232 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010233 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010234 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010235 }
10236
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010237 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010238 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010239 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010240 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010241 }
10242
Avi Kivity104f2262010-11-18 13:12:52 +020010243 /* When single-stepping over STI and MOV SS, we must clear the
10244 * corresponding interruptibility bits in the guest state. Otherwise
10245 * vmentry fails as it then expects bit 14 (BS) in pending debug
10246 * exceptions being set, but that's not correct for the guest debugging
10247 * case. */
10248 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10249 vmx_set_interrupt_shadow(vcpu, 0);
10250
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010251 if (static_cpu_has(X86_FEATURE_PKU) &&
10252 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10253 vcpu->arch.pkru != vmx->host_pkru)
10254 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010255
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010256 atomic_switch_perf_msrs(vmx);
10257
Yunhong Jiang64672c92016-06-13 14:19:59 -070010258 vmx_arm_hv_timer(vcpu);
10259
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010260 /*
10261 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10262 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10263 * is no need to worry about the conditional branch over the wrmsr
10264 * being speculatively taken.
10265 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010266 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010267
Nadav Har'Eld462b812011-05-24 15:26:10 +030010268 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010269
10270 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10271 (unsigned long)&current_evmcs->host_rsp : 0;
10272
Avi Kivity104f2262010-11-18 13:12:52 +020010273 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010274 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010275 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10276 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10277 "push %%" _ASM_CX " \n\t"
10278 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010279 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010280 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010281 /* Avoid VMWRITE when Enlightened VMCS is in use */
10282 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10283 "jz 2f \n\t"
10284 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10285 "jmp 1f \n\t"
10286 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010287 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010288 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010289 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010290 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10291 "mov %%cr2, %%" _ASM_DX " \n\t"
10292 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010293 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010294 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010295 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010296 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010297 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010298 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010299 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10300 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10301 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10302 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10303 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10304 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010305#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010306 "mov %c[r8](%0), %%r8 \n\t"
10307 "mov %c[r9](%0), %%r9 \n\t"
10308 "mov %c[r10](%0), %%r10 \n\t"
10309 "mov %c[r11](%0), %%r11 \n\t"
10310 "mov %c[r12](%0), %%r12 \n\t"
10311 "mov %c[r13](%0), %%r13 \n\t"
10312 "mov %c[r14](%0), %%r14 \n\t"
10313 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010314#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010315 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010316
Avi Kivity6aa8b732006-12-10 02:21:36 -080010317 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010318 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010319 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010320 "jmp 2f \n\t"
10321 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10322 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010323 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010324 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010325 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010326 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010327 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10328 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10329 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10330 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10331 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10332 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10333 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010334#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010335 "mov %%r8, %c[r8](%0) \n\t"
10336 "mov %%r9, %c[r9](%0) \n\t"
10337 "mov %%r10, %c[r10](%0) \n\t"
10338 "mov %%r11, %c[r11](%0) \n\t"
10339 "mov %%r12, %c[r12](%0) \n\t"
10340 "mov %%r13, %c[r13](%0) \n\t"
10341 "mov %%r14, %c[r14](%0) \n\t"
10342 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010343 "xor %%r8d, %%r8d \n\t"
10344 "xor %%r9d, %%r9d \n\t"
10345 "xor %%r10d, %%r10d \n\t"
10346 "xor %%r11d, %%r11d \n\t"
10347 "xor %%r12d, %%r12d \n\t"
10348 "xor %%r13d, %%r13d \n\t"
10349 "xor %%r14d, %%r14d \n\t"
10350 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010351#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010352 "mov %%cr2, %%" _ASM_AX " \n\t"
10353 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010354
Jim Mattson0cb5b302018-01-03 14:31:38 -080010355 "xor %%eax, %%eax \n\t"
10356 "xor %%ebx, %%ebx \n\t"
10357 "xor %%esi, %%esi \n\t"
10358 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010359 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010360 ".pushsection .rodata \n\t"
10361 ".global vmx_return \n\t"
10362 "vmx_return: " _ASM_PTR " 2b \n\t"
10363 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010364 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010365 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010366 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010367 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010368 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10369 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10370 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10371 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10372 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10373 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10374 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010375#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010376 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10377 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10378 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10379 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10380 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10381 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10382 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10383 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010384#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010385 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10386 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010387 : "cc", "memory"
10388#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010389 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010390 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010391#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010392 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010393#endif
10394 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010395
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010396 /*
10397 * We do not use IBRS in the kernel. If this vCPU has used the
10398 * SPEC_CTRL MSR it may have left it on; save the value and
10399 * turn it off. This is much more efficient than blindly adding
10400 * it to the atomic save/restore list. Especially as the former
10401 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10402 *
10403 * For non-nested case:
10404 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10405 * save it.
10406 *
10407 * For nested case:
10408 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10409 * save it.
10410 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010411 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010412 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010413
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010414 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010415
David Woodhouse117cc7a2018-01-12 11:11:27 +000010416 /* Eliminate branch target predictions from guest mode */
10417 vmexit_fill_RSB();
10418
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010419 /* All fields are clean at this point */
10420 if (static_branch_unlikely(&enable_evmcs))
10421 current_evmcs->hv_clean_fields |=
10422 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10423
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010424 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010425 if (vmx->host_debugctlmsr)
10426 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010427
Avi Kivityaa67f602012-08-01 16:48:03 +030010428#ifndef CONFIG_X86_64
10429 /*
10430 * The sysexit path does not restore ds/es, so we must set them to
10431 * a reasonable value ourselves.
10432 *
10433 * We can't defer this to vmx_load_host_state() since that function
10434 * may be executed in interrupt context, which saves and restore segments
10435 * around it, nullifying its effect.
10436 */
10437 loadsegment(ds, __USER_DS);
10438 loadsegment(es, __USER_DS);
10439#endif
10440
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010441 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010442 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010443 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010444 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010445 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010446 vcpu->arch.regs_dirty = 0;
10447
Gleb Natapove0b890d2013-09-25 12:51:33 +030010448 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010449 * eager fpu is enabled if PKEY is supported and CR4 is switched
10450 * back on host, so it is safe to read guest PKRU from current
10451 * XSAVE.
10452 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010453 if (static_cpu_has(X86_FEATURE_PKU) &&
10454 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10455 vcpu->arch.pkru = __read_pkru();
10456 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010457 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010458 }
10459
Gleb Natapove0b890d2013-09-25 12:51:33 +030010460 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010461 vmx->idt_vectoring_info = 0;
10462
10463 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10464 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10465 return;
10466
10467 vmx->loaded_vmcs->launched = 1;
10468 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010469
Avi Kivity51aa01d2010-07-20 14:31:20 +030010470 vmx_complete_atomic_exit(vmx);
10471 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010472 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010473}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010474STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010475
Sean Christopherson434a1e92018-03-20 12:17:18 -070010476static struct kvm *vmx_vm_alloc(void)
10477{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010478 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010479 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010480}
10481
10482static void vmx_vm_free(struct kvm *kvm)
10483{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010484 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010485}
10486
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010487static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010488{
10489 struct vcpu_vmx *vmx = to_vmx(vcpu);
10490 int cpu;
10491
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010492 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010493 return;
10494
10495 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010496 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010497 vmx_vcpu_put(vcpu);
10498 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010499 put_cpu();
10500}
10501
Jim Mattson2f1fe812016-07-08 15:36:06 -070010502/*
10503 * Ensure that the current vmcs of the logical processor is the
10504 * vmcs01 of the vcpu before calling free_nested().
10505 */
10506static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10507{
10508 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010509
Christoffer Dallec7660c2017-12-04 21:35:23 +010010510 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010511 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010512 free_nested(vmx);
10513 vcpu_put(vcpu);
10514}
10515
Avi Kivity6aa8b732006-12-10 02:21:36 -080010516static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10517{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010518 struct vcpu_vmx *vmx = to_vmx(vcpu);
10519
Kai Huang843e4332015-01-28 10:54:28 +080010520 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010521 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010522 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010523 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010524 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010525 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010526 kfree(vmx->guest_msrs);
10527 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010528 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010529}
10530
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010531static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010532{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010533 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010534 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010535 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010536 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010537
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010538 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010539 return ERR_PTR(-ENOMEM);
10540
Wanpeng Li991e7a02015-09-16 17:30:05 +080010541 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010542
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010543 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10544 if (err)
10545 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010546
Peter Feiner4e595162016-07-07 14:49:58 -070010547 err = -ENOMEM;
10548
10549 /*
10550 * If PML is turned on, failure on enabling PML just results in failure
10551 * of creating the vcpu, therefore we can simplify PML logic (by
10552 * avoiding dealing with cases, such as enabling PML partially on vcpus
10553 * for the guest, etc.
10554 */
10555 if (enable_pml) {
10556 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10557 if (!vmx->pml_pg)
10558 goto uninit_vcpu;
10559 }
10560
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010561 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010562 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10563 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010564
Peter Feiner4e595162016-07-07 14:49:58 -070010565 if (!vmx->guest_msrs)
10566 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010567
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010568 err = alloc_loaded_vmcs(&vmx->vmcs01);
10569 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010570 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010571
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010572 msr_bitmap = vmx->vmcs01.msr_bitmap;
10573 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10574 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10575 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10576 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10577 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10578 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10579 vmx->msr_bitmap_mode = 0;
10580
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010581 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010582 cpu = get_cpu();
10583 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010584 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010585 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010586 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010587 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010588 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010589 err = alloc_apic_access_page(kvm);
10590 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010591 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010592 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010593
Sean Christophersone90008d2018-03-05 12:04:37 -080010594 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010595 err = init_rmode_identity_map(kvm);
10596 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010597 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010598 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010599
Wanpeng Li5c614b32015-10-13 09:18:36 -070010600 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010601 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10602 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010603 vmx->nested.vpid02 = allocate_vpid();
10604 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010605
Wincy Van705699a2015-02-03 23:58:17 +080010606 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010607 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010608
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010609 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10610
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010611 /*
10612 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10613 * or POSTED_INTR_WAKEUP_VECTOR.
10614 */
10615 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10616 vmx->pi_desc.sn = 1;
10617
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010618 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010619
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010620free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010621 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010622 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010623free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010624 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010625free_pml:
10626 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010627uninit_vcpu:
10628 kvm_vcpu_uninit(&vmx->vcpu);
10629free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010630 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010631 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010632 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010633}
10634
Wanpeng Lib31c1142018-03-12 04:53:04 -070010635static int vmx_vm_init(struct kvm *kvm)
10636{
10637 if (!ple_gap)
10638 kvm->arch.pause_in_guest = true;
10639 return 0;
10640}
10641
Yang, Sheng002c7f72007-07-31 14:23:01 +030010642static void __init vmx_check_processor_compat(void *rtn)
10643{
10644 struct vmcs_config vmcs_conf;
10645
10646 *(int *)rtn = 0;
10647 if (setup_vmcs_config(&vmcs_conf) < 0)
10648 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010649 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010650 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10651 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10652 smp_processor_id());
10653 *(int *)rtn = -EIO;
10654 }
10655}
10656
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010657static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010658{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010659 u8 cache;
10660 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010661
Sheng Yang522c68c2009-04-27 20:35:43 +080010662 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010663 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010664 * 2. EPT with VT-d:
10665 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010666 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010667 * b. VT-d with snooping control feature: snooping control feature of
10668 * VT-d engine can guarantee the cache correctness. Just set it
10669 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010670 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010671 * consistent with host MTRR
10672 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010673 if (is_mmio) {
10674 cache = MTRR_TYPE_UNCACHABLE;
10675 goto exit;
10676 }
10677
10678 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010679 ipat = VMX_EPT_IPAT_BIT;
10680 cache = MTRR_TYPE_WRBACK;
10681 goto exit;
10682 }
10683
10684 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10685 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010686 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010687 cache = MTRR_TYPE_WRBACK;
10688 else
10689 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010690 goto exit;
10691 }
10692
Xiao Guangrongff536042015-06-15 16:55:22 +080010693 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010694
10695exit:
10696 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010697}
10698
Sheng Yang17cc3932010-01-05 19:02:27 +080010699static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010700{
Sheng Yang878403b2010-01-05 19:02:29 +080010701 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10702 return PT_DIRECTORY_LEVEL;
10703 else
10704 /* For shadow and EPT supported 1GB page */
10705 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010706}
10707
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010708static void vmcs_set_secondary_exec_control(u32 new_ctl)
10709{
10710 /*
10711 * These bits in the secondary execution controls field
10712 * are dynamic, the others are mostly based on the hypervisor
10713 * architecture and the guest's CPUID. Do not touch the
10714 * dynamic bits.
10715 */
10716 u32 mask =
10717 SECONDARY_EXEC_SHADOW_VMCS |
10718 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010719 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10720 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010721
10722 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10723
10724 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10725 (new_ctl & ~mask) | (cur_ctl & mask));
10726}
10727
David Matlack8322ebb2016-11-29 18:14:09 -080010728/*
10729 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10730 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10731 */
10732static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10733{
10734 struct vcpu_vmx *vmx = to_vmx(vcpu);
10735 struct kvm_cpuid_entry2 *entry;
10736
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010737 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10738 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010739
10740#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10741 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010742 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010743} while (0)
10744
10745 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10746 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10747 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10748 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10749 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10750 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10751 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10752 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10753 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10754 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10755 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10756 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10757 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10758 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10759 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10760
10761 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10762 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10763 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10764 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10765 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010766 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010767
10768#undef cr4_fixed1_update
10769}
10770
Sheng Yang0e851882009-12-18 16:48:46 +080010771static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10772{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010773 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010774
Paolo Bonzini80154d72017-08-24 13:55:35 +020010775 if (cpu_has_secondary_exec_ctrls()) {
10776 vmx_compute_secondary_exec_control(vmx);
10777 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010778 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010779
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010780 if (nested_vmx_allowed(vcpu))
10781 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10782 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10783 else
10784 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10785 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010786
10787 if (nested_vmx_allowed(vcpu))
10788 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010789}
10790
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010791static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10792{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010793 if (func == 1 && nested)
10794 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010795}
10796
Yang Zhang25d92082013-08-06 12:00:32 +030010797static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10798 struct x86_exception *fault)
10799{
Jan Kiszka533558b2014-01-04 18:47:20 +010010800 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010802 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010803 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010804
Bandan Dasc5f983f2017-05-05 15:25:14 -040010805 if (vmx->nested.pml_full) {
10806 exit_reason = EXIT_REASON_PML_FULL;
10807 vmx->nested.pml_full = false;
10808 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10809 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010810 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010811 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010812 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010813
10814 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010815 vmcs12->guest_physical_address = fault->address;
10816}
10817
Peter Feiner995f00a2017-06-30 17:26:32 -070010818static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10819{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010820 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010821}
10822
Nadav Har'El155a97a2013-08-05 11:07:16 +030010823/* Callbacks for nested_ept_init_mmu_context: */
10824
10825static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10826{
10827 /* return the page table to be shadowed - in our case, EPT12 */
10828 return get_vmcs12(vcpu)->ept_pointer;
10829}
10830
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010831static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010832{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010833 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010834 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010835 return 1;
10836
Paolo Bonziniad896af2013-10-02 16:56:14 +020010837 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010838 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010839 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010840 nested_ept_ad_enabled(vcpu),
10841 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010842 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10843 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10844 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10845
10846 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010847 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010848}
10849
10850static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10851{
10852 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10853}
10854
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010855static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10856 u16 error_code)
10857{
10858 bool inequality, bit;
10859
10860 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10861 inequality =
10862 (error_code & vmcs12->page_fault_error_code_mask) !=
10863 vmcs12->page_fault_error_code_match;
10864 return inequality ^ bit;
10865}
10866
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010867static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10868 struct x86_exception *fault)
10869{
10870 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10871
10872 WARN_ON(!is_guest_mode(vcpu));
10873
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010874 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10875 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010876 vmcs12->vm_exit_intr_error_code = fault->error_code;
10877 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10878 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10879 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10880 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010881 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010882 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010883 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010884}
10885
Paolo Bonzinic9923842017-12-13 14:16:30 +010010886static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10887 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010888
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010889static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010890{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010891 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010892 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010893 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010894 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010895
10896 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010897 /*
10898 * Translate L1 physical address to host physical
10899 * address for vmcs02. Keep the page pinned, so this
10900 * physical address remains valid. We keep a reference
10901 * to it so we can release it later.
10902 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010903 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010904 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010905 vmx->nested.apic_access_page = NULL;
10906 }
10907 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010908 /*
10909 * If translation failed, no matter: This feature asks
10910 * to exit when accessing the given address, and if it
10911 * can never be accessed, this feature won't do
10912 * anything anyway.
10913 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010914 if (!is_error_page(page)) {
10915 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010916 hpa = page_to_phys(vmx->nested.apic_access_page);
10917 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10918 } else {
10919 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10920 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10921 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010922 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010923
10924 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010925 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010926 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010927 vmx->nested.virtual_apic_page = NULL;
10928 }
10929 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010930
10931 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010932 * If translation failed, VM entry will fail because
10933 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10934 * Failing the vm entry is _not_ what the processor
10935 * does but it's basically the only possibility we
10936 * have. We could still enter the guest if CR8 load
10937 * exits are enabled, CR8 store exits are enabled, and
10938 * virtualize APIC access is disabled; in this case
10939 * the processor would never use the TPR shadow and we
10940 * could simply clear the bit from the execution
10941 * control. But such a configuration is useless, so
10942 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010943 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010944 if (!is_error_page(page)) {
10945 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010946 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10947 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10948 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010949 }
10950
Wincy Van705699a2015-02-03 23:58:17 +080010951 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010952 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10953 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010954 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010955 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010956 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010957 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10958 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010959 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010960 vmx->nested.pi_desc_page = page;
10961 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010962 vmx->nested.pi_desc =
10963 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10964 (unsigned long)(vmcs12->posted_intr_desc_addr &
10965 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010966 vmcs_write64(POSTED_INTR_DESC_ADDR,
10967 page_to_phys(vmx->nested.pi_desc_page) +
10968 (unsigned long)(vmcs12->posted_intr_desc_addr &
10969 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010970 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010971 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010972 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10973 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010974 else
10975 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10976 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010977}
10978
Jan Kiszkaf41245002014-03-07 20:03:13 +010010979static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10980{
10981 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10982 struct vcpu_vmx *vmx = to_vmx(vcpu);
10983
10984 if (vcpu->arch.virtual_tsc_khz == 0)
10985 return;
10986
10987 /* Make sure short timeouts reliably trigger an immediate vmexit.
10988 * hrtimer_start does not guarantee this. */
10989 if (preemption_timeout <= 1) {
10990 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10991 return;
10992 }
10993
10994 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10995 preemption_timeout *= 1000000;
10996 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10997 hrtimer_start(&vmx->nested.preemption_timer,
10998 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10999}
11000
Jim Mattson56a20512017-07-06 16:33:06 -070011001static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11002 struct vmcs12 *vmcs12)
11003{
11004 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11005 return 0;
11006
11007 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11008 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11009 return -EINVAL;
11010
11011 return 0;
11012}
11013
Wincy Van3af18d92015-02-03 23:49:31 +080011014static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11015 struct vmcs12 *vmcs12)
11016{
Wincy Van3af18d92015-02-03 23:49:31 +080011017 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11018 return 0;
11019
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011020 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011021 return -EINVAL;
11022
11023 return 0;
11024}
11025
Jim Mattson712b12d2017-08-24 13:24:47 -070011026static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11027 struct vmcs12 *vmcs12)
11028{
11029 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11030 return 0;
11031
11032 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11033 return -EINVAL;
11034
11035 return 0;
11036}
11037
Wincy Van3af18d92015-02-03 23:49:31 +080011038/*
11039 * Merge L0's and L1's MSR bitmap, return false to indicate that
11040 * we do not use the hardware.
11041 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011042static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11043 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011044{
Wincy Van82f0dd42015-02-03 23:57:18 +080011045 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011046 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011047 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011048 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011049 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011050 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011051 *
11052 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11053 * ensures that we do not accidentally generate an L02 MSR bitmap
11054 * from the L12 MSR bitmap that is too permissive.
11055 * 2. That L1 or L2s have actually used the MSR. This avoids
11056 * unnecessarily merging of the bitmap if the MSR is unused. This
11057 * works properly because we only update the L01 MSR bitmap lazily.
11058 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11059 * updated to reflect this when L1 (or its L2s) actually write to
11060 * the MSR.
11061 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011062 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11063 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011064
Paolo Bonzinic9923842017-12-13 14:16:30 +010011065 /* Nothing to do if the MSR bitmap is not in use. */
11066 if (!cpu_has_vmx_msr_bitmap() ||
11067 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11068 return false;
11069
Ashok Raj15d45072018-02-01 22:59:43 +010011070 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011071 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011072 return false;
11073
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011074 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11075 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011076 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011077
Radim Krčmářd048c092016-08-08 20:16:22 +020011078 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011079 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11080 /*
11081 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11082 * just lets the processor take the value from the virtual-APIC page;
11083 * take those 256 bits directly from the L1 bitmap.
11084 */
11085 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11086 unsigned word = msr / BITS_PER_LONG;
11087 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11088 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011089 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011090 } else {
11091 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11092 unsigned word = msr / BITS_PER_LONG;
11093 msr_bitmap_l0[word] = ~0;
11094 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11095 }
11096 }
11097
11098 nested_vmx_disable_intercept_for_msr(
11099 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011100 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011101 MSR_TYPE_W);
11102
11103 if (nested_cpu_has_vid(vmcs12)) {
11104 nested_vmx_disable_intercept_for_msr(
11105 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011106 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011107 MSR_TYPE_W);
11108 nested_vmx_disable_intercept_for_msr(
11109 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011110 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011111 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011112 }
Ashok Raj15d45072018-02-01 22:59:43 +010011113
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011114 if (spec_ctrl)
11115 nested_vmx_disable_intercept_for_msr(
11116 msr_bitmap_l1, msr_bitmap_l0,
11117 MSR_IA32_SPEC_CTRL,
11118 MSR_TYPE_R | MSR_TYPE_W);
11119
Ashok Raj15d45072018-02-01 22:59:43 +010011120 if (pred_cmd)
11121 nested_vmx_disable_intercept_for_msr(
11122 msr_bitmap_l1, msr_bitmap_l0,
11123 MSR_IA32_PRED_CMD,
11124 MSR_TYPE_W);
11125
Wincy Vanf2b93282015-02-03 23:56:03 +080011126 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011127 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011128
11129 return true;
11130}
11131
Liran Alon61ada742018-06-23 02:35:08 +030011132static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11133 struct vmcs12 *vmcs12)
11134{
11135 struct vmcs12 *shadow;
11136 struct page *page;
11137
11138 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11139 vmcs12->vmcs_link_pointer == -1ull)
11140 return;
11141
11142 shadow = get_shadow_vmcs12(vcpu);
11143 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11144
11145 memcpy(shadow, kmap(page), VMCS12_SIZE);
11146
11147 kunmap(page);
11148 kvm_release_page_clean(page);
11149}
11150
11151static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11152 struct vmcs12 *vmcs12)
11153{
11154 struct vcpu_vmx *vmx = to_vmx(vcpu);
11155
11156 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11157 vmcs12->vmcs_link_pointer == -1ull)
11158 return;
11159
11160 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11161 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11162}
11163
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011164static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11165 struct vmcs12 *vmcs12)
11166{
11167 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11168 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11169 return -EINVAL;
11170 else
11171 return 0;
11172}
11173
Wincy Vanf2b93282015-02-03 23:56:03 +080011174static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11175 struct vmcs12 *vmcs12)
11176{
Wincy Van82f0dd42015-02-03 23:57:18 +080011177 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011178 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011179 !nested_cpu_has_vid(vmcs12) &&
11180 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011181 return 0;
11182
11183 /*
11184 * If virtualize x2apic mode is enabled,
11185 * virtualize apic access must be disabled.
11186 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011187 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11188 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011189 return -EINVAL;
11190
Wincy Van608406e2015-02-03 23:57:51 +080011191 /*
11192 * If virtual interrupt delivery is enabled,
11193 * we must exit on external interrupts.
11194 */
11195 if (nested_cpu_has_vid(vmcs12) &&
11196 !nested_exit_on_intr(vcpu))
11197 return -EINVAL;
11198
Wincy Van705699a2015-02-03 23:58:17 +080011199 /*
11200 * bits 15:8 should be zero in posted_intr_nv,
11201 * the descriptor address has been already checked
11202 * in nested_get_vmcs12_pages.
11203 */
11204 if (nested_cpu_has_posted_intr(vmcs12) &&
11205 (!nested_cpu_has_vid(vmcs12) ||
11206 !nested_exit_intr_ack_set(vcpu) ||
11207 vmcs12->posted_intr_nv & 0xff00))
11208 return -EINVAL;
11209
Wincy Vanf2b93282015-02-03 23:56:03 +080011210 /* tpr shadow is needed by all apicv features. */
11211 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11212 return -EINVAL;
11213
11214 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011215}
11216
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011217static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11218 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011219 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011220{
Liran Alone2536742018-06-23 02:35:02 +030011221 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011222 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011223 u64 count, addr;
11224
Liran Alone2536742018-06-23 02:35:02 +030011225 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11226 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011227 WARN_ON(1);
11228 return -EINVAL;
11229 }
11230 if (count == 0)
11231 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011232 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011233 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11234 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011235 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011236 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11237 addr_field, maxphyaddr, count, addr);
11238 return -EINVAL;
11239 }
11240 return 0;
11241}
11242
11243static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11244 struct vmcs12 *vmcs12)
11245{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011246 if (vmcs12->vm_exit_msr_load_count == 0 &&
11247 vmcs12->vm_exit_msr_store_count == 0 &&
11248 vmcs12->vm_entry_msr_load_count == 0)
11249 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011250 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011251 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011252 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011253 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011254 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011255 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011256 return -EINVAL;
11257 return 0;
11258}
11259
Bandan Dasc5f983f2017-05-05 15:25:14 -040011260static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11261 struct vmcs12 *vmcs12)
11262{
11263 u64 address = vmcs12->pml_address;
11264 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11265
11266 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11267 if (!nested_cpu_has_ept(vmcs12) ||
11268 !IS_ALIGNED(address, 4096) ||
11269 address >> maxphyaddr)
11270 return -EINVAL;
11271 }
11272
11273 return 0;
11274}
11275
Liran Alona8a7c022018-06-23 02:35:06 +030011276static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11277 struct vmcs12 *vmcs12)
11278{
11279 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11280 return 0;
11281
11282 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11283 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11284 return -EINVAL;
11285
11286 return 0;
11287}
11288
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011289static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11290 struct vmx_msr_entry *e)
11291{
11292 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011293 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011294 return -EINVAL;
11295 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11296 e->index == MSR_IA32_UCODE_REV)
11297 return -EINVAL;
11298 if (e->reserved != 0)
11299 return -EINVAL;
11300 return 0;
11301}
11302
11303static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11304 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011305{
11306 if (e->index == MSR_FS_BASE ||
11307 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011308 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11309 nested_vmx_msr_check_common(vcpu, e))
11310 return -EINVAL;
11311 return 0;
11312}
11313
11314static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11315 struct vmx_msr_entry *e)
11316{
11317 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11318 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011319 return -EINVAL;
11320 return 0;
11321}
11322
11323/*
11324 * Load guest's/host's msr at nested entry/exit.
11325 * return 0 for success, entry index for failure.
11326 */
11327static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11328{
11329 u32 i;
11330 struct vmx_msr_entry e;
11331 struct msr_data msr;
11332
11333 msr.host_initiated = false;
11334 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011335 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11336 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011337 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011338 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11339 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011340 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011341 }
11342 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011343 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011344 "%s check failed (%u, 0x%x, 0x%x)\n",
11345 __func__, i, e.index, e.reserved);
11346 goto fail;
11347 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011348 msr.index = e.index;
11349 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011350 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011351 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011352 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11353 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011354 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011355 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011356 }
11357 return 0;
11358fail:
11359 return i + 1;
11360}
11361
11362static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11363{
11364 u32 i;
11365 struct vmx_msr_entry e;
11366
11367 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011368 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011369 if (kvm_vcpu_read_guest(vcpu,
11370 gpa + i * sizeof(e),
11371 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011372 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011373 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11374 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011375 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011376 }
11377 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011378 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011379 "%s check failed (%u, 0x%x, 0x%x)\n",
11380 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011381 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011382 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011383 msr_info.host_initiated = false;
11384 msr_info.index = e.index;
11385 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011386 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011387 "%s cannot read MSR (%u, 0x%x)\n",
11388 __func__, i, e.index);
11389 return -EINVAL;
11390 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011391 if (kvm_vcpu_write_guest(vcpu,
11392 gpa + i * sizeof(e) +
11393 offsetof(struct vmx_msr_entry, value),
11394 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011395 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011396 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011397 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011398 return -EINVAL;
11399 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011400 }
11401 return 0;
11402}
11403
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011404static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11405{
11406 unsigned long invalid_mask;
11407
11408 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11409 return (val & invalid_mask) == 0;
11410}
11411
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011412/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011413 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11414 * emulating VM entry into a guest with EPT enabled.
11415 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11416 * is assigned to entry_failure_code on failure.
11417 */
11418static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011419 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011420{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011421 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011422 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011423 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11424 return 1;
11425 }
11426
11427 /*
11428 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11429 * must not be dereferenced.
11430 */
11431 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11432 !nested_ept) {
11433 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11434 *entry_failure_code = ENTRY_FAIL_PDPTE;
11435 return 1;
11436 }
11437 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011438 }
11439
Junaid Shahid50c28f22018-06-27 14:59:11 -070011440 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011441 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011442
11443 vcpu->arch.cr3 = cr3;
11444 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11445
11446 kvm_init_mmu(vcpu, false);
11447
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011448 return 0;
11449}
11450
Jim Mattson6514dc32018-04-26 16:09:12 -070011451static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011452{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011453 struct vcpu_vmx *vmx = to_vmx(vcpu);
11454
11455 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11456 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11457 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11458 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11459 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11460 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11461 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11462 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11463 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11464 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11465 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11466 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11467 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11468 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11469 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11470 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11471 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11472 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11473 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11474 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11475 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11476 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11477 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11478 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11479 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11480 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11481 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11482 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11483 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11484 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11485 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011486
11487 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11488 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11489 vmcs12->guest_pending_dbg_exceptions);
11490 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11491 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11492
11493 if (nested_cpu_has_xsaves(vmcs12))
11494 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11495 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11496
11497 if (cpu_has_vmx_posted_intr())
11498 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11499
11500 /*
11501 * Whether page-faults are trapped is determined by a combination of
11502 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11503 * If enable_ept, L0 doesn't care about page faults and we should
11504 * set all of these to L1's desires. However, if !enable_ept, L0 does
11505 * care about (at least some) page faults, and because it is not easy
11506 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11507 * to exit on each and every L2 page fault. This is done by setting
11508 * MASK=MATCH=0 and (see below) EB.PF=1.
11509 * Note that below we don't need special code to set EB.PF beyond the
11510 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11511 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11512 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11513 */
11514 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11515 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11516 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11517 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11518
11519 /* All VMFUNCs are currently emulated through L0 vmexits. */
11520 if (cpu_has_vmx_vmfunc())
11521 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11522
11523 if (cpu_has_vmx_apicv()) {
11524 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11525 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11526 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11527 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11528 }
11529
11530 /*
11531 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11532 * Some constant fields are set here by vmx_set_constant_host_state().
11533 * Other fields are different per CPU, and will be set later when
11534 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11535 */
11536 vmx_set_constant_host_state(vmx);
11537
11538 /*
11539 * Set the MSR load/store lists to match L0's settings.
11540 */
11541 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11542 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11543 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11544 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11545 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11546
11547 set_cr4_guest_host_mask(vmx);
11548
11549 if (vmx_mpx_supported())
11550 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11551
11552 if (enable_vpid) {
11553 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11554 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11555 else
11556 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11557 }
11558
11559 /*
11560 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11561 */
11562 if (enable_ept) {
11563 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11564 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11565 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11566 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11567 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011568
11569 if (cpu_has_vmx_msr_bitmap())
11570 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011571}
11572
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011573/*
11574 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11575 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011576 * 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 +030011577 * guest in a way that will both be appropriate to L1's requests, and our
11578 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11579 * function also has additional necessary side-effects, like setting various
11580 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011581 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11582 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011583 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011584static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011585 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011586{
11587 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011588 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011589
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011590 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011591 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011592 vmx->nested.dirty_vmcs12 = false;
11593 }
11594
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011595 /*
11596 * First, the fields that are shadowed. This must be kept in sync
11597 * with vmx_shadow_fields.h.
11598 */
11599
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011600 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011601 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011602 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011603 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11604 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011605
11606 /*
11607 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11608 * HOST_FS_BASE, HOST_GS_BASE.
11609 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011610
Jim Mattson6514dc32018-04-26 16:09:12 -070011611 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011612 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011613 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11614 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11615 } else {
11616 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11617 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11618 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011619 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011620 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11621 vmcs12->vm_entry_intr_info_field);
11622 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11623 vmcs12->vm_entry_exception_error_code);
11624 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11625 vmcs12->vm_entry_instruction_len);
11626 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11627 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011628 vmx->loaded_vmcs->nmi_known_unmasked =
11629 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011630 } else {
11631 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11632 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011633 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011634
Jan Kiszkaf41245002014-03-07 20:03:13 +010011635 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011636
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011637 /* Preemption timer setting is only taken from vmcs01. */
11638 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11639 exec_control |= vmcs_config.pin_based_exec_ctrl;
11640 if (vmx->hv_deadline_tsc == -1)
11641 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11642
11643 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011644 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011645 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11646 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011647 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011648 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011649 }
Wincy Van705699a2015-02-03 23:58:17 +080011650
Jan Kiszkaf41245002014-03-07 20:03:13 +010011651 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011652
Jan Kiszkaf41245002014-03-07 20:03:13 +010011653 vmx->nested.preemption_timer_expired = false;
11654 if (nested_cpu_has_preemption_timer(vmcs12))
11655 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011656
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011657 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011658 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011659
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011660 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011661 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011662 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011663 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011664 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011665 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011666 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11667 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011668 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011669 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11670 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11671 ~SECONDARY_EXEC_ENABLE_PML;
11672 exec_control |= vmcs12_exec_ctrl;
11673 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011674
Liran Alon32c7acf2018-06-23 02:35:11 +030011675 /* VMCS shadowing for L2 is emulated for now */
11676 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11677
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011678 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011679 vmcs_write16(GUEST_INTR_STATUS,
11680 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011681
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011682 /*
11683 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11684 * nested_get_vmcs12_pages will either fix it up or
11685 * remove the VM execution control.
11686 */
11687 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11688 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11689
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011690 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11691 }
11692
Jim Mattson83bafef2016-10-04 10:48:38 -070011693 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011694 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11695 * entry, but only if the current (host) sp changed from the value
11696 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11697 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11698 * here we just force the write to happen on entry.
11699 */
11700 vmx->host_rsp = 0;
11701
11702 exec_control = vmx_exec_control(vmx); /* L0's desires */
11703 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11704 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11705 exec_control &= ~CPU_BASED_TPR_SHADOW;
11706 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011707
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011708 /*
11709 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11710 * nested_get_vmcs12_pages can't fix it up, the illegal value
11711 * will result in a VM entry failure.
11712 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011713 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011714 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011715 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011716 } else {
11717#ifdef CONFIG_X86_64
11718 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11719 CPU_BASED_CR8_STORE_EXITING;
11720#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011721 }
11722
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011723 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011724 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11725 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011726 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011727 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11728 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11729
11730 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11731
11732 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11733 * bitwise-or of what L1 wants to trap for L2, and what we want to
11734 * trap. Note that CR0.TS also needs updating - we do this later.
11735 */
11736 update_exception_bitmap(vcpu);
11737 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11738 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11739
Nadav Har'El8049d652013-08-05 11:07:06 +030011740 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11741 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11742 * bits are further modified by vmx_set_efer() below.
11743 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011744 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011745
11746 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11747 * emulated by vmx_set_efer(), below.
11748 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011749 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011750 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11751 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011752 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11753
Jim Mattson6514dc32018-04-26 16:09:12 -070011754 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011755 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011756 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011757 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011758 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011759 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011760 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011761
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011762 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11763
Peter Feinerc95ba922016-08-17 09:36:47 -070011764 if (kvm_has_tsc_control)
11765 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011766
11767 if (enable_vpid) {
11768 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011769 * There is no direct mapping between vpid02 and vpid12, the
11770 * vpid02 is per-vCPU for L0 and reused while the value of
11771 * vpid12 is changed w/ one invvpid during nested vmentry.
11772 * The vpid12 is allocated by L1 for L2, so it will not
11773 * influence global bitmap(for vpid01 and vpid02 allocation)
11774 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011775 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011776 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011777 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11778 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011779 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011780 }
11781 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011782 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011783 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011784 }
11785
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011786 if (enable_pml) {
11787 /*
11788 * Conceptually we want to copy the PML address and index from
11789 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11790 * since we always flush the log on each vmexit, this happens
11791 * to be equivalent to simply resetting the fields in vmcs02.
11792 */
11793 ASSERT(vmx->pml_pg);
11794 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11795 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11796 }
11797
Nadav Har'El155a97a2013-08-05 11:07:16 +030011798 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011799 if (nested_ept_init_mmu_context(vcpu)) {
11800 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11801 return 1;
11802 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011803 } else if (nested_cpu_has2(vmcs12,
11804 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011805 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011806 }
11807
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011808 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011809 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11810 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011811 * The CR0_READ_SHADOW is what L2 should have expected to read given
11812 * the specifications by L1; It's not enough to take
11813 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11814 * have more bits than L1 expected.
11815 */
11816 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11817 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11818
11819 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11820 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11821
Jim Mattson6514dc32018-04-26 16:09:12 -070011822 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011823 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011824 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11825 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11826 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11827 else
11828 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11829 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11830 vmx_set_efer(vcpu, vcpu->arch.efer);
11831
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011832 /*
11833 * Guest state is invalid and unrestricted guest is disabled,
11834 * which means L1 attempted VMEntry to L2 with invalid state.
11835 * Fail the VMEntry.
11836 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011837 if (vmx->emulation_required) {
11838 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011839 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011840 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011841
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011842 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011843 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011844 entry_failure_code))
11845 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011846
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011847 if (!enable_ept)
11848 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11849
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011850 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11851 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011852 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011853}
11854
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011855static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11856{
11857 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11858 nested_cpu_has_virtual_nmis(vmcs12))
11859 return -EINVAL;
11860
11861 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11862 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11863 return -EINVAL;
11864
11865 return 0;
11866}
11867
Jim Mattsonca0bde22016-11-30 12:03:46 -080011868static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11869{
11870 struct vcpu_vmx *vmx = to_vmx(vcpu);
11871
11872 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11873 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11874 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11875
Jim Mattson56a20512017-07-06 16:33:06 -070011876 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11877 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11878
Jim Mattsonca0bde22016-11-30 12:03:46 -080011879 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11880 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11881
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011882 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11883 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11884
Jim Mattson712b12d2017-08-24 13:24:47 -070011885 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11886 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11887
Jim Mattsonca0bde22016-11-30 12:03:46 -080011888 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11889 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11890
11891 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11892 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11893
Bandan Dasc5f983f2017-05-05 15:25:14 -040011894 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11895 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11896
Liran Alona8a7c022018-06-23 02:35:06 +030011897 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11898 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11899
Jim Mattsonca0bde22016-11-30 12:03:46 -080011900 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011901 vmx->nested.msrs.procbased_ctls_low,
11902 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011903 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11904 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011905 vmx->nested.msrs.secondary_ctls_low,
11906 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011907 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011908 vmx->nested.msrs.pinbased_ctls_low,
11909 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011910 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011911 vmx->nested.msrs.exit_ctls_low,
11912 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011913 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011914 vmx->nested.msrs.entry_ctls_low,
11915 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011916 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11917
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011918 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011919 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11920
Bandan Das41ab9372017-08-03 15:54:43 -040011921 if (nested_cpu_has_vmfunc(vmcs12)) {
11922 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011923 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011924 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11925
11926 if (nested_cpu_has_eptp_switching(vmcs12)) {
11927 if (!nested_cpu_has_ept(vmcs12) ||
11928 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11929 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11930 }
11931 }
Bandan Das27c42a12017-08-03 15:54:42 -040011932
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011933 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11934 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11935
Jim Mattsonca0bde22016-11-30 12:03:46 -080011936 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11937 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11938 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11939 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11940
Marc Orr04473782018-06-20 17:21:29 -070011941 /*
11942 * From the Intel SDM, volume 3:
11943 * Fields relevant to VM-entry event injection must be set properly.
11944 * These fields are the VM-entry interruption-information field, the
11945 * VM-entry exception error code, and the VM-entry instruction length.
11946 */
11947 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11948 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11949 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11950 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11951 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11952 bool should_have_error_code;
11953 bool urg = nested_cpu_has2(vmcs12,
11954 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11955 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11956
11957 /* VM-entry interruption-info field: interruption type */
11958 if (intr_type == INTR_TYPE_RESERVED ||
11959 (intr_type == INTR_TYPE_OTHER_EVENT &&
11960 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11961 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11962
11963 /* VM-entry interruption-info field: vector */
11964 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11965 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11966 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11967 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11968
11969 /* VM-entry interruption-info field: deliver error code */
11970 should_have_error_code =
11971 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11972 x86_exception_has_error_code(vector);
11973 if (has_error_code != should_have_error_code)
11974 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11975
11976 /* VM-entry exception error code */
11977 if (has_error_code &&
11978 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11979 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11980
11981 /* VM-entry interruption-info field: reserved bits */
11982 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11983 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11984
11985 /* VM-entry instruction length */
11986 switch (intr_type) {
11987 case INTR_TYPE_SOFT_EXCEPTION:
11988 case INTR_TYPE_SOFT_INTR:
11989 case INTR_TYPE_PRIV_SW_EXCEPTION:
11990 if ((vmcs12->vm_entry_instruction_len > 15) ||
11991 (vmcs12->vm_entry_instruction_len == 0 &&
11992 !nested_cpu_has_zero_length_injection(vcpu)))
11993 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11994 }
11995 }
11996
Jim Mattsonca0bde22016-11-30 12:03:46 -080011997 return 0;
11998}
11999
Liran Alonf145d902018-06-23 02:35:07 +030012000static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12001 struct vmcs12 *vmcs12)
12002{
12003 int r;
12004 struct page *page;
12005 struct vmcs12 *shadow;
12006
12007 if (vmcs12->vmcs_link_pointer == -1ull)
12008 return 0;
12009
12010 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12011 return -EINVAL;
12012
12013 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12014 if (is_error_page(page))
12015 return -EINVAL;
12016
12017 r = 0;
12018 shadow = kmap(page);
12019 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12020 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12021 r = -EINVAL;
12022 kunmap(page);
12023 kvm_release_page_clean(page);
12024 return r;
12025}
12026
Jim Mattsonca0bde22016-11-30 12:03:46 -080012027static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12028 u32 *exit_qual)
12029{
12030 bool ia32e;
12031
12032 *exit_qual = ENTRY_FAIL_DEFAULT;
12033
12034 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12035 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12036 return 1;
12037
Liran Alonf145d902018-06-23 02:35:07 +030012038 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012039 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12040 return 1;
12041 }
12042
12043 /*
12044 * If the load IA32_EFER VM-entry control is 1, the following checks
12045 * are performed on the field for the IA32_EFER MSR:
12046 * - Bits reserved in the IA32_EFER MSR must be 0.
12047 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12048 * the IA-32e mode guest VM-exit control. It must also be identical
12049 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12050 * CR0.PG) is 1.
12051 */
12052 if (to_vmx(vcpu)->nested.nested_run_pending &&
12053 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12054 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12055 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12056 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12057 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12058 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12059 return 1;
12060 }
12061
12062 /*
12063 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12064 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12065 * the values of the LMA and LME bits in the field must each be that of
12066 * the host address-space size VM-exit control.
12067 */
12068 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12069 ia32e = (vmcs12->vm_exit_controls &
12070 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12071 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12072 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12073 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12074 return 1;
12075 }
12076
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012077 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12078 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12079 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12080 return 1;
12081
Jim Mattsonca0bde22016-11-30 12:03:46 -080012082 return 0;
12083}
12084
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012085/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012086 * If exit_qual is NULL, this is being called from state restore (either RSM
12087 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012088 */
12089static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012090{
12091 struct vcpu_vmx *vmx = to_vmx(vcpu);
12092 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012093 bool from_vmentry = !!exit_qual;
12094 u32 dummy_exit_qual;
12095 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012096
Jim Mattson858e25c2016-11-30 12:03:47 -080012097 enter_guest_mode(vcpu);
12098
12099 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12100 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12101
Jim Mattsonde3a0022017-11-27 17:22:25 -060012102 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012103 vmx_segment_cache_clear(vmx);
12104
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012105 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12106 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12107
12108 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012109 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012110 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012111
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012112 if (from_vmentry) {
12113 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012114
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012115 r = EXIT_REASON_MSR_LOAD_FAIL;
12116 *exit_qual = nested_vmx_load_msr(vcpu,
12117 vmcs12->vm_entry_msr_load_addr,
12118 vmcs12->vm_entry_msr_load_count);
12119 if (*exit_qual)
12120 goto fail;
12121 } else {
12122 /*
12123 * The MMU is not initialized to point at the right entities yet and
12124 * "get pages" would need to read data from the guest (i.e. we will
12125 * need to perform gpa to hpa translation). Request a call
12126 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12127 * have already been set at vmentry time and should not be reset.
12128 */
12129 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12130 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012131
Jim Mattson858e25c2016-11-30 12:03:47 -080012132 /*
12133 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12134 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12135 * returned as far as L1 is concerned. It will only return (and set
12136 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12137 */
12138 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012139
12140fail:
12141 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12142 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12143 leave_guest_mode(vcpu);
12144 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012145 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012146}
12147
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012148/*
12149 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12150 * for running an L2 nested guest.
12151 */
12152static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12153{
12154 struct vmcs12 *vmcs12;
12155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012156 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012157 u32 exit_qual;
12158 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012159
Kyle Hueyeb277562016-11-29 12:40:39 -080012160 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012161 return 1;
12162
Kyle Hueyeb277562016-11-29 12:40:39 -080012163 if (!nested_vmx_check_vmcs12(vcpu))
12164 goto out;
12165
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012166 vmcs12 = get_vmcs12(vcpu);
12167
Liran Alona6192d42018-06-23 02:35:04 +030012168 /*
12169 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12170 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12171 * rather than RFLAGS.ZF, and no error number is stored to the
12172 * VM-instruction error field.
12173 */
12174 if (vmcs12->hdr.shadow_vmcs) {
12175 nested_vmx_failInvalid(vcpu);
12176 goto out;
12177 }
12178
Abel Gordon012f83c2013-04-18 14:39:25 +030012179 if (enable_shadow_vmcs)
12180 copy_shadow_to_vmcs12(vmx);
12181
Nadav Har'El7c177932011-05-25 23:12:04 +030012182 /*
12183 * The nested entry process starts with enforcing various prerequisites
12184 * on vmcs12 as required by the Intel SDM, and act appropriately when
12185 * they fail: As the SDM explains, some conditions should cause the
12186 * instruction to fail, while others will cause the instruction to seem
12187 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12188 * To speed up the normal (success) code path, we should avoid checking
12189 * for misconfigurations which will anyway be caught by the processor
12190 * when using the merged vmcs02.
12191 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012192 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12193 nested_vmx_failValid(vcpu,
12194 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12195 goto out;
12196 }
12197
Nadav Har'El7c177932011-05-25 23:12:04 +030012198 if (vmcs12->launch_state == launch) {
12199 nested_vmx_failValid(vcpu,
12200 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12201 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012202 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012203 }
12204
Jim Mattsonca0bde22016-11-30 12:03:46 -080012205 ret = check_vmentry_prereqs(vcpu, vmcs12);
12206 if (ret) {
12207 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012208 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012209 }
12210
Nadav Har'El7c177932011-05-25 23:12:04 +030012211 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012212 * After this point, the trap flag no longer triggers a singlestep trap
12213 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12214 * This is not 100% correct; for performance reasons, we delegate most
12215 * of the checks on host state to the processor. If those fail,
12216 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012217 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012218 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012219
Jim Mattsonca0bde22016-11-30 12:03:46 -080012220 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12221 if (ret) {
12222 nested_vmx_entry_failure(vcpu, vmcs12,
12223 EXIT_REASON_INVALID_STATE, exit_qual);
12224 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012225 }
12226
12227 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012228 * We're finally done with prerequisite checking, and can start with
12229 * the nested entry.
12230 */
12231
Jim Mattson6514dc32018-04-26 16:09:12 -070012232 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012233 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012234 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012235 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012236 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012237 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012238 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012239
Chao Gao135a06c2018-02-11 10:06:30 +080012240 /*
Liran Alon61ada742018-06-23 02:35:08 +030012241 * Must happen outside of enter_vmx_non_root_mode() as it will
12242 * also be used as part of restoring nVMX state for
12243 * snapshot restore (migration).
12244 *
12245 * In this flow, it is assumed that vmcs12 cache was
12246 * trasferred as part of captured nVMX state and should
12247 * therefore not be read from guest memory (which may not
12248 * exist on destination host yet).
12249 */
12250 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12251
12252 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012253 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12254 * by event injection, halt vcpu.
12255 */
12256 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012257 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12258 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012259 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012260 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012261 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012262
12263out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012264 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012265}
12266
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012267/*
12268 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12269 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12270 * This function returns the new value we should put in vmcs12.guest_cr0.
12271 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12272 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12273 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12274 * didn't trap the bit, because if L1 did, so would L0).
12275 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12276 * been modified by L2, and L1 knows it. So just leave the old value of
12277 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12278 * isn't relevant, because if L0 traps this bit it can set it to anything.
12279 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12280 * changed these bits, and therefore they need to be updated, but L0
12281 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12282 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12283 */
12284static inline unsigned long
12285vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12286{
12287 return
12288 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12289 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12290 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12291 vcpu->arch.cr0_guest_owned_bits));
12292}
12293
12294static inline unsigned long
12295vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12296{
12297 return
12298 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12299 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12300 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12301 vcpu->arch.cr4_guest_owned_bits));
12302}
12303
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012304static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12305 struct vmcs12 *vmcs12)
12306{
12307 u32 idt_vectoring;
12308 unsigned int nr;
12309
Wanpeng Li664f8e22017-08-24 03:35:09 -070012310 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012311 nr = vcpu->arch.exception.nr;
12312 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12313
12314 if (kvm_exception_is_soft(nr)) {
12315 vmcs12->vm_exit_instruction_len =
12316 vcpu->arch.event_exit_inst_len;
12317 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12318 } else
12319 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12320
12321 if (vcpu->arch.exception.has_error_code) {
12322 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12323 vmcs12->idt_vectoring_error_code =
12324 vcpu->arch.exception.error_code;
12325 }
12326
12327 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012328 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012329 vmcs12->idt_vectoring_info_field =
12330 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012331 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012332 nr = vcpu->arch.interrupt.nr;
12333 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12334
12335 if (vcpu->arch.interrupt.soft) {
12336 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12337 vmcs12->vm_entry_instruction_len =
12338 vcpu->arch.event_exit_inst_len;
12339 } else
12340 idt_vectoring |= INTR_TYPE_EXT_INTR;
12341
12342 vmcs12->idt_vectoring_info_field = idt_vectoring;
12343 }
12344}
12345
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012346static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12347{
12348 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012349 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012350 bool block_nested_events =
12351 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012352
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012353 if (vcpu->arch.exception.pending &&
12354 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012355 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012356 return -EBUSY;
12357 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012358 return 0;
12359 }
12360
Jan Kiszkaf41245002014-03-07 20:03:13 +010012361 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12362 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012363 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012364 return -EBUSY;
12365 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12366 return 0;
12367 }
12368
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012369 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012370 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012371 return -EBUSY;
12372 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12373 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12374 INTR_INFO_VALID_MASK, 0);
12375 /*
12376 * The NMI-triggered VM exit counts as injection:
12377 * clear this one and block further NMIs.
12378 */
12379 vcpu->arch.nmi_pending = 0;
12380 vmx_set_nmi_mask(vcpu, true);
12381 return 0;
12382 }
12383
12384 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12385 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012386 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012387 return -EBUSY;
12388 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012389 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012390 }
12391
David Hildenbrand6342c502017-01-25 11:58:58 +010012392 vmx_complete_nested_posted_interrupt(vcpu);
12393 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012394}
12395
Jan Kiszkaf41245002014-03-07 20:03:13 +010012396static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12397{
12398 ktime_t remaining =
12399 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12400 u64 value;
12401
12402 if (ktime_to_ns(remaining) <= 0)
12403 return 0;
12404
12405 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12406 do_div(value, 1000000);
12407 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12408}
12409
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012410/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012411 * Update the guest state fields of vmcs12 to reflect changes that
12412 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12413 * VM-entry controls is also updated, since this is really a guest
12414 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012415 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012416static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012417{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012418 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12419 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12420
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012421 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12422 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12423 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12424
12425 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12426 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12427 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12428 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12429 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12430 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12431 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12432 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12433 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12434 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12435 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12436 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12437 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12438 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12439 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12440 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12441 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12442 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12443 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12444 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12445 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12446 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12447 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12448 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12449 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12450 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12451 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12452 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12453 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12454 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12455 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12456 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12457 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12458 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12459 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12460 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12461
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012462 vmcs12->guest_interruptibility_info =
12463 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12464 vmcs12->guest_pending_dbg_exceptions =
12465 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012466 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12467 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12468 else
12469 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012470
Jan Kiszkaf41245002014-03-07 20:03:13 +010012471 if (nested_cpu_has_preemption_timer(vmcs12)) {
12472 if (vmcs12->vm_exit_controls &
12473 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12474 vmcs12->vmx_preemption_timer_value =
12475 vmx_get_preemption_timer_value(vcpu);
12476 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12477 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012478
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012479 /*
12480 * In some cases (usually, nested EPT), L2 is allowed to change its
12481 * own CR3 without exiting. If it has changed it, we must keep it.
12482 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12483 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12484 *
12485 * Additionally, restore L2's PDPTR to vmcs12.
12486 */
12487 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012488 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012489 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12490 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12491 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12492 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12493 }
12494
Jim Mattsond281e132017-06-01 12:44:46 -070012495 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012496
Wincy Van608406e2015-02-03 23:57:51 +080012497 if (nested_cpu_has_vid(vmcs12))
12498 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12499
Jan Kiszkac18911a2013-03-13 16:06:41 +010012500 vmcs12->vm_entry_controls =
12501 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012502 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012503
Jan Kiszka2996fca2014-06-16 13:59:43 +020012504 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12505 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12506 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12507 }
12508
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012509 /* TODO: These cannot have changed unless we have MSR bitmaps and
12510 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012511 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012512 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012513 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12514 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012515 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12516 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12517 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012518 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012519 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012520}
12521
12522/*
12523 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12524 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12525 * and this function updates it to reflect the changes to the guest state while
12526 * L2 was running (and perhaps made some exits which were handled directly by L0
12527 * without going back to L1), and to reflect the exit reason.
12528 * Note that we do not have to copy here all VMCS fields, just those that
12529 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12530 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12531 * which already writes to vmcs12 directly.
12532 */
12533static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12534 u32 exit_reason, u32 exit_intr_info,
12535 unsigned long exit_qualification)
12536{
12537 /* update guest state fields: */
12538 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012539
12540 /* update exit information fields: */
12541
Jan Kiszka533558b2014-01-04 18:47:20 +010012542 vmcs12->vm_exit_reason = exit_reason;
12543 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012544 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012545
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012546 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012547 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12548 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12549
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012550 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012551 vmcs12->launch_state = 1;
12552
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012553 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12554 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012555 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012556
12557 /*
12558 * Transfer the event that L0 or L1 may wanted to inject into
12559 * L2 to IDT_VECTORING_INFO_FIELD.
12560 */
12561 vmcs12_save_pending_event(vcpu, vmcs12);
12562 }
12563
12564 /*
12565 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12566 * preserved above and would only end up incorrectly in L1.
12567 */
12568 vcpu->arch.nmi_injected = false;
12569 kvm_clear_exception_queue(vcpu);
12570 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012571}
12572
Wanpeng Li5af41572017-11-05 16:54:49 -080012573static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12574 struct vmcs12 *vmcs12)
12575{
12576 u32 entry_failure_code;
12577
12578 nested_ept_uninit_mmu_context(vcpu);
12579
12580 /*
12581 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12582 * couldn't have changed.
12583 */
12584 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12585 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12586
12587 if (!enable_ept)
12588 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12589}
12590
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012591/*
12592 * A part of what we need to when the nested L2 guest exits and we want to
12593 * run its L1 parent, is to reset L1's guest state to the host state specified
12594 * in vmcs12.
12595 * This function is to be called not only on normal nested exit, but also on
12596 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12597 * Failures During or After Loading Guest State").
12598 * This function should be called when the active VMCS is L1's (vmcs01).
12599 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012600static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12601 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012602{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012603 struct kvm_segment seg;
12604
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012605 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12606 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012607 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012608 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12609 else
12610 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12611 vmx_set_efer(vcpu, vcpu->arch.efer);
12612
12613 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12614 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012615 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012616 /*
12617 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012618 * actually changed, because vmx_set_cr0 refers to efer set above.
12619 *
12620 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12621 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012622 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012623 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012624 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012625
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012626 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012627 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012628 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012629
Wanpeng Li5af41572017-11-05 16:54:49 -080012630 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012631
Liran Alon6f1e03b2018-05-22 17:16:14 +030012632 /*
12633 * If vmcs01 don't use VPID, CPU flushes TLB on every
12634 * VMEntry/VMExit. Thus, no need to flush TLB.
12635 *
12636 * If vmcs12 uses VPID, TLB entries populated by L2 are
12637 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12638 * with vmx->vpid. Thus, no need to flush TLB.
12639 *
12640 * Therefore, flush TLB only in case vmcs01 uses VPID and
12641 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12642 * are both tagged with vmx->vpid.
12643 */
12644 if (enable_vpid &&
12645 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012646 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012647 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012648
12649 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12650 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12651 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12652 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12653 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012654 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12655 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012656
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012657 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12658 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12659 vmcs_write64(GUEST_BNDCFGS, 0);
12660
Jan Kiszka44811c02013-08-04 17:17:27 +020012661 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012662 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012663 vcpu->arch.pat = vmcs12->host_ia32_pat;
12664 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012665 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12666 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12667 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012668
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012669 /* Set L1 segment info according to Intel SDM
12670 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12671 seg = (struct kvm_segment) {
12672 .base = 0,
12673 .limit = 0xFFFFFFFF,
12674 .selector = vmcs12->host_cs_selector,
12675 .type = 11,
12676 .present = 1,
12677 .s = 1,
12678 .g = 1
12679 };
12680 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12681 seg.l = 1;
12682 else
12683 seg.db = 1;
12684 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12685 seg = (struct kvm_segment) {
12686 .base = 0,
12687 .limit = 0xFFFFFFFF,
12688 .type = 3,
12689 .present = 1,
12690 .s = 1,
12691 .db = 1,
12692 .g = 1
12693 };
12694 seg.selector = vmcs12->host_ds_selector;
12695 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12696 seg.selector = vmcs12->host_es_selector;
12697 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12698 seg.selector = vmcs12->host_ss_selector;
12699 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12700 seg.selector = vmcs12->host_fs_selector;
12701 seg.base = vmcs12->host_fs_base;
12702 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12703 seg.selector = vmcs12->host_gs_selector;
12704 seg.base = vmcs12->host_gs_base;
12705 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12706 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012707 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012708 .limit = 0x67,
12709 .selector = vmcs12->host_tr_selector,
12710 .type = 11,
12711 .present = 1
12712 };
12713 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12714
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012715 kvm_set_dr(vcpu, 7, 0x400);
12716 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012717
Wincy Van3af18d92015-02-03 23:49:31 +080012718 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012719 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012720
Wincy Vanff651cb2014-12-11 08:52:58 +030012721 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12722 vmcs12->vm_exit_msr_load_count))
12723 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012724}
12725
12726/*
12727 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12728 * and modify vmcs12 to make it see what it would expect to see there if
12729 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12730 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012731static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12732 u32 exit_intr_info,
12733 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012734{
12735 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12737
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012738 /* trying to cancel vmlaunch/vmresume is a bug */
12739 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12740
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012741 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012742 * The only expected VM-instruction error is "VM entry with
12743 * invalid control field(s)." Anything else indicates a
12744 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012745 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012746 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12747 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12748
12749 leave_guest_mode(vcpu);
12750
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012751 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12752 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12753
Jim Mattson4f350c62017-09-14 16:31:44 -070012754 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012755 if (exit_reason == -1)
12756 sync_vmcs12(vcpu, vmcs12);
12757 else
12758 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12759 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012760
Liran Alon61ada742018-06-23 02:35:08 +030012761 /*
12762 * Must happen outside of sync_vmcs12() as it will
12763 * also be used to capture vmcs12 cache as part of
12764 * capturing nVMX state for snapshot (migration).
12765 *
12766 * Otherwise, this flush will dirty guest memory at a
12767 * point it is already assumed by user-space to be
12768 * immutable.
12769 */
12770 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12771
Jim Mattson4f350c62017-09-14 16:31:44 -070012772 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12773 vmcs12->vm_exit_msr_store_count))
12774 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012775 }
12776
Jim Mattson4f350c62017-09-14 16:31:44 -070012777 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012778 vm_entry_controls_reset_shadow(vmx);
12779 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012780 vmx_segment_cache_clear(vmx);
12781
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012782 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012783 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12784 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012785 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012786 if (vmx->hv_deadline_tsc == -1)
12787 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12788 PIN_BASED_VMX_PREEMPTION_TIMER);
12789 else
12790 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12791 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012792 if (kvm_has_tsc_control)
12793 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012794
Jim Mattson8d860bb2018-05-09 16:56:05 -040012795 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12796 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12797 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012798 } else if (!nested_cpu_has_ept(vmcs12) &&
12799 nested_cpu_has2(vmcs12,
12800 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012801 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012802 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012803
12804 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12805 vmx->host_rsp = 0;
12806
12807 /* Unpin physical memory we referred to in vmcs02 */
12808 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012809 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012810 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012811 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012812 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012813 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012814 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012815 }
Wincy Van705699a2015-02-03 23:58:17 +080012816 if (vmx->nested.pi_desc_page) {
12817 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012818 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012819 vmx->nested.pi_desc_page = NULL;
12820 vmx->nested.pi_desc = NULL;
12821 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012822
12823 /*
Tang Chen38b99172014-09-24 15:57:54 +080012824 * We are now running in L2, mmu_notifier will force to reload the
12825 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12826 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012827 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012828
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012829 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012830 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012831
12832 /* in case we halted in L2 */
12833 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012834
12835 if (likely(!vmx->fail)) {
12836 /*
12837 * TODO: SDM says that with acknowledge interrupt on
12838 * exit, bit 31 of the VM-exit interrupt information
12839 * (valid interrupt) is always set to 1 on
12840 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12841 * need kvm_cpu_has_interrupt(). See the commit
12842 * message for details.
12843 */
12844 if (nested_exit_intr_ack_set(vcpu) &&
12845 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12846 kvm_cpu_has_interrupt(vcpu)) {
12847 int irq = kvm_cpu_get_interrupt(vcpu);
12848 WARN_ON(irq < 0);
12849 vmcs12->vm_exit_intr_info = irq |
12850 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12851 }
12852
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012853 if (exit_reason != -1)
12854 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12855 vmcs12->exit_qualification,
12856 vmcs12->idt_vectoring_info_field,
12857 vmcs12->vm_exit_intr_info,
12858 vmcs12->vm_exit_intr_error_code,
12859 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012860
12861 load_vmcs12_host_state(vcpu, vmcs12);
12862
12863 return;
12864 }
12865
12866 /*
12867 * After an early L2 VM-entry failure, we're now back
12868 * in L1 which thinks it just finished a VMLAUNCH or
12869 * VMRESUME instruction, so we need to set the failure
12870 * flag and the VM-instruction error field of the VMCS
12871 * accordingly.
12872 */
12873 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012874
12875 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12876
Jim Mattson4f350c62017-09-14 16:31:44 -070012877 /*
12878 * The emulated instruction was already skipped in
12879 * nested_vmx_run, but the updated RIP was never
12880 * written back to the vmcs01.
12881 */
12882 skip_emulated_instruction(vcpu);
12883 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012884}
12885
Nadav Har'El7c177932011-05-25 23:12:04 +030012886/*
Jan Kiszka42124922014-01-04 18:47:19 +010012887 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12888 */
12889static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12890{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012891 if (is_guest_mode(vcpu)) {
12892 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012893 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012894 }
Jan Kiszka42124922014-01-04 18:47:19 +010012895 free_nested(to_vmx(vcpu));
12896}
12897
12898/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012899 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12900 * 23.7 "VM-entry failures during or after loading guest state" (this also
12901 * lists the acceptable exit-reason and exit-qualification parameters).
12902 * It should only be called before L2 actually succeeded to run, and when
12903 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12904 */
12905static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12906 struct vmcs12 *vmcs12,
12907 u32 reason, unsigned long qualification)
12908{
12909 load_vmcs12_host_state(vcpu, vmcs12);
12910 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12911 vmcs12->exit_qualification = qualification;
12912 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012913 if (enable_shadow_vmcs)
12914 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012915}
12916
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012917static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12918 struct x86_instruction_info *info,
12919 enum x86_intercept_stage stage)
12920{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012921 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12922 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12923
12924 /*
12925 * RDPID causes #UD if disabled through secondary execution controls.
12926 * Because it is marked as EmulateOnUD, we need to intercept it here.
12927 */
12928 if (info->intercept == x86_intercept_rdtscp &&
12929 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12930 ctxt->exception.vector = UD_VECTOR;
12931 ctxt->exception.error_code_valid = false;
12932 return X86EMUL_PROPAGATE_FAULT;
12933 }
12934
12935 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012936 return X86EMUL_CONTINUE;
12937}
12938
Yunhong Jiang64672c92016-06-13 14:19:59 -070012939#ifdef CONFIG_X86_64
12940/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12941static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12942 u64 divisor, u64 *result)
12943{
12944 u64 low = a << shift, high = a >> (64 - shift);
12945
12946 /* To avoid the overflow on divq */
12947 if (high >= divisor)
12948 return 1;
12949
12950 /* Low hold the result, high hold rem which is discarded */
12951 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12952 "rm" (divisor), "0" (low), "1" (high));
12953 *result = low;
12954
12955 return 0;
12956}
12957
12958static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12959{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012960 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012961 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012962
12963 if (kvm_mwait_in_guest(vcpu->kvm))
12964 return -EOPNOTSUPP;
12965
12966 vmx = to_vmx(vcpu);
12967 tscl = rdtsc();
12968 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12969 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012970 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12971
12972 if (delta_tsc > lapic_timer_advance_cycles)
12973 delta_tsc -= lapic_timer_advance_cycles;
12974 else
12975 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012976
12977 /* Convert to host delta tsc if tsc scaling is enabled */
12978 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12979 u64_shl_div_u64(delta_tsc,
12980 kvm_tsc_scaling_ratio_frac_bits,
12981 vcpu->arch.tsc_scaling_ratio,
12982 &delta_tsc))
12983 return -ERANGE;
12984
12985 /*
12986 * If the delta tsc can't fit in the 32 bit after the multi shift,
12987 * we can't use the preemption timer.
12988 * It's possible that it fits on later vmentries, but checking
12989 * on every vmentry is costly so we just use an hrtimer.
12990 */
12991 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12992 return -ERANGE;
12993
12994 vmx->hv_deadline_tsc = tscl + delta_tsc;
12995 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12996 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012997
12998 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012999}
13000
13001static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13002{
13003 struct vcpu_vmx *vmx = to_vmx(vcpu);
13004 vmx->hv_deadline_tsc = -1;
13005 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13006 PIN_BASED_VMX_PREEMPTION_TIMER);
13007}
13008#endif
13009
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013010static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013011{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013012 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013013 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013014}
13015
Kai Huang843e4332015-01-28 10:54:28 +080013016static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13017 struct kvm_memory_slot *slot)
13018{
13019 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13020 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13021}
13022
13023static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13024 struct kvm_memory_slot *slot)
13025{
13026 kvm_mmu_slot_set_dirty(kvm, slot);
13027}
13028
13029static void vmx_flush_log_dirty(struct kvm *kvm)
13030{
13031 kvm_flush_pml_buffers(kvm);
13032}
13033
Bandan Dasc5f983f2017-05-05 15:25:14 -040013034static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13035{
13036 struct vmcs12 *vmcs12;
13037 struct vcpu_vmx *vmx = to_vmx(vcpu);
13038 gpa_t gpa;
13039 struct page *page = NULL;
13040 u64 *pml_address;
13041
13042 if (is_guest_mode(vcpu)) {
13043 WARN_ON_ONCE(vmx->nested.pml_full);
13044
13045 /*
13046 * Check if PML is enabled for the nested guest.
13047 * Whether eptp bit 6 is set is already checked
13048 * as part of A/D emulation.
13049 */
13050 vmcs12 = get_vmcs12(vcpu);
13051 if (!nested_cpu_has_pml(vmcs12))
13052 return 0;
13053
Dan Carpenter47698862017-05-10 22:43:17 +030013054 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013055 vmx->nested.pml_full = true;
13056 return 1;
13057 }
13058
13059 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13060
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013061 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13062 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013063 return 0;
13064
13065 pml_address = kmap(page);
13066 pml_address[vmcs12->guest_pml_index--] = gpa;
13067 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013068 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013069 }
13070
13071 return 0;
13072}
13073
Kai Huang843e4332015-01-28 10:54:28 +080013074static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13075 struct kvm_memory_slot *memslot,
13076 gfn_t offset, unsigned long mask)
13077{
13078 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13079}
13080
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013081static void __pi_post_block(struct kvm_vcpu *vcpu)
13082{
13083 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13084 struct pi_desc old, new;
13085 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013086
13087 do {
13088 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013089 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13090 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013091
13092 dest = cpu_physical_id(vcpu->cpu);
13093
13094 if (x2apic_enabled())
13095 new.ndst = dest;
13096 else
13097 new.ndst = (dest << 8) & 0xFF00;
13098
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013099 /* set 'NV' to 'notification vector' */
13100 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013101 } while (cmpxchg64(&pi_desc->control, old.control,
13102 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013103
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013104 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13105 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013106 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013107 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013108 vcpu->pre_pcpu = -1;
13109 }
13110}
13111
Feng Wuefc64402015-09-18 22:29:51 +080013112/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013113 * This routine does the following things for vCPU which is going
13114 * to be blocked if VT-d PI is enabled.
13115 * - Store the vCPU to the wakeup list, so when interrupts happen
13116 * we can find the right vCPU to wake up.
13117 * - Change the Posted-interrupt descriptor as below:
13118 * 'NDST' <-- vcpu->pre_pcpu
13119 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13120 * - If 'ON' is set during this process, which means at least one
13121 * interrupt is posted for this vCPU, we cannot block it, in
13122 * this case, return 1, otherwise, return 0.
13123 *
13124 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013125static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013126{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013127 unsigned int dest;
13128 struct pi_desc old, new;
13129 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13130
13131 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013132 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13133 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013134 return 0;
13135
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013136 WARN_ON(irqs_disabled());
13137 local_irq_disable();
13138 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13139 vcpu->pre_pcpu = vcpu->cpu;
13140 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13141 list_add_tail(&vcpu->blocked_vcpu_list,
13142 &per_cpu(blocked_vcpu_on_cpu,
13143 vcpu->pre_pcpu));
13144 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13145 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013146
13147 do {
13148 old.control = new.control = pi_desc->control;
13149
Feng Wubf9f6ac2015-09-18 22:29:55 +080013150 WARN((pi_desc->sn == 1),
13151 "Warning: SN field of posted-interrupts "
13152 "is set before blocking\n");
13153
13154 /*
13155 * Since vCPU can be preempted during this process,
13156 * vcpu->cpu could be different with pre_pcpu, we
13157 * need to set pre_pcpu as the destination of wakeup
13158 * notification event, then we can find the right vCPU
13159 * to wakeup in wakeup handler if interrupts happen
13160 * when the vCPU is in blocked state.
13161 */
13162 dest = cpu_physical_id(vcpu->pre_pcpu);
13163
13164 if (x2apic_enabled())
13165 new.ndst = dest;
13166 else
13167 new.ndst = (dest << 8) & 0xFF00;
13168
13169 /* set 'NV' to 'wakeup vector' */
13170 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013171 } while (cmpxchg64(&pi_desc->control, old.control,
13172 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013173
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013174 /* We should not block the vCPU if an interrupt is posted for it. */
13175 if (pi_test_on(pi_desc) == 1)
13176 __pi_post_block(vcpu);
13177
13178 local_irq_enable();
13179 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013180}
13181
Yunhong Jiangbc225122016-06-13 14:19:58 -070013182static int vmx_pre_block(struct kvm_vcpu *vcpu)
13183{
13184 if (pi_pre_block(vcpu))
13185 return 1;
13186
Yunhong Jiang64672c92016-06-13 14:19:59 -070013187 if (kvm_lapic_hv_timer_in_use(vcpu))
13188 kvm_lapic_switch_to_sw_timer(vcpu);
13189
Yunhong Jiangbc225122016-06-13 14:19:58 -070013190 return 0;
13191}
13192
13193static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013194{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013195 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013196 return;
13197
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013198 WARN_ON(irqs_disabled());
13199 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013200 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013201 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013202}
13203
Yunhong Jiangbc225122016-06-13 14:19:58 -070013204static void vmx_post_block(struct kvm_vcpu *vcpu)
13205{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013206 if (kvm_x86_ops->set_hv_timer)
13207 kvm_lapic_switch_to_hv_timer(vcpu);
13208
Yunhong Jiangbc225122016-06-13 14:19:58 -070013209 pi_post_block(vcpu);
13210}
13211
Feng Wubf9f6ac2015-09-18 22:29:55 +080013212/*
Feng Wuefc64402015-09-18 22:29:51 +080013213 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13214 *
13215 * @kvm: kvm
13216 * @host_irq: host irq of the interrupt
13217 * @guest_irq: gsi of the interrupt
13218 * @set: set or unset PI
13219 * returns 0 on success, < 0 on failure
13220 */
13221static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13222 uint32_t guest_irq, bool set)
13223{
13224 struct kvm_kernel_irq_routing_entry *e;
13225 struct kvm_irq_routing_table *irq_rt;
13226 struct kvm_lapic_irq irq;
13227 struct kvm_vcpu *vcpu;
13228 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013229 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013230
13231 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013232 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13233 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013234 return 0;
13235
13236 idx = srcu_read_lock(&kvm->irq_srcu);
13237 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013238 if (guest_irq >= irq_rt->nr_rt_entries ||
13239 hlist_empty(&irq_rt->map[guest_irq])) {
13240 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13241 guest_irq, irq_rt->nr_rt_entries);
13242 goto out;
13243 }
Feng Wuefc64402015-09-18 22:29:51 +080013244
13245 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13246 if (e->type != KVM_IRQ_ROUTING_MSI)
13247 continue;
13248 /*
13249 * VT-d PI cannot support posting multicast/broadcast
13250 * interrupts to a vCPU, we still use interrupt remapping
13251 * for these kind of interrupts.
13252 *
13253 * For lowest-priority interrupts, we only support
13254 * those with single CPU as the destination, e.g. user
13255 * configures the interrupts via /proc/irq or uses
13256 * irqbalance to make the interrupts single-CPU.
13257 *
13258 * We will support full lowest-priority interrupt later.
13259 */
13260
Radim Krčmář371313132016-07-12 22:09:27 +020013261 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013262 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13263 /*
13264 * Make sure the IRTE is in remapped mode if
13265 * we don't handle it in posted mode.
13266 */
13267 ret = irq_set_vcpu_affinity(host_irq, NULL);
13268 if (ret < 0) {
13269 printk(KERN_INFO
13270 "failed to back to remapped mode, irq: %u\n",
13271 host_irq);
13272 goto out;
13273 }
13274
Feng Wuefc64402015-09-18 22:29:51 +080013275 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013276 }
Feng Wuefc64402015-09-18 22:29:51 +080013277
13278 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13279 vcpu_info.vector = irq.vector;
13280
hu huajun2698d822018-04-11 15:16:40 +080013281 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013282 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13283
13284 if (set)
13285 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013286 else
Feng Wuefc64402015-09-18 22:29:51 +080013287 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013288
13289 if (ret < 0) {
13290 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13291 __func__);
13292 goto out;
13293 }
13294 }
13295
13296 ret = 0;
13297out:
13298 srcu_read_unlock(&kvm->irq_srcu, idx);
13299 return ret;
13300}
13301
Ashok Rajc45dcc72016-06-22 14:59:56 +080013302static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13303{
13304 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13305 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13306 FEATURE_CONTROL_LMCE;
13307 else
13308 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13309 ~FEATURE_CONTROL_LMCE;
13310}
13311
Ladi Prosek72d7b372017-10-11 16:54:41 +020013312static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13313{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013314 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13315 if (to_vmx(vcpu)->nested.nested_run_pending)
13316 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013317 return 1;
13318}
13319
Ladi Prosek0234bf82017-10-11 16:54:40 +020013320static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13321{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013322 struct vcpu_vmx *vmx = to_vmx(vcpu);
13323
13324 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13325 if (vmx->nested.smm.guest_mode)
13326 nested_vmx_vmexit(vcpu, -1, 0, 0);
13327
13328 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13329 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013330 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013331 return 0;
13332}
13333
13334static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13335{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013336 struct vcpu_vmx *vmx = to_vmx(vcpu);
13337 int ret;
13338
13339 if (vmx->nested.smm.vmxon) {
13340 vmx->nested.vmxon = true;
13341 vmx->nested.smm.vmxon = false;
13342 }
13343
13344 if (vmx->nested.smm.guest_mode) {
13345 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013346 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013347 vcpu->arch.hflags |= HF_SMM_MASK;
13348 if (ret)
13349 return ret;
13350
13351 vmx->nested.smm.guest_mode = false;
13352 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013353 return 0;
13354}
13355
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013356static int enable_smi_window(struct kvm_vcpu *vcpu)
13357{
13358 return 0;
13359}
13360
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013361static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13362 struct kvm_nested_state __user *user_kvm_nested_state,
13363 u32 user_data_size)
13364{
13365 struct vcpu_vmx *vmx;
13366 struct vmcs12 *vmcs12;
13367 struct kvm_nested_state kvm_state = {
13368 .flags = 0,
13369 .format = 0,
13370 .size = sizeof(kvm_state),
13371 .vmx.vmxon_pa = -1ull,
13372 .vmx.vmcs_pa = -1ull,
13373 };
13374
13375 if (!vcpu)
13376 return kvm_state.size + 2 * VMCS12_SIZE;
13377
13378 vmx = to_vmx(vcpu);
13379 vmcs12 = get_vmcs12(vcpu);
13380 if (nested_vmx_allowed(vcpu) &&
13381 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13382 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13383 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13384
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013385 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013386 kvm_state.size += VMCS12_SIZE;
13387
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013388 if (is_guest_mode(vcpu) &&
13389 nested_cpu_has_shadow_vmcs(vmcs12) &&
13390 vmcs12->vmcs_link_pointer != -1ull)
13391 kvm_state.size += VMCS12_SIZE;
13392 }
13393
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013394 if (vmx->nested.smm.vmxon)
13395 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13396
13397 if (vmx->nested.smm.guest_mode)
13398 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13399
13400 if (is_guest_mode(vcpu)) {
13401 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13402
13403 if (vmx->nested.nested_run_pending)
13404 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13405 }
13406 }
13407
13408 if (user_data_size < kvm_state.size)
13409 goto out;
13410
13411 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13412 return -EFAULT;
13413
13414 if (vmx->nested.current_vmptr == -1ull)
13415 goto out;
13416
13417 /*
13418 * When running L2, the authoritative vmcs12 state is in the
13419 * vmcs02. When running L1, the authoritative vmcs12 state is
13420 * in the shadow vmcs linked to vmcs01, unless
13421 * sync_shadow_vmcs is set, in which case, the authoritative
13422 * vmcs12 state is in the vmcs12 already.
13423 */
13424 if (is_guest_mode(vcpu))
13425 sync_vmcs12(vcpu, vmcs12);
13426 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13427 copy_shadow_to_vmcs12(vmx);
13428
13429 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13430 return -EFAULT;
13431
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013432 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13433 vmcs12->vmcs_link_pointer != -1ull) {
13434 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13435 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13436 return -EFAULT;
13437 }
13438
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013439out:
13440 return kvm_state.size;
13441}
13442
13443static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13444 struct kvm_nested_state __user *user_kvm_nested_state,
13445 struct kvm_nested_state *kvm_state)
13446{
13447 struct vcpu_vmx *vmx = to_vmx(vcpu);
13448 struct vmcs12 *vmcs12;
13449 u32 exit_qual;
13450 int ret;
13451
13452 if (kvm_state->format != 0)
13453 return -EINVAL;
13454
13455 if (!nested_vmx_allowed(vcpu))
13456 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13457
13458 if (kvm_state->vmx.vmxon_pa == -1ull) {
13459 if (kvm_state->vmx.smm.flags)
13460 return -EINVAL;
13461
13462 if (kvm_state->vmx.vmcs_pa != -1ull)
13463 return -EINVAL;
13464
13465 vmx_leave_nested(vcpu);
13466 return 0;
13467 }
13468
13469 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13470 return -EINVAL;
13471
13472 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13473 return -EINVAL;
13474
13475 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13476 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13477 return -EINVAL;
13478
13479 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13480 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13481 return -EINVAL;
13482
13483 if (kvm_state->vmx.smm.flags &
13484 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13485 return -EINVAL;
13486
13487 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13488 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13489 return -EINVAL;
13490
13491 vmx_leave_nested(vcpu);
13492 if (kvm_state->vmx.vmxon_pa == -1ull)
13493 return 0;
13494
13495 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13496 ret = enter_vmx_operation(vcpu);
13497 if (ret)
13498 return ret;
13499
13500 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13501
13502 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13503 vmx->nested.smm.vmxon = true;
13504 vmx->nested.vmxon = false;
13505
13506 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13507 vmx->nested.smm.guest_mode = true;
13508 }
13509
13510 vmcs12 = get_vmcs12(vcpu);
13511 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13512 return -EFAULT;
13513
Liran Alon392b2f22018-06-23 02:35:01 +030013514 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013515 return -EINVAL;
13516
13517 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13518 return 0;
13519
13520 vmx->nested.nested_run_pending =
13521 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13522
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013523 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13524 vmcs12->vmcs_link_pointer != -1ull) {
13525 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13526 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13527 return -EINVAL;
13528
13529 if (copy_from_user(shadow_vmcs12,
13530 user_kvm_nested_state->data + VMCS12_SIZE,
13531 sizeof(*vmcs12)))
13532 return -EFAULT;
13533
13534 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13535 !shadow_vmcs12->hdr.shadow_vmcs)
13536 return -EINVAL;
13537 }
13538
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013539 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13540 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13541 return -EINVAL;
13542
13543 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13544 vmx->nested.nested_run_pending = 1;
13545
13546 vmx->nested.dirty_vmcs12 = true;
13547 ret = enter_vmx_non_root_mode(vcpu, NULL);
13548 if (ret)
13549 return -EINVAL;
13550
13551 return 0;
13552}
13553
Kees Cook404f6aa2016-08-08 16:29:06 -070013554static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013555 .cpu_has_kvm_support = cpu_has_kvm_support,
13556 .disabled_by_bios = vmx_disabled_by_bios,
13557 .hardware_setup = hardware_setup,
13558 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013559 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013560 .hardware_enable = hardware_enable,
13561 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013562 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013563 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013564
Wanpeng Lib31c1142018-03-12 04:53:04 -070013565 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013566 .vm_alloc = vmx_vm_alloc,
13567 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013568
Avi Kivity6aa8b732006-12-10 02:21:36 -080013569 .vcpu_create = vmx_create_vcpu,
13570 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013571 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013572
Avi Kivity04d2cc72007-09-10 18:10:54 +030013573 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013574 .vcpu_load = vmx_vcpu_load,
13575 .vcpu_put = vmx_vcpu_put,
13576
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013577 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013578 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013579 .get_msr = vmx_get_msr,
13580 .set_msr = vmx_set_msr,
13581 .get_segment_base = vmx_get_segment_base,
13582 .get_segment = vmx_get_segment,
13583 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013584 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013585 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013586 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013587 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013588 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013589 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013590 .set_cr3 = vmx_set_cr3,
13591 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013592 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013593 .get_idt = vmx_get_idt,
13594 .set_idt = vmx_set_idt,
13595 .get_gdt = vmx_get_gdt,
13596 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013597 .get_dr6 = vmx_get_dr6,
13598 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013599 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013600 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013601 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013602 .get_rflags = vmx_get_rflags,
13603 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013604
Avi Kivity6aa8b732006-12-10 02:21:36 -080013605 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013606
Avi Kivity6aa8b732006-12-10 02:21:36 -080013607 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013608 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013609 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013610 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13611 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013612 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013613 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013614 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013615 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013616 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013617 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013618 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013619 .get_nmi_mask = vmx_get_nmi_mask,
13620 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013621 .enable_nmi_window = enable_nmi_window,
13622 .enable_irq_window = enable_irq_window,
13623 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013624 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013625 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013626 .get_enable_apicv = vmx_get_enable_apicv,
13627 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013628 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013629 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013630 .hwapic_irr_update = vmx_hwapic_irr_update,
13631 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013632 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13633 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013634
Izik Eiduscbc94022007-10-25 00:29:55 +020013635 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013636 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013637 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013638 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013639
Avi Kivity586f9602010-11-18 13:09:54 +020013640 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013641
Sheng Yang17cc3932010-01-05 19:02:27 +080013642 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013643
13644 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013645
13646 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013647 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013648
13649 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013650
13651 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013652
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013653 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013654 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013655
13656 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013657
13658 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013659 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013660 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013661 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013662 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013663
13664 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013665
13666 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013667
13668 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13669 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13670 .flush_log_dirty = vmx_flush_log_dirty,
13671 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013672 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013673
Feng Wubf9f6ac2015-09-18 22:29:55 +080013674 .pre_block = vmx_pre_block,
13675 .post_block = vmx_post_block,
13676
Wei Huang25462f72015-06-19 15:45:05 +020013677 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013678
13679 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013680
13681#ifdef CONFIG_X86_64
13682 .set_hv_timer = vmx_set_hv_timer,
13683 .cancel_hv_timer = vmx_cancel_hv_timer,
13684#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013685
13686 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013687
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013688 .get_nested_state = vmx_get_nested_state,
13689 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013690 .get_vmcs12_pages = nested_get_vmcs12_pages,
13691
Ladi Prosek72d7b372017-10-11 16:54:41 +020013692 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013693 .pre_enter_smm = vmx_pre_enter_smm,
13694 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013695 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013696};
13697
13698static int __init vmx_init(void)
13699{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013700 int r;
13701
13702#if IS_ENABLED(CONFIG_HYPERV)
13703 /*
13704 * Enlightened VMCS usage should be recommended and the host needs
13705 * to support eVMCS v1 or above. We can also disable eVMCS support
13706 * with module parameter.
13707 */
13708 if (enlightened_vmcs &&
13709 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13710 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13711 KVM_EVMCS_VERSION) {
13712 int cpu;
13713
13714 /* Check that we have assist pages on all online CPUs */
13715 for_each_online_cpu(cpu) {
13716 if (!hv_get_vp_assist_page(cpu)) {
13717 enlightened_vmcs = false;
13718 break;
13719 }
13720 }
13721
13722 if (enlightened_vmcs) {
13723 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13724 static_branch_enable(&enable_evmcs);
13725 }
13726 } else {
13727 enlightened_vmcs = false;
13728 }
13729#endif
13730
13731 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013732 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013733 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013734 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013735
Dave Young2965faa2015-09-09 15:38:55 -070013736#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013737 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13738 crash_vmclear_local_loaded_vmcss);
13739#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013740 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013741
He, Qingfdef3ad2007-04-30 09:45:24 +030013742 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013743}
13744
13745static void __exit vmx_exit(void)
13746{
Dave Young2965faa2015-09-09 15:38:55 -070013747#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013748 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013749 synchronize_rcu();
13750#endif
13751
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013752 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013753
13754#if IS_ENABLED(CONFIG_HYPERV)
13755 if (static_branch_unlikely(&enable_evmcs)) {
13756 int cpu;
13757 struct hv_vp_assist_page *vp_ap;
13758 /*
13759 * Reset everything to support using non-enlightened VMCS
13760 * access later (e.g. when we reload the module with
13761 * enlightened_vmcs=0)
13762 */
13763 for_each_online_cpu(cpu) {
13764 vp_ap = hv_get_vp_assist_page(cpu);
13765
13766 if (!vp_ap)
13767 continue;
13768
13769 vp_ap->current_nested_vmcs = 0;
13770 vp_ap->enlighten_vmentry = 0;
13771 }
13772
13773 static_branch_disable(&enable_evmcs);
13774 }
13775#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013776}
13777
13778module_init(vmx_init)
13779module_exit(vmx_exit)