blob: cbe31b9d2b81b47053da3fe2d858d5a8fb91289b [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
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 Kiszkaf4124502014-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 Kiszkaf4124502014-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 Kivity313dbd492008-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 Mattsonc7c2c702017-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 Mattsonc7c2c702017-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 Kiszkaf4124502014-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{
3068 return cpu_has_vmx_invpcid() && enable_ept;
3069}
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 Kiszkaf4124502014-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 Kiszkaf4124502014-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'Elafa61f72013-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'Elafa61f72013-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 Kiszkaf4124502014-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 Xu8eb73e22017-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
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005032 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005033 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034}
5035
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005036static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005038 /*
5039 * Pass through host's Machine Check Enable value to hw_cr4, which
5040 * is in force while we are in guest mode. Do not let guests control
5041 * this bit, even if host CR4.MCE == 0.
5042 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005043 unsigned long hw_cr4;
5044
5045 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5046 if (enable_unrestricted_guest)
5047 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5048 else if (to_vmx(vcpu)->rmode.vm86_active)
5049 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5050 else
5051 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005052
Sean Christopherson64f7a112018-04-30 10:01:06 -07005053 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5054 if (cr4 & X86_CR4_UMIP) {
5055 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005056 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005057 hw_cr4 &= ~X86_CR4_UMIP;
5058 } else if (!is_guest_mode(vcpu) ||
5059 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5060 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5061 SECONDARY_EXEC_DESC);
5062 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005063
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005064 if (cr4 & X86_CR4_VMXE) {
5065 /*
5066 * To use VMXON (and later other VMX instructions), a guest
5067 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5068 * So basically the check on whether to allow nested VMX
5069 * is here.
5070 */
5071 if (!nested_vmx_allowed(vcpu))
5072 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005073 }
David Matlack38991522016-11-29 18:14:08 -08005074
5075 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005076 return 1;
5077
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005078 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005079
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005080 if (!enable_unrestricted_guest) {
5081 if (enable_ept) {
5082 if (!is_paging(vcpu)) {
5083 hw_cr4 &= ~X86_CR4_PAE;
5084 hw_cr4 |= X86_CR4_PSE;
5085 } else if (!(cr4 & X86_CR4_PAE)) {
5086 hw_cr4 &= ~X86_CR4_PAE;
5087 }
5088 }
5089
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005090 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005091 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5092 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5093 * to be manually disabled when guest switches to non-paging
5094 * mode.
5095 *
5096 * If !enable_unrestricted_guest, the CPU is always running
5097 * with CR0.PG=1 and CR4 needs to be modified.
5098 * If enable_unrestricted_guest, the CPU automatically
5099 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005100 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005101 if (!is_paging(vcpu))
5102 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5103 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005104
Sheng Yang14394422008-04-28 12:24:45 +08005105 vmcs_writel(CR4_READ_SHADOW, cr4);
5106 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005107 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108}
5109
Avi Kivity6aa8b732006-12-10 02:21:36 -08005110static void vmx_get_segment(struct kvm_vcpu *vcpu,
5111 struct kvm_segment *var, int seg)
5112{
Avi Kivitya9179492011-01-03 14:28:52 +02005113 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114 u32 ar;
5115
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005116 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005117 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005118 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005119 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005120 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005121 var->base = vmx_read_guest_seg_base(vmx, seg);
5122 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5123 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005124 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005125 var->base = vmx_read_guest_seg_base(vmx, seg);
5126 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5127 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5128 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005129 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130 var->type = ar & 15;
5131 var->s = (ar >> 4) & 1;
5132 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005133 /*
5134 * Some userspaces do not preserve unusable property. Since usable
5135 * segment has to be present according to VMX spec we can use present
5136 * property to amend userspace bug by making unusable segment always
5137 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5138 * segment as unusable.
5139 */
5140 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141 var->avl = (ar >> 12) & 1;
5142 var->l = (ar >> 13) & 1;
5143 var->db = (ar >> 14) & 1;
5144 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005145}
5146
Avi Kivitya9179492011-01-03 14:28:52 +02005147static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5148{
Avi Kivitya9179492011-01-03 14:28:52 +02005149 struct kvm_segment s;
5150
5151 if (to_vmx(vcpu)->rmode.vm86_active) {
5152 vmx_get_segment(vcpu, &s, seg);
5153 return s.base;
5154 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005155 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005156}
5157
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005158static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005159{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005160 struct vcpu_vmx *vmx = to_vmx(vcpu);
5161
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005162 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005163 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005164 else {
5165 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005166 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005167 }
Avi Kivity69c73022011-03-07 15:26:44 +02005168}
5169
Avi Kivity653e3102007-05-07 10:55:37 +03005170static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172 u32 ar;
5173
Avi Kivityf0495f92012-06-07 17:06:10 +03005174 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175 ar = 1 << 16;
5176 else {
5177 ar = var->type & 15;
5178 ar |= (var->s & 1) << 4;
5179 ar |= (var->dpl & 3) << 5;
5180 ar |= (var->present & 1) << 7;
5181 ar |= (var->avl & 1) << 12;
5182 ar |= (var->l & 1) << 13;
5183 ar |= (var->db & 1) << 14;
5184 ar |= (var->g & 1) << 15;
5185 }
Avi Kivity653e3102007-05-07 10:55:37 +03005186
5187 return ar;
5188}
5189
5190static void vmx_set_segment(struct kvm_vcpu *vcpu,
5191 struct kvm_segment *var, int seg)
5192{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005193 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005194 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005195
Avi Kivity2fb92db2011-04-27 19:42:18 +03005196 vmx_segment_cache_clear(vmx);
5197
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005198 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5199 vmx->rmode.segs[seg] = *var;
5200 if (seg == VCPU_SREG_TR)
5201 vmcs_write16(sf->selector, var->selector);
5202 else if (var->s)
5203 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005204 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005205 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005206
Avi Kivity653e3102007-05-07 10:55:37 +03005207 vmcs_writel(sf->base, var->base);
5208 vmcs_write32(sf->limit, var->limit);
5209 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005210
5211 /*
5212 * Fix the "Accessed" bit in AR field of segment registers for older
5213 * qemu binaries.
5214 * IA32 arch specifies that at the time of processor reset the
5215 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005216 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005217 * state vmexit when "unrestricted guest" mode is turned on.
5218 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5219 * tree. Newer qemu binaries with that qemu fix would not need this
5220 * kvm hack.
5221 */
5222 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005223 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005224
Gleb Natapovf924d662012-12-12 19:10:55 +02005225 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005226
5227out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005228 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229}
5230
Avi Kivity6aa8b732006-12-10 02:21:36 -08005231static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5232{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005233 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005234
5235 *db = (ar >> 14) & 1;
5236 *l = (ar >> 13) & 1;
5237}
5238
Gleb Natapov89a27f42010-02-16 10:51:48 +02005239static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005240{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005241 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5242 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243}
5244
Gleb Natapov89a27f42010-02-16 10:51:48 +02005245static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005246{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005247 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5248 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005249}
5250
Gleb Natapov89a27f42010-02-16 10:51:48 +02005251static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005252{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005253 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5254 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005255}
5256
Gleb Natapov89a27f42010-02-16 10:51:48 +02005257static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005258{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005259 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5260 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005261}
5262
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005263static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5264{
5265 struct kvm_segment var;
5266 u32 ar;
5267
5268 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005269 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005270 if (seg == VCPU_SREG_CS)
5271 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005272 ar = vmx_segment_access_rights(&var);
5273
5274 if (var.base != (var.selector << 4))
5275 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005276 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005277 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005278 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005279 return false;
5280
5281 return true;
5282}
5283
5284static bool code_segment_valid(struct kvm_vcpu *vcpu)
5285{
5286 struct kvm_segment cs;
5287 unsigned int cs_rpl;
5288
5289 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005290 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005291
Avi Kivity1872a3f2009-01-04 23:26:52 +02005292 if (cs.unusable)
5293 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005294 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005295 return false;
5296 if (!cs.s)
5297 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005298 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005299 if (cs.dpl > cs_rpl)
5300 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005301 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005302 if (cs.dpl != cs_rpl)
5303 return false;
5304 }
5305 if (!cs.present)
5306 return false;
5307
5308 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5309 return true;
5310}
5311
5312static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5313{
5314 struct kvm_segment ss;
5315 unsigned int ss_rpl;
5316
5317 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005318 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005319
Avi Kivity1872a3f2009-01-04 23:26:52 +02005320 if (ss.unusable)
5321 return true;
5322 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005323 return false;
5324 if (!ss.s)
5325 return false;
5326 if (ss.dpl != ss_rpl) /* DPL != RPL */
5327 return false;
5328 if (!ss.present)
5329 return false;
5330
5331 return true;
5332}
5333
5334static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5335{
5336 struct kvm_segment var;
5337 unsigned int rpl;
5338
5339 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005340 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005341
Avi Kivity1872a3f2009-01-04 23:26:52 +02005342 if (var.unusable)
5343 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005344 if (!var.s)
5345 return false;
5346 if (!var.present)
5347 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005348 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005349 if (var.dpl < rpl) /* DPL < RPL */
5350 return false;
5351 }
5352
5353 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5354 * rights flags
5355 */
5356 return true;
5357}
5358
5359static bool tr_valid(struct kvm_vcpu *vcpu)
5360{
5361 struct kvm_segment tr;
5362
5363 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5364
Avi Kivity1872a3f2009-01-04 23:26:52 +02005365 if (tr.unusable)
5366 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005367 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005368 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005369 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005370 return false;
5371 if (!tr.present)
5372 return false;
5373
5374 return true;
5375}
5376
5377static bool ldtr_valid(struct kvm_vcpu *vcpu)
5378{
5379 struct kvm_segment ldtr;
5380
5381 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5382
Avi Kivity1872a3f2009-01-04 23:26:52 +02005383 if (ldtr.unusable)
5384 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005385 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005386 return false;
5387 if (ldtr.type != 2)
5388 return false;
5389 if (!ldtr.present)
5390 return false;
5391
5392 return true;
5393}
5394
5395static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5396{
5397 struct kvm_segment cs, ss;
5398
5399 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5400 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5401
Nadav Amitb32a9912015-03-29 16:33:04 +03005402 return ((cs.selector & SEGMENT_RPL_MASK) ==
5403 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005404}
5405
5406/*
5407 * Check if guest state is valid. Returns true if valid, false if
5408 * not.
5409 * We assume that registers are always usable
5410 */
5411static bool guest_state_valid(struct kvm_vcpu *vcpu)
5412{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005413 if (enable_unrestricted_guest)
5414 return true;
5415
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005416 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005417 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005418 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5419 return false;
5420 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5421 return false;
5422 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5423 return false;
5424 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5425 return false;
5426 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5427 return false;
5428 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5429 return false;
5430 } else {
5431 /* protected mode guest state checks */
5432 if (!cs_ss_rpl_check(vcpu))
5433 return false;
5434 if (!code_segment_valid(vcpu))
5435 return false;
5436 if (!stack_segment_valid(vcpu))
5437 return false;
5438 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5439 return false;
5440 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5441 return false;
5442 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5443 return false;
5444 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5445 return false;
5446 if (!tr_valid(vcpu))
5447 return false;
5448 if (!ldtr_valid(vcpu))
5449 return false;
5450 }
5451 /* TODO:
5452 * - Add checks on RIP
5453 * - Add checks on RFLAGS
5454 */
5455
5456 return true;
5457}
5458
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005459static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5460{
5461 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5462}
5463
Mike Dayd77c26f2007-10-08 09:02:08 -04005464static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005465{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005466 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005467 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005468 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005469
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005470 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005471 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005472 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5473 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005474 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005475 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005476 r = kvm_write_guest_page(kvm, fn++, &data,
5477 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005478 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005479 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005480 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5481 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005482 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005483 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5484 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005485 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005486 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005487 r = kvm_write_guest_page(kvm, fn, &data,
5488 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5489 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005490out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005491 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005492 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493}
5494
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005495static int init_rmode_identity_map(struct kvm *kvm)
5496{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005497 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005498 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005499 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005500 u32 tmp;
5501
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005502 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005503 mutex_lock(&kvm->slots_lock);
5504
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005505 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005506 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005507
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005508 if (!kvm_vmx->ept_identity_map_addr)
5509 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5510 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005511
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005512 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005513 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005514 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005515 goto out2;
5516
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005517 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005518 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5519 if (r < 0)
5520 goto out;
5521 /* Set up identity-mapping pagetable for EPT in real mode */
5522 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5523 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5524 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5525 r = kvm_write_guest_page(kvm, identity_map_pfn,
5526 &tmp, i * sizeof(tmp), sizeof(tmp));
5527 if (r < 0)
5528 goto out;
5529 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005530 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005531
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005532out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005533 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005534
5535out2:
5536 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005537 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005538}
5539
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540static void seg_setup(int seg)
5541{
Mathias Krause772e0312012-08-30 01:30:19 +02005542 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005543 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544
5545 vmcs_write16(sf->selector, 0);
5546 vmcs_writel(sf->base, 0);
5547 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005548 ar = 0x93;
5549 if (seg == VCPU_SREG_CS)
5550 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005551
5552 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553}
5554
Sheng Yangf78e0e22007-10-29 09:40:42 +08005555static int alloc_apic_access_page(struct kvm *kvm)
5556{
Xiao Guangrong44841412012-09-07 14:14:20 +08005557 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005558 int r = 0;
5559
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005560 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005561 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005562 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005563 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5564 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005565 if (r)
5566 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005567
Tang Chen73a6d942014-09-11 13:38:00 +08005568 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005569 if (is_error_page(page)) {
5570 r = -EFAULT;
5571 goto out;
5572 }
5573
Tang Chenc24ae0d2014-09-24 15:57:58 +08005574 /*
5575 * Do not pin the page in memory, so that memory hot-unplug
5576 * is able to migrate it.
5577 */
5578 put_page(page);
5579 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005580out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005581 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005582 return r;
5583}
5584
Wanpeng Li991e7a02015-09-16 17:30:05 +08005585static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005586{
5587 int vpid;
5588
Avi Kivity919818a2009-03-23 18:01:29 +02005589 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005590 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005591 spin_lock(&vmx_vpid_lock);
5592 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005593 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005594 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005595 else
5596 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005597 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005598 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005599}
5600
Wanpeng Li991e7a02015-09-16 17:30:05 +08005601static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005602{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005603 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005604 return;
5605 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005606 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005607 spin_unlock(&vmx_vpid_lock);
5608}
5609
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005610static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5611 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005612{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005613 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005614
5615 if (!cpu_has_vmx_msr_bitmap())
5616 return;
5617
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005618 if (static_branch_unlikely(&enable_evmcs))
5619 evmcs_touch_msr_bitmap();
5620
Sheng Yang25c5f222008-03-28 13:18:56 +08005621 /*
5622 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5623 * have the write-low and read-high bitmap offsets the wrong way round.
5624 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5625 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005626 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005627 if (type & MSR_TYPE_R)
5628 /* read-low */
5629 __clear_bit(msr, msr_bitmap + 0x000 / f);
5630
5631 if (type & MSR_TYPE_W)
5632 /* write-low */
5633 __clear_bit(msr, msr_bitmap + 0x800 / f);
5634
Sheng Yang25c5f222008-03-28 13:18:56 +08005635 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5636 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005637 if (type & MSR_TYPE_R)
5638 /* read-high */
5639 __clear_bit(msr, msr_bitmap + 0x400 / f);
5640
5641 if (type & MSR_TYPE_W)
5642 /* write-high */
5643 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5644
5645 }
5646}
5647
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005648static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5649 u32 msr, int type)
5650{
5651 int f = sizeof(unsigned long);
5652
5653 if (!cpu_has_vmx_msr_bitmap())
5654 return;
5655
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005656 if (static_branch_unlikely(&enable_evmcs))
5657 evmcs_touch_msr_bitmap();
5658
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005659 /*
5660 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5661 * have the write-low and read-high bitmap offsets the wrong way round.
5662 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5663 */
5664 if (msr <= 0x1fff) {
5665 if (type & MSR_TYPE_R)
5666 /* read-low */
5667 __set_bit(msr, msr_bitmap + 0x000 / f);
5668
5669 if (type & MSR_TYPE_W)
5670 /* write-low */
5671 __set_bit(msr, msr_bitmap + 0x800 / f);
5672
5673 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5674 msr &= 0x1fff;
5675 if (type & MSR_TYPE_R)
5676 /* read-high */
5677 __set_bit(msr, msr_bitmap + 0x400 / f);
5678
5679 if (type & MSR_TYPE_W)
5680 /* write-high */
5681 __set_bit(msr, msr_bitmap + 0xc00 / f);
5682
5683 }
5684}
5685
5686static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5687 u32 msr, int type, bool value)
5688{
5689 if (value)
5690 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5691 else
5692 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5693}
5694
Wincy Vanf2b93282015-02-03 23:56:03 +08005695/*
5696 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5697 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5698 */
5699static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5700 unsigned long *msr_bitmap_nested,
5701 u32 msr, int type)
5702{
5703 int f = sizeof(unsigned long);
5704
Wincy Vanf2b93282015-02-03 23:56:03 +08005705 /*
5706 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5707 * have the write-low and read-high bitmap offsets the wrong way round.
5708 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5709 */
5710 if (msr <= 0x1fff) {
5711 if (type & MSR_TYPE_R &&
5712 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5713 /* read-low */
5714 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5715
5716 if (type & MSR_TYPE_W &&
5717 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5718 /* write-low */
5719 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5720
5721 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5722 msr &= 0x1fff;
5723 if (type & MSR_TYPE_R &&
5724 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5725 /* read-high */
5726 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5727
5728 if (type & MSR_TYPE_W &&
5729 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5730 /* write-high */
5731 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5732
5733 }
5734}
5735
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005736static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005737{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005738 u8 mode = 0;
5739
5740 if (cpu_has_secondary_exec_ctrls() &&
5741 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5742 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5743 mode |= MSR_BITMAP_MODE_X2APIC;
5744 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5745 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5746 }
5747
5748 if (is_long_mode(vcpu))
5749 mode |= MSR_BITMAP_MODE_LM;
5750
5751 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005752}
5753
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005754#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5755
5756static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5757 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005758{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005759 int msr;
5760
5761 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5762 unsigned word = msr / BITS_PER_LONG;
5763 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5764 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005765 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005766
5767 if (mode & MSR_BITMAP_MODE_X2APIC) {
5768 /*
5769 * TPR reads and writes can be virtualized even if virtual interrupt
5770 * delivery is not in use.
5771 */
5772 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5773 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5774 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5775 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5776 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5777 }
5778 }
5779}
5780
5781static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5782{
5783 struct vcpu_vmx *vmx = to_vmx(vcpu);
5784 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5785 u8 mode = vmx_msr_bitmap_mode(vcpu);
5786 u8 changed = mode ^ vmx->msr_bitmap_mode;
5787
5788 if (!changed)
5789 return;
5790
5791 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5792 !(mode & MSR_BITMAP_MODE_LM));
5793
5794 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5795 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5796
5797 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005798}
5799
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005800static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005801{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005802 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005803}
5804
David Matlackc9f04402017-08-01 14:00:40 -07005805static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5806{
5807 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5808 gfn_t gfn;
5809
5810 /*
5811 * Don't need to mark the APIC access page dirty; it is never
5812 * written to by the CPU during APIC virtualization.
5813 */
5814
5815 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5816 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5817 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5818 }
5819
5820 if (nested_cpu_has_posted_intr(vmcs12)) {
5821 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5822 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5823 }
5824}
5825
5826
David Hildenbrand6342c502017-01-25 11:58:58 +01005827static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005828{
5829 struct vcpu_vmx *vmx = to_vmx(vcpu);
5830 int max_irr;
5831 void *vapic_page;
5832 u16 status;
5833
David Matlackc9f04402017-08-01 14:00:40 -07005834 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5835 return;
Wincy Van705699a2015-02-03 23:58:17 +08005836
David Matlackc9f04402017-08-01 14:00:40 -07005837 vmx->nested.pi_pending = false;
5838 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5839 return;
Wincy Van705699a2015-02-03 23:58:17 +08005840
David Matlackc9f04402017-08-01 14:00:40 -07005841 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5842 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005843 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005844 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5845 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005846 kunmap(vmx->nested.virtual_apic_page);
5847
5848 status = vmcs_read16(GUEST_INTR_STATUS);
5849 if ((u8)max_irr > ((u8)status & 0xff)) {
5850 status &= ~0xff;
5851 status |= (u8)max_irr;
5852 vmcs_write16(GUEST_INTR_STATUS, status);
5853 }
5854 }
David Matlackc9f04402017-08-01 14:00:40 -07005855
5856 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005857}
5858
Wincy Van06a55242017-04-28 13:13:59 +08005859static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5860 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005861{
5862#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005863 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5864
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005865 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005866 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005867 * The vector of interrupt to be delivered to vcpu had
5868 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005869 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005870 * Following cases will be reached in this block, and
5871 * we always send a notification event in all cases as
5872 * explained below.
5873 *
5874 * Case 1: vcpu keeps in non-root mode. Sending a
5875 * notification event posts the interrupt to vcpu.
5876 *
5877 * Case 2: vcpu exits to root mode and is still
5878 * runnable. PIR will be synced to vIRR before the
5879 * next vcpu entry. Sending a notification event in
5880 * this case has no effect, as vcpu is not in root
5881 * mode.
5882 *
5883 * Case 3: vcpu exits to root mode and is blocked.
5884 * vcpu_block() has already synced PIR to vIRR and
5885 * never blocks vcpu if vIRR is not cleared. Therefore,
5886 * a blocked vcpu here does not wait for any requested
5887 * interrupts in PIR, and sending a notification event
5888 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005889 */
Feng Wu28b835d2015-09-18 22:29:54 +08005890
Wincy Van06a55242017-04-28 13:13:59 +08005891 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005892 return true;
5893 }
5894#endif
5895 return false;
5896}
5897
Wincy Van705699a2015-02-03 23:58:17 +08005898static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5899 int vector)
5900{
5901 struct vcpu_vmx *vmx = to_vmx(vcpu);
5902
5903 if (is_guest_mode(vcpu) &&
5904 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005905 /*
5906 * If a posted intr is not recognized by hardware,
5907 * we will accomplish it in the next vmentry.
5908 */
5909 vmx->nested.pi_pending = true;
5910 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005911 /* the PIR and ON have been set by L1. */
5912 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5913 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005914 return 0;
5915 }
5916 return -1;
5917}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005918/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005919 * Send interrupt to vcpu via posted interrupt way.
5920 * 1. If target vcpu is running(non-root mode), send posted interrupt
5921 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5922 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5923 * interrupt from PIR in next vmentry.
5924 */
5925static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5926{
5927 struct vcpu_vmx *vmx = to_vmx(vcpu);
5928 int r;
5929
Wincy Van705699a2015-02-03 23:58:17 +08005930 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5931 if (!r)
5932 return;
5933
Yang Zhanga20ed542013-04-11 19:25:15 +08005934 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5935 return;
5936
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005937 /* If a previous notification has sent the IPI, nothing to do. */
5938 if (pi_test_and_set_on(&vmx->pi_desc))
5939 return;
5940
Wincy Van06a55242017-04-28 13:13:59 +08005941 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005942 kvm_vcpu_kick(vcpu);
5943}
5944
Avi Kivity6aa8b732006-12-10 02:21:36 -08005945/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005946 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5947 * will not change in the lifetime of the guest.
5948 * Note that host-state that does change is set elsewhere. E.g., host-state
5949 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5950 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005951static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005952{
5953 u32 low32, high32;
5954 unsigned long tmpl;
5955 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005956 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005957
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005958 cr0 = read_cr0();
5959 WARN_ON(cr0 & X86_CR0_TS);
5960 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005961
5962 /*
5963 * Save the most likely value for this task's CR3 in the VMCS.
5964 * We can't use __get_current_cr3_fast() because we're not atomic.
5965 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005966 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005967 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005968 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005969
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005970 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005971 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005972 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005973 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005974
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005975 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005976#ifdef CONFIG_X86_64
5977 /*
5978 * Load null selectors, so we can avoid reloading them in
5979 * __vmx_load_host_state(), in case userspace uses the null selectors
5980 * too (the expected case).
5981 */
5982 vmcs_write16(HOST_DS_SELECTOR, 0);
5983 vmcs_write16(HOST_ES_SELECTOR, 0);
5984#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005985 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5986 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005987#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005988 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5989 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5990
Juergen Gross87930012017-09-04 12:25:27 +02005991 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005992 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005993 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005994
Avi Kivity83287ea422012-09-16 15:10:57 +03005995 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005996
5997 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5998 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5999 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6000 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6001
6002 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6003 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6004 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6005 }
6006}
6007
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006008static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6009{
6010 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6011 if (enable_ept)
6012 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006013 if (is_guest_mode(&vmx->vcpu))
6014 vmx->vcpu.arch.cr4_guest_owned_bits &=
6015 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006016 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6017}
6018
Yang Zhang01e439b2013-04-11 19:25:12 +08006019static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6020{
6021 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6022
Andrey Smetanind62caab2015-11-10 15:36:33 +03006023 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006024 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006025
6026 if (!enable_vnmi)
6027 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6028
Yunhong Jiang64672c92016-06-13 14:19:59 -07006029 /* Enable the preemption timer dynamically */
6030 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006031 return pin_based_exec_ctrl;
6032}
6033
Andrey Smetanind62caab2015-11-10 15:36:33 +03006034static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6035{
6036 struct vcpu_vmx *vmx = to_vmx(vcpu);
6037
6038 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006039 if (cpu_has_secondary_exec_ctrls()) {
6040 if (kvm_vcpu_apicv_active(vcpu))
6041 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6042 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6043 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6044 else
6045 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6046 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6047 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6048 }
6049
6050 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006051 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006052}
6053
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006054static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6055{
6056 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006057
6058 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6059 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6060
Paolo Bonzini35754c92015-07-29 12:05:37 +02006061 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006062 exec_control &= ~CPU_BASED_TPR_SHADOW;
6063#ifdef CONFIG_X86_64
6064 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6065 CPU_BASED_CR8_LOAD_EXITING;
6066#endif
6067 }
6068 if (!enable_ept)
6069 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6070 CPU_BASED_CR3_LOAD_EXITING |
6071 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006072 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6073 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6074 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006075 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6076 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006077 return exec_control;
6078}
6079
Jim Mattson45ec3682017-08-23 16:32:04 -07006080static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006081{
Jim Mattson45ec3682017-08-23 16:32:04 -07006082 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006083 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006084}
6085
Jim Mattson75f4fc82017-08-23 16:32:03 -07006086static bool vmx_rdseed_supported(void)
6087{
6088 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006089 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006090}
6091
Paolo Bonzini80154d72017-08-24 13:55:35 +02006092static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006093{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006094 struct kvm_vcpu *vcpu = &vmx->vcpu;
6095
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006096 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006097
Paolo Bonzini80154d72017-08-24 13:55:35 +02006098 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006099 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6100 if (vmx->vpid == 0)
6101 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6102 if (!enable_ept) {
6103 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6104 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006105 /* Enable INVPCID for non-ept guests may cause performance regression. */
6106 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107 }
6108 if (!enable_unrestricted_guest)
6109 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006110 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006111 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006112 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006113 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6114 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006115 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006116
6117 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6118 * in vmx_set_cr4. */
6119 exec_control &= ~SECONDARY_EXEC_DESC;
6120
Abel Gordonabc4fc52013-04-18 14:35:25 +03006121 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6122 (handle_vmptrld).
6123 We can NOT enable shadow_vmcs here because we don't have yet
6124 a current VMCS12
6125 */
6126 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006127
6128 if (!enable_pml)
6129 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006130
Paolo Bonzini3db13482017-08-24 14:48:03 +02006131 if (vmx_xsaves_supported()) {
6132 /* Exposing XSAVES only when XSAVE is exposed */
6133 bool xsaves_enabled =
6134 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6135 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6136
6137 if (!xsaves_enabled)
6138 exec_control &= ~SECONDARY_EXEC_XSAVES;
6139
6140 if (nested) {
6141 if (xsaves_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006145 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006146 ~SECONDARY_EXEC_XSAVES;
6147 }
6148 }
6149
Paolo Bonzini80154d72017-08-24 13:55:35 +02006150 if (vmx_rdtscp_supported()) {
6151 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6152 if (!rdtscp_enabled)
6153 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6154
6155 if (nested) {
6156 if (rdtscp_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006160 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006161 ~SECONDARY_EXEC_RDTSCP;
6162 }
6163 }
6164
6165 if (vmx_invpcid_supported()) {
6166 /* Exposing INVPCID only when PCID is exposed */
6167 bool invpcid_enabled =
6168 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6169 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6170
6171 if (!invpcid_enabled) {
6172 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6173 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6174 }
6175
6176 if (nested) {
6177 if (invpcid_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006181 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006182 ~SECONDARY_EXEC_ENABLE_INVPCID;
6183 }
6184 }
6185
Jim Mattson45ec3682017-08-23 16:32:04 -07006186 if (vmx_rdrand_supported()) {
6187 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6188 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006189 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006190
6191 if (nested) {
6192 if (rdrand_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006196 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006197 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006198 }
6199 }
6200
Jim Mattson75f4fc82017-08-23 16:32:03 -07006201 if (vmx_rdseed_supported()) {
6202 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6203 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006204 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006205
6206 if (nested) {
6207 if (rdseed_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006211 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006212 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006213 }
6214 }
6215
Paolo Bonzini80154d72017-08-24 13:55:35 +02006216 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006217}
6218
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006219static void ept_set_mmio_spte_mask(void)
6220{
6221 /*
6222 * EPT Misconfigurations can be generated if the value of bits 2:0
6223 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006224 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006225 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6226 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006227}
6228
Wanpeng Lif53cd632014-12-02 19:14:58 +08006229#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006230/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231 * Sets up the vmcs for emulated real mode.
6232 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006233static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006234{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006235#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006237#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006238 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239
Abel Gordon4607c2d2013-04-18 14:35:55 +03006240 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006241 /*
6242 * At vCPU creation, "VMWRITE to any supported field
6243 * in the VMCS" is supported, so use the more
6244 * permissive vmx_vmread_bitmap to specify both read
6245 * and write permissions for the shadow VMCS.
6246 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006247 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006248 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006249 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006250 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006251 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006252
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6254
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006256 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006257 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006258
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006259 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006260
Dan Williamsdfa169b2016-06-02 11:17:24 -07006261 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006262 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006263 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006264 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006265 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006266
Andrey Smetanind62caab2015-11-10 15:36:33 +03006267 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006268 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6269 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6270 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6271 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6272
6273 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006274
Li RongQing0bcf2612015-12-03 13:29:34 +08006275 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006276 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006277 }
6278
Wanpeng Lib31c1142018-03-12 04:53:04 -07006279 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006280 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006281 vmx->ple_window = ple_window;
6282 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006283 }
6284
Xiao Guangrongc3707952011-07-12 03:28:04 +08006285 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6286 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006287 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6288
Avi Kivity9581d442010-10-19 16:46:55 +02006289 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6290 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006291 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006292#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293 rdmsrl(MSR_FS_BASE, a);
6294 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6295 rdmsrl(MSR_GS_BASE, a);
6296 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6297#else
6298 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6299 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6300#endif
6301
Bandan Das2a499e42017-08-03 15:54:41 -04006302 if (cpu_has_vmx_vmfunc())
6303 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6304
Eddie Dong2cc51562007-05-21 07:28:09 +03006305 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6306 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006307 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006308 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006309 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310
Radim Krčmář74545702015-04-27 15:11:25 +02006311 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6312 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006313
Paolo Bonzini03916db2014-07-24 14:21:57 +02006314 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006315 u32 index = vmx_msr_index[i];
6316 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006317 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006318
6319 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6320 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006321 if (wrmsr_safe(index, data_low, data_high) < 0)
6322 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006323 vmx->guest_msrs[j].index = i;
6324 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006325 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006326 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006327 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006329 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6330 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006331
6332 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006333
6334 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006335 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006336
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006337 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6338 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6339
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006340 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006341
Wanpeng Lif53cd632014-12-02 19:14:58 +08006342 if (vmx_xsaves_supported())
6343 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6344
Peter Feiner4e595162016-07-07 14:49:58 -07006345 if (enable_pml) {
6346 ASSERT(vmx->pml_pg);
6347 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6348 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6349 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006350}
6351
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006352static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006353{
6354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006355 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006356 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006357
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006358 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006359 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006360
Wanpeng Li518e7b92018-02-28 14:03:31 +08006361 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006362 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006363 kvm_set_cr8(vcpu, 0);
6364
6365 if (!init_event) {
6366 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6367 MSR_IA32_APICBASE_ENABLE;
6368 if (kvm_vcpu_is_reset_bsp(vcpu))
6369 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6370 apic_base_msr.host_initiated = true;
6371 kvm_set_apic_base(vcpu, &apic_base_msr);
6372 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006373
Avi Kivity2fb92db2011-04-27 19:42:18 +03006374 vmx_segment_cache_clear(vmx);
6375
Avi Kivity5706be02008-08-20 15:07:31 +03006376 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006377 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006378 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006379
6380 seg_setup(VCPU_SREG_DS);
6381 seg_setup(VCPU_SREG_ES);
6382 seg_setup(VCPU_SREG_FS);
6383 seg_setup(VCPU_SREG_GS);
6384 seg_setup(VCPU_SREG_SS);
6385
6386 vmcs_write16(GUEST_TR_SELECTOR, 0);
6387 vmcs_writel(GUEST_TR_BASE, 0);
6388 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6389 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6390
6391 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6392 vmcs_writel(GUEST_LDTR_BASE, 0);
6393 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6394 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6395
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006396 if (!init_event) {
6397 vmcs_write32(GUEST_SYSENTER_CS, 0);
6398 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6399 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6400 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6401 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006402
Wanpeng Lic37c2872017-11-20 14:52:21 -08006403 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006404 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006405
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006406 vmcs_writel(GUEST_GDTR_BASE, 0);
6407 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6408
6409 vmcs_writel(GUEST_IDTR_BASE, 0);
6410 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6411
Anthony Liguori443381a2010-12-06 10:53:38 -06006412 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006413 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006414 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006415 if (kvm_mpx_supported())
6416 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006417
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006418 setup_msrs(vmx);
6419
Avi Kivity6aa8b732006-12-10 02:21:36 -08006420 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6421
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006422 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006423 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006424 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006425 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006426 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006427 vmcs_write32(TPR_THRESHOLD, 0);
6428 }
6429
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006430 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006431
Sheng Yang2384d2b2008-01-17 15:14:33 +08006432 if (vmx->vpid != 0)
6433 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6434
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006435 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006436 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006437 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006438 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006439 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006440
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006441 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006442
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006443 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006444 if (init_event)
6445 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006446}
6447
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006448/*
6449 * In nested virtualization, check if L1 asked to exit on external interrupts.
6450 * For most existing hypervisors, this will always return true.
6451 */
6452static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6453{
6454 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6455 PIN_BASED_EXT_INTR_MASK;
6456}
6457
Bandan Das77b0f5d2014-04-19 18:17:45 -04006458/*
6459 * In nested virtualization, check if L1 has set
6460 * VM_EXIT_ACK_INTR_ON_EXIT
6461 */
6462static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6463{
6464 return get_vmcs12(vcpu)->vm_exit_controls &
6465 VM_EXIT_ACK_INTR_ON_EXIT;
6466}
6467
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006468static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6469{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006470 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006471}
6472
Jan Kiszkac9a79532014-03-07 20:03:15 +01006473static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006474{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006475 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6476 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006477}
6478
Jan Kiszkac9a79532014-03-07 20:03:15 +01006479static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006480{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006481 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006482 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006483 enable_irq_window(vcpu);
6484 return;
6485 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006486
Paolo Bonzini47c01522016-12-19 11:44:07 +01006487 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6488 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006489}
6490
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006491static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006492{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006494 uint32_t intr;
6495 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006496
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006497 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006498
Avi Kivityfa89a812008-09-01 15:57:51 +03006499 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006500 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006501 int inc_eip = 0;
6502 if (vcpu->arch.interrupt.soft)
6503 inc_eip = vcpu->arch.event_exit_inst_len;
6504 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006505 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006506 return;
6507 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006508 intr = irq | INTR_INFO_VALID_MASK;
6509 if (vcpu->arch.interrupt.soft) {
6510 intr |= INTR_TYPE_SOFT_INTR;
6511 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6512 vmx->vcpu.arch.event_exit_inst_len);
6513 } else
6514 intr |= INTR_TYPE_EXT_INTR;
6515 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006516
6517 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006518}
6519
Sheng Yangf08864b2008-05-15 18:23:25 +08006520static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6521{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006522 struct vcpu_vmx *vmx = to_vmx(vcpu);
6523
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006524 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006525 /*
6526 * Tracking the NMI-blocked state in software is built upon
6527 * finding the next open IRQ window. This, in turn, depends on
6528 * well-behaving guests: They have to keep IRQs disabled at
6529 * least as long as the NMI handler runs. Otherwise we may
6530 * cause NMI nesting, maybe breaking the guest. But as this is
6531 * highly unlikely, we can live with the residual risk.
6532 */
6533 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6534 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6535 }
6536
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006537 ++vcpu->stat.nmi_injections;
6538 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006539
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006540 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006541 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006542 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006543 return;
6544 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006545
Sheng Yangf08864b2008-05-15 18:23:25 +08006546 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6547 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006548
6549 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006550}
6551
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006552static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6553{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006554 struct vcpu_vmx *vmx = to_vmx(vcpu);
6555 bool masked;
6556
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006557 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006558 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006559 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006560 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006561 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6562 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6563 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006564}
6565
6566static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6567{
6568 struct vcpu_vmx *vmx = to_vmx(vcpu);
6569
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006570 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006571 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6572 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6573 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6574 }
6575 } else {
6576 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6577 if (masked)
6578 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6579 GUEST_INTR_STATE_NMI);
6580 else
6581 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6582 GUEST_INTR_STATE_NMI);
6583 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006584}
6585
Jan Kiszka2505dc92013-04-14 12:12:47 +02006586static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6587{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006588 if (to_vmx(vcpu)->nested.nested_run_pending)
6589 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006590
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006591 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006592 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6593 return 0;
6594
Jan Kiszka2505dc92013-04-14 12:12:47 +02006595 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6596 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6597 | GUEST_INTR_STATE_NMI));
6598}
6599
Gleb Natapov78646122009-03-23 12:12:11 +02006600static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6601{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006602 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6603 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006604 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6605 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006606}
6607
Izik Eiduscbc94022007-10-25 00:29:55 +02006608static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6609{
6610 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006611
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006612 if (enable_unrestricted_guest)
6613 return 0;
6614
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006615 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6616 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006617 if (ret)
6618 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006619 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006620 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006621}
6622
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006623static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6624{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006625 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006626 return 0;
6627}
6628
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006629static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006630{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006631 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006632 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006633 /*
6634 * Update instruction length as we may reinject the exception
6635 * from user space while in guest debugging mode.
6636 */
6637 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6638 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006639 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006640 return false;
6641 /* fall through */
6642 case DB_VECTOR:
6643 if (vcpu->guest_debug &
6644 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6645 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006646 /* fall through */
6647 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006648 case OF_VECTOR:
6649 case BR_VECTOR:
6650 case UD_VECTOR:
6651 case DF_VECTOR:
6652 case SS_VECTOR:
6653 case GP_VECTOR:
6654 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006655 return true;
6656 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006657 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006658 return false;
6659}
6660
6661static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6662 int vec, u32 err_code)
6663{
6664 /*
6665 * Instruction with address size override prefix opcode 0x67
6666 * Cause the #SS fault with 0 error code in VM86 mode.
6667 */
6668 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6669 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6670 if (vcpu->arch.halt_request) {
6671 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006672 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006673 }
6674 return 1;
6675 }
6676 return 0;
6677 }
6678
6679 /*
6680 * Forward all other exceptions that are valid in real mode.
6681 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6682 * the required debugging infrastructure rework.
6683 */
6684 kvm_queue_exception(vcpu, vec);
6685 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006686}
6687
Andi Kleena0861c02009-06-08 17:37:09 +08006688/*
6689 * Trigger machine check on the host. We assume all the MSRs are already set up
6690 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6691 * We pass a fake environment to the machine check handler because we want
6692 * the guest to be always treated like user space, no matter what context
6693 * it used internally.
6694 */
6695static void kvm_machine_check(void)
6696{
6697#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6698 struct pt_regs regs = {
6699 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6700 .flags = X86_EFLAGS_IF,
6701 };
6702
6703 do_machine_check(&regs, 0);
6704#endif
6705}
6706
Avi Kivity851ba692009-08-24 11:10:17 +03006707static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006708{
6709 /* already handled by vcpu_run */
6710 return 1;
6711}
6712
Avi Kivity851ba692009-08-24 11:10:17 +03006713static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006714{
Avi Kivity1155f762007-11-22 11:30:47 +02006715 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006716 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006717 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006718 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006719 u32 vect_info;
6720 enum emulation_result er;
6721
Avi Kivity1155f762007-11-22 11:30:47 +02006722 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006723 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006724
Andi Kleena0861c02009-06-08 17:37:09 +08006725 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006726 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006727
Jim Mattsonef85b672016-12-12 11:01:37 -08006728 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006729 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006730
Wanpeng Li082d06e2018-04-03 16:28:48 -07006731 if (is_invalid_opcode(intr_info))
6732 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006733
Avi Kivity6aa8b732006-12-10 02:21:36 -08006734 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006735 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006736 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006737
Liran Alon9e869482018-03-12 13:12:51 +02006738 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6739 WARN_ON_ONCE(!enable_vmware_backdoor);
6740 er = emulate_instruction(vcpu,
6741 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6742 if (er == EMULATE_USER_EXIT)
6743 return 0;
6744 else if (er != EMULATE_DONE)
6745 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6746 return 1;
6747 }
6748
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006749 /*
6750 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6751 * MMIO, it is better to report an internal error.
6752 * See the comments in vmx_handle_exit.
6753 */
6754 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6755 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6756 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6757 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006758 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006759 vcpu->run->internal.data[0] = vect_info;
6760 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006761 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006762 return 0;
6763 }
6764
Avi Kivity6aa8b732006-12-10 02:21:36 -08006765 if (is_page_fault(intr_info)) {
6766 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006767 /* EPT won't cause page fault directly */
6768 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006769 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006770 }
6771
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006772 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006773
6774 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6775 return handle_rmode_exception(vcpu, ex_no, error_code);
6776
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006777 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006778 case AC_VECTOR:
6779 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6780 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006781 case DB_VECTOR:
6782 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6783 if (!(vcpu->guest_debug &
6784 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006785 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006786 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006787 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006788 skip_emulated_instruction(vcpu);
6789
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006790 kvm_queue_exception(vcpu, DB_VECTOR);
6791 return 1;
6792 }
6793 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6794 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6795 /* fall through */
6796 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006797 /*
6798 * Update instruction length as we may reinject #BP from
6799 * user space while in guest debugging mode. Reading it for
6800 * #DB as well causes no harm, it is not used in that case.
6801 */
6802 vmx->vcpu.arch.event_exit_inst_len =
6803 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006804 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006805 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006806 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6807 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006808 break;
6809 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006810 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6811 kvm_run->ex.exception = ex_no;
6812 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006813 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006814 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006815 return 0;
6816}
6817
Avi Kivity851ba692009-08-24 11:10:17 +03006818static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006819{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006820 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006821 return 1;
6822}
6823
Avi Kivity851ba692009-08-24 11:10:17 +03006824static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006825{
Avi Kivity851ba692009-08-24 11:10:17 +03006826 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006827 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006828 return 0;
6829}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006830
Avi Kivity851ba692009-08-24 11:10:17 +03006831static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006832{
He, Qingbfdaab02007-09-12 14:18:28 +08006833 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006834 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006835 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006836
He, Qingbfdaab02007-09-12 14:18:28 +08006837 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006838 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006839
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006840 ++vcpu->stat.io_exits;
6841
Sean Christopherson432baf62018-03-08 08:57:26 -08006842 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006843 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006844
6845 port = exit_qualification >> 16;
6846 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006847 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006848
Sean Christophersondca7f122018-03-08 08:57:27 -08006849 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006850}
6851
Ingo Molnar102d8322007-02-19 14:37:47 +02006852static void
6853vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6854{
6855 /*
6856 * Patch in the VMCALL instruction:
6857 */
6858 hypercall[0] = 0x0f;
6859 hypercall[1] = 0x01;
6860 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006861}
6862
Guo Chao0fa06072012-06-28 15:16:19 +08006863/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006864static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6865{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006866 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006867 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6868 unsigned long orig_val = val;
6869
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006870 /*
6871 * We get here when L2 changed cr0 in a way that did not change
6872 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006873 * but did change L0 shadowed bits. So we first calculate the
6874 * effective cr0 value that L1 would like to write into the
6875 * hardware. It consists of the L2-owned bits from the new
6876 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006877 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006878 val = (val & ~vmcs12->cr0_guest_host_mask) |
6879 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6880
David Matlack38991522016-11-29 18:14:08 -08006881 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006882 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006883
6884 if (kvm_set_cr0(vcpu, val))
6885 return 1;
6886 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006887 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006888 } else {
6889 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006890 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006891 return 1;
David Matlack38991522016-11-29 18:14:08 -08006892
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006893 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006894 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006895}
6896
6897static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6898{
6899 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006900 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6901 unsigned long orig_val = val;
6902
6903 /* analogously to handle_set_cr0 */
6904 val = (val & ~vmcs12->cr4_guest_host_mask) |
6905 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6906 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006907 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006908 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006909 return 0;
6910 } else
6911 return kvm_set_cr4(vcpu, val);
6912}
6913
Paolo Bonzini0367f202016-07-12 10:44:55 +02006914static int handle_desc(struct kvm_vcpu *vcpu)
6915{
6916 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6917 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6918}
6919
Avi Kivity851ba692009-08-24 11:10:17 +03006920static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006921{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006922 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006923 int cr;
6924 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006925 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006926 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006927
He, Qingbfdaab02007-09-12 14:18:28 +08006928 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929 cr = exit_qualification & 15;
6930 reg = (exit_qualification >> 8) & 15;
6931 switch ((exit_qualification >> 4) & 3) {
6932 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006933 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006934 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935 switch (cr) {
6936 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006937 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006938 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006939 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006940 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006941 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006942 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006943 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006944 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006945 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006946 case 8: {
6947 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006948 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006949 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006951 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006952 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006953 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006954 return ret;
6955 /*
6956 * TODO: we might be squashing a
6957 * KVM_GUESTDBG_SINGLESTEP-triggered
6958 * KVM_EXIT_DEBUG here.
6959 */
Avi Kivity851ba692009-08-24 11:10:17 +03006960 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006961 return 0;
6962 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006963 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006964 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006965 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006966 WARN_ONCE(1, "Guest should always own CR0.TS");
6967 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006968 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006969 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970 case 1: /*mov from cr*/
6971 switch (cr) {
6972 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006973 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006974 val = kvm_read_cr3(vcpu);
6975 kvm_register_write(vcpu, reg, val);
6976 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006977 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006978 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006979 val = kvm_get_cr8(vcpu);
6980 kvm_register_write(vcpu, reg, val);
6981 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006982 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006983 }
6984 break;
6985 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006986 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006987 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006988 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006989
Kyle Huey6affcbe2016-11-29 12:40:40 -08006990 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006991 default:
6992 break;
6993 }
Avi Kivity851ba692009-08-24 11:10:17 +03006994 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006995 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006996 (int)(exit_qualification >> 4) & 3, cr);
6997 return 0;
6998}
6999
Avi Kivity851ba692009-08-24 11:10:17 +03007000static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007001{
He, Qingbfdaab02007-09-12 14:18:28 +08007002 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007003 int dr, dr7, reg;
7004
7005 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7006 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7007
7008 /* First, if DR does not exist, trigger UD */
7009 if (!kvm_require_dr(vcpu, dr))
7010 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007011
Jan Kiszkaf2483412010-01-20 18:20:20 +01007012 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007013 if (!kvm_require_cpl(vcpu, 0))
7014 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007015 dr7 = vmcs_readl(GUEST_DR7);
7016 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007017 /*
7018 * As the vm-exit takes precedence over the debug trap, we
7019 * need to emulate the latter, either for the host or the
7020 * guest debugging itself.
7021 */
7022 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007023 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007024 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007025 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007026 vcpu->run->debug.arch.exception = DB_VECTOR;
7027 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007028 return 0;
7029 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007030 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007031 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007032 kvm_queue_exception(vcpu, DB_VECTOR);
7033 return 1;
7034 }
7035 }
7036
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007037 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007038 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7039 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007040
7041 /*
7042 * No more DR vmexits; force a reload of the debug registers
7043 * and reenter on this instruction. The next vmexit will
7044 * retrieve the full state of the debug registers.
7045 */
7046 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7047 return 1;
7048 }
7049
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007050 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7051 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007052 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007053
7054 if (kvm_get_dr(vcpu, dr, &val))
7055 return 1;
7056 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007057 } else
Nadav Amit57773922014-06-18 17:19:23 +03007058 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007059 return 1;
7060
Kyle Huey6affcbe2016-11-29 12:40:40 -08007061 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062}
7063
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007064static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7065{
7066 return vcpu->arch.dr6;
7067}
7068
7069static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7070{
7071}
7072
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007073static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7074{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007075 get_debugreg(vcpu->arch.db[0], 0);
7076 get_debugreg(vcpu->arch.db[1], 1);
7077 get_debugreg(vcpu->arch.db[2], 2);
7078 get_debugreg(vcpu->arch.db[3], 3);
7079 get_debugreg(vcpu->arch.dr6, 6);
7080 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7081
7082 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007083 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007084}
7085
Gleb Natapov020df072010-04-13 10:05:23 +03007086static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7087{
7088 vmcs_writel(GUEST_DR7, val);
7089}
7090
Avi Kivity851ba692009-08-24 11:10:17 +03007091static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092{
Kyle Huey6a908b62016-11-29 12:40:37 -08007093 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007094}
7095
Avi Kivity851ba692009-08-24 11:10:17 +03007096static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007098 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007099 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007100
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007101 msr_info.index = ecx;
7102 msr_info.host_initiated = false;
7103 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007104 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007105 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007106 return 1;
7107 }
7108
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007109 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007110
Avi Kivity6aa8b732006-12-10 02:21:36 -08007111 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007112 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7113 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007114 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115}
7116
Avi Kivity851ba692009-08-24 11:10:17 +03007117static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007118{
Will Auld8fe8ab42012-11-29 12:42:12 -08007119 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007120 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7121 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7122 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007123
Will Auld8fe8ab42012-11-29 12:42:12 -08007124 msr.data = data;
7125 msr.index = ecx;
7126 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007127 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007128 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007129 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130 return 1;
7131 }
7132
Avi Kivity59200272010-01-25 19:47:02 +02007133 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007134 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135}
7136
Avi Kivity851ba692009-08-24 11:10:17 +03007137static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007138{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007139 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007140 return 1;
7141}
7142
Avi Kivity851ba692009-08-24 11:10:17 +03007143static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007145 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7146 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007147
Avi Kivity3842d132010-07-27 12:30:24 +03007148 kvm_make_request(KVM_REQ_EVENT, vcpu);
7149
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007150 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007151 return 1;
7152}
7153
Avi Kivity851ba692009-08-24 11:10:17 +03007154static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155{
Avi Kivityd3bef152007-06-05 15:53:05 +03007156 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007157}
7158
Avi Kivity851ba692009-08-24 11:10:17 +03007159static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007160{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007161 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007162}
7163
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007164static int handle_invd(struct kvm_vcpu *vcpu)
7165{
Andre Przywara51d8b662010-12-21 11:12:02 +01007166 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007167}
7168
Avi Kivity851ba692009-08-24 11:10:17 +03007169static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007170{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007171 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007172
7173 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007174 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007175}
7176
Avi Kivityfee84b02011-11-10 14:57:25 +02007177static int handle_rdpmc(struct kvm_vcpu *vcpu)
7178{
7179 int err;
7180
7181 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007182 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007183}
7184
Avi Kivity851ba692009-08-24 11:10:17 +03007185static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007186{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007187 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007188}
7189
Dexuan Cui2acf9232010-06-10 11:27:12 +08007190static int handle_xsetbv(struct kvm_vcpu *vcpu)
7191{
7192 u64 new_bv = kvm_read_edx_eax(vcpu);
7193 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7194
7195 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007196 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007197 return 1;
7198}
7199
Wanpeng Lif53cd632014-12-02 19:14:58 +08007200static int handle_xsaves(struct kvm_vcpu *vcpu)
7201{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007202 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007203 WARN(1, "this should never happen\n");
7204 return 1;
7205}
7206
7207static int handle_xrstors(struct kvm_vcpu *vcpu)
7208{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007209 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007210 WARN(1, "this should never happen\n");
7211 return 1;
7212}
7213
Avi Kivity851ba692009-08-24 11:10:17 +03007214static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007215{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007216 if (likely(fasteoi)) {
7217 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7218 int access_type, offset;
7219
7220 access_type = exit_qualification & APIC_ACCESS_TYPE;
7221 offset = exit_qualification & APIC_ACCESS_OFFSET;
7222 /*
7223 * Sane guest uses MOV to write EOI, with written value
7224 * not cared. So make a short-circuit here by avoiding
7225 * heavy instruction emulation.
7226 */
7227 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7228 (offset == APIC_EOI)) {
7229 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007230 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007231 }
7232 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007233 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007234}
7235
Yang Zhangc7c9c562013-01-25 10:18:51 +08007236static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7237{
7238 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7239 int vector = exit_qualification & 0xff;
7240
7241 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7242 kvm_apic_set_eoi_accelerated(vcpu, vector);
7243 return 1;
7244}
7245
Yang Zhang83d4c282013-01-25 10:18:49 +08007246static int handle_apic_write(struct kvm_vcpu *vcpu)
7247{
7248 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7249 u32 offset = exit_qualification & 0xfff;
7250
7251 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7252 kvm_apic_write_nodecode(vcpu, offset);
7253 return 1;
7254}
7255
Avi Kivity851ba692009-08-24 11:10:17 +03007256static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007257{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007258 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007259 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007260 bool has_error_code = false;
7261 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007262 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007263 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007264
7265 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007266 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007267 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007268
7269 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7270
7271 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007272 if (reason == TASK_SWITCH_GATE && idt_v) {
7273 switch (type) {
7274 case INTR_TYPE_NMI_INTR:
7275 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007276 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007277 break;
7278 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007279 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007280 kvm_clear_interrupt_queue(vcpu);
7281 break;
7282 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007283 if (vmx->idt_vectoring_info &
7284 VECTORING_INFO_DELIVER_CODE_MASK) {
7285 has_error_code = true;
7286 error_code =
7287 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7288 }
7289 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007290 case INTR_TYPE_SOFT_EXCEPTION:
7291 kvm_clear_exception_queue(vcpu);
7292 break;
7293 default:
7294 break;
7295 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007296 }
Izik Eidus37817f22008-03-24 23:14:53 +02007297 tss_selector = exit_qualification;
7298
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007299 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7300 type != INTR_TYPE_EXT_INTR &&
7301 type != INTR_TYPE_NMI_INTR))
7302 skip_emulated_instruction(vcpu);
7303
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007304 if (kvm_task_switch(vcpu, tss_selector,
7305 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7306 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007307 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7308 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7309 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007310 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007311 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007312
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007313 /*
7314 * TODO: What about debug traps on tss switch?
7315 * Are we supposed to inject them and update dr6?
7316 */
7317
7318 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007319}
7320
Avi Kivity851ba692009-08-24 11:10:17 +03007321static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007322{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007323 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007324 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007325 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007326
Sheng Yangf9c617f2009-03-25 10:08:52 +08007327 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007328
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007329 /*
7330 * EPT violation happened while executing iret from NMI,
7331 * "blocked by NMI" bit has to be set before next VM entry.
7332 * There are errata that may cause this bit to not be set:
7333 * AAK134, BY25.
7334 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007335 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007336 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007337 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007338 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7339
Sheng Yang14394422008-04-28 12:24:45 +08007340 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007341 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007342
Junaid Shahid27959a42016-12-06 16:46:10 -08007343 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007344 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007345 ? PFERR_USER_MASK : 0;
7346 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007347 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007348 ? PFERR_WRITE_MASK : 0;
7349 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007350 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007351 ? PFERR_FETCH_MASK : 0;
7352 /* ept page table entry is present? */
7353 error_code |= (exit_qualification &
7354 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7355 EPT_VIOLATION_EXECUTABLE))
7356 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007357
Paolo Bonzinieebed242016-11-28 14:39:58 +01007358 error_code |= (exit_qualification & 0x100) != 0 ?
7359 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007360
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007361 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007362 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007363}
7364
Avi Kivity851ba692009-08-24 11:10:17 +03007365static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007366{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007367 gpa_t gpa;
7368
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007369 /*
7370 * A nested guest cannot optimize MMIO vmexits, because we have an
7371 * nGPA here instead of the required GPA.
7372 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007373 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007374 if (!is_guest_mode(vcpu) &&
7375 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007376 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007377 /*
7378 * Doing kvm_skip_emulated_instruction() depends on undefined
7379 * behavior: Intel's manual doesn't mandate
7380 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7381 * occurs and while on real hardware it was observed to be set,
7382 * other hypervisors (namely Hyper-V) don't set it, we end up
7383 * advancing IP with some random value. Disable fast mmio when
7384 * running nested and keep it for real hardware in hope that
7385 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7386 */
7387 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7388 return kvm_skip_emulated_instruction(vcpu);
7389 else
7390 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7391 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007392 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007393
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007394 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007395}
7396
Avi Kivity851ba692009-08-24 11:10:17 +03007397static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007398{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007399 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007400 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7401 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007402 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007403 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007404
7405 return 1;
7406}
7407
Mohammed Gamal80ced182009-09-01 12:48:18 +02007408static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007409{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007410 struct vcpu_vmx *vmx = to_vmx(vcpu);
7411 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007412 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007413 u32 cpu_exec_ctrl;
7414 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007415 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007416
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007417 /*
7418 * We should never reach the point where we are emulating L2
7419 * due to invalid guest state as that means we incorrectly
7420 * allowed a nested VMEntry with an invalid vmcs12.
7421 */
7422 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7423
Avi Kivity49e9d552010-09-19 14:34:08 +02007424 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7425 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007426
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007427 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007428 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007429 return handle_interrupt_window(&vmx->vcpu);
7430
Radim Krčmář72875d82017-04-26 22:32:19 +02007431 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007432 return 1;
7433
Liran Alon9b8ae632017-11-05 16:56:34 +02007434 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007435
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007436 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007437 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007438 ret = 0;
7439 goto out;
7440 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007441
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007442 if (err != EMULATE_DONE)
7443 goto emulation_error;
7444
7445 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7446 vcpu->arch.exception.pending)
7447 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007448
Gleb Natapov8d76c492013-05-08 18:38:44 +03007449 if (vcpu->arch.halt_request) {
7450 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007451 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007452 goto out;
7453 }
7454
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007455 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007456 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007457 if (need_resched())
7458 schedule();
7459 }
7460
Mohammed Gamal80ced182009-09-01 12:48:18 +02007461out:
7462 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007463
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007464emulation_error:
7465 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7466 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7467 vcpu->run->internal.ndata = 0;
7468 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007469}
7470
7471static void grow_ple_window(struct kvm_vcpu *vcpu)
7472{
7473 struct vcpu_vmx *vmx = to_vmx(vcpu);
7474 int old = vmx->ple_window;
7475
Babu Mogerc8e88712018-03-16 16:37:24 -04007476 vmx->ple_window = __grow_ple_window(old, ple_window,
7477 ple_window_grow,
7478 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007479
7480 if (vmx->ple_window != old)
7481 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007482
7483 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007484}
7485
7486static void shrink_ple_window(struct kvm_vcpu *vcpu)
7487{
7488 struct vcpu_vmx *vmx = to_vmx(vcpu);
7489 int old = vmx->ple_window;
7490
Babu Mogerc8e88712018-03-16 16:37:24 -04007491 vmx->ple_window = __shrink_ple_window(old, ple_window,
7492 ple_window_shrink,
7493 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007494
7495 if (vmx->ple_window != old)
7496 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007497
7498 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007499}
7500
7501/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007502 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7503 */
7504static void wakeup_handler(void)
7505{
7506 struct kvm_vcpu *vcpu;
7507 int cpu = smp_processor_id();
7508
7509 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7510 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7511 blocked_vcpu_list) {
7512 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7513
7514 if (pi_test_on(pi_desc) == 1)
7515 kvm_vcpu_kick(vcpu);
7516 }
7517 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7518}
7519
Peng Haoe01bca22018-04-07 05:47:32 +08007520static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007521{
7522 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7523 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7524 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7525 0ull, VMX_EPT_EXECUTABLE_MASK,
7526 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007527 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007528
7529 ept_set_mmio_spte_mask();
7530 kvm_enable_tdp();
7531}
7532
Tiejun Chenf2c76482014-10-28 10:14:47 +08007533static __init int hardware_setup(void)
7534{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007535 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007536 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007537
7538 rdmsrl_safe(MSR_EFER, &host_efer);
7539
7540 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7541 kvm_define_shared_msr(i, vmx_msr_index[i]);
7542
Radim Krčmář23611332016-09-29 22:41:33 +02007543 for (i = 0; i < VMX_BITMAP_NR; i++) {
7544 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7545 if (!vmx_bitmap[i])
7546 goto out;
7547 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007548
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007549 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7550 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7551
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007552 if (setup_vmcs_config(&vmcs_config) < 0) {
7553 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007554 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007555 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007556
7557 if (boot_cpu_has(X86_FEATURE_NX))
7558 kvm_enable_efer_bits(EFER_NX);
7559
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007560 if (boot_cpu_has(X86_FEATURE_MPX)) {
7561 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7562 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7563 }
7564
Wanpeng Li08d839c2017-03-23 05:30:08 -07007565 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7566 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007567 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007568
Tiejun Chenf2c76482014-10-28 10:14:47 +08007569 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007570 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007571 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007572 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007573 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007574
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007575 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007576 enable_ept_ad_bits = 0;
7577
Wanpeng Li8ad81822017-10-09 15:51:53 -07007578 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007579 enable_unrestricted_guest = 0;
7580
Paolo Bonziniad15a292015-01-30 16:18:49 +01007581 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007582 flexpriority_enabled = 0;
7583
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007584 if (!cpu_has_virtual_nmis())
7585 enable_vnmi = 0;
7586
Paolo Bonziniad15a292015-01-30 16:18:49 +01007587 /*
7588 * set_apic_access_page_addr() is used to reload apic access
7589 * page upon invalidation. No need to do anything if not
7590 * using the APIC_ACCESS_ADDR VMCS field.
7591 */
7592 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007593 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007594
7595 if (!cpu_has_vmx_tpr_shadow())
7596 kvm_x86_ops->update_cr8_intercept = NULL;
7597
7598 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7599 kvm_disable_largepages();
7600
Wanpeng Li0f107682017-09-28 18:06:24 -07007601 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007602 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007603 ple_window = 0;
7604 ple_window_grow = 0;
7605 ple_window_max = 0;
7606 ple_window_shrink = 0;
7607 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007608
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007609 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007610 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007611 kvm_x86_ops->sync_pir_to_irr = NULL;
7612 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007613
Haozhong Zhang64903d62015-10-20 15:39:09 +08007614 if (cpu_has_vmx_tsc_scaling()) {
7615 kvm_has_tsc_control = true;
7616 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7617 kvm_tsc_scaling_ratio_frac_bits = 48;
7618 }
7619
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007620 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7621
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007622 if (enable_ept)
7623 vmx_enable_tdp();
7624 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007625 kvm_disable_tdp();
7626
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007627 if (!nested) {
7628 kvm_x86_ops->get_nested_state = NULL;
7629 kvm_x86_ops->set_nested_state = NULL;
7630 }
7631
Kai Huang843e4332015-01-28 10:54:28 +08007632 /*
7633 * Only enable PML when hardware supports PML feature, and both EPT
7634 * and EPT A/D bit features are enabled -- PML depends on them to work.
7635 */
7636 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7637 enable_pml = 0;
7638
7639 if (!enable_pml) {
7640 kvm_x86_ops->slot_enable_log_dirty = NULL;
7641 kvm_x86_ops->slot_disable_log_dirty = NULL;
7642 kvm_x86_ops->flush_log_dirty = NULL;
7643 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7644 }
7645
Yunhong Jiang64672c92016-06-13 14:19:59 -07007646 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7647 u64 vmx_msr;
7648
7649 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7650 cpu_preemption_timer_multi =
7651 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7652 } else {
7653 kvm_x86_ops->set_hv_timer = NULL;
7654 kvm_x86_ops->cancel_hv_timer = NULL;
7655 }
7656
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007657 if (!cpu_has_vmx_shadow_vmcs())
7658 enable_shadow_vmcs = 0;
7659 if (enable_shadow_vmcs)
7660 init_vmcs_shadow_fields();
7661
Feng Wubf9f6ac2015-09-18 22:29:55 +08007662 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007663 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007664
Ashok Rajc45dcc72016-06-22 14:59:56 +08007665 kvm_mce_cap_supported |= MCG_LMCE_P;
7666
Tiejun Chenf2c76482014-10-28 10:14:47 +08007667 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007668
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007669out:
Radim Krčmář23611332016-09-29 22:41:33 +02007670 for (i = 0; i < VMX_BITMAP_NR; i++)
7671 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007672
7673 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007674}
7675
7676static __exit void hardware_unsetup(void)
7677{
Radim Krčmář23611332016-09-29 22:41:33 +02007678 int i;
7679
7680 for (i = 0; i < VMX_BITMAP_NR; i++)
7681 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007682
Tiejun Chenf2c76482014-10-28 10:14:47 +08007683 free_kvm_area();
7684}
7685
Avi Kivity6aa8b732006-12-10 02:21:36 -08007686/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007687 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7688 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7689 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007690static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007691{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007692 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007693 grow_ple_window(vcpu);
7694
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007695 /*
7696 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7697 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7698 * never set PAUSE_EXITING and just set PLE if supported,
7699 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7700 */
7701 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007702 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007703}
7704
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007705static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007706{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007707 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007708}
7709
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007710static int handle_mwait(struct kvm_vcpu *vcpu)
7711{
7712 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7713 return handle_nop(vcpu);
7714}
7715
Jim Mattson45ec3682017-08-23 16:32:04 -07007716static int handle_invalid_op(struct kvm_vcpu *vcpu)
7717{
7718 kvm_queue_exception(vcpu, UD_VECTOR);
7719 return 1;
7720}
7721
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007722static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7723{
7724 return 1;
7725}
7726
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007727static int handle_monitor(struct kvm_vcpu *vcpu)
7728{
7729 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7730 return handle_nop(vcpu);
7731}
7732
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007733/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007734 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7735 * set the success or error code of an emulated VMX instruction, as specified
7736 * by Vol 2B, VMX Instruction Reference, "Conventions".
7737 */
7738static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7739{
7740 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7741 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7742 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7743}
7744
7745static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7746{
7747 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7748 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7749 X86_EFLAGS_SF | X86_EFLAGS_OF))
7750 | X86_EFLAGS_CF);
7751}
7752
Abel Gordon145c28d2013-04-18 14:36:55 +03007753static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007754 u32 vm_instruction_error)
7755{
7756 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7757 /*
7758 * failValid writes the error number to the current VMCS, which
7759 * can't be done there isn't a current VMCS.
7760 */
7761 nested_vmx_failInvalid(vcpu);
7762 return;
7763 }
7764 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7765 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7766 X86_EFLAGS_SF | X86_EFLAGS_OF))
7767 | X86_EFLAGS_ZF);
7768 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7769 /*
7770 * We don't need to force a shadow sync because
7771 * VM_INSTRUCTION_ERROR is not shadowed
7772 */
7773}
Abel Gordon145c28d2013-04-18 14:36:55 +03007774
Wincy Vanff651cb2014-12-11 08:52:58 +03007775static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7776{
7777 /* TODO: not to reset guest simply here. */
7778 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007779 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007780}
7781
Jan Kiszkaf4124502014-03-07 20:03:13 +01007782static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7783{
7784 struct vcpu_vmx *vmx =
7785 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7786
7787 vmx->nested.preemption_timer_expired = true;
7788 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7789 kvm_vcpu_kick(&vmx->vcpu);
7790
7791 return HRTIMER_NORESTART;
7792}
7793
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007794/*
Bandan Das19677e32014-05-06 02:19:15 -04007795 * Decode the memory-address operand of a vmx instruction, as recorded on an
7796 * exit caused by such an instruction (run by a guest hypervisor).
7797 * On success, returns 0. When the operand is invalid, returns 1 and throws
7798 * #UD or #GP.
7799 */
7800static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7801 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007802 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007803{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007804 gva_t off;
7805 bool exn;
7806 struct kvm_segment s;
7807
Bandan Das19677e32014-05-06 02:19:15 -04007808 /*
7809 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7810 * Execution", on an exit, vmx_instruction_info holds most of the
7811 * addressing components of the operand. Only the displacement part
7812 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7813 * For how an actual address is calculated from all these components,
7814 * refer to Vol. 1, "Operand Addressing".
7815 */
7816 int scaling = vmx_instruction_info & 3;
7817 int addr_size = (vmx_instruction_info >> 7) & 7;
7818 bool is_reg = vmx_instruction_info & (1u << 10);
7819 int seg_reg = (vmx_instruction_info >> 15) & 7;
7820 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7821 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7822 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7823 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7824
7825 if (is_reg) {
7826 kvm_queue_exception(vcpu, UD_VECTOR);
7827 return 1;
7828 }
7829
7830 /* Addr = segment_base + offset */
7831 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007832 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007833 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007834 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007835 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007836 off += kvm_register_read(vcpu, index_reg)<<scaling;
7837 vmx_get_segment(vcpu, &s, seg_reg);
7838 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007839
7840 if (addr_size == 1) /* 32 bit */
7841 *ret &= 0xffffffff;
7842
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007843 /* Checks for #GP/#SS exceptions. */
7844 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007845 if (is_long_mode(vcpu)) {
7846 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7847 * non-canonical form. This is the only check on the memory
7848 * destination for long mode!
7849 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007850 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007851 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007852 /* Protected mode: apply checks for segment validity in the
7853 * following order:
7854 * - segment type check (#GP(0) may be thrown)
7855 * - usability check (#GP(0)/#SS(0))
7856 * - limit check (#GP(0)/#SS(0))
7857 */
7858 if (wr)
7859 /* #GP(0) if the destination operand is located in a
7860 * read-only data segment or any code segment.
7861 */
7862 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7863 else
7864 /* #GP(0) if the source operand is located in an
7865 * execute-only code segment
7866 */
7867 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007868 if (exn) {
7869 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7870 return 1;
7871 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007872 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7873 */
7874 exn = (s.unusable != 0);
7875 /* Protected mode: #GP(0)/#SS(0) if the memory
7876 * operand is outside the segment limit.
7877 */
7878 exn = exn || (off + sizeof(u64) > s.limit);
7879 }
7880 if (exn) {
7881 kvm_queue_exception_e(vcpu,
7882 seg_reg == VCPU_SREG_SS ?
7883 SS_VECTOR : GP_VECTOR,
7884 0);
7885 return 1;
7886 }
7887
Bandan Das19677e32014-05-06 02:19:15 -04007888 return 0;
7889}
7890
Radim Krčmářcbf71272017-05-19 15:48:51 +02007891static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007892{
7893 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007894 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007895
7896 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007897 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007898 return 1;
7899
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007900 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007901 kvm_inject_page_fault(vcpu, &e);
7902 return 1;
7903 }
7904
Bandan Das3573e222014-05-06 02:19:16 -04007905 return 0;
7906}
7907
Liran Alonabfc52c2018-06-23 02:35:13 +03007908/*
7909 * Allocate a shadow VMCS and associate it with the currently loaded
7910 * VMCS, unless such a shadow VMCS already exists. The newly allocated
7911 * VMCS is also VMCLEARed, so that it is ready for use.
7912 */
7913static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
7914{
7915 struct vcpu_vmx *vmx = to_vmx(vcpu);
7916 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
7917
7918 /*
7919 * We should allocate a shadow vmcs for vmcs01 only when L1
7920 * executes VMXON and free it when L1 executes VMXOFF.
7921 * As it is invalid to execute VMXON twice, we shouldn't reach
7922 * here when vmcs01 already have an allocated shadow vmcs.
7923 */
7924 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
7925
7926 if (!loaded_vmcs->shadow_vmcs) {
7927 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
7928 if (loaded_vmcs->shadow_vmcs)
7929 vmcs_clear(loaded_vmcs->shadow_vmcs);
7930 }
7931 return loaded_vmcs->shadow_vmcs;
7932}
7933
Jim Mattsone29acc52016-11-30 12:03:43 -08007934static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7935{
7936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007937 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007938
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007939 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7940 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007941 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007942
7943 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7944 if (!vmx->nested.cached_vmcs12)
7945 goto out_cached_vmcs12;
7946
Liran Alon61ada742018-06-23 02:35:08 +03007947 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7948 if (!vmx->nested.cached_shadow_vmcs12)
7949 goto out_cached_shadow_vmcs12;
7950
Liran Alonabfc52c2018-06-23 02:35:13 +03007951 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
7952 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08007953
Jim Mattsone29acc52016-11-30 12:03:43 -08007954 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7955 HRTIMER_MODE_REL_PINNED);
7956 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7957
7958 vmx->nested.vmxon = true;
7959 return 0;
7960
7961out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03007962 kfree(vmx->nested.cached_shadow_vmcs12);
7963
7964out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08007965 kfree(vmx->nested.cached_vmcs12);
7966
7967out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007968 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007969
Jim Mattsonde3a0022017-11-27 17:22:25 -06007970out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007971 return -ENOMEM;
7972}
7973
Bandan Das3573e222014-05-06 02:19:16 -04007974/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007975 * Emulate the VMXON instruction.
7976 * Currently, we just remember that VMX is active, and do not save or even
7977 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7978 * do not currently need to store anything in that guest-allocated memory
7979 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7980 * argument is different from the VMXON pointer (which the spec says they do).
7981 */
7982static int handle_vmon(struct kvm_vcpu *vcpu)
7983{
Jim Mattsone29acc52016-11-30 12:03:43 -08007984 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007985 gpa_t vmptr;
7986 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007987 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007988 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7989 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007990
Jim Mattson70f3aac2017-04-26 08:53:46 -07007991 /*
7992 * The Intel VMX Instruction Reference lists a bunch of bits that are
7993 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7994 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7995 * Otherwise, we should fail with #UD. But most faulting conditions
7996 * have already been checked by hardware, prior to the VM-exit for
7997 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7998 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007999 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008000 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008001 kvm_queue_exception(vcpu, UD_VECTOR);
8002 return 1;
8003 }
8004
Felix Wilhelm727ba742018-06-11 09:43:44 +02008005 /* CPL=0 must be checked manually. */
8006 if (vmx_get_cpl(vcpu)) {
8007 kvm_queue_exception(vcpu, UD_VECTOR);
8008 return 1;
8009 }
8010
Abel Gordon145c28d2013-04-18 14:36:55 +03008011 if (vmx->nested.vmxon) {
8012 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008013 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008014 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008015
Haozhong Zhang3b840802016-06-22 14:59:54 +08008016 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008017 != VMXON_NEEDED_FEATURES) {
8018 kvm_inject_gp(vcpu, 0);
8019 return 1;
8020 }
8021
Radim Krčmářcbf71272017-05-19 15:48:51 +02008022 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008023 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008024
8025 /*
8026 * SDM 3: 24.11.5
8027 * The first 4 bytes of VMXON region contain the supported
8028 * VMCS revision identifier
8029 *
8030 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8031 * which replaces physical address width with 32
8032 */
8033 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8034 nested_vmx_failInvalid(vcpu);
8035 return kvm_skip_emulated_instruction(vcpu);
8036 }
8037
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008038 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8039 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008040 nested_vmx_failInvalid(vcpu);
8041 return kvm_skip_emulated_instruction(vcpu);
8042 }
8043 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8044 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008045 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008046 nested_vmx_failInvalid(vcpu);
8047 return kvm_skip_emulated_instruction(vcpu);
8048 }
8049 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008050 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008051
8052 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008053 ret = enter_vmx_operation(vcpu);
8054 if (ret)
8055 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008056
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008057 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008058 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008059}
8060
8061/*
8062 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8063 * for running VMX instructions (except VMXON, whose prerequisites are
8064 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008065 * Note that many of these exceptions have priority over VM exits, so they
8066 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008067 */
8068static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8069{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008070 if (vmx_get_cpl(vcpu)) {
8071 kvm_queue_exception(vcpu, UD_VECTOR);
8072 return 0;
8073 }
8074
Jim Mattson70f3aac2017-04-26 08:53:46 -07008075 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008076 kvm_queue_exception(vcpu, UD_VECTOR);
8077 return 0;
8078 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008079 return 1;
8080}
8081
David Matlack8ca44e82017-08-01 14:00:39 -07008082static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8083{
8084 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8085 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8086}
8087
Abel Gordone7953d72013-04-18 14:37:55 +03008088static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8089{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008090 if (vmx->nested.current_vmptr == -1ull)
8091 return;
8092
Abel Gordon012f83c2013-04-18 14:39:25 +03008093 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008094 /* copy to memory all shadowed fields in case
8095 they were modified */
8096 copy_shadow_to_vmcs12(vmx);
8097 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008098 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008099 }
Wincy Van705699a2015-02-03 23:58:17 +08008100 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008101
8102 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008103 kvm_vcpu_write_guest_page(&vmx->vcpu,
8104 vmx->nested.current_vmptr >> PAGE_SHIFT,
8105 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008106
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008107 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008108}
8109
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008110/*
8111 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8112 * just stops using VMX.
8113 */
8114static void free_nested(struct vcpu_vmx *vmx)
8115{
Wanpeng Lib7455822017-11-22 14:04:00 -08008116 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008117 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008118
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008119 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008120 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008121 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008122 vmx->nested.posted_intr_nv = -1;
8123 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008124 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008125 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008126 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8127 free_vmcs(vmx->vmcs01.shadow_vmcs);
8128 vmx->vmcs01.shadow_vmcs = NULL;
8129 }
David Matlack4f2777b2016-07-13 17:16:37 -07008130 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008131 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008132 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008133 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008134 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008135 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008136 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008137 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008138 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008139 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008140 }
Wincy Van705699a2015-02-03 23:58:17 +08008141 if (vmx->nested.pi_desc_page) {
8142 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008143 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008144 vmx->nested.pi_desc_page = NULL;
8145 vmx->nested.pi_desc = NULL;
8146 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008147
Jim Mattsonde3a0022017-11-27 17:22:25 -06008148 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008149}
8150
8151/* Emulate the VMXOFF instruction */
8152static int handle_vmoff(struct kvm_vcpu *vcpu)
8153{
8154 if (!nested_vmx_check_permission(vcpu))
8155 return 1;
8156 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008157 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008158 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008159}
8160
Nadav Har'El27d6c862011-05-25 23:06:59 +03008161/* Emulate the VMCLEAR instruction */
8162static int handle_vmclear(struct kvm_vcpu *vcpu)
8163{
8164 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008165 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008166 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008167
8168 if (!nested_vmx_check_permission(vcpu))
8169 return 1;
8170
Radim Krčmářcbf71272017-05-19 15:48:51 +02008171 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008172 return 1;
8173
Radim Krčmářcbf71272017-05-19 15:48:51 +02008174 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8175 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8176 return kvm_skip_emulated_instruction(vcpu);
8177 }
8178
8179 if (vmptr == vmx->nested.vmxon_ptr) {
8180 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8181 return kvm_skip_emulated_instruction(vcpu);
8182 }
8183
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008184 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008185 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008186
Jim Mattson587d7e722017-03-02 12:41:48 -08008187 kvm_vcpu_write_guest(vcpu,
8188 vmptr + offsetof(struct vmcs12, launch_state),
8189 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008190
Nadav Har'El27d6c862011-05-25 23:06:59 +03008191 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008192 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008193}
8194
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008195static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8196
8197/* Emulate the VMLAUNCH instruction */
8198static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8199{
8200 return nested_vmx_run(vcpu, true);
8201}
8202
8203/* Emulate the VMRESUME instruction */
8204static int handle_vmresume(struct kvm_vcpu *vcpu)
8205{
8206
8207 return nested_vmx_run(vcpu, false);
8208}
8209
Nadav Har'El49f705c2011-05-25 23:08:30 +03008210/*
8211 * Read a vmcs12 field. Since these can have varying lengths and we return
8212 * one type, we chose the biggest type (u64) and zero-extend the return value
8213 * to that size. Note that the caller, handle_vmread, might need to use only
8214 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8215 * 64-bit fields are to be returned).
8216 */
Liran Alone2536742018-06-23 02:35:02 +03008217static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008218 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008219{
8220 short offset = vmcs_field_to_offset(field);
8221 char *p;
8222
8223 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008224 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008225
Liran Alone2536742018-06-23 02:35:02 +03008226 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008227
Jim Mattsond37f4262017-12-22 12:12:16 -08008228 switch (vmcs_field_width(field)) {
8229 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008230 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008231 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008232 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008233 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008234 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008235 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008236 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008237 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008238 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008239 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008240 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008241 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008242 WARN_ON(1);
8243 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008244 }
8245}
8246
Abel Gordon20b97fe2013-04-18 14:36:25 +03008247
Liran Alone2536742018-06-23 02:35:02 +03008248static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008249 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008250 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008251 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008252 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008253 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008254
Jim Mattsond37f4262017-12-22 12:12:16 -08008255 switch (vmcs_field_width(field)) {
8256 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008257 *(u16 *)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_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008260 *(u32 *)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_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008263 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008265 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008266 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008267 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008268 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008269 WARN_ON(1);
8270 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008271 }
8272
8273}
8274
Jim Mattsonf4160e42018-05-29 09:11:33 -07008275/*
8276 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8277 * they have been modified by the L1 guest. Note that the "read-only"
8278 * VM-exit information fields are actually writable if the vCPU is
8279 * configured to support "VMWRITE to any supported field in the VMCS."
8280 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008281static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8282{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008283 const u16 *fields[] = {
8284 shadow_read_write_fields,
8285 shadow_read_only_fields
8286 };
8287 const int max_fields[] = {
8288 max_shadow_read_write_fields,
8289 max_shadow_read_only_fields
8290 };
8291 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008292 unsigned long field;
8293 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008294 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008295
Jan Kiszka282da872014-10-08 18:05:39 +02008296 preempt_disable();
8297
Abel Gordon16f5b902013-04-18 14:38:25 +03008298 vmcs_load(shadow_vmcs);
8299
Jim Mattsonf4160e42018-05-29 09:11:33 -07008300 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8301 for (i = 0; i < max_fields[q]; i++) {
8302 field = fields[q][i];
8303 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008304 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008305 }
8306 /*
8307 * Skip the VM-exit information fields if they are read-only.
8308 */
8309 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8310 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008311 }
8312
8313 vmcs_clear(shadow_vmcs);
8314 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008315
8316 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008317}
8318
Abel Gordonc3114422013-04-18 14:38:55 +03008319static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8320{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008321 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008322 shadow_read_write_fields,
8323 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008324 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008325 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008326 max_shadow_read_write_fields,
8327 max_shadow_read_only_fields
8328 };
8329 int i, q;
8330 unsigned long field;
8331 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008332 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008333
8334 vmcs_load(shadow_vmcs);
8335
Mathias Krausec2bae892013-06-26 20:36:21 +02008336 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008337 for (i = 0; i < max_fields[q]; i++) {
8338 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008339 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008340 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008341 }
8342 }
8343
8344 vmcs_clear(shadow_vmcs);
8345 vmcs_load(vmx->loaded_vmcs->vmcs);
8346}
8347
Nadav Har'El49f705c2011-05-25 23:08:30 +03008348/*
8349 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8350 * used before) all generate the same failure when it is missing.
8351 */
8352static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8353{
8354 struct vcpu_vmx *vmx = to_vmx(vcpu);
8355 if (vmx->nested.current_vmptr == -1ull) {
8356 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357 return 0;
8358 }
8359 return 1;
8360}
8361
8362static int handle_vmread(struct kvm_vcpu *vcpu)
8363{
8364 unsigned long field;
8365 u64 field_value;
8366 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8367 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8368 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008369 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008370
Kyle Hueyeb277562016-11-29 12:40:39 -08008371 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008372 return 1;
8373
Kyle Huey6affcbe2016-11-29 12:40:40 -08008374 if (!nested_vmx_check_vmcs12(vcpu))
8375 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008376
Liran Alon6d894f42018-06-23 02:35:09 +03008377 if (!is_guest_mode(vcpu))
8378 vmcs12 = get_vmcs12(vcpu);
8379 else {
8380 /*
8381 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8382 * to shadowed-field sets the ALU flags for VMfailInvalid.
8383 */
8384 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8385 nested_vmx_failInvalid(vcpu);
8386 return kvm_skip_emulated_instruction(vcpu);
8387 }
8388 vmcs12 = get_shadow_vmcs12(vcpu);
8389 }
8390
Nadav Har'El49f705c2011-05-25 23:08:30 +03008391 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008392 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008393 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008394 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008395 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008396 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008397 }
8398 /*
8399 * Now copy part of this value to register or memory, as requested.
8400 * Note that the number of bits actually copied is 32 or 64 depending
8401 * on the guest's mode (32 or 64 bit), not on the given field's length.
8402 */
8403 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008404 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008405 field_value);
8406 } else {
8407 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008408 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008409 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008410 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008411 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8412 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008413 }
8414
8415 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008416 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008417}
8418
8419
8420static int handle_vmwrite(struct kvm_vcpu *vcpu)
8421{
8422 unsigned long field;
8423 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8426 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008427
Nadav Har'El49f705c2011-05-25 23:08:30 +03008428 /* The value to write might be 32 or 64 bits, depending on L1's long
8429 * mode, and eventually we need to write that into a field of several
8430 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008431 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008432 * bits into the vmcs12 field.
8433 */
8434 u64 field_value = 0;
8435 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008436 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008437
Kyle Hueyeb277562016-11-29 12:40:39 -08008438 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008439 return 1;
8440
Kyle Huey6affcbe2016-11-29 12:40:40 -08008441 if (!nested_vmx_check_vmcs12(vcpu))
8442 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008443
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008445 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008446 (((vmx_instruction_info) >> 3) & 0xf));
8447 else {
8448 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008449 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008451 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8452 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008453 kvm_inject_page_fault(vcpu, &e);
8454 return 1;
8455 }
8456 }
8457
8458
Nadav Amit27e6fb52014-06-18 17:19:26 +03008459 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008460 /*
8461 * If the vCPU supports "VMWRITE to any supported field in the
8462 * VMCS," then the "read-only" fields are actually read/write.
8463 */
8464 if (vmcs_field_readonly(field) &&
8465 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008466 nested_vmx_failValid(vcpu,
8467 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008468 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008469 }
8470
Liran Alon6d894f42018-06-23 02:35:09 +03008471 if (!is_guest_mode(vcpu))
8472 vmcs12 = get_vmcs12(vcpu);
8473 else {
8474 /*
8475 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8476 * to shadowed-field sets the ALU flags for VMfailInvalid.
8477 */
8478 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8479 nested_vmx_failInvalid(vcpu);
8480 return kvm_skip_emulated_instruction(vcpu);
8481 }
8482 vmcs12 = get_shadow_vmcs12(vcpu);
8483
8484 }
8485
8486 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008488 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008489 }
8490
Liran Alon6d894f42018-06-23 02:35:09 +03008491 /*
8492 * Do not track vmcs12 dirty-state if in guest-mode
8493 * as we actually dirty shadow vmcs12 instead of vmcs12.
8494 */
8495 if (!is_guest_mode(vcpu)) {
8496 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008497#define SHADOW_FIELD_RW(x) case x:
8498#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008499 /*
8500 * The fields that can be updated by L1 without a vmexit are
8501 * always updated in the vmcs02, the others go down the slow
8502 * path of prepare_vmcs02.
8503 */
8504 break;
8505 default:
8506 vmx->nested.dirty_vmcs12 = true;
8507 break;
8508 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008509 }
8510
Nadav Har'El49f705c2011-05-25 23:08:30 +03008511 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008512 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008513}
8514
Jim Mattsona8bc2842016-11-30 12:03:44 -08008515static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8516{
8517 vmx->nested.current_vmptr = vmptr;
8518 if (enable_shadow_vmcs) {
8519 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8520 SECONDARY_EXEC_SHADOW_VMCS);
8521 vmcs_write64(VMCS_LINK_POINTER,
8522 __pa(vmx->vmcs01.shadow_vmcs));
8523 vmx->nested.sync_shadow_vmcs = true;
8524 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008525 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008526}
8527
Nadav Har'El63846662011-05-25 23:07:29 +03008528/* Emulate the VMPTRLD instruction */
8529static int handle_vmptrld(struct kvm_vcpu *vcpu)
8530{
8531 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008532 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008533
8534 if (!nested_vmx_check_permission(vcpu))
8535 return 1;
8536
Radim Krčmářcbf71272017-05-19 15:48:51 +02008537 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008538 return 1;
8539
Radim Krčmářcbf71272017-05-19 15:48:51 +02008540 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8541 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8542 return kvm_skip_emulated_instruction(vcpu);
8543 }
8544
8545 if (vmptr == vmx->nested.vmxon_ptr) {
8546 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8547 return kvm_skip_emulated_instruction(vcpu);
8548 }
8549
Nadav Har'El63846662011-05-25 23:07:29 +03008550 if (vmx->nested.current_vmptr != vmptr) {
8551 struct vmcs12 *new_vmcs12;
8552 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008553 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8554 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008555 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008556 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008557 }
8558 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008559 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008560 (new_vmcs12->hdr.shadow_vmcs &&
8561 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008562 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008563 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008564 nested_vmx_failValid(vcpu,
8565 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008566 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008567 }
Nadav Har'El63846662011-05-25 23:07:29 +03008568
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008569 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008570 /*
8571 * Load VMCS12 from guest memory since it is not already
8572 * cached.
8573 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008574 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8575 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008576 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008577
Jim Mattsona8bc2842016-11-30 12:03:44 -08008578 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008579 }
8580
8581 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008582 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008583}
8584
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008585/* Emulate the VMPTRST instruction */
8586static int handle_vmptrst(struct kvm_vcpu *vcpu)
8587{
8588 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8589 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8590 gva_t vmcs_gva;
8591 struct x86_exception e;
8592
8593 if (!nested_vmx_check_permission(vcpu))
8594 return 1;
8595
8596 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008597 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008598 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008599 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008600 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8601 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8602 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008603 kvm_inject_page_fault(vcpu, &e);
8604 return 1;
8605 }
8606 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008607 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008608}
8609
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008610/* Emulate the INVEPT instruction */
8611static int handle_invept(struct kvm_vcpu *vcpu)
8612{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008613 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008614 u32 vmx_instruction_info, types;
8615 unsigned long type;
8616 gva_t gva;
8617 struct x86_exception e;
8618 struct {
8619 u64 eptp, gpa;
8620 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008621
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008622 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008623 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008624 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008625 kvm_queue_exception(vcpu, UD_VECTOR);
8626 return 1;
8627 }
8628
8629 if (!nested_vmx_check_permission(vcpu))
8630 return 1;
8631
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008632 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008633 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008634
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008635 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008636
Jim Mattson85c856b2016-10-26 08:38:38 -07008637 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 nested_vmx_failValid(vcpu,
8639 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008640 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008641 }
8642
8643 /* According to the Intel VMX instruction reference, the memory
8644 * operand is read even if it isn't needed (e.g., for type==global)
8645 */
8646 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008647 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008648 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008649 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008650 kvm_inject_page_fault(vcpu, &e);
8651 return 1;
8652 }
8653
8654 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008655 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008656 /*
8657 * TODO: track mappings and invalidate
8658 * single context requests appropriately
8659 */
8660 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008661 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008662 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008663 nested_vmx_succeed(vcpu);
8664 break;
8665 default:
8666 BUG_ON(1);
8667 break;
8668 }
8669
Kyle Huey6affcbe2016-11-29 12:40:40 -08008670 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008671}
8672
Petr Matouseka642fc32014-09-23 20:22:30 +02008673static int handle_invvpid(struct kvm_vcpu *vcpu)
8674{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008675 struct vcpu_vmx *vmx = to_vmx(vcpu);
8676 u32 vmx_instruction_info;
8677 unsigned long type, types;
8678 gva_t gva;
8679 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008680 struct {
8681 u64 vpid;
8682 u64 gla;
8683 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008684
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008685 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008686 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008687 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008688 kvm_queue_exception(vcpu, UD_VECTOR);
8689 return 1;
8690 }
8691
8692 if (!nested_vmx_check_permission(vcpu))
8693 return 1;
8694
8695 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8696 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8697
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008698 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008699 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008700
Jim Mattson85c856b2016-10-26 08:38:38 -07008701 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008702 nested_vmx_failValid(vcpu,
8703 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008704 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008705 }
8706
8707 /* according to the intel vmx instruction reference, the memory
8708 * operand is read even if it isn't needed (e.g., for type==global)
8709 */
8710 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8711 vmx_instruction_info, false, &gva))
8712 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008713 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008714 kvm_inject_page_fault(vcpu, &e);
8715 return 1;
8716 }
Jim Mattson40352602017-06-28 09:37:37 -07008717 if (operand.vpid >> 16) {
8718 nested_vmx_failValid(vcpu,
8719 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8720 return kvm_skip_emulated_instruction(vcpu);
8721 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008722
8723 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008724 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008725 if (!operand.vpid ||
8726 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008727 nested_vmx_failValid(vcpu,
8728 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8729 return kvm_skip_emulated_instruction(vcpu);
8730 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008731 if (cpu_has_vmx_invvpid_individual_addr() &&
8732 vmx->nested.vpid02) {
8733 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8734 vmx->nested.vpid02, operand.gla);
8735 } else
8736 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8737 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008738 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008739 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008740 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008741 nested_vmx_failValid(vcpu,
8742 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008743 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008744 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008745 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008746 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008747 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008748 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008749 break;
8750 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008751 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008752 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008753 }
8754
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008755 nested_vmx_succeed(vcpu);
8756
Kyle Huey6affcbe2016-11-29 12:40:40 -08008757 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008758}
8759
Kai Huang843e4332015-01-28 10:54:28 +08008760static int handle_pml_full(struct kvm_vcpu *vcpu)
8761{
8762 unsigned long exit_qualification;
8763
8764 trace_kvm_pml_full(vcpu->vcpu_id);
8765
8766 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8767
8768 /*
8769 * PML buffer FULL happened while executing iret from NMI,
8770 * "blocked by NMI" bit has to be set before next VM entry.
8771 */
8772 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008773 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008774 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8775 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8776 GUEST_INTR_STATE_NMI);
8777
8778 /*
8779 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8780 * here.., and there's no userspace involvement needed for PML.
8781 */
8782 return 1;
8783}
8784
Yunhong Jiang64672c92016-06-13 14:19:59 -07008785static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8786{
8787 kvm_lapic_expired_hv_timer(vcpu);
8788 return 1;
8789}
8790
Bandan Das41ab9372017-08-03 15:54:43 -04008791static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8792{
8793 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008794 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8795
8796 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008797 switch (address & VMX_EPTP_MT_MASK) {
8798 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008799 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008800 return false;
8801 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008802 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008803 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008804 return false;
8805 break;
8806 default:
8807 return false;
8808 }
8809
David Hildenbrandbb97a012017-08-10 23:15:28 +02008810 /* only 4 levels page-walk length are valid */
8811 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008812 return false;
8813
8814 /* Reserved bits should not be set */
8815 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8816 return false;
8817
8818 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008819 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008820 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008821 return false;
8822 }
8823
8824 return true;
8825}
8826
8827static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8828 struct vmcs12 *vmcs12)
8829{
8830 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8831 u64 address;
8832 bool accessed_dirty;
8833 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8834
8835 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8836 !nested_cpu_has_ept(vmcs12))
8837 return 1;
8838
8839 if (index >= VMFUNC_EPTP_ENTRIES)
8840 return 1;
8841
8842
8843 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8844 &address, index * 8, 8))
8845 return 1;
8846
David Hildenbrandbb97a012017-08-10 23:15:28 +02008847 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008848
8849 /*
8850 * If the (L2) guest does a vmfunc to the currently
8851 * active ept pointer, we don't have to do anything else
8852 */
8853 if (vmcs12->ept_pointer != address) {
8854 if (!valid_ept_address(vcpu, address))
8855 return 1;
8856
8857 kvm_mmu_unload(vcpu);
8858 mmu->ept_ad = accessed_dirty;
8859 mmu->base_role.ad_disabled = !accessed_dirty;
8860 vmcs12->ept_pointer = address;
8861 /*
8862 * TODO: Check what's the correct approach in case
8863 * mmu reload fails. Currently, we just let the next
8864 * reload potentially fail
8865 */
8866 kvm_mmu_reload(vcpu);
8867 }
8868
8869 return 0;
8870}
8871
Bandan Das2a499e42017-08-03 15:54:41 -04008872static int handle_vmfunc(struct kvm_vcpu *vcpu)
8873{
Bandan Das27c42a12017-08-03 15:54:42 -04008874 struct vcpu_vmx *vmx = to_vmx(vcpu);
8875 struct vmcs12 *vmcs12;
8876 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8877
8878 /*
8879 * VMFUNC is only supported for nested guests, but we always enable the
8880 * secondary control for simplicity; for non-nested mode, fake that we
8881 * didn't by injecting #UD.
8882 */
8883 if (!is_guest_mode(vcpu)) {
8884 kvm_queue_exception(vcpu, UD_VECTOR);
8885 return 1;
8886 }
8887
8888 vmcs12 = get_vmcs12(vcpu);
8889 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8890 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008891
8892 switch (function) {
8893 case 0:
8894 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8895 goto fail;
8896 break;
8897 default:
8898 goto fail;
8899 }
8900 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008901
8902fail:
8903 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8904 vmcs_read32(VM_EXIT_INTR_INFO),
8905 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008906 return 1;
8907}
8908
Nadav Har'El0140cae2011-05-25 23:06:28 +03008909/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008910 * The exit handlers return 1 if the exit was handled fully and guest execution
8911 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8912 * to be done to userspace and return 0.
8913 */
Mathias Krause772e0312012-08-30 01:30:19 +02008914static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008915 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8916 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008917 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008918 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008919 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008920 [EXIT_REASON_CR_ACCESS] = handle_cr,
8921 [EXIT_REASON_DR_ACCESS] = handle_dr,
8922 [EXIT_REASON_CPUID] = handle_cpuid,
8923 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8924 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8925 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8926 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008927 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008928 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008929 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008930 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008931 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008932 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008933 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008934 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008935 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008936 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008937 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008938 [EXIT_REASON_VMOFF] = handle_vmoff,
8939 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008940 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8941 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008942 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008943 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008944 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008945 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008946 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008947 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008948 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8949 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008950 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8951 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008952 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008953 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008954 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008955 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008956 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008957 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008958 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008959 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008960 [EXIT_REASON_XSAVES] = handle_xsaves,
8961 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008962 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008963 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008964 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008965};
8966
8967static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008968 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008969
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008970static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8971 struct vmcs12 *vmcs12)
8972{
8973 unsigned long exit_qualification;
8974 gpa_t bitmap, last_bitmap;
8975 unsigned int port;
8976 int size;
8977 u8 b;
8978
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008979 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008980 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008981
8982 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8983
8984 port = exit_qualification >> 16;
8985 size = (exit_qualification & 7) + 1;
8986
8987 last_bitmap = (gpa_t)-1;
8988 b = -1;
8989
8990 while (size > 0) {
8991 if (port < 0x8000)
8992 bitmap = vmcs12->io_bitmap_a;
8993 else if (port < 0x10000)
8994 bitmap = vmcs12->io_bitmap_b;
8995 else
Joe Perches1d804d02015-03-30 16:46:09 -07008996 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008997 bitmap += (port & 0x7fff) / 8;
8998
8999 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009000 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009001 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009002 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009003 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009004
9005 port++;
9006 size--;
9007 last_bitmap = bitmap;
9008 }
9009
Joe Perches1d804d02015-03-30 16:46:09 -07009010 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009011}
9012
Nadav Har'El644d7112011-05-25 23:12:35 +03009013/*
9014 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9015 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9016 * disinterest in the current event (read or write a specific MSR) by using an
9017 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9018 */
9019static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9020 struct vmcs12 *vmcs12, u32 exit_reason)
9021{
9022 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9023 gpa_t bitmap;
9024
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009025 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009026 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009027
9028 /*
9029 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9030 * for the four combinations of read/write and low/high MSR numbers.
9031 * First we need to figure out which of the four to use:
9032 */
9033 bitmap = vmcs12->msr_bitmap;
9034 if (exit_reason == EXIT_REASON_MSR_WRITE)
9035 bitmap += 2048;
9036 if (msr_index >= 0xc0000000) {
9037 msr_index -= 0xc0000000;
9038 bitmap += 1024;
9039 }
9040
9041 /* Then read the msr_index'th bit from this bitmap: */
9042 if (msr_index < 1024*8) {
9043 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009044 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009045 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009046 return 1 & (b >> (msr_index & 7));
9047 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009048 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009049}
9050
9051/*
9052 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9053 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9054 * intercept (via guest_host_mask etc.) the current event.
9055 */
9056static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9057 struct vmcs12 *vmcs12)
9058{
9059 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9060 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009061 int reg;
9062 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009063
9064 switch ((exit_qualification >> 4) & 3) {
9065 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009066 reg = (exit_qualification >> 8) & 15;
9067 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009068 switch (cr) {
9069 case 0:
9070 if (vmcs12->cr0_guest_host_mask &
9071 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009072 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009073 break;
9074 case 3:
9075 if ((vmcs12->cr3_target_count >= 1 &&
9076 vmcs12->cr3_target_value0 == val) ||
9077 (vmcs12->cr3_target_count >= 2 &&
9078 vmcs12->cr3_target_value1 == val) ||
9079 (vmcs12->cr3_target_count >= 3 &&
9080 vmcs12->cr3_target_value2 == val) ||
9081 (vmcs12->cr3_target_count >= 4 &&
9082 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009083 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009084 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009085 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009086 break;
9087 case 4:
9088 if (vmcs12->cr4_guest_host_mask &
9089 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009090 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009091 break;
9092 case 8:
9093 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009094 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009095 break;
9096 }
9097 break;
9098 case 2: /* clts */
9099 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9100 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009101 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009102 break;
9103 case 1: /* mov from cr */
9104 switch (cr) {
9105 case 3:
9106 if (vmcs12->cpu_based_vm_exec_control &
9107 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009108 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009109 break;
9110 case 8:
9111 if (vmcs12->cpu_based_vm_exec_control &
9112 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009113 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009114 break;
9115 }
9116 break;
9117 case 3: /* lmsw */
9118 /*
9119 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9120 * cr0. Other attempted changes are ignored, with no exit.
9121 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009122 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009123 if (vmcs12->cr0_guest_host_mask & 0xe &
9124 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009125 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009126 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9127 !(vmcs12->cr0_read_shadow & 0x1) &&
9128 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009129 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009130 break;
9131 }
Joe Perches1d804d02015-03-30 16:46:09 -07009132 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009133}
9134
Liran Alona7cde482018-06-23 02:35:10 +03009135static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9136 struct vmcs12 *vmcs12, gpa_t bitmap)
9137{
9138 u32 vmx_instruction_info;
9139 unsigned long field;
9140 u8 b;
9141
9142 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9143 return true;
9144
9145 /* Decode instruction info and find the field to access */
9146 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9147 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9148
9149 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9150 if (field >> 15)
9151 return true;
9152
9153 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9154 return true;
9155
9156 return 1 & (b >> (field & 7));
9157}
9158
Nadav Har'El644d7112011-05-25 23:12:35 +03009159/*
9160 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9161 * should handle it ourselves in L0 (and then continue L2). Only call this
9162 * when in is_guest_mode (L2).
9163 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009164static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009165{
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9167 struct vcpu_vmx *vmx = to_vmx(vcpu);
9168 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9169
Jim Mattson4f350c62017-09-14 16:31:44 -07009170 if (vmx->nested.nested_run_pending)
9171 return false;
9172
9173 if (unlikely(vmx->fail)) {
9174 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9175 vmcs_read32(VM_INSTRUCTION_ERROR));
9176 return true;
9177 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009178
David Matlackc9f04402017-08-01 14:00:40 -07009179 /*
9180 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009181 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9182 * Page). The CPU may write to these pages via their host
9183 * physical address while L2 is running, bypassing any
9184 * address-translation-based dirty tracking (e.g. EPT write
9185 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009186 *
9187 * Mark them dirty on every exit from L2 to prevent them from
9188 * getting out of sync with dirty tracking.
9189 */
9190 nested_mark_vmcs12_pages_dirty(vcpu);
9191
Jim Mattson4f350c62017-09-14 16:31:44 -07009192 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9193 vmcs_readl(EXIT_QUALIFICATION),
9194 vmx->idt_vectoring_info,
9195 intr_info,
9196 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9197 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009198
9199 switch (exit_reason) {
9200 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009201 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009202 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009203 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009204 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009205 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009206 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009207 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009208 else if (is_debug(intr_info) &&
9209 vcpu->guest_debug &
9210 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9211 return false;
9212 else if (is_breakpoint(intr_info) &&
9213 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9214 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009215 return vmcs12->exception_bitmap &
9216 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9217 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009218 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009219 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009220 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009221 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009222 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009223 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009224 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009225 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009226 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009227 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009228 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009229 case EXIT_REASON_HLT:
9230 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9231 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009232 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009233 case EXIT_REASON_INVLPG:
9234 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9235 case EXIT_REASON_RDPMC:
9236 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009237 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009238 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009239 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009240 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009241 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009242 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009243 case EXIT_REASON_VMREAD:
9244 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9245 vmcs12->vmread_bitmap);
9246 case EXIT_REASON_VMWRITE:
9247 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9248 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009249 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9250 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009251 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009252 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009253 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009254 /*
9255 * VMX instructions trap unconditionally. This allows L1 to
9256 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9257 */
Joe Perches1d804d02015-03-30 16:46:09 -07009258 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009259 case EXIT_REASON_CR_ACCESS:
9260 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9261 case EXIT_REASON_DR_ACCESS:
9262 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9263 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009264 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009265 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9266 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009267 case EXIT_REASON_MSR_READ:
9268 case EXIT_REASON_MSR_WRITE:
9269 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9270 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009271 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009272 case EXIT_REASON_MWAIT_INSTRUCTION:
9273 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009274 case EXIT_REASON_MONITOR_TRAP_FLAG:
9275 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009276 case EXIT_REASON_MONITOR_INSTRUCTION:
9277 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9278 case EXIT_REASON_PAUSE_INSTRUCTION:
9279 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9280 nested_cpu_has2(vmcs12,
9281 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9282 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009283 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009284 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009285 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009286 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009287 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009288 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009289 /*
9290 * The controls for "virtualize APIC accesses," "APIC-
9291 * register virtualization," and "virtual-interrupt
9292 * delivery" only come from vmcs12.
9293 */
Joe Perches1d804d02015-03-30 16:46:09 -07009294 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009295 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009296 /*
9297 * L0 always deals with the EPT violation. If nested EPT is
9298 * used, and the nested mmu code discovers that the address is
9299 * missing in the guest EPT table (EPT12), the EPT violation
9300 * will be injected with nested_ept_inject_page_fault()
9301 */
Joe Perches1d804d02015-03-30 16:46:09 -07009302 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009303 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009304 /*
9305 * L2 never uses directly L1's EPT, but rather L0's own EPT
9306 * table (shadow on EPT) or a merged EPT table that L0 built
9307 * (EPT on EPT). So any problems with the structure of the
9308 * table is L0's fault.
9309 */
Joe Perches1d804d02015-03-30 16:46:09 -07009310 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009311 case EXIT_REASON_INVPCID:
9312 return
9313 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9314 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009315 case EXIT_REASON_WBINVD:
9316 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9317 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009318 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009319 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9320 /*
9321 * This should never happen, since it is not possible to
9322 * set XSS to a non-zero value---neither in L1 nor in L2.
9323 * If if it were, XSS would have to be checked against
9324 * the XSS exit bitmap in vmcs12.
9325 */
9326 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009327 case EXIT_REASON_PREEMPTION_TIMER:
9328 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009329 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009330 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009331 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009332 case EXIT_REASON_VMFUNC:
9333 /* VM functions are emulated through L2->L0 vmexits. */
9334 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009335 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009336 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009337 }
9338}
9339
Paolo Bonzini7313c692017-07-27 10:31:25 +02009340static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9341{
9342 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9343
9344 /*
9345 * At this point, the exit interruption info in exit_intr_info
9346 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9347 * we need to query the in-kernel LAPIC.
9348 */
9349 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9350 if ((exit_intr_info &
9351 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9352 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9353 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9354 vmcs12->vm_exit_intr_error_code =
9355 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9356 }
9357
9358 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9359 vmcs_readl(EXIT_QUALIFICATION));
9360 return 1;
9361}
9362
Avi Kivity586f9602010-11-18 13:09:54 +02009363static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9364{
9365 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9366 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9367}
9368
Kai Huanga3eaa862015-11-04 13:46:05 +08009369static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009370{
Kai Huanga3eaa862015-11-04 13:46:05 +08009371 if (vmx->pml_pg) {
9372 __free_page(vmx->pml_pg);
9373 vmx->pml_pg = NULL;
9374 }
Kai Huang843e4332015-01-28 10:54:28 +08009375}
9376
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009377static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009378{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009379 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009380 u64 *pml_buf;
9381 u16 pml_idx;
9382
9383 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9384
9385 /* Do nothing if PML buffer is empty */
9386 if (pml_idx == (PML_ENTITY_NUM - 1))
9387 return;
9388
9389 /* PML index always points to next available PML buffer entity */
9390 if (pml_idx >= PML_ENTITY_NUM)
9391 pml_idx = 0;
9392 else
9393 pml_idx++;
9394
9395 pml_buf = page_address(vmx->pml_pg);
9396 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9397 u64 gpa;
9398
9399 gpa = pml_buf[pml_idx];
9400 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009401 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009402 }
9403
9404 /* reset PML index */
9405 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9406}
9407
9408/*
9409 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9410 * Called before reporting dirty_bitmap to userspace.
9411 */
9412static void kvm_flush_pml_buffers(struct kvm *kvm)
9413{
9414 int i;
9415 struct kvm_vcpu *vcpu;
9416 /*
9417 * We only need to kick vcpu out of guest mode here, as PML buffer
9418 * is flushed at beginning of all VMEXITs, and it's obvious that only
9419 * vcpus running in guest are possible to have unflushed GPAs in PML
9420 * buffer.
9421 */
9422 kvm_for_each_vcpu(i, vcpu, kvm)
9423 kvm_vcpu_kick(vcpu);
9424}
9425
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009426static void vmx_dump_sel(char *name, uint32_t sel)
9427{
9428 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009429 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009430 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9431 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9432 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9433}
9434
9435static void vmx_dump_dtsel(char *name, uint32_t limit)
9436{
9437 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9438 name, vmcs_read32(limit),
9439 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9440}
9441
9442static void dump_vmcs(void)
9443{
9444 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9445 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9446 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9447 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9448 u32 secondary_exec_control = 0;
9449 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009450 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009451 int i, n;
9452
9453 if (cpu_has_secondary_exec_ctrls())
9454 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9455
9456 pr_err("*** Guest State ***\n");
9457 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9458 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9459 vmcs_readl(CR0_GUEST_HOST_MASK));
9460 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9461 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9462 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9463 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9464 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9465 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009466 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9467 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9468 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9469 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009470 }
9471 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9472 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9473 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9474 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9475 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9476 vmcs_readl(GUEST_SYSENTER_ESP),
9477 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9478 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9479 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9480 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9481 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9482 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9483 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9484 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9485 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9486 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9487 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9488 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9489 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009490 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9491 efer, vmcs_read64(GUEST_IA32_PAT));
9492 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9493 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009494 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009495 if (cpu_has_load_perf_global_ctrl &&
9496 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009497 pr_err("PerfGlobCtl = 0x%016llx\n",
9498 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009499 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009500 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009501 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9502 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9503 vmcs_read32(GUEST_ACTIVITY_STATE));
9504 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9505 pr_err("InterruptStatus = %04x\n",
9506 vmcs_read16(GUEST_INTR_STATUS));
9507
9508 pr_err("*** Host State ***\n");
9509 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9510 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9511 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9512 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9513 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9514 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9515 vmcs_read16(HOST_TR_SELECTOR));
9516 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9517 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9518 vmcs_readl(HOST_TR_BASE));
9519 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9520 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9521 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9522 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9523 vmcs_readl(HOST_CR4));
9524 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9525 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9526 vmcs_read32(HOST_IA32_SYSENTER_CS),
9527 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9528 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009529 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9530 vmcs_read64(HOST_IA32_EFER),
9531 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009532 if (cpu_has_load_perf_global_ctrl &&
9533 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009534 pr_err("PerfGlobCtl = 0x%016llx\n",
9535 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009536
9537 pr_err("*** Control State ***\n");
9538 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9539 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9540 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9541 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9542 vmcs_read32(EXCEPTION_BITMAP),
9543 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9544 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9545 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9546 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9547 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9548 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9549 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9550 vmcs_read32(VM_EXIT_INTR_INFO),
9551 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9552 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9553 pr_err(" reason=%08x qualification=%016lx\n",
9554 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9555 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9556 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9557 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009558 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009559 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009560 pr_err("TSC Multiplier = 0x%016llx\n",
9561 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009562 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9563 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9564 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9565 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9566 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009567 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009568 n = vmcs_read32(CR3_TARGET_COUNT);
9569 for (i = 0; i + 1 < n; i += 4)
9570 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9571 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9572 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9573 if (i < n)
9574 pr_err("CR3 target%u=%016lx\n",
9575 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9576 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9577 pr_err("PLE Gap=%08x Window=%08x\n",
9578 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9579 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9580 pr_err("Virtual processor ID = 0x%04x\n",
9581 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9582}
9583
Avi Kivity6aa8b732006-12-10 02:21:36 -08009584/*
9585 * The guest has exited. See if we can fix it or if we need userspace
9586 * assistance.
9587 */
Avi Kivity851ba692009-08-24 11:10:17 +03009588static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009589{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009591 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009592 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009593
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009594 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9595
Kai Huang843e4332015-01-28 10:54:28 +08009596 /*
9597 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9598 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9599 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9600 * mode as if vcpus is in root mode, the PML buffer must has been
9601 * flushed already.
9602 */
9603 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009604 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009605
Mohammed Gamal80ced182009-09-01 12:48:18 +02009606 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009607 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009608 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009609
Paolo Bonzini7313c692017-07-27 10:31:25 +02009610 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9611 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009612
Mohammed Gamal51207022010-05-31 22:40:54 +03009613 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009614 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009615 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9616 vcpu->run->fail_entry.hardware_entry_failure_reason
9617 = exit_reason;
9618 return 0;
9619 }
9620
Avi Kivity29bd8a72007-09-10 17:27:03 +03009621 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009622 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9623 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009624 = vmcs_read32(VM_INSTRUCTION_ERROR);
9625 return 0;
9626 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009627
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009628 /*
9629 * Note:
9630 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9631 * delivery event since it indicates guest is accessing MMIO.
9632 * The vm-exit can be triggered again after return to guest that
9633 * will cause infinite loop.
9634 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009635 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009636 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009637 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009638 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009639 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9640 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9641 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009642 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009643 vcpu->run->internal.data[0] = vectoring_info;
9644 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009645 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9646 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9647 vcpu->run->internal.ndata++;
9648 vcpu->run->internal.data[3] =
9649 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9650 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009651 return 0;
9652 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009653
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009654 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009655 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9656 if (vmx_interrupt_allowed(vcpu)) {
9657 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9658 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9659 vcpu->arch.nmi_pending) {
9660 /*
9661 * This CPU don't support us in finding the end of an
9662 * NMI-blocked window if the guest runs with IRQs
9663 * disabled. So we pull the trigger after 1 s of
9664 * futile waiting, but inform the user about this.
9665 */
9666 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9667 "state on VCPU %d after 1 s timeout\n",
9668 __func__, vcpu->vcpu_id);
9669 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9670 }
9671 }
9672
Avi Kivity6aa8b732006-12-10 02:21:36 -08009673 if (exit_reason < kvm_vmx_max_exit_handlers
9674 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009675 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009676 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009677 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9678 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009679 kvm_queue_exception(vcpu, UD_VECTOR);
9680 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009681 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009682}
9683
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009684static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009685{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009686 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9687
9688 if (is_guest_mode(vcpu) &&
9689 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9690 return;
9691
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009692 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009693 vmcs_write32(TPR_THRESHOLD, 0);
9694 return;
9695 }
9696
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009697 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009698}
9699
Jim Mattson8d860bb2018-05-09 16:56:05 -04009700static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009701{
9702 u32 sec_exec_control;
9703
Jim Mattson8d860bb2018-05-09 16:56:05 -04009704 if (!lapic_in_kernel(vcpu))
9705 return;
9706
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009707 /* Postpone execution until vmcs01 is the current VMCS. */
9708 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009709 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009710 return;
9711 }
9712
Paolo Bonzini35754c92015-07-29 12:05:37 +02009713 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009714 return;
9715
9716 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009717 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9718 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009719
Jim Mattson8d860bb2018-05-09 16:56:05 -04009720 switch (kvm_get_apic_mode(vcpu)) {
9721 case LAPIC_MODE_INVALID:
9722 WARN_ONCE(true, "Invalid local APIC state");
9723 case LAPIC_MODE_DISABLED:
9724 break;
9725 case LAPIC_MODE_XAPIC:
9726 if (flexpriority_enabled) {
9727 sec_exec_control |=
9728 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9729 vmx_flush_tlb(vcpu, true);
9730 }
9731 break;
9732 case LAPIC_MODE_X2APIC:
9733 if (cpu_has_vmx_virtualize_x2apic_mode())
9734 sec_exec_control |=
9735 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9736 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009737 }
9738 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9739
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009740 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009741}
9742
Tang Chen38b99172014-09-24 15:57:54 +08009743static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9744{
Jim Mattsonab5df312018-05-09 17:02:03 -04009745 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009746 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009747 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009748 }
Tang Chen38b99172014-09-24 15:57:54 +08009749}
9750
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009751static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009752{
9753 u16 status;
9754 u8 old;
9755
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009756 if (max_isr == -1)
9757 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009758
9759 status = vmcs_read16(GUEST_INTR_STATUS);
9760 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009761 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009762 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009763 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009764 vmcs_write16(GUEST_INTR_STATUS, status);
9765 }
9766}
9767
9768static void vmx_set_rvi(int vector)
9769{
9770 u16 status;
9771 u8 old;
9772
Wei Wang4114c272014-11-05 10:53:43 +08009773 if (vector == -1)
9774 vector = 0;
9775
Yang Zhangc7c9c562013-01-25 10:18:51 +08009776 status = vmcs_read16(GUEST_INTR_STATUS);
9777 old = (u8)status & 0xff;
9778 if ((u8)vector != old) {
9779 status &= ~0xff;
9780 status |= (u8)vector;
9781 vmcs_write16(GUEST_INTR_STATUS, status);
9782 }
9783}
9784
9785static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9786{
Liran Alon851c1a182017-12-24 18:12:56 +02009787 /*
9788 * When running L2, updating RVI is only relevant when
9789 * vmcs12 virtual-interrupt-delivery enabled.
9790 * However, it can be enabled only when L1 also
9791 * intercepts external-interrupts and in that case
9792 * we should not update vmcs02 RVI but instead intercept
9793 * interrupt. Therefore, do nothing when running L2.
9794 */
9795 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009796 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009797}
9798
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009799static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009800{
9801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009802 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009803 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009804
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009805 WARN_ON(!vcpu->arch.apicv_active);
9806 if (pi_test_on(&vmx->pi_desc)) {
9807 pi_clear_on(&vmx->pi_desc);
9808 /*
9809 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9810 * But on x86 this is just a compiler barrier anyway.
9811 */
9812 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009813 max_irr_updated =
9814 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9815
9816 /*
9817 * If we are running L2 and L1 has a new pending interrupt
9818 * which can be injected, we should re-evaluate
9819 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009820 * If L1 intercepts external-interrupts, we should
9821 * exit from L2 to L1. Otherwise, interrupt should be
9822 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009823 */
Liran Alon851c1a182017-12-24 18:12:56 +02009824 if (is_guest_mode(vcpu) && max_irr_updated) {
9825 if (nested_exit_on_intr(vcpu))
9826 kvm_vcpu_exiting_guest_mode(vcpu);
9827 else
9828 kvm_make_request(KVM_REQ_EVENT, vcpu);
9829 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009830 } else {
9831 max_irr = kvm_lapic_find_highest_irr(vcpu);
9832 }
9833 vmx_hwapic_irr_update(vcpu, max_irr);
9834 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009835}
9836
Andrey Smetanin63086302015-11-10 15:36:32 +03009837static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009838{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009839 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009840 return;
9841
Yang Zhangc7c9c562013-01-25 10:18:51 +08009842 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9843 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9844 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9845 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9846}
9847
Paolo Bonzini967235d2016-12-19 14:03:45 +01009848static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9849{
9850 struct vcpu_vmx *vmx = to_vmx(vcpu);
9851
9852 pi_clear_on(&vmx->pi_desc);
9853 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9854}
9855
Avi Kivity51aa01d2010-07-20 14:31:20 +03009856static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009857{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009858 u32 exit_intr_info = 0;
9859 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009860
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009861 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9862 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009863 return;
9864
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009865 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9866 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9867 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009868
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009869 /* if exit due to PF check for async PF */
9870 if (is_page_fault(exit_intr_info))
9871 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9872
Andi Kleena0861c02009-06-08 17:37:09 +08009873 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009874 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9875 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009876 kvm_machine_check();
9877
Gleb Natapov20f65982009-05-11 13:35:55 +03009878 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009879 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009880 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009881 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009882 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009883 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009884}
Gleb Natapov20f65982009-05-11 13:35:55 +03009885
Yang Zhanga547c6d2013-04-11 19:25:10 +08009886static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9887{
9888 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9889
Yang Zhanga547c6d2013-04-11 19:25:10 +08009890 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9891 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9892 unsigned int vector;
9893 unsigned long entry;
9894 gate_desc *desc;
9895 struct vcpu_vmx *vmx = to_vmx(vcpu);
9896#ifdef CONFIG_X86_64
9897 unsigned long tmp;
9898#endif
9899
9900 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9901 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009902 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009903 asm volatile(
9904#ifdef CONFIG_X86_64
9905 "mov %%" _ASM_SP ", %[sp]\n\t"
9906 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9907 "push $%c[ss]\n\t"
9908 "push %[sp]\n\t"
9909#endif
9910 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009911 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009912 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009913 :
9914#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009915 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009916#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009917 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009918 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009919 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009920 [ss]"i"(__KERNEL_DS),
9921 [cs]"i"(__KERNEL_CS)
9922 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009923 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009924}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009925STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009926
Tom Lendackybc226f02018-05-10 22:06:39 +02009927static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009928{
Tom Lendackybc226f02018-05-10 22:06:39 +02009929 switch (index) {
9930 case MSR_IA32_SMBASE:
9931 /*
9932 * We cannot do SMM unless we can run the guest in big
9933 * real mode.
9934 */
9935 return enable_unrestricted_guest || emulate_invalid_guest_state;
9936 case MSR_AMD64_VIRT_SPEC_CTRL:
9937 /* This is AMD only. */
9938 return false;
9939 default:
9940 return true;
9941 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009942}
9943
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009944static bool vmx_mpx_supported(void)
9945{
9946 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9947 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9948}
9949
Wanpeng Li55412b22014-12-02 19:21:30 +08009950static bool vmx_xsaves_supported(void)
9951{
9952 return vmcs_config.cpu_based_2nd_exec_ctrl &
9953 SECONDARY_EXEC_XSAVES;
9954}
9955
Avi Kivity51aa01d2010-07-20 14:31:20 +03009956static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9957{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009958 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009959 bool unblock_nmi;
9960 u8 vector;
9961 bool idtv_info_valid;
9962
9963 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009964
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009965 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009966 if (vmx->loaded_vmcs->nmi_known_unmasked)
9967 return;
9968 /*
9969 * Can't use vmx->exit_intr_info since we're not sure what
9970 * the exit reason is.
9971 */
9972 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9973 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9974 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9975 /*
9976 * SDM 3: 27.7.1.2 (September 2008)
9977 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9978 * a guest IRET fault.
9979 * SDM 3: 23.2.2 (September 2008)
9980 * Bit 12 is undefined in any of the following cases:
9981 * If the VM exit sets the valid bit in the IDT-vectoring
9982 * information field.
9983 * If the VM exit is due to a double fault.
9984 */
9985 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9986 vector != DF_VECTOR && !idtv_info_valid)
9987 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9988 GUEST_INTR_STATE_NMI);
9989 else
9990 vmx->loaded_vmcs->nmi_known_unmasked =
9991 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9992 & GUEST_INTR_STATE_NMI);
9993 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9994 vmx->loaded_vmcs->vnmi_blocked_time +=
9995 ktime_to_ns(ktime_sub(ktime_get(),
9996 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009997}
9998
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009999static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010000 u32 idt_vectoring_info,
10001 int instr_len_field,
10002 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010003{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010004 u8 vector;
10005 int type;
10006 bool idtv_info_valid;
10007
10008 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010009
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010010 vcpu->arch.nmi_injected = false;
10011 kvm_clear_exception_queue(vcpu);
10012 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010013
10014 if (!idtv_info_valid)
10015 return;
10016
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010017 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010018
Avi Kivity668f6122008-07-02 09:28:55 +030010019 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10020 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010021
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010022 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010023 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010024 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010025 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010026 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010027 * Clear bit "block by NMI" before VM entry if a NMI
10028 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010029 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010030 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010031 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010032 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010033 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010034 /* fall through */
10035 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010036 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010037 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010038 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010039 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010040 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010041 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010042 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010043 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010044 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010045 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010046 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010047 break;
10048 default:
10049 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010050 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010051}
10052
Avi Kivity83422e12010-07-20 14:43:23 +030010053static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10054{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010055 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010056 VM_EXIT_INSTRUCTION_LEN,
10057 IDT_VECTORING_ERROR_CODE);
10058}
10059
Avi Kivityb463a6f2010-07-20 15:06:17 +030010060static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10061{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010062 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010063 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10064 VM_ENTRY_INSTRUCTION_LEN,
10065 VM_ENTRY_EXCEPTION_ERROR_CODE);
10066
10067 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10068}
10069
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010070static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10071{
10072 int i, nr_msrs;
10073 struct perf_guest_switch_msr *msrs;
10074
10075 msrs = perf_guest_get_msrs(&nr_msrs);
10076
10077 if (!msrs)
10078 return;
10079
10080 for (i = 0; i < nr_msrs; i++)
10081 if (msrs[i].host == msrs[i].guest)
10082 clear_atomic_switch_msr(vmx, msrs[i].msr);
10083 else
10084 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10085 msrs[i].host);
10086}
10087
Jiang Biao33365e72016-11-03 15:03:37 +080010088static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010089{
10090 struct vcpu_vmx *vmx = to_vmx(vcpu);
10091 u64 tscl;
10092 u32 delta_tsc;
10093
10094 if (vmx->hv_deadline_tsc == -1)
10095 return;
10096
10097 tscl = rdtsc();
10098 if (vmx->hv_deadline_tsc > tscl)
10099 /* sure to be 32 bit only because checked on set_hv_timer */
10100 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10101 cpu_preemption_timer_multi);
10102 else
10103 delta_tsc = 0;
10104
10105 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10106}
10107
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010108static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010109{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010110 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010111 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010112
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010113 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010114 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010115 vmx->loaded_vmcs->soft_vnmi_blocked))
10116 vmx->loaded_vmcs->entry_time = ktime_get();
10117
Avi Kivity104f2262010-11-18 13:12:52 +020010118 /* Don't enter VMX if guest state is invalid, let the exit handler
10119 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010120 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010121 return;
10122
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010123 if (vmx->ple_window_dirty) {
10124 vmx->ple_window_dirty = false;
10125 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10126 }
10127
Abel Gordon012f83c2013-04-18 14:39:25 +030010128 if (vmx->nested.sync_shadow_vmcs) {
10129 copy_vmcs12_to_shadow(vmx);
10130 vmx->nested.sync_shadow_vmcs = false;
10131 }
10132
Avi Kivity104f2262010-11-18 13:12:52 +020010133 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10134 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10135 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10136 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10137
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010138 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010139 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010140 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010141 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010142 }
10143
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010144 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010145 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010146 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010147 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010148 }
10149
Avi Kivity104f2262010-11-18 13:12:52 +020010150 /* When single-stepping over STI and MOV SS, we must clear the
10151 * corresponding interruptibility bits in the guest state. Otherwise
10152 * vmentry fails as it then expects bit 14 (BS) in pending debug
10153 * exceptions being set, but that's not correct for the guest debugging
10154 * case. */
10155 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10156 vmx_set_interrupt_shadow(vcpu, 0);
10157
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010158 if (static_cpu_has(X86_FEATURE_PKU) &&
10159 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10160 vcpu->arch.pkru != vmx->host_pkru)
10161 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010162
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010163 atomic_switch_perf_msrs(vmx);
10164
Yunhong Jiang64672c92016-06-13 14:19:59 -070010165 vmx_arm_hv_timer(vcpu);
10166
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010167 /*
10168 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10169 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10170 * is no need to worry about the conditional branch over the wrmsr
10171 * being speculatively taken.
10172 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010173 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010174
Nadav Har'Eld462b812011-05-24 15:26:10 +030010175 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010176
10177 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10178 (unsigned long)&current_evmcs->host_rsp : 0;
10179
Avi Kivity104f2262010-11-18 13:12:52 +020010180 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010181 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010182 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10183 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10184 "push %%" _ASM_CX " \n\t"
10185 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010186 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010187 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010188 /* Avoid VMWRITE when Enlightened VMCS is in use */
10189 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10190 "jz 2f \n\t"
10191 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10192 "jmp 1f \n\t"
10193 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010194 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010195 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010196 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010197 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10198 "mov %%cr2, %%" _ASM_DX " \n\t"
10199 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010200 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010201 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010202 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010203 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010204 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010205 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010206 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10207 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10208 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10209 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10210 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10211 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010212#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010213 "mov %c[r8](%0), %%r8 \n\t"
10214 "mov %c[r9](%0), %%r9 \n\t"
10215 "mov %c[r10](%0), %%r10 \n\t"
10216 "mov %c[r11](%0), %%r11 \n\t"
10217 "mov %c[r12](%0), %%r12 \n\t"
10218 "mov %c[r13](%0), %%r13 \n\t"
10219 "mov %c[r14](%0), %%r14 \n\t"
10220 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010221#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010222 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010223
Avi Kivity6aa8b732006-12-10 02:21:36 -080010224 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010225 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010226 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010227 "jmp 2f \n\t"
10228 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10229 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010230 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010231 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010232 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010233 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010234 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10235 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10236 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10237 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10238 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10239 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10240 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010241#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010242 "mov %%r8, %c[r8](%0) \n\t"
10243 "mov %%r9, %c[r9](%0) \n\t"
10244 "mov %%r10, %c[r10](%0) \n\t"
10245 "mov %%r11, %c[r11](%0) \n\t"
10246 "mov %%r12, %c[r12](%0) \n\t"
10247 "mov %%r13, %c[r13](%0) \n\t"
10248 "mov %%r14, %c[r14](%0) \n\t"
10249 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010250 "xor %%r8d, %%r8d \n\t"
10251 "xor %%r9d, %%r9d \n\t"
10252 "xor %%r10d, %%r10d \n\t"
10253 "xor %%r11d, %%r11d \n\t"
10254 "xor %%r12d, %%r12d \n\t"
10255 "xor %%r13d, %%r13d \n\t"
10256 "xor %%r14d, %%r14d \n\t"
10257 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010258#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010259 "mov %%cr2, %%" _ASM_AX " \n\t"
10260 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010261
Jim Mattson0cb5b302018-01-03 14:31:38 -080010262 "xor %%eax, %%eax \n\t"
10263 "xor %%ebx, %%ebx \n\t"
10264 "xor %%esi, %%esi \n\t"
10265 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010266 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010267 ".pushsection .rodata \n\t"
10268 ".global vmx_return \n\t"
10269 "vmx_return: " _ASM_PTR " 2b \n\t"
10270 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010271 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010272 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010273 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010274 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010275 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10276 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10277 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10278 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10279 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10280 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10281 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010282#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010283 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10284 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10285 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10286 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10287 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10288 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10289 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10290 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010291#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010292 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10293 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010294 : "cc", "memory"
10295#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010296 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010297 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010298#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010299 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010300#endif
10301 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010302
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010303 /*
10304 * We do not use IBRS in the kernel. If this vCPU has used the
10305 * SPEC_CTRL MSR it may have left it on; save the value and
10306 * turn it off. This is much more efficient than blindly adding
10307 * it to the atomic save/restore list. Especially as the former
10308 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10309 *
10310 * For non-nested case:
10311 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10312 * save it.
10313 *
10314 * For nested case:
10315 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10316 * save it.
10317 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010318 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010319 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010320
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010321 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010322
David Woodhouse117cc7a2018-01-12 11:11:27 +000010323 /* Eliminate branch target predictions from guest mode */
10324 vmexit_fill_RSB();
10325
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010326 /* All fields are clean at this point */
10327 if (static_branch_unlikely(&enable_evmcs))
10328 current_evmcs->hv_clean_fields |=
10329 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10330
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010331 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010332 if (vmx->host_debugctlmsr)
10333 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010334
Avi Kivityaa67f602012-08-01 16:48:03 +030010335#ifndef CONFIG_X86_64
10336 /*
10337 * The sysexit path does not restore ds/es, so we must set them to
10338 * a reasonable value ourselves.
10339 *
10340 * We can't defer this to vmx_load_host_state() since that function
10341 * may be executed in interrupt context, which saves and restore segments
10342 * around it, nullifying its effect.
10343 */
10344 loadsegment(ds, __USER_DS);
10345 loadsegment(es, __USER_DS);
10346#endif
10347
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010348 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010349 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010350 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010351 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010352 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010353 vcpu->arch.regs_dirty = 0;
10354
Gleb Natapove0b890d2013-09-25 12:51:33 +030010355 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010356 * eager fpu is enabled if PKEY is supported and CR4 is switched
10357 * back on host, so it is safe to read guest PKRU from current
10358 * XSAVE.
10359 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010360 if (static_cpu_has(X86_FEATURE_PKU) &&
10361 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10362 vcpu->arch.pkru = __read_pkru();
10363 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010364 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010365 }
10366
Gleb Natapove0b890d2013-09-25 12:51:33 +030010367 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010368 vmx->idt_vectoring_info = 0;
10369
10370 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10371 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10372 return;
10373
10374 vmx->loaded_vmcs->launched = 1;
10375 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010376
Avi Kivity51aa01d2010-07-20 14:31:20 +030010377 vmx_complete_atomic_exit(vmx);
10378 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010379 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010380}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010381STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010382
Sean Christopherson434a1e92018-03-20 12:17:18 -070010383static struct kvm *vmx_vm_alloc(void)
10384{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010385 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010386 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010387}
10388
10389static void vmx_vm_free(struct kvm *kvm)
10390{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010391 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010392}
10393
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010394static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010395{
10396 struct vcpu_vmx *vmx = to_vmx(vcpu);
10397 int cpu;
10398
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010399 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010400 return;
10401
10402 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010403 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010404 vmx_vcpu_put(vcpu);
10405 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010406 put_cpu();
10407}
10408
Jim Mattson2f1fe812016-07-08 15:36:06 -070010409/*
10410 * Ensure that the current vmcs of the logical processor is the
10411 * vmcs01 of the vcpu before calling free_nested().
10412 */
10413static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10414{
10415 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010416
Christoffer Dallec7660c2017-12-04 21:35:23 +010010417 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010418 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010419 free_nested(vmx);
10420 vcpu_put(vcpu);
10421}
10422
Avi Kivity6aa8b732006-12-10 02:21:36 -080010423static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10424{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010425 struct vcpu_vmx *vmx = to_vmx(vcpu);
10426
Kai Huang843e4332015-01-28 10:54:28 +080010427 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010428 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010429 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010430 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010431 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010432 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010433 kfree(vmx->guest_msrs);
10434 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010435 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010436}
10437
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010438static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010439{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010440 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010441 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010442 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010443 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010444
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010445 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010446 return ERR_PTR(-ENOMEM);
10447
Wanpeng Li991e7a02015-09-16 17:30:05 +080010448 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010449
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010450 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10451 if (err)
10452 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010453
Peter Feiner4e595162016-07-07 14:49:58 -070010454 err = -ENOMEM;
10455
10456 /*
10457 * If PML is turned on, failure on enabling PML just results in failure
10458 * of creating the vcpu, therefore we can simplify PML logic (by
10459 * avoiding dealing with cases, such as enabling PML partially on vcpus
10460 * for the guest, etc.
10461 */
10462 if (enable_pml) {
10463 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10464 if (!vmx->pml_pg)
10465 goto uninit_vcpu;
10466 }
10467
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010468 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010469 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10470 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010471
Peter Feiner4e595162016-07-07 14:49:58 -070010472 if (!vmx->guest_msrs)
10473 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010474
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010475 err = alloc_loaded_vmcs(&vmx->vmcs01);
10476 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010477 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010478
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010479 msr_bitmap = vmx->vmcs01.msr_bitmap;
10480 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10481 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10482 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10483 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10484 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10485 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10486 vmx->msr_bitmap_mode = 0;
10487
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010488 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010489 cpu = get_cpu();
10490 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010491 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010492 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010493 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010494 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010495 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010496 err = alloc_apic_access_page(kvm);
10497 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010498 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010499 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010500
Sean Christophersone90008d2018-03-05 12:04:37 -080010501 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010502 err = init_rmode_identity_map(kvm);
10503 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010504 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010505 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010506
Wanpeng Li5c614b32015-10-13 09:18:36 -070010507 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010508 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10509 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010510 vmx->nested.vpid02 = allocate_vpid();
10511 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010512
Wincy Van705699a2015-02-03 23:58:17 +080010513 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010514 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010515
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010516 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10517
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010518 /*
10519 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10520 * or POSTED_INTR_WAKEUP_VECTOR.
10521 */
10522 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10523 vmx->pi_desc.sn = 1;
10524
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010525 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010526
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010527free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010528 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010529 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010530free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010531 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010532free_pml:
10533 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010534uninit_vcpu:
10535 kvm_vcpu_uninit(&vmx->vcpu);
10536free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010537 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010538 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010539 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010540}
10541
Wanpeng Lib31c1142018-03-12 04:53:04 -070010542static int vmx_vm_init(struct kvm *kvm)
10543{
10544 if (!ple_gap)
10545 kvm->arch.pause_in_guest = true;
10546 return 0;
10547}
10548
Yang, Sheng002c7f72007-07-31 14:23:01 +030010549static void __init vmx_check_processor_compat(void *rtn)
10550{
10551 struct vmcs_config vmcs_conf;
10552
10553 *(int *)rtn = 0;
10554 if (setup_vmcs_config(&vmcs_conf) < 0)
10555 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010556 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010557 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10558 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10559 smp_processor_id());
10560 *(int *)rtn = -EIO;
10561 }
10562}
10563
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010564static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010565{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010566 u8 cache;
10567 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010568
Sheng Yang522c68c2009-04-27 20:35:43 +080010569 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010570 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010571 * 2. EPT with VT-d:
10572 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010573 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010574 * b. VT-d with snooping control feature: snooping control feature of
10575 * VT-d engine can guarantee the cache correctness. Just set it
10576 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010577 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010578 * consistent with host MTRR
10579 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010580 if (is_mmio) {
10581 cache = MTRR_TYPE_UNCACHABLE;
10582 goto exit;
10583 }
10584
10585 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010586 ipat = VMX_EPT_IPAT_BIT;
10587 cache = MTRR_TYPE_WRBACK;
10588 goto exit;
10589 }
10590
10591 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10592 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010593 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010594 cache = MTRR_TYPE_WRBACK;
10595 else
10596 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010597 goto exit;
10598 }
10599
Xiao Guangrongff536042015-06-15 16:55:22 +080010600 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010601
10602exit:
10603 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010604}
10605
Sheng Yang17cc3932010-01-05 19:02:27 +080010606static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010607{
Sheng Yang878403b2010-01-05 19:02:29 +080010608 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10609 return PT_DIRECTORY_LEVEL;
10610 else
10611 /* For shadow and EPT supported 1GB page */
10612 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010613}
10614
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010615static void vmcs_set_secondary_exec_control(u32 new_ctl)
10616{
10617 /*
10618 * These bits in the secondary execution controls field
10619 * are dynamic, the others are mostly based on the hypervisor
10620 * architecture and the guest's CPUID. Do not touch the
10621 * dynamic bits.
10622 */
10623 u32 mask =
10624 SECONDARY_EXEC_SHADOW_VMCS |
10625 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010626 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10627 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010628
10629 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10630
10631 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10632 (new_ctl & ~mask) | (cur_ctl & mask));
10633}
10634
David Matlack8322ebb2016-11-29 18:14:09 -080010635/*
10636 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10637 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10638 */
10639static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10640{
10641 struct vcpu_vmx *vmx = to_vmx(vcpu);
10642 struct kvm_cpuid_entry2 *entry;
10643
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010644 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10645 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010646
10647#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10648 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010649 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010650} while (0)
10651
10652 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10653 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10654 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10655 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10656 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10657 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10658 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10659 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10660 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10661 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10662 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10663 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10664 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10665 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10666 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10667
10668 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10669 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10670 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10671 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10672 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010673 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010674
10675#undef cr4_fixed1_update
10676}
10677
Sheng Yang0e851882009-12-18 16:48:46 +080010678static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10679{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010681
Paolo Bonzini80154d72017-08-24 13:55:35 +020010682 if (cpu_has_secondary_exec_ctrls()) {
10683 vmx_compute_secondary_exec_control(vmx);
10684 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010685 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010686
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010687 if (nested_vmx_allowed(vcpu))
10688 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10689 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10690 else
10691 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10692 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010693
10694 if (nested_vmx_allowed(vcpu))
10695 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010696}
10697
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010698static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10699{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010700 if (func == 1 && nested)
10701 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010702}
10703
Yang Zhang25d92082013-08-06 12:00:32 +030010704static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10705 struct x86_exception *fault)
10706{
Jan Kiszka533558b2014-01-04 18:47:20 +010010707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010708 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010709 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010710 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010711
Bandan Dasc5f983f2017-05-05 15:25:14 -040010712 if (vmx->nested.pml_full) {
10713 exit_reason = EXIT_REASON_PML_FULL;
10714 vmx->nested.pml_full = false;
10715 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10716 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010717 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010718 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010719 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010720
10721 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010722 vmcs12->guest_physical_address = fault->address;
10723}
10724
Peter Feiner995f00a2017-06-30 17:26:32 -070010725static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10726{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010727 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010728}
10729
Nadav Har'El155a97a2013-08-05 11:07:16 +030010730/* Callbacks for nested_ept_init_mmu_context: */
10731
10732static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10733{
10734 /* return the page table to be shadowed - in our case, EPT12 */
10735 return get_vmcs12(vcpu)->ept_pointer;
10736}
10737
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010738static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010739{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010740 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010741 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010742 return 1;
10743
10744 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010745 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010746 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010747 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010748 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010749 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10750 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10751 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10752
10753 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010754 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010755}
10756
10757static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10758{
10759 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10760}
10761
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010762static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10763 u16 error_code)
10764{
10765 bool inequality, bit;
10766
10767 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10768 inequality =
10769 (error_code & vmcs12->page_fault_error_code_mask) !=
10770 vmcs12->page_fault_error_code_match;
10771 return inequality ^ bit;
10772}
10773
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010774static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10775 struct x86_exception *fault)
10776{
10777 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10778
10779 WARN_ON(!is_guest_mode(vcpu));
10780
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010781 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10782 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010783 vmcs12->vm_exit_intr_error_code = fault->error_code;
10784 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10785 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10786 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10787 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010788 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010789 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010790 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010791}
10792
Paolo Bonzinic9923842017-12-13 14:16:30 +010010793static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10794 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010795
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010796static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010797{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010798 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010799 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010800 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010801 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010802
10803 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010804 /*
10805 * Translate L1 physical address to host physical
10806 * address for vmcs02. Keep the page pinned, so this
10807 * physical address remains valid. We keep a reference
10808 * to it so we can release it later.
10809 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010810 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010811 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010812 vmx->nested.apic_access_page = NULL;
10813 }
10814 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010815 /*
10816 * If translation failed, no matter: This feature asks
10817 * to exit when accessing the given address, and if it
10818 * can never be accessed, this feature won't do
10819 * anything anyway.
10820 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010821 if (!is_error_page(page)) {
10822 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010823 hpa = page_to_phys(vmx->nested.apic_access_page);
10824 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10825 } else {
10826 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10827 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10828 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010829 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010830
10831 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010832 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010833 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010834 vmx->nested.virtual_apic_page = NULL;
10835 }
10836 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010837
10838 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010839 * If translation failed, VM entry will fail because
10840 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10841 * Failing the vm entry is _not_ what the processor
10842 * does but it's basically the only possibility we
10843 * have. We could still enter the guest if CR8 load
10844 * exits are enabled, CR8 store exits are enabled, and
10845 * virtualize APIC access is disabled; in this case
10846 * the processor would never use the TPR shadow and we
10847 * could simply clear the bit from the execution
10848 * control. But such a configuration is useless, so
10849 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010850 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010851 if (!is_error_page(page)) {
10852 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010853 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10854 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10855 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010856 }
10857
Wincy Van705699a2015-02-03 23:58:17 +080010858 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010859 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10860 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010861 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010862 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010863 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010864 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10865 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010866 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010867 vmx->nested.pi_desc_page = page;
10868 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010869 vmx->nested.pi_desc =
10870 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10871 (unsigned long)(vmcs12->posted_intr_desc_addr &
10872 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010873 vmcs_write64(POSTED_INTR_DESC_ADDR,
10874 page_to_phys(vmx->nested.pi_desc_page) +
10875 (unsigned long)(vmcs12->posted_intr_desc_addr &
10876 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010877 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010878 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010879 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10880 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010881 else
10882 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10883 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010884}
10885
Jan Kiszkaf4124502014-03-07 20:03:13 +010010886static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10887{
10888 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10889 struct vcpu_vmx *vmx = to_vmx(vcpu);
10890
10891 if (vcpu->arch.virtual_tsc_khz == 0)
10892 return;
10893
10894 /* Make sure short timeouts reliably trigger an immediate vmexit.
10895 * hrtimer_start does not guarantee this. */
10896 if (preemption_timeout <= 1) {
10897 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10898 return;
10899 }
10900
10901 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10902 preemption_timeout *= 1000000;
10903 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10904 hrtimer_start(&vmx->nested.preemption_timer,
10905 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10906}
10907
Jim Mattson56a20512017-07-06 16:33:06 -070010908static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10909 struct vmcs12 *vmcs12)
10910{
10911 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10912 return 0;
10913
10914 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10915 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10916 return -EINVAL;
10917
10918 return 0;
10919}
10920
Wincy Van3af18d92015-02-03 23:49:31 +080010921static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10922 struct vmcs12 *vmcs12)
10923{
Wincy Van3af18d92015-02-03 23:49:31 +080010924 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10925 return 0;
10926
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010927 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010928 return -EINVAL;
10929
10930 return 0;
10931}
10932
Jim Mattson712b12d2017-08-24 13:24:47 -070010933static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10934 struct vmcs12 *vmcs12)
10935{
10936 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10937 return 0;
10938
10939 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10940 return -EINVAL;
10941
10942 return 0;
10943}
10944
Wincy Van3af18d92015-02-03 23:49:31 +080010945/*
10946 * Merge L0's and L1's MSR bitmap, return false to indicate that
10947 * we do not use the hardware.
10948 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010949static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10950 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010951{
Wincy Van82f0dd42015-02-03 23:57:18 +080010952 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010953 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010954 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010955 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010956 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010957 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010958 *
10959 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10960 * ensures that we do not accidentally generate an L02 MSR bitmap
10961 * from the L12 MSR bitmap that is too permissive.
10962 * 2. That L1 or L2s have actually used the MSR. This avoids
10963 * unnecessarily merging of the bitmap if the MSR is unused. This
10964 * works properly because we only update the L01 MSR bitmap lazily.
10965 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10966 * updated to reflect this when L1 (or its L2s) actually write to
10967 * the MSR.
10968 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010969 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10970 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010971
Paolo Bonzinic9923842017-12-13 14:16:30 +010010972 /* Nothing to do if the MSR bitmap is not in use. */
10973 if (!cpu_has_vmx_msr_bitmap() ||
10974 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10975 return false;
10976
Ashok Raj15d45072018-02-01 22:59:43 +010010977 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010978 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010979 return false;
10980
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010981 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10982 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010983 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010984
Radim Krčmářd048c092016-08-08 20:16:22 +020010985 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010986 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10987 /*
10988 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10989 * just lets the processor take the value from the virtual-APIC page;
10990 * take those 256 bits directly from the L1 bitmap.
10991 */
10992 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10993 unsigned word = msr / BITS_PER_LONG;
10994 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10995 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010996 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010997 } else {
10998 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10999 unsigned word = msr / BITS_PER_LONG;
11000 msr_bitmap_l0[word] = ~0;
11001 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11002 }
11003 }
11004
11005 nested_vmx_disable_intercept_for_msr(
11006 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011007 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011008 MSR_TYPE_W);
11009
11010 if (nested_cpu_has_vid(vmcs12)) {
11011 nested_vmx_disable_intercept_for_msr(
11012 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011013 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011014 MSR_TYPE_W);
11015 nested_vmx_disable_intercept_for_msr(
11016 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011017 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011018 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011019 }
Ashok Raj15d45072018-02-01 22:59:43 +010011020
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011021 if (spec_ctrl)
11022 nested_vmx_disable_intercept_for_msr(
11023 msr_bitmap_l1, msr_bitmap_l0,
11024 MSR_IA32_SPEC_CTRL,
11025 MSR_TYPE_R | MSR_TYPE_W);
11026
Ashok Raj15d45072018-02-01 22:59:43 +010011027 if (pred_cmd)
11028 nested_vmx_disable_intercept_for_msr(
11029 msr_bitmap_l1, msr_bitmap_l0,
11030 MSR_IA32_PRED_CMD,
11031 MSR_TYPE_W);
11032
Wincy Vanf2b93282015-02-03 23:56:03 +080011033 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011034 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011035
11036 return true;
11037}
11038
Liran Alon61ada742018-06-23 02:35:08 +030011039static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11040 struct vmcs12 *vmcs12)
11041{
11042 struct vmcs12 *shadow;
11043 struct page *page;
11044
11045 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11046 vmcs12->vmcs_link_pointer == -1ull)
11047 return;
11048
11049 shadow = get_shadow_vmcs12(vcpu);
11050 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11051
11052 memcpy(shadow, kmap(page), VMCS12_SIZE);
11053
11054 kunmap(page);
11055 kvm_release_page_clean(page);
11056}
11057
11058static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11059 struct vmcs12 *vmcs12)
11060{
11061 struct vcpu_vmx *vmx = to_vmx(vcpu);
11062
11063 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11064 vmcs12->vmcs_link_pointer == -1ull)
11065 return;
11066
11067 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11068 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11069}
11070
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011071static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11072 struct vmcs12 *vmcs12)
11073{
11074 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11075 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11076 return -EINVAL;
11077 else
11078 return 0;
11079}
11080
Wincy Vanf2b93282015-02-03 23:56:03 +080011081static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11082 struct vmcs12 *vmcs12)
11083{
Wincy Van82f0dd42015-02-03 23:57:18 +080011084 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011085 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011086 !nested_cpu_has_vid(vmcs12) &&
11087 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011088 return 0;
11089
11090 /*
11091 * If virtualize x2apic mode is enabled,
11092 * virtualize apic access must be disabled.
11093 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011094 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11095 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011096 return -EINVAL;
11097
Wincy Van608406e2015-02-03 23:57:51 +080011098 /*
11099 * If virtual interrupt delivery is enabled,
11100 * we must exit on external interrupts.
11101 */
11102 if (nested_cpu_has_vid(vmcs12) &&
11103 !nested_exit_on_intr(vcpu))
11104 return -EINVAL;
11105
Wincy Van705699a2015-02-03 23:58:17 +080011106 /*
11107 * bits 15:8 should be zero in posted_intr_nv,
11108 * the descriptor address has been already checked
11109 * in nested_get_vmcs12_pages.
11110 */
11111 if (nested_cpu_has_posted_intr(vmcs12) &&
11112 (!nested_cpu_has_vid(vmcs12) ||
11113 !nested_exit_intr_ack_set(vcpu) ||
11114 vmcs12->posted_intr_nv & 0xff00))
11115 return -EINVAL;
11116
Wincy Vanf2b93282015-02-03 23:56:03 +080011117 /* tpr shadow is needed by all apicv features. */
11118 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11119 return -EINVAL;
11120
11121 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011122}
11123
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011124static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11125 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011126 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011127{
Liran Alone2536742018-06-23 02:35:02 +030011128 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011129 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011130 u64 count, addr;
11131
Liran Alone2536742018-06-23 02:35:02 +030011132 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11133 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011134 WARN_ON(1);
11135 return -EINVAL;
11136 }
11137 if (count == 0)
11138 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011139 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011140 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11141 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011142 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011143 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11144 addr_field, maxphyaddr, count, addr);
11145 return -EINVAL;
11146 }
11147 return 0;
11148}
11149
11150static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11151 struct vmcs12 *vmcs12)
11152{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011153 if (vmcs12->vm_exit_msr_load_count == 0 &&
11154 vmcs12->vm_exit_msr_store_count == 0 &&
11155 vmcs12->vm_entry_msr_load_count == 0)
11156 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011157 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011158 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011159 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011160 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011161 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011162 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011163 return -EINVAL;
11164 return 0;
11165}
11166
Bandan Dasc5f983f2017-05-05 15:25:14 -040011167static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11168 struct vmcs12 *vmcs12)
11169{
11170 u64 address = vmcs12->pml_address;
11171 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11172
11173 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11174 if (!nested_cpu_has_ept(vmcs12) ||
11175 !IS_ALIGNED(address, 4096) ||
11176 address >> maxphyaddr)
11177 return -EINVAL;
11178 }
11179
11180 return 0;
11181}
11182
Liran Alona8a7c022018-06-23 02:35:06 +030011183static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11184 struct vmcs12 *vmcs12)
11185{
11186 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11187 return 0;
11188
11189 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11190 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11191 return -EINVAL;
11192
11193 return 0;
11194}
11195
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011196static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11197 struct vmx_msr_entry *e)
11198{
11199 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011200 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011201 return -EINVAL;
11202 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11203 e->index == MSR_IA32_UCODE_REV)
11204 return -EINVAL;
11205 if (e->reserved != 0)
11206 return -EINVAL;
11207 return 0;
11208}
11209
11210static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11211 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011212{
11213 if (e->index == MSR_FS_BASE ||
11214 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011215 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11216 nested_vmx_msr_check_common(vcpu, e))
11217 return -EINVAL;
11218 return 0;
11219}
11220
11221static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11222 struct vmx_msr_entry *e)
11223{
11224 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11225 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011226 return -EINVAL;
11227 return 0;
11228}
11229
11230/*
11231 * Load guest's/host's msr at nested entry/exit.
11232 * return 0 for success, entry index for failure.
11233 */
11234static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11235{
11236 u32 i;
11237 struct vmx_msr_entry e;
11238 struct msr_data msr;
11239
11240 msr.host_initiated = false;
11241 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011242 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11243 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011244 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011245 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11246 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011247 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011248 }
11249 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011250 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011251 "%s check failed (%u, 0x%x, 0x%x)\n",
11252 __func__, i, e.index, e.reserved);
11253 goto fail;
11254 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011255 msr.index = e.index;
11256 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011257 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011258 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011259 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11260 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011261 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011262 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011263 }
11264 return 0;
11265fail:
11266 return i + 1;
11267}
11268
11269static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11270{
11271 u32 i;
11272 struct vmx_msr_entry e;
11273
11274 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011275 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011276 if (kvm_vcpu_read_guest(vcpu,
11277 gpa + i * sizeof(e),
11278 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011279 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011280 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11281 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011282 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011283 }
11284 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011285 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011286 "%s check failed (%u, 0x%x, 0x%x)\n",
11287 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011288 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011289 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011290 msr_info.host_initiated = false;
11291 msr_info.index = e.index;
11292 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011293 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011294 "%s cannot read MSR (%u, 0x%x)\n",
11295 __func__, i, e.index);
11296 return -EINVAL;
11297 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011298 if (kvm_vcpu_write_guest(vcpu,
11299 gpa + i * sizeof(e) +
11300 offsetof(struct vmx_msr_entry, value),
11301 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011302 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011303 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011304 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011305 return -EINVAL;
11306 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011307 }
11308 return 0;
11309}
11310
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011311static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11312{
11313 unsigned long invalid_mask;
11314
11315 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11316 return (val & invalid_mask) == 0;
11317}
11318
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011319/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011320 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11321 * emulating VM entry into a guest with EPT enabled.
11322 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11323 * is assigned to entry_failure_code on failure.
11324 */
11325static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011326 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011327{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011328 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011329 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011330 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11331 return 1;
11332 }
11333
11334 /*
11335 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11336 * must not be dereferenced.
11337 */
11338 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11339 !nested_ept) {
11340 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11341 *entry_failure_code = ENTRY_FAIL_PDPTE;
11342 return 1;
11343 }
11344 }
11345
11346 vcpu->arch.cr3 = cr3;
11347 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11348 }
11349
11350 kvm_mmu_reset_context(vcpu);
11351 return 0;
11352}
11353
Jim Mattson6514dc32018-04-26 16:09:12 -070011354static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011355{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011356 struct vcpu_vmx *vmx = to_vmx(vcpu);
11357
11358 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11359 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11360 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11361 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11362 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11363 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11364 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11365 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11366 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11367 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11368 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11369 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11370 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11371 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11372 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11373 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11374 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11375 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11376 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11377 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11378 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11379 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11380 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11381 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11382 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11383 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11384 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11385 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11386 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11387 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11388 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011389
11390 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11391 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11392 vmcs12->guest_pending_dbg_exceptions);
11393 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11394 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11395
11396 if (nested_cpu_has_xsaves(vmcs12))
11397 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11398 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11399
11400 if (cpu_has_vmx_posted_intr())
11401 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11402
11403 /*
11404 * Whether page-faults are trapped is determined by a combination of
11405 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11406 * If enable_ept, L0 doesn't care about page faults and we should
11407 * set all of these to L1's desires. However, if !enable_ept, L0 does
11408 * care about (at least some) page faults, and because it is not easy
11409 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11410 * to exit on each and every L2 page fault. This is done by setting
11411 * MASK=MATCH=0 and (see below) EB.PF=1.
11412 * Note that below we don't need special code to set EB.PF beyond the
11413 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11414 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11415 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11416 */
11417 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11418 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11419 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11420 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11421
11422 /* All VMFUNCs are currently emulated through L0 vmexits. */
11423 if (cpu_has_vmx_vmfunc())
11424 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11425
11426 if (cpu_has_vmx_apicv()) {
11427 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11428 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11429 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11430 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11431 }
11432
11433 /*
11434 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11435 * Some constant fields are set here by vmx_set_constant_host_state().
11436 * Other fields are different per CPU, and will be set later when
11437 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11438 */
11439 vmx_set_constant_host_state(vmx);
11440
11441 /*
11442 * Set the MSR load/store lists to match L0's settings.
11443 */
11444 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11445 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11446 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11447 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11448 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11449
11450 set_cr4_guest_host_mask(vmx);
11451
11452 if (vmx_mpx_supported())
11453 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11454
11455 if (enable_vpid) {
11456 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11457 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11458 else
11459 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11460 }
11461
11462 /*
11463 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11464 */
11465 if (enable_ept) {
11466 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11467 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11468 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11469 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11470 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011471
11472 if (cpu_has_vmx_msr_bitmap())
11473 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011474}
11475
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011476/*
11477 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11478 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011479 * 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 +030011480 * guest in a way that will both be appropriate to L1's requests, and our
11481 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11482 * function also has additional necessary side-effects, like setting various
11483 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011484 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11485 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011486 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011487static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011488 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011489{
11490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011491 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011492
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011493 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011494 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011495 vmx->nested.dirty_vmcs12 = false;
11496 }
11497
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011498 /*
11499 * First, the fields that are shadowed. This must be kept in sync
11500 * with vmx_shadow_fields.h.
11501 */
11502
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011503 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011504 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011505 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011506 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11507 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011508
11509 /*
11510 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11511 * HOST_FS_BASE, HOST_GS_BASE.
11512 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011513
Jim Mattson6514dc32018-04-26 16:09:12 -070011514 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011515 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011516 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11517 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11518 } else {
11519 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11520 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11521 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011522 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011523 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11524 vmcs12->vm_entry_intr_info_field);
11525 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11526 vmcs12->vm_entry_exception_error_code);
11527 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11528 vmcs12->vm_entry_instruction_len);
11529 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11530 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011531 vmx->loaded_vmcs->nmi_known_unmasked =
11532 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011533 } else {
11534 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11535 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011536 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011537
Jan Kiszkaf4124502014-03-07 20:03:13 +010011538 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011539
Paolo Bonzini93140062016-07-06 13:23:51 +020011540 /* Preemption timer setting is only taken from vmcs01. */
11541 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11542 exec_control |= vmcs_config.pin_based_exec_ctrl;
11543 if (vmx->hv_deadline_tsc == -1)
11544 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11545
11546 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011547 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011548 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11549 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011550 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011551 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011552 }
Wincy Van705699a2015-02-03 23:58:17 +080011553
Jan Kiszkaf4124502014-03-07 20:03:13 +010011554 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011555
Jan Kiszkaf4124502014-03-07 20:03:13 +010011556 vmx->nested.preemption_timer_expired = false;
11557 if (nested_cpu_has_preemption_timer(vmcs12))
11558 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011559
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011560 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011561 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011562
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011563 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011564 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011565 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011566 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011567 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011568 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011569 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11570 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011571 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011572 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11573 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11574 ~SECONDARY_EXEC_ENABLE_PML;
11575 exec_control |= vmcs12_exec_ctrl;
11576 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011577
Liran Alon32c7acf2018-06-23 02:35:11 +030011578 /* VMCS shadowing for L2 is emulated for now */
11579 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11580
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011581 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011582 vmcs_write16(GUEST_INTR_STATUS,
11583 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011584
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011585 /*
11586 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11587 * nested_get_vmcs12_pages will either fix it up or
11588 * remove the VM execution control.
11589 */
11590 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11591 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11592
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011593 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11594 }
11595
Jim Mattson83bafef2016-10-04 10:48:38 -070011596 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011597 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11598 * entry, but only if the current (host) sp changed from the value
11599 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11600 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11601 * here we just force the write to happen on entry.
11602 */
11603 vmx->host_rsp = 0;
11604
11605 exec_control = vmx_exec_control(vmx); /* L0's desires */
11606 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11607 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11608 exec_control &= ~CPU_BASED_TPR_SHADOW;
11609 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011610
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011611 /*
11612 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11613 * nested_get_vmcs12_pages can't fix it up, the illegal value
11614 * will result in a VM entry failure.
11615 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011616 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011617 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011618 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011619 } else {
11620#ifdef CONFIG_X86_64
11621 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11622 CPU_BASED_CR8_STORE_EXITING;
11623#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011624 }
11625
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011626 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011627 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11628 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011629 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011630 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11631 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11632
11633 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11634
11635 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11636 * bitwise-or of what L1 wants to trap for L2, and what we want to
11637 * trap. Note that CR0.TS also needs updating - we do this later.
11638 */
11639 update_exception_bitmap(vcpu);
11640 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11641 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11642
Nadav Har'El8049d652013-08-05 11:07:06 +030011643 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11644 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11645 * bits are further modified by vmx_set_efer() below.
11646 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011647 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011648
11649 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11650 * emulated by vmx_set_efer(), below.
11651 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011652 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011653 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11654 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011655 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11656
Jim Mattson6514dc32018-04-26 16:09:12 -070011657 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011658 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011659 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011660 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011661 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011662 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011663 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011664
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011665 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11666
Peter Feinerc95ba922016-08-17 09:36:47 -070011667 if (kvm_has_tsc_control)
11668 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011669
11670 if (enable_vpid) {
11671 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011672 * There is no direct mapping between vpid02 and vpid12, the
11673 * vpid02 is per-vCPU for L0 and reused while the value of
11674 * vpid12 is changed w/ one invvpid during nested vmentry.
11675 * The vpid12 is allocated by L1 for L2, so it will not
11676 * influence global bitmap(for vpid01 and vpid02 allocation)
11677 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011678 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011679 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011680 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11681 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011682 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011683 }
11684 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011685 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011686 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011687 }
11688
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011689 if (enable_pml) {
11690 /*
11691 * Conceptually we want to copy the PML address and index from
11692 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11693 * since we always flush the log on each vmexit, this happens
11694 * to be equivalent to simply resetting the fields in vmcs02.
11695 */
11696 ASSERT(vmx->pml_pg);
11697 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11698 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11699 }
11700
Nadav Har'El155a97a2013-08-05 11:07:16 +030011701 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011702 if (nested_ept_init_mmu_context(vcpu)) {
11703 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11704 return 1;
11705 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011706 } else if (nested_cpu_has2(vmcs12,
11707 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011708 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011709 }
11710
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011711 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011712 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11713 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011714 * The CR0_READ_SHADOW is what L2 should have expected to read given
11715 * the specifications by L1; It's not enough to take
11716 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11717 * have more bits than L1 expected.
11718 */
11719 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11720 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11721
11722 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11723 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11724
Jim Mattson6514dc32018-04-26 16:09:12 -070011725 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011726 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011727 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11728 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11729 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11730 else
11731 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11732 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11733 vmx_set_efer(vcpu, vcpu->arch.efer);
11734
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011735 /*
11736 * Guest state is invalid and unrestricted guest is disabled,
11737 * which means L1 attempted VMEntry to L2 with invalid state.
11738 * Fail the VMEntry.
11739 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011740 if (vmx->emulation_required) {
11741 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011742 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011743 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011744
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011745 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011746 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011747 entry_failure_code))
11748 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011749
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011750 if (!enable_ept)
11751 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11752
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011753 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11754 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011755 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011756}
11757
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011758static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11759{
11760 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11761 nested_cpu_has_virtual_nmis(vmcs12))
11762 return -EINVAL;
11763
11764 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11765 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11766 return -EINVAL;
11767
11768 return 0;
11769}
11770
Jim Mattsonca0bde22016-11-30 12:03:46 -080011771static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11772{
11773 struct vcpu_vmx *vmx = to_vmx(vcpu);
11774
11775 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11776 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11777 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11778
Jim Mattson56a20512017-07-06 16:33:06 -070011779 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11780 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11781
Jim Mattsonca0bde22016-11-30 12:03:46 -080011782 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11783 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11784
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011785 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11786 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11787
Jim Mattson712b12d2017-08-24 13:24:47 -070011788 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11789 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11790
Jim Mattsonca0bde22016-11-30 12:03:46 -080011791 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11792 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11793
11794 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11795 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11796
Bandan Dasc5f983f2017-05-05 15:25:14 -040011797 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11798 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11799
Liran Alona8a7c022018-06-23 02:35:06 +030011800 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11801 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11802
Jim Mattsonca0bde22016-11-30 12:03:46 -080011803 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011804 vmx->nested.msrs.procbased_ctls_low,
11805 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011806 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11807 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011808 vmx->nested.msrs.secondary_ctls_low,
11809 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011810 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011811 vmx->nested.msrs.pinbased_ctls_low,
11812 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011813 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011814 vmx->nested.msrs.exit_ctls_low,
11815 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011816 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011817 vmx->nested.msrs.entry_ctls_low,
11818 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011819 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11820
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011821 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011822 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11823
Bandan Das41ab9372017-08-03 15:54:43 -040011824 if (nested_cpu_has_vmfunc(vmcs12)) {
11825 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011826 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011827 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11828
11829 if (nested_cpu_has_eptp_switching(vmcs12)) {
11830 if (!nested_cpu_has_ept(vmcs12) ||
11831 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11832 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11833 }
11834 }
Bandan Das27c42a12017-08-03 15:54:42 -040011835
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011836 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11837 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11838
Jim Mattsonca0bde22016-11-30 12:03:46 -080011839 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11840 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11841 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11842 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11843
Marc Orr04473782018-06-20 17:21:29 -070011844 /*
11845 * From the Intel SDM, volume 3:
11846 * Fields relevant to VM-entry event injection must be set properly.
11847 * These fields are the VM-entry interruption-information field, the
11848 * VM-entry exception error code, and the VM-entry instruction length.
11849 */
11850 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11851 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11852 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11853 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11854 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11855 bool should_have_error_code;
11856 bool urg = nested_cpu_has2(vmcs12,
11857 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11858 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11859
11860 /* VM-entry interruption-info field: interruption type */
11861 if (intr_type == INTR_TYPE_RESERVED ||
11862 (intr_type == INTR_TYPE_OTHER_EVENT &&
11863 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11864 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11865
11866 /* VM-entry interruption-info field: vector */
11867 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11868 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11869 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11870 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11871
11872 /* VM-entry interruption-info field: deliver error code */
11873 should_have_error_code =
11874 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11875 x86_exception_has_error_code(vector);
11876 if (has_error_code != should_have_error_code)
11877 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11878
11879 /* VM-entry exception error code */
11880 if (has_error_code &&
11881 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11882 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11883
11884 /* VM-entry interruption-info field: reserved bits */
11885 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11886 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11887
11888 /* VM-entry instruction length */
11889 switch (intr_type) {
11890 case INTR_TYPE_SOFT_EXCEPTION:
11891 case INTR_TYPE_SOFT_INTR:
11892 case INTR_TYPE_PRIV_SW_EXCEPTION:
11893 if ((vmcs12->vm_entry_instruction_len > 15) ||
11894 (vmcs12->vm_entry_instruction_len == 0 &&
11895 !nested_cpu_has_zero_length_injection(vcpu)))
11896 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11897 }
11898 }
11899
Jim Mattsonca0bde22016-11-30 12:03:46 -080011900 return 0;
11901}
11902
Liran Alonf145d902018-06-23 02:35:07 +030011903static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11904 struct vmcs12 *vmcs12)
11905{
11906 int r;
11907 struct page *page;
11908 struct vmcs12 *shadow;
11909
11910 if (vmcs12->vmcs_link_pointer == -1ull)
11911 return 0;
11912
11913 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11914 return -EINVAL;
11915
11916 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11917 if (is_error_page(page))
11918 return -EINVAL;
11919
11920 r = 0;
11921 shadow = kmap(page);
11922 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11923 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11924 r = -EINVAL;
11925 kunmap(page);
11926 kvm_release_page_clean(page);
11927 return r;
11928}
11929
Jim Mattsonca0bde22016-11-30 12:03:46 -080011930static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11931 u32 *exit_qual)
11932{
11933 bool ia32e;
11934
11935 *exit_qual = ENTRY_FAIL_DEFAULT;
11936
11937 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11938 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11939 return 1;
11940
Liran Alonf145d902018-06-23 02:35:07 +030011941 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080011942 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11943 return 1;
11944 }
11945
11946 /*
11947 * If the load IA32_EFER VM-entry control is 1, the following checks
11948 * are performed on the field for the IA32_EFER MSR:
11949 * - Bits reserved in the IA32_EFER MSR must be 0.
11950 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11951 * the IA-32e mode guest VM-exit control. It must also be identical
11952 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11953 * CR0.PG) is 1.
11954 */
11955 if (to_vmx(vcpu)->nested.nested_run_pending &&
11956 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11957 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11958 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11959 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11960 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11961 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11962 return 1;
11963 }
11964
11965 /*
11966 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11967 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11968 * the values of the LMA and LME bits in the field must each be that of
11969 * the host address-space size VM-exit control.
11970 */
11971 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11972 ia32e = (vmcs12->vm_exit_controls &
11973 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11974 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11975 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11976 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11977 return 1;
11978 }
11979
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011980 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11981 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11982 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11983 return 1;
11984
Jim Mattsonca0bde22016-11-30 12:03:46 -080011985 return 0;
11986}
11987
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011988/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011989 * If exit_qual is NULL, this is being called from state restore (either RSM
11990 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011991 */
11992static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011993{
11994 struct vcpu_vmx *vmx = to_vmx(vcpu);
11995 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011996 bool from_vmentry = !!exit_qual;
11997 u32 dummy_exit_qual;
11998 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011999
Jim Mattson858e25c2016-11-30 12:03:47 -080012000 enter_guest_mode(vcpu);
12001
12002 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12003 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12004
Jim Mattsonde3a0022017-11-27 17:22:25 -060012005 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012006 vmx_segment_cache_clear(vmx);
12007
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012008 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12009 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12010
12011 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012012 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012013 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012014
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012015 if (from_vmentry) {
12016 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012017
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012018 r = EXIT_REASON_MSR_LOAD_FAIL;
12019 *exit_qual = nested_vmx_load_msr(vcpu,
12020 vmcs12->vm_entry_msr_load_addr,
12021 vmcs12->vm_entry_msr_load_count);
12022 if (*exit_qual)
12023 goto fail;
12024 } else {
12025 /*
12026 * The MMU is not initialized to point at the right entities yet and
12027 * "get pages" would need to read data from the guest (i.e. we will
12028 * need to perform gpa to hpa translation). Request a call
12029 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12030 * have already been set at vmentry time and should not be reset.
12031 */
12032 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12033 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012034
Jim Mattson858e25c2016-11-30 12:03:47 -080012035 /*
12036 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12037 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12038 * returned as far as L1 is concerned. It will only return (and set
12039 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12040 */
12041 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012042
12043fail:
12044 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12045 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12046 leave_guest_mode(vcpu);
12047 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012048 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012049}
12050
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012051/*
12052 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12053 * for running an L2 nested guest.
12054 */
12055static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12056{
12057 struct vmcs12 *vmcs12;
12058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012059 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012060 u32 exit_qual;
12061 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012062
Kyle Hueyeb277562016-11-29 12:40:39 -080012063 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012064 return 1;
12065
Kyle Hueyeb277562016-11-29 12:40:39 -080012066 if (!nested_vmx_check_vmcs12(vcpu))
12067 goto out;
12068
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012069 vmcs12 = get_vmcs12(vcpu);
12070
Liran Alona6192d42018-06-23 02:35:04 +030012071 /*
12072 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12073 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12074 * rather than RFLAGS.ZF, and no error number is stored to the
12075 * VM-instruction error field.
12076 */
12077 if (vmcs12->hdr.shadow_vmcs) {
12078 nested_vmx_failInvalid(vcpu);
12079 goto out;
12080 }
12081
Abel Gordon012f83c2013-04-18 14:39:25 +030012082 if (enable_shadow_vmcs)
12083 copy_shadow_to_vmcs12(vmx);
12084
Nadav Har'El7c177932011-05-25 23:12:04 +030012085 /*
12086 * The nested entry process starts with enforcing various prerequisites
12087 * on vmcs12 as required by the Intel SDM, and act appropriately when
12088 * they fail: As the SDM explains, some conditions should cause the
12089 * instruction to fail, while others will cause the instruction to seem
12090 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12091 * To speed up the normal (success) code path, we should avoid checking
12092 * for misconfigurations which will anyway be caught by the processor
12093 * when using the merged vmcs02.
12094 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012095 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12096 nested_vmx_failValid(vcpu,
12097 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12098 goto out;
12099 }
12100
Nadav Har'El7c177932011-05-25 23:12:04 +030012101 if (vmcs12->launch_state == launch) {
12102 nested_vmx_failValid(vcpu,
12103 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12104 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012105 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012106 }
12107
Jim Mattsonca0bde22016-11-30 12:03:46 -080012108 ret = check_vmentry_prereqs(vcpu, vmcs12);
12109 if (ret) {
12110 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012111 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012112 }
12113
Nadav Har'El7c177932011-05-25 23:12:04 +030012114 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012115 * After this point, the trap flag no longer triggers a singlestep trap
12116 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12117 * This is not 100% correct; for performance reasons, we delegate most
12118 * of the checks on host state to the processor. If those fail,
12119 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012120 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012121 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012122
Jim Mattsonca0bde22016-11-30 12:03:46 -080012123 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12124 if (ret) {
12125 nested_vmx_entry_failure(vcpu, vmcs12,
12126 EXIT_REASON_INVALID_STATE, exit_qual);
12127 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012128 }
12129
12130 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012131 * We're finally done with prerequisite checking, and can start with
12132 * the nested entry.
12133 */
12134
Jim Mattson6514dc32018-04-26 16:09:12 -070012135 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012136 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012137 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012138 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012139 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012140 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012141 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012142
Chao Gao135a06c2018-02-11 10:06:30 +080012143 /*
Liran Alon61ada742018-06-23 02:35:08 +030012144 * Must happen outside of enter_vmx_non_root_mode() as it will
12145 * also be used as part of restoring nVMX state for
12146 * snapshot restore (migration).
12147 *
12148 * In this flow, it is assumed that vmcs12 cache was
12149 * trasferred as part of captured nVMX state and should
12150 * therefore not be read from guest memory (which may not
12151 * exist on destination host yet).
12152 */
12153 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12154
12155 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012156 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12157 * by event injection, halt vcpu.
12158 */
12159 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012160 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12161 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012162 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012163 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012164 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012165
12166out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012167 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012168}
12169
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012170/*
12171 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12172 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12173 * This function returns the new value we should put in vmcs12.guest_cr0.
12174 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12175 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12176 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12177 * didn't trap the bit, because if L1 did, so would L0).
12178 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12179 * been modified by L2, and L1 knows it. So just leave the old value of
12180 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12181 * isn't relevant, because if L0 traps this bit it can set it to anything.
12182 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12183 * changed these bits, and therefore they need to be updated, but L0
12184 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12185 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12186 */
12187static inline unsigned long
12188vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12189{
12190 return
12191 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12192 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12193 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12194 vcpu->arch.cr0_guest_owned_bits));
12195}
12196
12197static inline unsigned long
12198vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12199{
12200 return
12201 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12202 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12203 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12204 vcpu->arch.cr4_guest_owned_bits));
12205}
12206
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012207static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12208 struct vmcs12 *vmcs12)
12209{
12210 u32 idt_vectoring;
12211 unsigned int nr;
12212
Wanpeng Li664f8e22017-08-24 03:35:09 -070012213 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012214 nr = vcpu->arch.exception.nr;
12215 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12216
12217 if (kvm_exception_is_soft(nr)) {
12218 vmcs12->vm_exit_instruction_len =
12219 vcpu->arch.event_exit_inst_len;
12220 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12221 } else
12222 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12223
12224 if (vcpu->arch.exception.has_error_code) {
12225 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12226 vmcs12->idt_vectoring_error_code =
12227 vcpu->arch.exception.error_code;
12228 }
12229
12230 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012231 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012232 vmcs12->idt_vectoring_info_field =
12233 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012234 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012235 nr = vcpu->arch.interrupt.nr;
12236 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12237
12238 if (vcpu->arch.interrupt.soft) {
12239 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12240 vmcs12->vm_entry_instruction_len =
12241 vcpu->arch.event_exit_inst_len;
12242 } else
12243 idt_vectoring |= INTR_TYPE_EXT_INTR;
12244
12245 vmcs12->idt_vectoring_info_field = idt_vectoring;
12246 }
12247}
12248
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012249static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12250{
12251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012252 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012253 bool block_nested_events =
12254 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012255
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012256 if (vcpu->arch.exception.pending &&
12257 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012258 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012259 return -EBUSY;
12260 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012261 return 0;
12262 }
12263
Jan Kiszkaf4124502014-03-07 20:03:13 +010012264 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12265 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012266 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012267 return -EBUSY;
12268 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12269 return 0;
12270 }
12271
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012272 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012273 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012274 return -EBUSY;
12275 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12276 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12277 INTR_INFO_VALID_MASK, 0);
12278 /*
12279 * The NMI-triggered VM exit counts as injection:
12280 * clear this one and block further NMIs.
12281 */
12282 vcpu->arch.nmi_pending = 0;
12283 vmx_set_nmi_mask(vcpu, true);
12284 return 0;
12285 }
12286
12287 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12288 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012289 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012290 return -EBUSY;
12291 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012292 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012293 }
12294
David Hildenbrand6342c502017-01-25 11:58:58 +010012295 vmx_complete_nested_posted_interrupt(vcpu);
12296 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012297}
12298
Jan Kiszkaf4124502014-03-07 20:03:13 +010012299static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12300{
12301 ktime_t remaining =
12302 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12303 u64 value;
12304
12305 if (ktime_to_ns(remaining) <= 0)
12306 return 0;
12307
12308 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12309 do_div(value, 1000000);
12310 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12311}
12312
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012313/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012314 * Update the guest state fields of vmcs12 to reflect changes that
12315 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12316 * VM-entry controls is also updated, since this is really a guest
12317 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012318 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012319static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012320{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012321 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12322 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12323
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012324 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12325 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12326 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12327
12328 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12329 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12330 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12331 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12332 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12333 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12334 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12335 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12336 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12337 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12338 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12339 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12340 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12341 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12342 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12343 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12344 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12345 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12346 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12347 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12348 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12349 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12350 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12351 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12352 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12353 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12354 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12355 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12356 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12357 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12358 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12359 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12360 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12361 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12362 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12363 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12364
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012365 vmcs12->guest_interruptibility_info =
12366 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12367 vmcs12->guest_pending_dbg_exceptions =
12368 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012369 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12370 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12371 else
12372 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012373
Jan Kiszkaf4124502014-03-07 20:03:13 +010012374 if (nested_cpu_has_preemption_timer(vmcs12)) {
12375 if (vmcs12->vm_exit_controls &
12376 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12377 vmcs12->vmx_preemption_timer_value =
12378 vmx_get_preemption_timer_value(vcpu);
12379 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12380 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012381
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012382 /*
12383 * In some cases (usually, nested EPT), L2 is allowed to change its
12384 * own CR3 without exiting. If it has changed it, we must keep it.
12385 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12386 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12387 *
12388 * Additionally, restore L2's PDPTR to vmcs12.
12389 */
12390 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012391 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012392 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12393 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12394 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12395 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12396 }
12397
Jim Mattsond281e132017-06-01 12:44:46 -070012398 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012399
Wincy Van608406e2015-02-03 23:57:51 +080012400 if (nested_cpu_has_vid(vmcs12))
12401 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12402
Jan Kiszkac18911a2013-03-13 16:06:41 +010012403 vmcs12->vm_entry_controls =
12404 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012405 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012406
Jan Kiszka2996fca2014-06-16 13:59:43 +020012407 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12408 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12409 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12410 }
12411
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012412 /* TODO: These cannot have changed unless we have MSR bitmaps and
12413 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012414 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012415 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012416 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12417 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012418 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12419 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12420 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012421 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012422 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012423}
12424
12425/*
12426 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12427 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12428 * and this function updates it to reflect the changes to the guest state while
12429 * L2 was running (and perhaps made some exits which were handled directly by L0
12430 * without going back to L1), and to reflect the exit reason.
12431 * Note that we do not have to copy here all VMCS fields, just those that
12432 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12433 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12434 * which already writes to vmcs12 directly.
12435 */
12436static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12437 u32 exit_reason, u32 exit_intr_info,
12438 unsigned long exit_qualification)
12439{
12440 /* update guest state fields: */
12441 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012442
12443 /* update exit information fields: */
12444
Jan Kiszka533558b2014-01-04 18:47:20 +010012445 vmcs12->vm_exit_reason = exit_reason;
12446 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012447 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012448
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012449 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012450 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12451 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12452
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012453 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012454 vmcs12->launch_state = 1;
12455
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012456 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12457 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012458 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012459
12460 /*
12461 * Transfer the event that L0 or L1 may wanted to inject into
12462 * L2 to IDT_VECTORING_INFO_FIELD.
12463 */
12464 vmcs12_save_pending_event(vcpu, vmcs12);
12465 }
12466
12467 /*
12468 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12469 * preserved above and would only end up incorrectly in L1.
12470 */
12471 vcpu->arch.nmi_injected = false;
12472 kvm_clear_exception_queue(vcpu);
12473 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012474}
12475
Wanpeng Li5af41572017-11-05 16:54:49 -080012476static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12477 struct vmcs12 *vmcs12)
12478{
12479 u32 entry_failure_code;
12480
12481 nested_ept_uninit_mmu_context(vcpu);
12482
12483 /*
12484 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12485 * couldn't have changed.
12486 */
12487 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12488 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12489
12490 if (!enable_ept)
12491 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12492}
12493
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012494/*
12495 * A part of what we need to when the nested L2 guest exits and we want to
12496 * run its L1 parent, is to reset L1's guest state to the host state specified
12497 * in vmcs12.
12498 * This function is to be called not only on normal nested exit, but also on
12499 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12500 * Failures During or After Loading Guest State").
12501 * This function should be called when the active VMCS is L1's (vmcs01).
12502 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012503static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12504 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012505{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012506 struct kvm_segment seg;
12507
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012508 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12509 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012510 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012511 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12512 else
12513 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12514 vmx_set_efer(vcpu, vcpu->arch.efer);
12515
12516 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12517 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012518 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012519 /*
12520 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012521 * actually changed, because vmx_set_cr0 refers to efer set above.
12522 *
12523 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12524 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012525 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012526 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012527 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012528
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012529 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012530 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012531 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012532
Wanpeng Li5af41572017-11-05 16:54:49 -080012533 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012534
Liran Alon6f1e03b2018-05-22 17:16:14 +030012535 /*
12536 * If vmcs01 don't use VPID, CPU flushes TLB on every
12537 * VMEntry/VMExit. Thus, no need to flush TLB.
12538 *
12539 * If vmcs12 uses VPID, TLB entries populated by L2 are
12540 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12541 * with vmx->vpid. Thus, no need to flush TLB.
12542 *
12543 * Therefore, flush TLB only in case vmcs01 uses VPID and
12544 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12545 * are both tagged with vmx->vpid.
12546 */
12547 if (enable_vpid &&
12548 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012549 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012550 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012551
12552 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12553 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12554 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12555 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12556 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012557 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12558 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012559
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012560 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12561 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12562 vmcs_write64(GUEST_BNDCFGS, 0);
12563
Jan Kiszka44811c02013-08-04 17:17:27 +020012564 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012565 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012566 vcpu->arch.pat = vmcs12->host_ia32_pat;
12567 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012568 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12569 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12570 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012571
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012572 /* Set L1 segment info according to Intel SDM
12573 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12574 seg = (struct kvm_segment) {
12575 .base = 0,
12576 .limit = 0xFFFFFFFF,
12577 .selector = vmcs12->host_cs_selector,
12578 .type = 11,
12579 .present = 1,
12580 .s = 1,
12581 .g = 1
12582 };
12583 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12584 seg.l = 1;
12585 else
12586 seg.db = 1;
12587 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12588 seg = (struct kvm_segment) {
12589 .base = 0,
12590 .limit = 0xFFFFFFFF,
12591 .type = 3,
12592 .present = 1,
12593 .s = 1,
12594 .db = 1,
12595 .g = 1
12596 };
12597 seg.selector = vmcs12->host_ds_selector;
12598 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12599 seg.selector = vmcs12->host_es_selector;
12600 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12601 seg.selector = vmcs12->host_ss_selector;
12602 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12603 seg.selector = vmcs12->host_fs_selector;
12604 seg.base = vmcs12->host_fs_base;
12605 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12606 seg.selector = vmcs12->host_gs_selector;
12607 seg.base = vmcs12->host_gs_base;
12608 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12609 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012610 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012611 .limit = 0x67,
12612 .selector = vmcs12->host_tr_selector,
12613 .type = 11,
12614 .present = 1
12615 };
12616 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12617
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012618 kvm_set_dr(vcpu, 7, 0x400);
12619 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012620
Wincy Van3af18d92015-02-03 23:49:31 +080012621 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012622 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012623
Wincy Vanff651cb2014-12-11 08:52:58 +030012624 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12625 vmcs12->vm_exit_msr_load_count))
12626 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012627}
12628
12629/*
12630 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12631 * and modify vmcs12 to make it see what it would expect to see there if
12632 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12633 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012634static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12635 u32 exit_intr_info,
12636 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012637{
12638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012639 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12640
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012641 /* trying to cancel vmlaunch/vmresume is a bug */
12642 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12643
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012644 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012645 * The only expected VM-instruction error is "VM entry with
12646 * invalid control field(s)." Anything else indicates a
12647 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012648 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012649 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12650 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12651
12652 leave_guest_mode(vcpu);
12653
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012654 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12655 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12656
Jim Mattson4f350c62017-09-14 16:31:44 -070012657 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012658 if (exit_reason == -1)
12659 sync_vmcs12(vcpu, vmcs12);
12660 else
12661 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12662 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012663
Liran Alon61ada742018-06-23 02:35:08 +030012664 /*
12665 * Must happen outside of sync_vmcs12() as it will
12666 * also be used to capture vmcs12 cache as part of
12667 * capturing nVMX state for snapshot (migration).
12668 *
12669 * Otherwise, this flush will dirty guest memory at a
12670 * point it is already assumed by user-space to be
12671 * immutable.
12672 */
12673 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12674
Jim Mattson4f350c62017-09-14 16:31:44 -070012675 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12676 vmcs12->vm_exit_msr_store_count))
12677 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012678 }
12679
Jim Mattson4f350c62017-09-14 16:31:44 -070012680 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012681 vm_entry_controls_reset_shadow(vmx);
12682 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012683 vmx_segment_cache_clear(vmx);
12684
Paolo Bonzini93140062016-07-06 13:23:51 +020012685 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012686 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12687 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012688 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012689 if (vmx->hv_deadline_tsc == -1)
12690 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12691 PIN_BASED_VMX_PREEMPTION_TIMER);
12692 else
12693 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12694 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012695 if (kvm_has_tsc_control)
12696 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012697
Jim Mattson8d860bb2018-05-09 16:56:05 -040012698 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12699 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12700 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012701 } else if (!nested_cpu_has_ept(vmcs12) &&
12702 nested_cpu_has2(vmcs12,
12703 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012704 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012705 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012706
12707 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12708 vmx->host_rsp = 0;
12709
12710 /* Unpin physical memory we referred to in vmcs02 */
12711 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012712 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012713 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012714 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012715 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012716 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012717 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012718 }
Wincy Van705699a2015-02-03 23:58:17 +080012719 if (vmx->nested.pi_desc_page) {
12720 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012721 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012722 vmx->nested.pi_desc_page = NULL;
12723 vmx->nested.pi_desc = NULL;
12724 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012725
12726 /*
Tang Chen38b99172014-09-24 15:57:54 +080012727 * We are now running in L2, mmu_notifier will force to reload the
12728 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12729 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012730 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012731
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012732 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012733 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012734
12735 /* in case we halted in L2 */
12736 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012737
12738 if (likely(!vmx->fail)) {
12739 /*
12740 * TODO: SDM says that with acknowledge interrupt on
12741 * exit, bit 31 of the VM-exit interrupt information
12742 * (valid interrupt) is always set to 1 on
12743 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12744 * need kvm_cpu_has_interrupt(). See the commit
12745 * message for details.
12746 */
12747 if (nested_exit_intr_ack_set(vcpu) &&
12748 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12749 kvm_cpu_has_interrupt(vcpu)) {
12750 int irq = kvm_cpu_get_interrupt(vcpu);
12751 WARN_ON(irq < 0);
12752 vmcs12->vm_exit_intr_info = irq |
12753 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12754 }
12755
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012756 if (exit_reason != -1)
12757 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12758 vmcs12->exit_qualification,
12759 vmcs12->idt_vectoring_info_field,
12760 vmcs12->vm_exit_intr_info,
12761 vmcs12->vm_exit_intr_error_code,
12762 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012763
12764 load_vmcs12_host_state(vcpu, vmcs12);
12765
12766 return;
12767 }
12768
12769 /*
12770 * After an early L2 VM-entry failure, we're now back
12771 * in L1 which thinks it just finished a VMLAUNCH or
12772 * VMRESUME instruction, so we need to set the failure
12773 * flag and the VM-instruction error field of the VMCS
12774 * accordingly.
12775 */
12776 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012777
12778 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12779
Jim Mattson4f350c62017-09-14 16:31:44 -070012780 /*
12781 * The emulated instruction was already skipped in
12782 * nested_vmx_run, but the updated RIP was never
12783 * written back to the vmcs01.
12784 */
12785 skip_emulated_instruction(vcpu);
12786 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012787}
12788
Nadav Har'El7c177932011-05-25 23:12:04 +030012789/*
Jan Kiszka42124922014-01-04 18:47:19 +010012790 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12791 */
12792static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12793{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012794 if (is_guest_mode(vcpu)) {
12795 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012796 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012797 }
Jan Kiszka42124922014-01-04 18:47:19 +010012798 free_nested(to_vmx(vcpu));
12799}
12800
12801/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012802 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12803 * 23.7 "VM-entry failures during or after loading guest state" (this also
12804 * lists the acceptable exit-reason and exit-qualification parameters).
12805 * It should only be called before L2 actually succeeded to run, and when
12806 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12807 */
12808static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12809 struct vmcs12 *vmcs12,
12810 u32 reason, unsigned long qualification)
12811{
12812 load_vmcs12_host_state(vcpu, vmcs12);
12813 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12814 vmcs12->exit_qualification = qualification;
12815 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012816 if (enable_shadow_vmcs)
12817 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012818}
12819
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012820static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12821 struct x86_instruction_info *info,
12822 enum x86_intercept_stage stage)
12823{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012824 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12825 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12826
12827 /*
12828 * RDPID causes #UD if disabled through secondary execution controls.
12829 * Because it is marked as EmulateOnUD, we need to intercept it here.
12830 */
12831 if (info->intercept == x86_intercept_rdtscp &&
12832 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12833 ctxt->exception.vector = UD_VECTOR;
12834 ctxt->exception.error_code_valid = false;
12835 return X86EMUL_PROPAGATE_FAULT;
12836 }
12837
12838 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012839 return X86EMUL_CONTINUE;
12840}
12841
Yunhong Jiang64672c92016-06-13 14:19:59 -070012842#ifdef CONFIG_X86_64
12843/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12844static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12845 u64 divisor, u64 *result)
12846{
12847 u64 low = a << shift, high = a >> (64 - shift);
12848
12849 /* To avoid the overflow on divq */
12850 if (high >= divisor)
12851 return 1;
12852
12853 /* Low hold the result, high hold rem which is discarded */
12854 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12855 "rm" (divisor), "0" (low), "1" (high));
12856 *result = low;
12857
12858 return 0;
12859}
12860
12861static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12862{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012863 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012864 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012865
12866 if (kvm_mwait_in_guest(vcpu->kvm))
12867 return -EOPNOTSUPP;
12868
12869 vmx = to_vmx(vcpu);
12870 tscl = rdtsc();
12871 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12872 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012873 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12874
12875 if (delta_tsc > lapic_timer_advance_cycles)
12876 delta_tsc -= lapic_timer_advance_cycles;
12877 else
12878 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012879
12880 /* Convert to host delta tsc if tsc scaling is enabled */
12881 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12882 u64_shl_div_u64(delta_tsc,
12883 kvm_tsc_scaling_ratio_frac_bits,
12884 vcpu->arch.tsc_scaling_ratio,
12885 &delta_tsc))
12886 return -ERANGE;
12887
12888 /*
12889 * If the delta tsc can't fit in the 32 bit after the multi shift,
12890 * we can't use the preemption timer.
12891 * It's possible that it fits on later vmentries, but checking
12892 * on every vmentry is costly so we just use an hrtimer.
12893 */
12894 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12895 return -ERANGE;
12896
12897 vmx->hv_deadline_tsc = tscl + delta_tsc;
12898 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12899 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012900
12901 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012902}
12903
12904static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12905{
12906 struct vcpu_vmx *vmx = to_vmx(vcpu);
12907 vmx->hv_deadline_tsc = -1;
12908 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12909 PIN_BASED_VMX_PREEMPTION_TIMER);
12910}
12911#endif
12912
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012913static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012914{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012915 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012916 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012917}
12918
Kai Huang843e4332015-01-28 10:54:28 +080012919static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12920 struct kvm_memory_slot *slot)
12921{
12922 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12923 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12924}
12925
12926static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12927 struct kvm_memory_slot *slot)
12928{
12929 kvm_mmu_slot_set_dirty(kvm, slot);
12930}
12931
12932static void vmx_flush_log_dirty(struct kvm *kvm)
12933{
12934 kvm_flush_pml_buffers(kvm);
12935}
12936
Bandan Dasc5f983f2017-05-05 15:25:14 -040012937static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12938{
12939 struct vmcs12 *vmcs12;
12940 struct vcpu_vmx *vmx = to_vmx(vcpu);
12941 gpa_t gpa;
12942 struct page *page = NULL;
12943 u64 *pml_address;
12944
12945 if (is_guest_mode(vcpu)) {
12946 WARN_ON_ONCE(vmx->nested.pml_full);
12947
12948 /*
12949 * Check if PML is enabled for the nested guest.
12950 * Whether eptp bit 6 is set is already checked
12951 * as part of A/D emulation.
12952 */
12953 vmcs12 = get_vmcs12(vcpu);
12954 if (!nested_cpu_has_pml(vmcs12))
12955 return 0;
12956
Dan Carpenter47698862017-05-10 22:43:17 +030012957 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012958 vmx->nested.pml_full = true;
12959 return 1;
12960 }
12961
12962 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12963
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012964 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12965 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012966 return 0;
12967
12968 pml_address = kmap(page);
12969 pml_address[vmcs12->guest_pml_index--] = gpa;
12970 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012971 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012972 }
12973
12974 return 0;
12975}
12976
Kai Huang843e4332015-01-28 10:54:28 +080012977static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12978 struct kvm_memory_slot *memslot,
12979 gfn_t offset, unsigned long mask)
12980{
12981 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12982}
12983
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012984static void __pi_post_block(struct kvm_vcpu *vcpu)
12985{
12986 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12987 struct pi_desc old, new;
12988 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012989
12990 do {
12991 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012992 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12993 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012994
12995 dest = cpu_physical_id(vcpu->cpu);
12996
12997 if (x2apic_enabled())
12998 new.ndst = dest;
12999 else
13000 new.ndst = (dest << 8) & 0xFF00;
13001
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013002 /* set 'NV' to 'notification vector' */
13003 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013004 } while (cmpxchg64(&pi_desc->control, old.control,
13005 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013006
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013007 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13008 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013009 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013010 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013011 vcpu->pre_pcpu = -1;
13012 }
13013}
13014
Feng Wuefc64402015-09-18 22:29:51 +080013015/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013016 * This routine does the following things for vCPU which is going
13017 * to be blocked if VT-d PI is enabled.
13018 * - Store the vCPU to the wakeup list, so when interrupts happen
13019 * we can find the right vCPU to wake up.
13020 * - Change the Posted-interrupt descriptor as below:
13021 * 'NDST' <-- vcpu->pre_pcpu
13022 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13023 * - If 'ON' is set during this process, which means at least one
13024 * interrupt is posted for this vCPU, we cannot block it, in
13025 * this case, return 1, otherwise, return 0.
13026 *
13027 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013028static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013029{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013030 unsigned int dest;
13031 struct pi_desc old, new;
13032 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13033
13034 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013035 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13036 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013037 return 0;
13038
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013039 WARN_ON(irqs_disabled());
13040 local_irq_disable();
13041 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13042 vcpu->pre_pcpu = vcpu->cpu;
13043 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13044 list_add_tail(&vcpu->blocked_vcpu_list,
13045 &per_cpu(blocked_vcpu_on_cpu,
13046 vcpu->pre_pcpu));
13047 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13048 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013049
13050 do {
13051 old.control = new.control = pi_desc->control;
13052
Feng Wubf9f6ac2015-09-18 22:29:55 +080013053 WARN((pi_desc->sn == 1),
13054 "Warning: SN field of posted-interrupts "
13055 "is set before blocking\n");
13056
13057 /*
13058 * Since vCPU can be preempted during this process,
13059 * vcpu->cpu could be different with pre_pcpu, we
13060 * need to set pre_pcpu as the destination of wakeup
13061 * notification event, then we can find the right vCPU
13062 * to wakeup in wakeup handler if interrupts happen
13063 * when the vCPU is in blocked state.
13064 */
13065 dest = cpu_physical_id(vcpu->pre_pcpu);
13066
13067 if (x2apic_enabled())
13068 new.ndst = dest;
13069 else
13070 new.ndst = (dest << 8) & 0xFF00;
13071
13072 /* set 'NV' to 'wakeup vector' */
13073 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013074 } while (cmpxchg64(&pi_desc->control, old.control,
13075 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013076
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013077 /* We should not block the vCPU if an interrupt is posted for it. */
13078 if (pi_test_on(pi_desc) == 1)
13079 __pi_post_block(vcpu);
13080
13081 local_irq_enable();
13082 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013083}
13084
Yunhong Jiangbc225122016-06-13 14:19:58 -070013085static int vmx_pre_block(struct kvm_vcpu *vcpu)
13086{
13087 if (pi_pre_block(vcpu))
13088 return 1;
13089
Yunhong Jiang64672c92016-06-13 14:19:59 -070013090 if (kvm_lapic_hv_timer_in_use(vcpu))
13091 kvm_lapic_switch_to_sw_timer(vcpu);
13092
Yunhong Jiangbc225122016-06-13 14:19:58 -070013093 return 0;
13094}
13095
13096static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013097{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013098 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013099 return;
13100
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013101 WARN_ON(irqs_disabled());
13102 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013103 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013104 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013105}
13106
Yunhong Jiangbc225122016-06-13 14:19:58 -070013107static void vmx_post_block(struct kvm_vcpu *vcpu)
13108{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013109 if (kvm_x86_ops->set_hv_timer)
13110 kvm_lapic_switch_to_hv_timer(vcpu);
13111
Yunhong Jiangbc225122016-06-13 14:19:58 -070013112 pi_post_block(vcpu);
13113}
13114
Feng Wubf9f6ac2015-09-18 22:29:55 +080013115/*
Feng Wuefc64402015-09-18 22:29:51 +080013116 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13117 *
13118 * @kvm: kvm
13119 * @host_irq: host irq of the interrupt
13120 * @guest_irq: gsi of the interrupt
13121 * @set: set or unset PI
13122 * returns 0 on success, < 0 on failure
13123 */
13124static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13125 uint32_t guest_irq, bool set)
13126{
13127 struct kvm_kernel_irq_routing_entry *e;
13128 struct kvm_irq_routing_table *irq_rt;
13129 struct kvm_lapic_irq irq;
13130 struct kvm_vcpu *vcpu;
13131 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013132 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013133
13134 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013135 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13136 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013137 return 0;
13138
13139 idx = srcu_read_lock(&kvm->irq_srcu);
13140 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013141 if (guest_irq >= irq_rt->nr_rt_entries ||
13142 hlist_empty(&irq_rt->map[guest_irq])) {
13143 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13144 guest_irq, irq_rt->nr_rt_entries);
13145 goto out;
13146 }
Feng Wuefc64402015-09-18 22:29:51 +080013147
13148 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13149 if (e->type != KVM_IRQ_ROUTING_MSI)
13150 continue;
13151 /*
13152 * VT-d PI cannot support posting multicast/broadcast
13153 * interrupts to a vCPU, we still use interrupt remapping
13154 * for these kind of interrupts.
13155 *
13156 * For lowest-priority interrupts, we only support
13157 * those with single CPU as the destination, e.g. user
13158 * configures the interrupts via /proc/irq or uses
13159 * irqbalance to make the interrupts single-CPU.
13160 *
13161 * We will support full lowest-priority interrupt later.
13162 */
13163
Radim Krčmář371313132016-07-12 22:09:27 +020013164 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013165 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13166 /*
13167 * Make sure the IRTE is in remapped mode if
13168 * we don't handle it in posted mode.
13169 */
13170 ret = irq_set_vcpu_affinity(host_irq, NULL);
13171 if (ret < 0) {
13172 printk(KERN_INFO
13173 "failed to back to remapped mode, irq: %u\n",
13174 host_irq);
13175 goto out;
13176 }
13177
Feng Wuefc64402015-09-18 22:29:51 +080013178 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013179 }
Feng Wuefc64402015-09-18 22:29:51 +080013180
13181 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13182 vcpu_info.vector = irq.vector;
13183
hu huajun2698d822018-04-11 15:16:40 +080013184 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013185 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13186
13187 if (set)
13188 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013189 else
Feng Wuefc64402015-09-18 22:29:51 +080013190 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013191
13192 if (ret < 0) {
13193 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13194 __func__);
13195 goto out;
13196 }
13197 }
13198
13199 ret = 0;
13200out:
13201 srcu_read_unlock(&kvm->irq_srcu, idx);
13202 return ret;
13203}
13204
Ashok Rajc45dcc72016-06-22 14:59:56 +080013205static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13206{
13207 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13208 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13209 FEATURE_CONTROL_LMCE;
13210 else
13211 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13212 ~FEATURE_CONTROL_LMCE;
13213}
13214
Ladi Prosek72d7b372017-10-11 16:54:41 +020013215static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13216{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013217 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13218 if (to_vmx(vcpu)->nested.nested_run_pending)
13219 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013220 return 1;
13221}
13222
Ladi Prosek0234bf82017-10-11 16:54:40 +020013223static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13224{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013225 struct vcpu_vmx *vmx = to_vmx(vcpu);
13226
13227 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13228 if (vmx->nested.smm.guest_mode)
13229 nested_vmx_vmexit(vcpu, -1, 0, 0);
13230
13231 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13232 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013233 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013234 return 0;
13235}
13236
13237static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13238{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013239 struct vcpu_vmx *vmx = to_vmx(vcpu);
13240 int ret;
13241
13242 if (vmx->nested.smm.vmxon) {
13243 vmx->nested.vmxon = true;
13244 vmx->nested.smm.vmxon = false;
13245 }
13246
13247 if (vmx->nested.smm.guest_mode) {
13248 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013249 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013250 vcpu->arch.hflags |= HF_SMM_MASK;
13251 if (ret)
13252 return ret;
13253
13254 vmx->nested.smm.guest_mode = false;
13255 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013256 return 0;
13257}
13258
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013259static int enable_smi_window(struct kvm_vcpu *vcpu)
13260{
13261 return 0;
13262}
13263
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013264static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13265 struct kvm_nested_state __user *user_kvm_nested_state,
13266 u32 user_data_size)
13267{
13268 struct vcpu_vmx *vmx;
13269 struct vmcs12 *vmcs12;
13270 struct kvm_nested_state kvm_state = {
13271 .flags = 0,
13272 .format = 0,
13273 .size = sizeof(kvm_state),
13274 .vmx.vmxon_pa = -1ull,
13275 .vmx.vmcs_pa = -1ull,
13276 };
13277
13278 if (!vcpu)
13279 return kvm_state.size + 2 * VMCS12_SIZE;
13280
13281 vmx = to_vmx(vcpu);
13282 vmcs12 = get_vmcs12(vcpu);
13283 if (nested_vmx_allowed(vcpu) &&
13284 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13285 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13286 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13287
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013288 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013289 kvm_state.size += VMCS12_SIZE;
13290
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013291 if (is_guest_mode(vcpu) &&
13292 nested_cpu_has_shadow_vmcs(vmcs12) &&
13293 vmcs12->vmcs_link_pointer != -1ull)
13294 kvm_state.size += VMCS12_SIZE;
13295 }
13296
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013297 if (vmx->nested.smm.vmxon)
13298 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13299
13300 if (vmx->nested.smm.guest_mode)
13301 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13302
13303 if (is_guest_mode(vcpu)) {
13304 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13305
13306 if (vmx->nested.nested_run_pending)
13307 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13308 }
13309 }
13310
13311 if (user_data_size < kvm_state.size)
13312 goto out;
13313
13314 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13315 return -EFAULT;
13316
13317 if (vmx->nested.current_vmptr == -1ull)
13318 goto out;
13319
13320 /*
13321 * When running L2, the authoritative vmcs12 state is in the
13322 * vmcs02. When running L1, the authoritative vmcs12 state is
13323 * in the shadow vmcs linked to vmcs01, unless
13324 * sync_shadow_vmcs is set, in which case, the authoritative
13325 * vmcs12 state is in the vmcs12 already.
13326 */
13327 if (is_guest_mode(vcpu))
13328 sync_vmcs12(vcpu, vmcs12);
13329 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13330 copy_shadow_to_vmcs12(vmx);
13331
13332 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13333 return -EFAULT;
13334
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013335 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13336 vmcs12->vmcs_link_pointer != -1ull) {
13337 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13338 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13339 return -EFAULT;
13340 }
13341
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013342out:
13343 return kvm_state.size;
13344}
13345
13346static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13347 struct kvm_nested_state __user *user_kvm_nested_state,
13348 struct kvm_nested_state *kvm_state)
13349{
13350 struct vcpu_vmx *vmx = to_vmx(vcpu);
13351 struct vmcs12 *vmcs12;
13352 u32 exit_qual;
13353 int ret;
13354
13355 if (kvm_state->format != 0)
13356 return -EINVAL;
13357
13358 if (!nested_vmx_allowed(vcpu))
13359 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13360
13361 if (kvm_state->vmx.vmxon_pa == -1ull) {
13362 if (kvm_state->vmx.smm.flags)
13363 return -EINVAL;
13364
13365 if (kvm_state->vmx.vmcs_pa != -1ull)
13366 return -EINVAL;
13367
13368 vmx_leave_nested(vcpu);
13369 return 0;
13370 }
13371
13372 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13373 return -EINVAL;
13374
13375 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13376 return -EINVAL;
13377
13378 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13379 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13380 return -EINVAL;
13381
13382 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13383 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13384 return -EINVAL;
13385
13386 if (kvm_state->vmx.smm.flags &
13387 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13388 return -EINVAL;
13389
13390 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13391 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13392 return -EINVAL;
13393
13394 vmx_leave_nested(vcpu);
13395 if (kvm_state->vmx.vmxon_pa == -1ull)
13396 return 0;
13397
13398 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13399 ret = enter_vmx_operation(vcpu);
13400 if (ret)
13401 return ret;
13402
13403 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13404
13405 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13406 vmx->nested.smm.vmxon = true;
13407 vmx->nested.vmxon = false;
13408
13409 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13410 vmx->nested.smm.guest_mode = true;
13411 }
13412
13413 vmcs12 = get_vmcs12(vcpu);
13414 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13415 return -EFAULT;
13416
Liran Alon392b2f22018-06-23 02:35:01 +030013417 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013418 return -EINVAL;
13419
13420 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13421 return 0;
13422
13423 vmx->nested.nested_run_pending =
13424 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13425
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013426 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13427 vmcs12->vmcs_link_pointer != -1ull) {
13428 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13429 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13430 return -EINVAL;
13431
13432 if (copy_from_user(shadow_vmcs12,
13433 user_kvm_nested_state->data + VMCS12_SIZE,
13434 sizeof(*vmcs12)))
13435 return -EFAULT;
13436
13437 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13438 !shadow_vmcs12->hdr.shadow_vmcs)
13439 return -EINVAL;
13440 }
13441
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013442 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13443 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13444 return -EINVAL;
13445
13446 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13447 vmx->nested.nested_run_pending = 1;
13448
13449 vmx->nested.dirty_vmcs12 = true;
13450 ret = enter_vmx_non_root_mode(vcpu, NULL);
13451 if (ret)
13452 return -EINVAL;
13453
13454 return 0;
13455}
13456
Kees Cook404f6aa2016-08-08 16:29:06 -070013457static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013458 .cpu_has_kvm_support = cpu_has_kvm_support,
13459 .disabled_by_bios = vmx_disabled_by_bios,
13460 .hardware_setup = hardware_setup,
13461 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013462 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013463 .hardware_enable = hardware_enable,
13464 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013465 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013466 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013467
Wanpeng Lib31c1142018-03-12 04:53:04 -070013468 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013469 .vm_alloc = vmx_vm_alloc,
13470 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013471
Avi Kivity6aa8b732006-12-10 02:21:36 -080013472 .vcpu_create = vmx_create_vcpu,
13473 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013474 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013475
Avi Kivity04d2cc72007-09-10 18:10:54 +030013476 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013477 .vcpu_load = vmx_vcpu_load,
13478 .vcpu_put = vmx_vcpu_put,
13479
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013480 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013481 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013482 .get_msr = vmx_get_msr,
13483 .set_msr = vmx_set_msr,
13484 .get_segment_base = vmx_get_segment_base,
13485 .get_segment = vmx_get_segment,
13486 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013487 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013488 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013489 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013490 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013491 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013492 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013493 .set_cr3 = vmx_set_cr3,
13494 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013495 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013496 .get_idt = vmx_get_idt,
13497 .set_idt = vmx_set_idt,
13498 .get_gdt = vmx_get_gdt,
13499 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013500 .get_dr6 = vmx_get_dr6,
13501 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013502 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013503 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013504 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013505 .get_rflags = vmx_get_rflags,
13506 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013507
Avi Kivity6aa8b732006-12-10 02:21:36 -080013508 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013509
Avi Kivity6aa8b732006-12-10 02:21:36 -080013510 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013511 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013512 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013513 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13514 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013515 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013516 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013517 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013518 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013519 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013520 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013521 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013522 .get_nmi_mask = vmx_get_nmi_mask,
13523 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013524 .enable_nmi_window = enable_nmi_window,
13525 .enable_irq_window = enable_irq_window,
13526 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013527 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013528 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013529 .get_enable_apicv = vmx_get_enable_apicv,
13530 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013531 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013532 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013533 .hwapic_irr_update = vmx_hwapic_irr_update,
13534 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013535 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13536 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013537
Izik Eiduscbc94022007-10-25 00:29:55 +020013538 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013539 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013540 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013541 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013542
Avi Kivity586f9602010-11-18 13:09:54 +020013543 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013544
Sheng Yang17cc3932010-01-05 19:02:27 +080013545 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013546
13547 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013548
13549 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013550 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013551
13552 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013553
13554 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013555
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013556 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013557 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013558
13559 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013560
13561 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013562 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013563 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013564 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013565 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013566
13567 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013568
13569 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013570
13571 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13572 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13573 .flush_log_dirty = vmx_flush_log_dirty,
13574 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013575 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013576
Feng Wubf9f6ac2015-09-18 22:29:55 +080013577 .pre_block = vmx_pre_block,
13578 .post_block = vmx_post_block,
13579
Wei Huang25462f72015-06-19 15:45:05 +020013580 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013581
13582 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013583
13584#ifdef CONFIG_X86_64
13585 .set_hv_timer = vmx_set_hv_timer,
13586 .cancel_hv_timer = vmx_cancel_hv_timer,
13587#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013588
13589 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013590
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013591 .get_nested_state = vmx_get_nested_state,
13592 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013593 .get_vmcs12_pages = nested_get_vmcs12_pages,
13594
Ladi Prosek72d7b372017-10-11 16:54:41 +020013595 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013596 .pre_enter_smm = vmx_pre_enter_smm,
13597 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013598 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013599};
13600
13601static int __init vmx_init(void)
13602{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013603 int r;
13604
13605#if IS_ENABLED(CONFIG_HYPERV)
13606 /*
13607 * Enlightened VMCS usage should be recommended and the host needs
13608 * to support eVMCS v1 or above. We can also disable eVMCS support
13609 * with module parameter.
13610 */
13611 if (enlightened_vmcs &&
13612 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13613 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13614 KVM_EVMCS_VERSION) {
13615 int cpu;
13616
13617 /* Check that we have assist pages on all online CPUs */
13618 for_each_online_cpu(cpu) {
13619 if (!hv_get_vp_assist_page(cpu)) {
13620 enlightened_vmcs = false;
13621 break;
13622 }
13623 }
13624
13625 if (enlightened_vmcs) {
13626 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13627 static_branch_enable(&enable_evmcs);
13628 }
13629 } else {
13630 enlightened_vmcs = false;
13631 }
13632#endif
13633
13634 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013635 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013636 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013637 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013638
Dave Young2965faa2015-09-09 15:38:55 -070013639#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013640 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13641 crash_vmclear_local_loaded_vmcss);
13642#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013643 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013644
He, Qingfdef3ad2007-04-30 09:45:24 +030013645 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013646}
13647
13648static void __exit vmx_exit(void)
13649{
Dave Young2965faa2015-09-09 15:38:55 -070013650#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013651 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013652 synchronize_rcu();
13653#endif
13654
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013655 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013656
13657#if IS_ENABLED(CONFIG_HYPERV)
13658 if (static_branch_unlikely(&enable_evmcs)) {
13659 int cpu;
13660 struct hv_vp_assist_page *vp_ap;
13661 /*
13662 * Reset everything to support using non-enlightened VMCS
13663 * access later (e.g. when we reload the module with
13664 * enlightened_vmcs=0)
13665 */
13666 for_each_online_cpu(cpu) {
13667 vp_ap = hv_get_vp_assist_page(cpu);
13668
13669 if (!vp_ap)
13670 continue;
13671
13672 vp_ap->current_nested_vmcs = 0;
13673 vp_ap->enlighten_vmentry = 0;
13674 }
13675
13676 static_branch_disable(&enable_evmcs);
13677 }
13678#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013679}
13680
13681module_init(vmx_init)
13682module_exit(vmx_exit)