blob: f91a1599738e2de0c3f3a8e96467c114e37f4908 [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;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003311
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003312 if (enable_ept) {
3313 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003315 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003316 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003317 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003318 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003320 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msrs->ept_caps &= vmx_capability.ept;
3322 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003323 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3324 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003325 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003327 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003329 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003330 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003331
Bandan Das27c42a12017-08-03 15:54:42 -04003332 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003334 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003335 /*
3336 * Advertise EPTP switching unconditionally
3337 * since we emulate it
3338 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003339 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003341 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003342 }
3343
Paolo Bonzinief697a72016-03-18 16:58:38 +01003344 /*
3345 * Old versions of KVM use the single-context version without
3346 * checking for support, so declare that it is supported even
3347 * though it is treated as global context. The alternative is
3348 * not failing the single-context invvpid, and it is worse.
3349 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003350 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003351 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003352 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003354 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003355 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003356
Radim Krčmář0790ec12015-03-17 14:02:32 +01003357 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003359 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3360
Jan Kiszkac18911a2013-03-13 16:06:41 +01003361 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003362 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->misc_low,
3364 msrs->misc_high);
3365 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3366 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003367 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003368 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003369 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003370 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003371
3372 /*
3373 * This MSR reports some information about VMX support. We
3374 * should return information about the VMX we emulate for the
3375 * guest, and the VMCS structure we give it - not about the
3376 * VMX support of the underlying hardware.
3377 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003378 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003379 VMCS12_REVISION |
3380 VMX_BASIC_TRUE_CTLS |
3381 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3382 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3383
3384 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003386
3387 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003388 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003389 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3390 * We picked the standard core2 setting.
3391 */
3392#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3393#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003394 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3395 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003396
3397 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003398 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3399 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003400
3401 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003402 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003403}
3404
David Matlack38991522016-11-29 18:14:08 -08003405/*
3406 * if fixed0[i] == 1: val[i] must be 1
3407 * if fixed1[i] == 0: val[i] must be 0
3408 */
3409static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3410{
3411 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003412}
3413
3414static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3415{
David Matlack38991522016-11-29 18:14:08 -08003416 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003417}
3418
3419static inline u64 vmx_control_msr(u32 low, u32 high)
3420{
3421 return low | ((u64)high << 32);
3422}
3423
David Matlack62cc6b9d2016-11-29 18:14:07 -08003424static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3425{
3426 superset &= mask;
3427 subset &= mask;
3428
3429 return (superset | subset) == superset;
3430}
3431
3432static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3433{
3434 const u64 feature_and_reserved =
3435 /* feature (except bit 48; see below) */
3436 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3437 /* reserved */
3438 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003440
3441 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3442 return -EINVAL;
3443
3444 /*
3445 * KVM does not emulate a version of VMX that constrains physical
3446 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3447 */
3448 if (data & BIT_ULL(48))
3449 return -EINVAL;
3450
3451 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3452 vmx_basic_vmcs_revision_id(data))
3453 return -EINVAL;
3454
3455 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3456 return -EINVAL;
3457
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003459 return 0;
3460}
3461
3462static int
3463vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3464{
3465 u64 supported;
3466 u32 *lowp, *highp;
3467
3468 switch (msr_index) {
3469 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003470 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3471 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003472 break;
3473 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003474 lowp = &vmx->nested.msrs.procbased_ctls_low;
3475 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003476 break;
3477 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003478 lowp = &vmx->nested.msrs.exit_ctls_low;
3479 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003480 break;
3481 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482 lowp = &vmx->nested.msrs.entry_ctls_low;
3483 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003484 break;
3485 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003486 lowp = &vmx->nested.msrs.secondary_ctls_low;
3487 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003488 break;
3489 default:
3490 BUG();
3491 }
3492
3493 supported = vmx_control_msr(*lowp, *highp);
3494
3495 /* Check must-be-1 bits are still 1. */
3496 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3497 return -EINVAL;
3498
3499 /* Check must-be-0 bits are still 0. */
3500 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3501 return -EINVAL;
3502
3503 *lowp = data;
3504 *highp = data >> 32;
3505 return 0;
3506}
3507
3508static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3509{
3510 const u64 feature_and_reserved_bits =
3511 /* feature */
3512 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3513 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3514 /* reserved */
3515 GENMASK_ULL(13, 9) | BIT_ULL(31);
3516 u64 vmx_misc;
3517
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003518 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3519 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003520
3521 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3522 return -EINVAL;
3523
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003525 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3526 vmx_misc_preemption_timer_rate(data) !=
3527 vmx_misc_preemption_timer_rate(vmx_misc))
3528 return -EINVAL;
3529
3530 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3531 return -EINVAL;
3532
3533 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3534 return -EINVAL;
3535
3536 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3537 return -EINVAL;
3538
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 vmx->nested.msrs.misc_low = data;
3540 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003541
3542 /*
3543 * If L1 has read-only VM-exit information fields, use the
3544 * less permissive vmx_vmwrite_bitmap to specify write
3545 * permissions for the shadow VMCS.
3546 */
3547 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3548 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3549
David Matlack62cc6b9d2016-11-29 18:14:07 -08003550 return 0;
3551}
3552
3553static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3554{
3555 u64 vmx_ept_vpid_cap;
3556
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003557 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3558 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559
3560 /* Every bit is either reserved or a feature bit. */
3561 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3562 return -EINVAL;
3563
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 vmx->nested.msrs.ept_caps = data;
3565 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003566 return 0;
3567}
3568
3569static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3570{
3571 u64 *msr;
3572
3573 switch (msr_index) {
3574 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003575 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003576 break;
3577 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003579 break;
3580 default:
3581 BUG();
3582 }
3583
3584 /*
3585 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3586 * must be 1 in the restored value.
3587 */
3588 if (!is_bitwise_subset(data, *msr, -1ULL))
3589 return -EINVAL;
3590
3591 *msr = data;
3592 return 0;
3593}
3594
3595/*
3596 * Called when userspace is restoring VMX MSRs.
3597 *
3598 * Returns 0 on success, non-0 otherwise.
3599 */
3600static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3601{
3602 struct vcpu_vmx *vmx = to_vmx(vcpu);
3603
Jim Mattsona943ac52018-05-29 09:11:32 -07003604 /*
3605 * Don't allow changes to the VMX capability MSRs while the vCPU
3606 * is in VMX operation.
3607 */
3608 if (vmx->nested.vmxon)
3609 return -EBUSY;
3610
David Matlack62cc6b9d2016-11-29 18:14:07 -08003611 switch (msr_index) {
3612 case MSR_IA32_VMX_BASIC:
3613 return vmx_restore_vmx_basic(vmx, data);
3614 case MSR_IA32_VMX_PINBASED_CTLS:
3615 case MSR_IA32_VMX_PROCBASED_CTLS:
3616 case MSR_IA32_VMX_EXIT_CTLS:
3617 case MSR_IA32_VMX_ENTRY_CTLS:
3618 /*
3619 * The "non-true" VMX capability MSRs are generated from the
3620 * "true" MSRs, so we do not support restoring them directly.
3621 *
3622 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3623 * should restore the "true" MSRs with the must-be-1 bits
3624 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3625 * DEFAULT SETTINGS".
3626 */
3627 return -EINVAL;
3628 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3629 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3630 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3631 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3632 case MSR_IA32_VMX_PROCBASED_CTLS2:
3633 return vmx_restore_control_msr(vmx, msr_index, data);
3634 case MSR_IA32_VMX_MISC:
3635 return vmx_restore_vmx_misc(vmx, data);
3636 case MSR_IA32_VMX_CR0_FIXED0:
3637 case MSR_IA32_VMX_CR4_FIXED0:
3638 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3639 case MSR_IA32_VMX_CR0_FIXED1:
3640 case MSR_IA32_VMX_CR4_FIXED1:
3641 /*
3642 * These MSRs are generated based on the vCPU's CPUID, so we
3643 * do not support restoring them directly.
3644 */
3645 return -EINVAL;
3646 case MSR_IA32_VMX_EPT_VPID_CAP:
3647 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3648 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003650 return 0;
3651 default:
3652 /*
3653 * The rest of the VMX capability MSRs do not support restore.
3654 */
3655 return -EINVAL;
3656 }
3657}
3658
Jan Kiszkacae50132014-01-04 18:47:22 +01003659/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003661{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003662 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003663 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003664 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003665 break;
3666 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3667 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003668 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003669 msrs->pinbased_ctls_low,
3670 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003671 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3672 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003673 break;
3674 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3675 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003676 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 msrs->procbased_ctls_low,
3678 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003679 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3680 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003681 break;
3682 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3683 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003684 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->exit_ctls_low,
3686 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003687 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3688 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003689 break;
3690 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3691 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003692 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 msrs->entry_ctls_low,
3694 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003695 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3696 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003697 break;
3698 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003699 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003700 msrs->misc_low,
3701 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003702 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003704 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003705 break;
3706 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003707 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708 break;
3709 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003710 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711 break;
3712 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714 break;
3715 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003716 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717 break;
3718 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003719 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003720 msrs->secondary_ctls_low,
3721 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003722 break;
3723 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003724 *pdata = msrs->ept_caps |
3725 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003726 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003727 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003728 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003729 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003731 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003732 }
3733
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003734 return 0;
3735}
3736
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003737static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3738 uint64_t val)
3739{
3740 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3741
3742 return !(val & ~valid_bits);
3743}
3744
Tom Lendacky801e4592018-02-21 13:39:51 -06003745static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3746{
Paolo Bonzini13893092018-02-26 13:40:09 +01003747 switch (msr->index) {
3748 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3749 if (!nested)
3750 return 1;
3751 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3752 default:
3753 return 1;
3754 }
3755
3756 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003757}
3758
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003759/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 * Reads an msr value (of 'msr_index') into 'pdata'.
3761 * Returns 0 on success, non-0 otherwise.
3762 * Assumes vcpu_load() was already called.
3763 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003764static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003765{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003766 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003767 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003769 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003770#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003772 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 break;
3774 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003775 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003777 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003778 vmx_load_host_state(vmx);
3779 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003780 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003781#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003783 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003784 case MSR_IA32_SPEC_CTRL:
3785 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003786 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3787 return 1;
3788
3789 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3790 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003791 case MSR_IA32_ARCH_CAPABILITIES:
3792 if (!msr_info->host_initiated &&
3793 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3794 return 1;
3795 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3796 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003798 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 break;
3800 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003801 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 break;
3803 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003804 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003806 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003807 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003808 (!msr_info->host_initiated &&
3809 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003810 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003811 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003812 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003813 case MSR_IA32_MCG_EXT_CTL:
3814 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003815 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003816 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003817 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003818 msr_info->data = vcpu->arch.mcg_ext_ctl;
3819 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003820 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003821 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003822 break;
3823 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3824 if (!nested_vmx_allowed(vcpu))
3825 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003826 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3827 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003828 case MSR_IA32_XSS:
3829 if (!vmx_xsaves_supported())
3830 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003831 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003832 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003833 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003834 if (!msr_info->host_initiated &&
3835 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003836 return 1;
3837 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003839 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003840 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003841 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003842 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003844 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 }
3846
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 return 0;
3848}
3849
Jan Kiszkacae50132014-01-04 18:47:22 +01003850static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3851
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852/*
3853 * Writes msr value into into the appropriate "register".
3854 * Returns 0 on success, non-0 otherwise.
3855 * Assumes vcpu_load() was already called.
3856 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003857static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003859 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003860 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003861 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003862 u32 msr_index = msr_info->index;
3863 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003864
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003866 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003867 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003868 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003869#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003871 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 vmcs_writel(GUEST_FS_BASE, data);
3873 break;
3874 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003875 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 vmcs_writel(GUEST_GS_BASE, data);
3877 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003878 case MSR_KERNEL_GS_BASE:
3879 vmx_load_host_state(vmx);
3880 vmx->msr_guest_kernel_gs_base = data;
3881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882#endif
3883 case MSR_IA32_SYSENTER_CS:
3884 vmcs_write32(GUEST_SYSENTER_CS, data);
3885 break;
3886 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003887 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 break;
3889 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003890 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003892 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003893 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003894 (!msr_info->host_initiated &&
3895 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003896 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003897 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003898 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003900 vmcs_write64(GUEST_BNDCFGS, data);
3901 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003902 case MSR_IA32_SPEC_CTRL:
3903 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003904 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3905 return 1;
3906
3907 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003908 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003909 return 1;
3910
3911 vmx->spec_ctrl = data;
3912
3913 if (!data)
3914 break;
3915
3916 /*
3917 * For non-nested:
3918 * When it's written (to non-zero) for the first time, pass
3919 * it through.
3920 *
3921 * For nested:
3922 * The handling of the MSR bitmap for L2 guests is done in
3923 * nested_vmx_merge_msr_bitmap. We should not touch the
3924 * vmcs02.msr_bitmap here since it gets completely overwritten
3925 * in the merging. We update the vmcs01 here for L1 as well
3926 * since it will end up touching the MSR anyway now.
3927 */
3928 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3929 MSR_IA32_SPEC_CTRL,
3930 MSR_TYPE_RW);
3931 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003932 case MSR_IA32_PRED_CMD:
3933 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003934 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3935 return 1;
3936
3937 if (data & ~PRED_CMD_IBPB)
3938 return 1;
3939
3940 if (!data)
3941 break;
3942
3943 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3944
3945 /*
3946 * For non-nested:
3947 * When it's written (to non-zero) for the first time, pass
3948 * it through.
3949 *
3950 * For nested:
3951 * The handling of the MSR bitmap for L2 guests is done in
3952 * nested_vmx_merge_msr_bitmap. We should not touch the
3953 * vmcs02.msr_bitmap here since it gets completely overwritten
3954 * in the merging.
3955 */
3956 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3957 MSR_TYPE_W);
3958 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003959 case MSR_IA32_ARCH_CAPABILITIES:
3960 if (!msr_info->host_initiated)
3961 return 1;
3962 vmx->arch_capabilities = data;
3963 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003964 case MSR_IA32_CR_PAT:
3965 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003966 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3967 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003968 vmcs_write64(GUEST_IA32_PAT, data);
3969 vcpu->arch.pat = data;
3970 break;
3971 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003972 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003973 break;
Will Auldba904632012-11-29 12:42:50 -08003974 case MSR_IA32_TSC_ADJUST:
3975 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003976 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003977 case MSR_IA32_MCG_EXT_CTL:
3978 if ((!msr_info->host_initiated &&
3979 !(to_vmx(vcpu)->msr_ia32_feature_control &
3980 FEATURE_CONTROL_LMCE)) ||
3981 (data & ~MCG_EXT_CTL_LMCE_EN))
3982 return 1;
3983 vcpu->arch.mcg_ext_ctl = data;
3984 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003985 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003986 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003987 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003988 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3989 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003990 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003991 if (msr_info->host_initiated && data == 0)
3992 vmx_leave_nested(vcpu);
3993 break;
3994 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003995 if (!msr_info->host_initiated)
3996 return 1; /* they are read-only */
3997 if (!nested_vmx_allowed(vcpu))
3998 return 1;
3999 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004000 case MSR_IA32_XSS:
4001 if (!vmx_xsaves_supported())
4002 return 1;
4003 /*
4004 * The only supported bit as of Skylake is bit 8, but
4005 * it is not supported on KVM.
4006 */
4007 if (data != 0)
4008 return 1;
4009 vcpu->arch.ia32_xss = data;
4010 if (vcpu->arch.ia32_xss != host_xss)
4011 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4012 vcpu->arch.ia32_xss, host_xss);
4013 else
4014 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4015 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004016 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004017 if (!msr_info->host_initiated &&
4018 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004019 return 1;
4020 /* Check reserved bit, higher 32 bits should be zero */
4021 if ((data >> 32) != 0)
4022 return 1;
4023 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004025 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004026 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004027 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004028 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004029 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4030 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004031 ret = kvm_set_shared_msr(msr->index, msr->data,
4032 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004033 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004034 if (ret)
4035 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004036 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004037 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004039 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040 }
4041
Eddie Dong2cc51562007-05-21 07:28:09 +03004042 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043}
4044
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004045static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004047 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4048 switch (reg) {
4049 case VCPU_REGS_RSP:
4050 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4051 break;
4052 case VCPU_REGS_RIP:
4053 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4054 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004055 case VCPU_EXREG_PDPTR:
4056 if (enable_ept)
4057 ept_save_pdptrs(vcpu);
4058 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004059 default:
4060 break;
4061 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062}
4063
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064static __init int cpu_has_kvm_support(void)
4065{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004066 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067}
4068
4069static __init int vmx_disabled_by_bios(void)
4070{
4071 u64 msr;
4072
4073 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004074 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004075 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004076 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4077 && tboot_enabled())
4078 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004079 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004080 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004081 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004082 && !tboot_enabled()) {
4083 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004084 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004085 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004086 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004087 /* launched w/o TXT and VMX disabled */
4088 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4089 && !tboot_enabled())
4090 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004091 }
4092
4093 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094}
4095
Dongxiao Xu7725b892010-05-11 18:29:38 +08004096static void kvm_cpu_vmxon(u64 addr)
4097{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004098 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004099 intel_pt_handle_vmx(1);
4100
Dongxiao Xu7725b892010-05-11 18:29:38 +08004101 asm volatile (ASM_VMX_VMXON_RAX
4102 : : "a"(&addr), "m"(addr)
4103 : "memory", "cc");
4104}
4105
Radim Krčmář13a34e02014-08-28 15:13:03 +02004106static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107{
4108 int cpu = raw_smp_processor_id();
4109 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004110 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004112 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004113 return -EBUSY;
4114
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004115 /*
4116 * This can happen if we hot-added a CPU but failed to allocate
4117 * VP assist page for it.
4118 */
4119 if (static_branch_unlikely(&enable_evmcs) &&
4120 !hv_get_vp_assist_page(cpu))
4121 return -EFAULT;
4122
Nadav Har'Eld462b812011-05-24 15:26:10 +03004123 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004124 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4125 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004126
4127 /*
4128 * Now we can enable the vmclear operation in kdump
4129 * since the loaded_vmcss_on_cpu list on this cpu
4130 * has been initialized.
4131 *
4132 * Though the cpu is not in VMX operation now, there
4133 * is no problem to enable the vmclear operation
4134 * for the loaded_vmcss_on_cpu list is empty!
4135 */
4136 crash_enable_local_vmclear(cpu);
4137
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004139
4140 test_bits = FEATURE_CONTROL_LOCKED;
4141 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4142 if (tboot_enabled())
4143 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4144
4145 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004147 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4148 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004149 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004150 if (enable_ept)
4151 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004152
4153 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154}
4155
Nadav Har'Eld462b812011-05-24 15:26:10 +03004156static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004157{
4158 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004159 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004160
Nadav Har'Eld462b812011-05-24 15:26:10 +03004161 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4162 loaded_vmcss_on_cpu_link)
4163 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004164}
4165
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004166
4167/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4168 * tricks.
4169 */
4170static void kvm_cpu_vmxoff(void)
4171{
4172 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004173
4174 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004175 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004176}
4177
Radim Krčmář13a34e02014-08-28 15:13:03 +02004178static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004180 vmclear_local_loaded_vmcss();
4181 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182}
4183
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004184static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004185 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186{
4187 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004188 u32 ctl = ctl_min | ctl_opt;
4189
4190 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4191
4192 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4193 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4194
4195 /* Ensure minimum (required) set of control bits are supported. */
4196 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004197 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004198
4199 *result = ctl;
4200 return 0;
4201}
4202
Avi Kivity110312c2010-12-21 12:54:20 +02004203static __init bool allow_1_setting(u32 msr, u32 ctl)
4204{
4205 u32 vmx_msr_low, vmx_msr_high;
4206
4207 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4208 return vmx_msr_high & ctl;
4209}
4210
Yang, Sheng002c7f72007-07-31 14:23:01 +03004211static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004212{
4213 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004214 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004215 u32 _pin_based_exec_control = 0;
4216 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004217 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004218 u32 _vmexit_control = 0;
4219 u32 _vmentry_control = 0;
4220
Paolo Bonzini13893092018-02-26 13:40:09 +01004221 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304222 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004223#ifdef CONFIG_X86_64
4224 CPU_BASED_CR8_LOAD_EXITING |
4225 CPU_BASED_CR8_STORE_EXITING |
4226#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004227 CPU_BASED_CR3_LOAD_EXITING |
4228 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004229 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004230 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004231 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004232 CPU_BASED_MWAIT_EXITING |
4233 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004234 CPU_BASED_INVLPG_EXITING |
4235 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004236
Sheng Yangf78e0e22007-10-29 09:40:42 +08004237 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004238 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004239 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004240 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4241 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004242 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004243#ifdef CONFIG_X86_64
4244 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4245 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4246 ~CPU_BASED_CR8_STORE_EXITING;
4247#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004248 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004249 min2 = 0;
4250 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004251 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004252 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004253 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004254 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004255 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004256 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004257 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004258 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004259 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004260 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004261 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004262 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004263 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004264 SECONDARY_EXEC_RDSEED_EXITING |
4265 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004266 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004267 SECONDARY_EXEC_TSC_SCALING |
4268 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004269 if (adjust_vmx_controls(min2, opt2,
4270 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004271 &_cpu_based_2nd_exec_control) < 0)
4272 return -EIO;
4273 }
4274#ifndef CONFIG_X86_64
4275 if (!(_cpu_based_2nd_exec_control &
4276 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4277 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4278#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004279
4280 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4281 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004282 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004283 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4284 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004285
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004286 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4287 &vmx_capability.ept, &vmx_capability.vpid);
4288
Sheng Yangd56f5462008-04-25 10:13:16 +08004289 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004290 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4291 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004292 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4293 CPU_BASED_CR3_STORE_EXITING |
4294 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004295 } else if (vmx_capability.ept) {
4296 vmx_capability.ept = 0;
4297 pr_warn_once("EPT CAP should not exist if not support "
4298 "1-setting enable EPT VM-execution control\n");
4299 }
4300 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4301 vmx_capability.vpid) {
4302 vmx_capability.vpid = 0;
4303 pr_warn_once("VPID CAP should not exist if not support "
4304 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004305 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004306
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004307 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004308#ifdef CONFIG_X86_64
4309 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4310#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004311 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004312 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004313 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4314 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004315 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004316
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004317 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4318 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4319 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004320 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4321 &_pin_based_exec_control) < 0)
4322 return -EIO;
4323
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004324 if (cpu_has_broken_vmx_preemption_timer())
4325 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004326 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004327 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004328 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4329
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004330 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004331 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004332 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4333 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004334 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004336 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004337
4338 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4339 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004340 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004341
4342#ifdef CONFIG_X86_64
4343 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4344 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004345 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004346#endif
4347
4348 /* Require Write-Back (WB) memory type for VMCS accesses. */
4349 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004350 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004351
Yang, Sheng002c7f72007-07-31 14:23:01 +03004352 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004353 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004354 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004355
Liran Alon2307af12018-06-29 22:59:04 +03004356 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004357
Yang, Sheng002c7f72007-07-31 14:23:01 +03004358 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4359 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004360 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004361 vmcs_conf->vmexit_ctrl = _vmexit_control;
4362 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004363
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004364 if (static_branch_unlikely(&enable_evmcs))
4365 evmcs_sanitize_exec_ctrls(vmcs_conf);
4366
Avi Kivity110312c2010-12-21 12:54:20 +02004367 cpu_has_load_ia32_efer =
4368 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4369 VM_ENTRY_LOAD_IA32_EFER)
4370 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4371 VM_EXIT_LOAD_IA32_EFER);
4372
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004373 cpu_has_load_perf_global_ctrl =
4374 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4375 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4376 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4377 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4378
4379 /*
4380 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004381 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004382 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4383 *
4384 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4385 *
4386 * AAK155 (model 26)
4387 * AAP115 (model 30)
4388 * AAT100 (model 37)
4389 * BC86,AAY89,BD102 (model 44)
4390 * BA97 (model 46)
4391 *
4392 */
4393 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4394 switch (boot_cpu_data.x86_model) {
4395 case 26:
4396 case 30:
4397 case 37:
4398 case 44:
4399 case 46:
4400 cpu_has_load_perf_global_ctrl = false;
4401 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4402 "does not work properly. Using workaround\n");
4403 break;
4404 default:
4405 break;
4406 }
4407 }
4408
Borislav Petkov782511b2016-04-04 22:25:03 +02004409 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004410 rdmsrl(MSR_IA32_XSS, host_xss);
4411
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004412 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004413}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414
4415static struct vmcs *alloc_vmcs_cpu(int cpu)
4416{
4417 int node = cpu_to_node(cpu);
4418 struct page *pages;
4419 struct vmcs *vmcs;
4420
Vlastimil Babka96db8002015-09-08 15:03:50 -07004421 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422 if (!pages)
4423 return NULL;
4424 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004425 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004426
4427 /* KVM supports Enlightened VMCS v1 only */
4428 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004429 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004430 else
Liran Alon392b2f22018-06-23 02:35:01 +03004431 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004432
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 return vmcs;
4434}
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436static void free_vmcs(struct vmcs *vmcs)
4437{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004438 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439}
4440
Nadav Har'Eld462b812011-05-24 15:26:10 +03004441/*
4442 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4443 */
4444static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4445{
4446 if (!loaded_vmcs->vmcs)
4447 return;
4448 loaded_vmcs_clear(loaded_vmcs);
4449 free_vmcs(loaded_vmcs->vmcs);
4450 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004451 if (loaded_vmcs->msr_bitmap)
4452 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004453 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454}
4455
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004456static struct vmcs *alloc_vmcs(void)
4457{
4458 return alloc_vmcs_cpu(raw_smp_processor_id());
4459}
4460
4461static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4462{
4463 loaded_vmcs->vmcs = alloc_vmcs();
4464 if (!loaded_vmcs->vmcs)
4465 return -ENOMEM;
4466
4467 loaded_vmcs->shadow_vmcs = NULL;
4468 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004469
4470 if (cpu_has_vmx_msr_bitmap()) {
4471 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4472 if (!loaded_vmcs->msr_bitmap)
4473 goto out_vmcs;
4474 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004475
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004476 if (IS_ENABLED(CONFIG_HYPERV) &&
4477 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004478 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4479 struct hv_enlightened_vmcs *evmcs =
4480 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4481
4482 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4483 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004484 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004485 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004486
4487out_vmcs:
4488 free_loaded_vmcs(loaded_vmcs);
4489 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004490}
4491
Sam Ravnborg39959582007-06-01 00:47:13 -07004492static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493{
4494 int cpu;
4495
Zachary Amsden3230bb42009-09-29 11:38:37 -10004496 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004498 per_cpu(vmxarea, cpu) = NULL;
4499 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500}
4501
Jim Mattsond37f4262017-12-22 12:12:16 -08004502enum vmcs_field_width {
4503 VMCS_FIELD_WIDTH_U16 = 0,
4504 VMCS_FIELD_WIDTH_U64 = 1,
4505 VMCS_FIELD_WIDTH_U32 = 2,
4506 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004507};
4508
Jim Mattsond37f4262017-12-22 12:12:16 -08004509static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004510{
4511 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004512 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004513 return (field >> 13) & 0x3 ;
4514}
4515
4516static inline int vmcs_field_readonly(unsigned long field)
4517{
4518 return (((field >> 10) & 0x3) == 1);
4519}
4520
Bandan Dasfe2b2012014-04-21 15:20:14 -04004521static void init_vmcs_shadow_fields(void)
4522{
4523 int i, j;
4524
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004525 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4526 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004527 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004528 (i + 1 == max_shadow_read_only_fields ||
4529 shadow_read_only_fields[i + 1] != field + 1))
4530 pr_err("Missing field from shadow_read_only_field %x\n",
4531 field + 1);
4532
4533 clear_bit(field, vmx_vmread_bitmap);
4534#ifdef CONFIG_X86_64
4535 if (field & 1)
4536 continue;
4537#endif
4538 if (j < i)
4539 shadow_read_only_fields[j] = field;
4540 j++;
4541 }
4542 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004543
4544 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004545 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004546 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004547 (i + 1 == max_shadow_read_write_fields ||
4548 shadow_read_write_fields[i + 1] != field + 1))
4549 pr_err("Missing field from shadow_read_write_field %x\n",
4550 field + 1);
4551
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004552 /*
4553 * PML and the preemption timer can be emulated, but the
4554 * processor cannot vmwrite to fields that don't exist
4555 * on bare metal.
4556 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004557 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004558 case GUEST_PML_INDEX:
4559 if (!cpu_has_vmx_pml())
4560 continue;
4561 break;
4562 case VMX_PREEMPTION_TIMER_VALUE:
4563 if (!cpu_has_vmx_preemption_timer())
4564 continue;
4565 break;
4566 case GUEST_INTR_STATUS:
4567 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004568 continue;
4569 break;
4570 default:
4571 break;
4572 }
4573
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004574 clear_bit(field, vmx_vmwrite_bitmap);
4575 clear_bit(field, vmx_vmread_bitmap);
4576#ifdef CONFIG_X86_64
4577 if (field & 1)
4578 continue;
4579#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004580 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004581 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004582 j++;
4583 }
4584 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004585}
4586
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587static __init int alloc_kvm_area(void)
4588{
4589 int cpu;
4590
Zachary Amsden3230bb42009-09-29 11:38:37 -10004591 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004592 struct vmcs *vmcs;
4593
4594 vmcs = alloc_vmcs_cpu(cpu);
4595 if (!vmcs) {
4596 free_kvm_area();
4597 return -ENOMEM;
4598 }
4599
Liran Alon2307af12018-06-29 22:59:04 +03004600 /*
4601 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4602 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4603 * revision_id reported by MSR_IA32_VMX_BASIC.
4604 *
4605 * However, even though not explictly documented by
4606 * TLFS, VMXArea passed as VMXON argument should
4607 * still be marked with revision_id reported by
4608 * physical CPU.
4609 */
4610 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004611 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004612
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613 per_cpu(vmxarea, cpu) = vmcs;
4614 }
4615 return 0;
4616}
4617
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004618static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004619 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004621 if (!emulate_invalid_guest_state) {
4622 /*
4623 * CS and SS RPL should be equal during guest entry according
4624 * to VMX spec, but in reality it is not always so. Since vcpu
4625 * is in the middle of the transition from real mode to
4626 * protected mode it is safe to assume that RPL 0 is a good
4627 * default value.
4628 */
4629 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004630 save->selector &= ~SEGMENT_RPL_MASK;
4631 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004632 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004634 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635}
4636
4637static void enter_pmode(struct kvm_vcpu *vcpu)
4638{
4639 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641
Gleb Natapovd99e4152012-12-20 16:57:45 +02004642 /*
4643 * Update real mode segment cache. It may be not up-to-date if sement
4644 * register was written while vcpu was in a guest mode.
4645 */
4646 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4647 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4648 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4649 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4650 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4651 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4652
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004653 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654
Avi Kivity2fb92db2011-04-27 19:42:18 +03004655 vmx_segment_cache_clear(vmx);
4656
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004657 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004658
4659 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004660 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4661 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004662 vmcs_writel(GUEST_RFLAGS, flags);
4663
Rusty Russell66aee912007-07-17 23:34:16 +10004664 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4665 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666
4667 update_exception_bitmap(vcpu);
4668
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004669 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4670 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4671 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4672 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4673 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4674 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675}
4676
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004677static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678{
Mathias Krause772e0312012-08-30 01:30:19 +02004679 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004680 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681
Gleb Natapovd99e4152012-12-20 16:57:45 +02004682 var.dpl = 0x3;
4683 if (seg == VCPU_SREG_CS)
4684 var.type = 0x3;
4685
4686 if (!emulate_invalid_guest_state) {
4687 var.selector = var.base >> 4;
4688 var.base = var.base & 0xffff0;
4689 var.limit = 0xffff;
4690 var.g = 0;
4691 var.db = 0;
4692 var.present = 1;
4693 var.s = 1;
4694 var.l = 0;
4695 var.unusable = 0;
4696 var.type = 0x3;
4697 var.avl = 0;
4698 if (save->base & 0xf)
4699 printk_once(KERN_WARNING "kvm: segment base is not "
4700 "paragraph aligned when entering "
4701 "protected mode (seg=%d)", seg);
4702 }
4703
4704 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004705 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004706 vmcs_write32(sf->limit, var.limit);
4707 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708}
4709
4710static void enter_rmode(struct kvm_vcpu *vcpu)
4711{
4712 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004714 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4718 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004723
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004724 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725
Gleb Natapov776e58e2011-03-13 12:34:27 +02004726 /*
4727 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004728 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004729 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004730 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004731 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4732 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004733
Avi Kivity2fb92db2011-04-27 19:42:18 +03004734 vmx_segment_cache_clear(vmx);
4735
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004736 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4739
4740 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004741 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004743 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744
4745 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004746 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747 update_exception_bitmap(vcpu);
4748
Gleb Natapovd99e4152012-12-20 16:57:45 +02004749 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4750 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4751 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4752 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4753 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4754 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004755
Eddie Dong8668a3c2007-10-10 14:26:45 +08004756 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757}
4758
Amit Shah401d10d2009-02-20 22:53:37 +05304759static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4760{
4761 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004762 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4763
4764 if (!msr)
4765 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304766
Avi Kivity44ea2b12009-09-06 15:55:37 +03004767 /*
4768 * Force kernel_gs_base reloading before EFER changes, as control
4769 * of this msr depends on is_long_mode().
4770 */
4771 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004772 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304773 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004774 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304775 msr->data = efer;
4776 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004777 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304778
4779 msr->data = efer & ~EFER_LME;
4780 }
4781 setup_msrs(vmx);
4782}
4783
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004784#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785
4786static void enter_lmode(struct kvm_vcpu *vcpu)
4787{
4788 u32 guest_tr_ar;
4789
Avi Kivity2fb92db2011-04-27 19:42:18 +03004790 vmx_segment_cache_clear(to_vmx(vcpu));
4791
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004793 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004794 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4795 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004797 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4798 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 }
Avi Kivityda38f432010-07-06 11:30:49 +03004800 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801}
4802
4803static void exit_lmode(struct kvm_vcpu *vcpu)
4804{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004805 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004806 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004807}
4808
4809#endif
4810
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004811static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4812 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004813{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004814 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004815 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4816 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004817 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004818 } else {
4819 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004820 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004821}
4822
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004823static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004824{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004825 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004826}
4827
Avi Kivitye8467fd2009-12-29 18:43:06 +02004828static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4829{
4830 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4831
4832 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4833 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4834}
4835
Avi Kivityaff48ba2010-12-05 18:56:11 +02004836static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4837{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004838 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004839 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4840 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4841}
4842
Anthony Liguori25c4c272007-04-27 09:29:21 +03004843static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004844{
Avi Kivityfc78f512009-12-07 12:16:48 +02004845 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4846
4847 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4848 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004849}
4850
Sheng Yang14394422008-04-28 12:24:45 +08004851static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4852{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004853 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4854
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004855 if (!test_bit(VCPU_EXREG_PDPTR,
4856 (unsigned long *)&vcpu->arch.regs_dirty))
4857 return;
4858
Sheng Yang14394422008-04-28 12:24:45 +08004859 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004860 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4861 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4862 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4863 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004864 }
4865}
4866
Avi Kivity8f5d5492009-05-31 18:41:29 +03004867static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4868{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004869 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4870
Avi Kivity8f5d5492009-05-31 18:41:29 +03004871 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004872 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4873 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4874 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4875 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004876 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004877
4878 __set_bit(VCPU_EXREG_PDPTR,
4879 (unsigned long *)&vcpu->arch.regs_avail);
4880 __set_bit(VCPU_EXREG_PDPTR,
4881 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004882}
4883
David Matlack38991522016-11-29 18:14:08 -08004884static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4885{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004886 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4887 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004888 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4889
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004890 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004891 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4892 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4893 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4894
4895 return fixed_bits_valid(val, fixed0, fixed1);
4896}
4897
4898static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4899{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004900 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4901 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004902
4903 return fixed_bits_valid(val, fixed0, fixed1);
4904}
4905
4906static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4907{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004908 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4909 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004910
4911 return fixed_bits_valid(val, fixed0, fixed1);
4912}
4913
4914/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4915#define nested_guest_cr4_valid nested_cr4_valid
4916#define nested_host_cr4_valid nested_cr4_valid
4917
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004918static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004919
4920static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4921 unsigned long cr0,
4922 struct kvm_vcpu *vcpu)
4923{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004924 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4925 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004926 if (!(cr0 & X86_CR0_PG)) {
4927 /* From paging/starting to nonpaging */
4928 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004929 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004930 (CPU_BASED_CR3_LOAD_EXITING |
4931 CPU_BASED_CR3_STORE_EXITING));
4932 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004933 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004934 } else if (!is_paging(vcpu)) {
4935 /* From nonpaging to paging */
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 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004943
4944 if (!(cr0 & X86_CR0_WP))
4945 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004946}
4947
Avi Kivity6aa8b732006-12-10 02:21:36 -08004948static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4949{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004950 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004951 unsigned long hw_cr0;
4952
Gleb Natapov50378782013-02-04 16:00:28 +02004953 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004954 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004955 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004956 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004957 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004958
Gleb Natapov218e7632013-01-21 15:36:45 +02004959 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4960 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961
Gleb Natapov218e7632013-01-21 15:36:45 +02004962 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4963 enter_rmode(vcpu);
4964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004966#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004967 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004968 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004970 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 exit_lmode(vcpu);
4972 }
4973#endif
4974
Sean Christophersonb4d18512018-03-05 12:04:40 -08004975 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004976 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4977
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004979 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004980 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004981
4982 /* depends on vcpu->arch.cr0 to be set to a new value */
4983 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984}
4985
Yu Zhang855feb62017-08-24 20:27:55 +08004986static int get_ept_level(struct kvm_vcpu *vcpu)
4987{
4988 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4989 return 5;
4990 return 4;
4991}
4992
Peter Feiner995f00a2017-06-30 17:26:32 -07004993static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004994{
Yu Zhang855feb62017-08-24 20:27:55 +08004995 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004996
Yu Zhang855feb62017-08-24 20:27:55 +08004997 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004998
Peter Feiner995f00a2017-06-30 17:26:32 -07004999 if (enable_ept_ad_bits &&
5000 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005001 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005002 eptp |= (root_hpa & PAGE_MASK);
5003
5004 return eptp;
5005}
5006
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5008{
Sheng Yang14394422008-04-28 12:24:45 +08005009 unsigned long guest_cr3;
5010 u64 eptp;
5011
5012 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005013 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005014 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005015 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005016 if (enable_unrestricted_guest || is_paging(vcpu) ||
5017 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005018 guest_cr3 = kvm_read_cr3(vcpu);
5019 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005020 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005021 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005022 }
5023
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005024 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005025 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026}
5027
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005028static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005030 /*
5031 * Pass through host's Machine Check Enable value to hw_cr4, which
5032 * is in force while we are in guest mode. Do not let guests control
5033 * this bit, even if host CR4.MCE == 0.
5034 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005035 unsigned long hw_cr4;
5036
5037 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5038 if (enable_unrestricted_guest)
5039 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5040 else if (to_vmx(vcpu)->rmode.vm86_active)
5041 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5042 else
5043 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005044
Sean Christopherson64f7a112018-04-30 10:01:06 -07005045 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5046 if (cr4 & X86_CR4_UMIP) {
5047 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005048 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005049 hw_cr4 &= ~X86_CR4_UMIP;
5050 } else if (!is_guest_mode(vcpu) ||
5051 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5052 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5053 SECONDARY_EXEC_DESC);
5054 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005055
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005056 if (cr4 & X86_CR4_VMXE) {
5057 /*
5058 * To use VMXON (and later other VMX instructions), a guest
5059 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5060 * So basically the check on whether to allow nested VMX
5061 * is here.
5062 */
5063 if (!nested_vmx_allowed(vcpu))
5064 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005065 }
David Matlack38991522016-11-29 18:14:08 -08005066
5067 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005068 return 1;
5069
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005070 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005071
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005072 if (!enable_unrestricted_guest) {
5073 if (enable_ept) {
5074 if (!is_paging(vcpu)) {
5075 hw_cr4 &= ~X86_CR4_PAE;
5076 hw_cr4 |= X86_CR4_PSE;
5077 } else if (!(cr4 & X86_CR4_PAE)) {
5078 hw_cr4 &= ~X86_CR4_PAE;
5079 }
5080 }
5081
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005082 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005083 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5084 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5085 * to be manually disabled when guest switches to non-paging
5086 * mode.
5087 *
5088 * If !enable_unrestricted_guest, the CPU is always running
5089 * with CR0.PG=1 and CR4 needs to be modified.
5090 * If enable_unrestricted_guest, the CPU automatically
5091 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005092 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005093 if (!is_paging(vcpu))
5094 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5095 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005096
Sheng Yang14394422008-04-28 12:24:45 +08005097 vmcs_writel(CR4_READ_SHADOW, cr4);
5098 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005099 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
5101
Avi Kivity6aa8b732006-12-10 02:21:36 -08005102static void vmx_get_segment(struct kvm_vcpu *vcpu,
5103 struct kvm_segment *var, int seg)
5104{
Avi Kivitya9179492011-01-03 14:28:52 +02005105 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106 u32 ar;
5107
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005108 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005109 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005110 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005111 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005112 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005113 var->base = vmx_read_guest_seg_base(vmx, seg);
5114 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5115 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005116 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005117 var->base = vmx_read_guest_seg_base(vmx, seg);
5118 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5119 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5120 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005121 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122 var->type = ar & 15;
5123 var->s = (ar >> 4) & 1;
5124 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005125 /*
5126 * Some userspaces do not preserve unusable property. Since usable
5127 * segment has to be present according to VMX spec we can use present
5128 * property to amend userspace bug by making unusable segment always
5129 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5130 * segment as unusable.
5131 */
5132 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133 var->avl = (ar >> 12) & 1;
5134 var->l = (ar >> 13) & 1;
5135 var->db = (ar >> 14) & 1;
5136 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137}
5138
Avi Kivitya9179492011-01-03 14:28:52 +02005139static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5140{
Avi Kivitya9179492011-01-03 14:28:52 +02005141 struct kvm_segment s;
5142
5143 if (to_vmx(vcpu)->rmode.vm86_active) {
5144 vmx_get_segment(vcpu, &s, seg);
5145 return s.base;
5146 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005147 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005148}
5149
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005150static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005151{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005152 struct vcpu_vmx *vmx = to_vmx(vcpu);
5153
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005154 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005155 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005156 else {
5157 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005158 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005159 }
Avi Kivity69c73022011-03-07 15:26:44 +02005160}
5161
Avi Kivity653e3102007-05-07 10:55:37 +03005162static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 u32 ar;
5165
Avi Kivityf0495f92012-06-07 17:06:10 +03005166 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167 ar = 1 << 16;
5168 else {
5169 ar = var->type & 15;
5170 ar |= (var->s & 1) << 4;
5171 ar |= (var->dpl & 3) << 5;
5172 ar |= (var->present & 1) << 7;
5173 ar |= (var->avl & 1) << 12;
5174 ar |= (var->l & 1) << 13;
5175 ar |= (var->db & 1) << 14;
5176 ar |= (var->g & 1) << 15;
5177 }
Avi Kivity653e3102007-05-07 10:55:37 +03005178
5179 return ar;
5180}
5181
5182static void vmx_set_segment(struct kvm_vcpu *vcpu,
5183 struct kvm_segment *var, int seg)
5184{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005185 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005186 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005187
Avi Kivity2fb92db2011-04-27 19:42:18 +03005188 vmx_segment_cache_clear(vmx);
5189
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005190 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5191 vmx->rmode.segs[seg] = *var;
5192 if (seg == VCPU_SREG_TR)
5193 vmcs_write16(sf->selector, var->selector);
5194 else if (var->s)
5195 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005196 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005197 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005198
Avi Kivity653e3102007-05-07 10:55:37 +03005199 vmcs_writel(sf->base, var->base);
5200 vmcs_write32(sf->limit, var->limit);
5201 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005202
5203 /*
5204 * Fix the "Accessed" bit in AR field of segment registers for older
5205 * qemu binaries.
5206 * IA32 arch specifies that at the time of processor reset the
5207 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005208 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005209 * state vmexit when "unrestricted guest" mode is turned on.
5210 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5211 * tree. Newer qemu binaries with that qemu fix would not need this
5212 * kvm hack.
5213 */
5214 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005215 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005216
Gleb Natapovf924d662012-12-12 19:10:55 +02005217 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005218
5219out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005220 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005221}
5222
Avi Kivity6aa8b732006-12-10 02:21:36 -08005223static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5224{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005225 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005226
5227 *db = (ar >> 14) & 1;
5228 *l = (ar >> 13) & 1;
5229}
5230
Gleb Natapov89a27f42010-02-16 10:51:48 +02005231static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005233 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5234 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235}
5236
Gleb Natapov89a27f42010-02-16 10:51:48 +02005237static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005239 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5240 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005241}
5242
Gleb Natapov89a27f42010-02-16 10:51:48 +02005243static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005245 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5246 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247}
5248
Gleb Natapov89a27f42010-02-16 10:51:48 +02005249static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005251 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5252 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253}
5254
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005255static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5256{
5257 struct kvm_segment var;
5258 u32 ar;
5259
5260 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005261 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005262 if (seg == VCPU_SREG_CS)
5263 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005264 ar = vmx_segment_access_rights(&var);
5265
5266 if (var.base != (var.selector << 4))
5267 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005268 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005269 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005270 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005271 return false;
5272
5273 return true;
5274}
5275
5276static bool code_segment_valid(struct kvm_vcpu *vcpu)
5277{
5278 struct kvm_segment cs;
5279 unsigned int cs_rpl;
5280
5281 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005282 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005283
Avi Kivity1872a3f2009-01-04 23:26:52 +02005284 if (cs.unusable)
5285 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005286 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005287 return false;
5288 if (!cs.s)
5289 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005290 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005291 if (cs.dpl > cs_rpl)
5292 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005293 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005294 if (cs.dpl != cs_rpl)
5295 return false;
5296 }
5297 if (!cs.present)
5298 return false;
5299
5300 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5301 return true;
5302}
5303
5304static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5305{
5306 struct kvm_segment ss;
5307 unsigned int ss_rpl;
5308
5309 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005310 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005311
Avi Kivity1872a3f2009-01-04 23:26:52 +02005312 if (ss.unusable)
5313 return true;
5314 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005315 return false;
5316 if (!ss.s)
5317 return false;
5318 if (ss.dpl != ss_rpl) /* DPL != RPL */
5319 return false;
5320 if (!ss.present)
5321 return false;
5322
5323 return true;
5324}
5325
5326static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5327{
5328 struct kvm_segment var;
5329 unsigned int rpl;
5330
5331 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005332 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005333
Avi Kivity1872a3f2009-01-04 23:26:52 +02005334 if (var.unusable)
5335 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005336 if (!var.s)
5337 return false;
5338 if (!var.present)
5339 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005340 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005341 if (var.dpl < rpl) /* DPL < RPL */
5342 return false;
5343 }
5344
5345 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5346 * rights flags
5347 */
5348 return true;
5349}
5350
5351static bool tr_valid(struct kvm_vcpu *vcpu)
5352{
5353 struct kvm_segment tr;
5354
5355 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5356
Avi Kivity1872a3f2009-01-04 23:26:52 +02005357 if (tr.unusable)
5358 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005359 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005361 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362 return false;
5363 if (!tr.present)
5364 return false;
5365
5366 return true;
5367}
5368
5369static bool ldtr_valid(struct kvm_vcpu *vcpu)
5370{
5371 struct kvm_segment ldtr;
5372
5373 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5374
Avi Kivity1872a3f2009-01-04 23:26:52 +02005375 if (ldtr.unusable)
5376 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005377 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005378 return false;
5379 if (ldtr.type != 2)
5380 return false;
5381 if (!ldtr.present)
5382 return false;
5383
5384 return true;
5385}
5386
5387static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5388{
5389 struct kvm_segment cs, ss;
5390
5391 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5392 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5393
Nadav Amitb32a9912015-03-29 16:33:04 +03005394 return ((cs.selector & SEGMENT_RPL_MASK) ==
5395 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005396}
5397
5398/*
5399 * Check if guest state is valid. Returns true if valid, false if
5400 * not.
5401 * We assume that registers are always usable
5402 */
5403static bool guest_state_valid(struct kvm_vcpu *vcpu)
5404{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005405 if (enable_unrestricted_guest)
5406 return true;
5407
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005408 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005409 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005410 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5411 return false;
5412 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5413 return false;
5414 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5415 return false;
5416 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5417 return false;
5418 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5419 return false;
5420 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5421 return false;
5422 } else {
5423 /* protected mode guest state checks */
5424 if (!cs_ss_rpl_check(vcpu))
5425 return false;
5426 if (!code_segment_valid(vcpu))
5427 return false;
5428 if (!stack_segment_valid(vcpu))
5429 return false;
5430 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5431 return false;
5432 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5433 return false;
5434 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5435 return false;
5436 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5437 return false;
5438 if (!tr_valid(vcpu))
5439 return false;
5440 if (!ldtr_valid(vcpu))
5441 return false;
5442 }
5443 /* TODO:
5444 * - Add checks on RIP
5445 * - Add checks on RFLAGS
5446 */
5447
5448 return true;
5449}
5450
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005451static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5452{
5453 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5454}
5455
Mike Dayd77c26f2007-10-08 09:02:08 -04005456static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005458 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005459 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005460 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005462 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005463 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005464 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5465 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005466 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005467 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005468 r = kvm_write_guest_page(kvm, fn++, &data,
5469 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005470 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005471 goto out;
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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5476 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005477 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005478 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005479 r = kvm_write_guest_page(kvm, fn, &data,
5480 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5481 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005482out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005483 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005484 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485}
5486
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005487static int init_rmode_identity_map(struct kvm *kvm)
5488{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005489 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005490 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005491 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005492 u32 tmp;
5493
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005494 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005495 mutex_lock(&kvm->slots_lock);
5496
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005497 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005498 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005499
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005500 if (!kvm_vmx->ept_identity_map_addr)
5501 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5502 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005503
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005504 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005505 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005506 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005507 goto out2;
5508
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005509 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005510 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5511 if (r < 0)
5512 goto out;
5513 /* Set up identity-mapping pagetable for EPT in real mode */
5514 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5515 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5516 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5517 r = kvm_write_guest_page(kvm, identity_map_pfn,
5518 &tmp, i * sizeof(tmp), sizeof(tmp));
5519 if (r < 0)
5520 goto out;
5521 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005522 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005523
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005524out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005525 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005526
5527out2:
5528 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005529 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005530}
5531
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532static void seg_setup(int seg)
5533{
Mathias Krause772e0312012-08-30 01:30:19 +02005534 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005535 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536
5537 vmcs_write16(sf->selector, 0);
5538 vmcs_writel(sf->base, 0);
5539 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005540 ar = 0x93;
5541 if (seg == VCPU_SREG_CS)
5542 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005543
5544 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545}
5546
Sheng Yangf78e0e22007-10-29 09:40:42 +08005547static int alloc_apic_access_page(struct kvm *kvm)
5548{
Xiao Guangrong44841412012-09-07 14:14:20 +08005549 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005550 int r = 0;
5551
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005552 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005553 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005554 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005555 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5556 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005557 if (r)
5558 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005559
Tang Chen73a6d942014-09-11 13:38:00 +08005560 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005561 if (is_error_page(page)) {
5562 r = -EFAULT;
5563 goto out;
5564 }
5565
Tang Chenc24ae0d2014-09-24 15:57:58 +08005566 /*
5567 * Do not pin the page in memory, so that memory hot-unplug
5568 * is able to migrate it.
5569 */
5570 put_page(page);
5571 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005572out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005573 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005574 return r;
5575}
5576
Wanpeng Li991e7a02015-09-16 17:30:05 +08005577static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005578{
5579 int vpid;
5580
Avi Kivity919818a2009-03-23 18:01:29 +02005581 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005582 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005583 spin_lock(&vmx_vpid_lock);
5584 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005585 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005586 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005587 else
5588 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005589 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005590 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005591}
5592
Wanpeng Li991e7a02015-09-16 17:30:05 +08005593static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005594{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005595 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005596 return;
5597 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005598 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005599 spin_unlock(&vmx_vpid_lock);
5600}
5601
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005602static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5603 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005604{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005605 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005606
5607 if (!cpu_has_vmx_msr_bitmap())
5608 return;
5609
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005610 if (static_branch_unlikely(&enable_evmcs))
5611 evmcs_touch_msr_bitmap();
5612
Sheng Yang25c5f222008-03-28 13:18:56 +08005613 /*
5614 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5615 * have the write-low and read-high bitmap offsets the wrong way round.
5616 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5617 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005618 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005619 if (type & MSR_TYPE_R)
5620 /* read-low */
5621 __clear_bit(msr, msr_bitmap + 0x000 / f);
5622
5623 if (type & MSR_TYPE_W)
5624 /* write-low */
5625 __clear_bit(msr, msr_bitmap + 0x800 / f);
5626
Sheng Yang25c5f222008-03-28 13:18:56 +08005627 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5628 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005629 if (type & MSR_TYPE_R)
5630 /* read-high */
5631 __clear_bit(msr, msr_bitmap + 0x400 / f);
5632
5633 if (type & MSR_TYPE_W)
5634 /* write-high */
5635 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5636
5637 }
5638}
5639
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005640static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5641 u32 msr, int type)
5642{
5643 int f = sizeof(unsigned long);
5644
5645 if (!cpu_has_vmx_msr_bitmap())
5646 return;
5647
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005648 if (static_branch_unlikely(&enable_evmcs))
5649 evmcs_touch_msr_bitmap();
5650
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005651 /*
5652 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5653 * have the write-low and read-high bitmap offsets the wrong way round.
5654 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5655 */
5656 if (msr <= 0x1fff) {
5657 if (type & MSR_TYPE_R)
5658 /* read-low */
5659 __set_bit(msr, msr_bitmap + 0x000 / f);
5660
5661 if (type & MSR_TYPE_W)
5662 /* write-low */
5663 __set_bit(msr, msr_bitmap + 0x800 / f);
5664
5665 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5666 msr &= 0x1fff;
5667 if (type & MSR_TYPE_R)
5668 /* read-high */
5669 __set_bit(msr, msr_bitmap + 0x400 / f);
5670
5671 if (type & MSR_TYPE_W)
5672 /* write-high */
5673 __set_bit(msr, msr_bitmap + 0xc00 / f);
5674
5675 }
5676}
5677
5678static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5679 u32 msr, int type, bool value)
5680{
5681 if (value)
5682 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5683 else
5684 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5685}
5686
Wincy Vanf2b93282015-02-03 23:56:03 +08005687/*
5688 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5689 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5690 */
5691static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5692 unsigned long *msr_bitmap_nested,
5693 u32 msr, int type)
5694{
5695 int f = sizeof(unsigned long);
5696
Wincy Vanf2b93282015-02-03 23:56:03 +08005697 /*
5698 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5699 * have the write-low and read-high bitmap offsets the wrong way round.
5700 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5701 */
5702 if (msr <= 0x1fff) {
5703 if (type & MSR_TYPE_R &&
5704 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5705 /* read-low */
5706 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5707
5708 if (type & MSR_TYPE_W &&
5709 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5710 /* write-low */
5711 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5712
5713 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5714 msr &= 0x1fff;
5715 if (type & MSR_TYPE_R &&
5716 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5717 /* read-high */
5718 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5719
5720 if (type & MSR_TYPE_W &&
5721 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5722 /* write-high */
5723 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5724
5725 }
5726}
5727
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005728static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005729{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005730 u8 mode = 0;
5731
5732 if (cpu_has_secondary_exec_ctrls() &&
5733 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5734 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5735 mode |= MSR_BITMAP_MODE_X2APIC;
5736 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5737 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5738 }
5739
5740 if (is_long_mode(vcpu))
5741 mode |= MSR_BITMAP_MODE_LM;
5742
5743 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005744}
5745
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005746#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5747
5748static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5749 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005750{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005751 int msr;
5752
5753 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5754 unsigned word = msr / BITS_PER_LONG;
5755 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5756 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005757 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005758
5759 if (mode & MSR_BITMAP_MODE_X2APIC) {
5760 /*
5761 * TPR reads and writes can be virtualized even if virtual interrupt
5762 * delivery is not in use.
5763 */
5764 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5765 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5766 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5767 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5768 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5769 }
5770 }
5771}
5772
5773static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5774{
5775 struct vcpu_vmx *vmx = to_vmx(vcpu);
5776 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5777 u8 mode = vmx_msr_bitmap_mode(vcpu);
5778 u8 changed = mode ^ vmx->msr_bitmap_mode;
5779
5780 if (!changed)
5781 return;
5782
5783 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5784 !(mode & MSR_BITMAP_MODE_LM));
5785
5786 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5787 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5788
5789 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005790}
5791
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005792static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005793{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005794 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005795}
5796
David Matlackc9f04402017-08-01 14:00:40 -07005797static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5798{
5799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5800 gfn_t gfn;
5801
5802 /*
5803 * Don't need to mark the APIC access page dirty; it is never
5804 * written to by the CPU during APIC virtualization.
5805 */
5806
5807 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5808 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5809 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5810 }
5811
5812 if (nested_cpu_has_posted_intr(vmcs12)) {
5813 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5814 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5815 }
5816}
5817
5818
David Hildenbrand6342c502017-01-25 11:58:58 +01005819static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005820{
5821 struct vcpu_vmx *vmx = to_vmx(vcpu);
5822 int max_irr;
5823 void *vapic_page;
5824 u16 status;
5825
David Matlackc9f04402017-08-01 14:00:40 -07005826 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5827 return;
Wincy Van705699a2015-02-03 23:58:17 +08005828
David Matlackc9f04402017-08-01 14:00:40 -07005829 vmx->nested.pi_pending = false;
5830 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5831 return;
Wincy Van705699a2015-02-03 23:58:17 +08005832
David Matlackc9f04402017-08-01 14:00:40 -07005833 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5834 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005835 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005836 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5837 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005838 kunmap(vmx->nested.virtual_apic_page);
5839
5840 status = vmcs_read16(GUEST_INTR_STATUS);
5841 if ((u8)max_irr > ((u8)status & 0xff)) {
5842 status &= ~0xff;
5843 status |= (u8)max_irr;
5844 vmcs_write16(GUEST_INTR_STATUS, status);
5845 }
5846 }
David Matlackc9f04402017-08-01 14:00:40 -07005847
5848 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005849}
5850
Wincy Van06a55242017-04-28 13:13:59 +08005851static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5852 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005853{
5854#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005855 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5856
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005857 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005858 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005859 * The vector of interrupt to be delivered to vcpu had
5860 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005861 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005862 * Following cases will be reached in this block, and
5863 * we always send a notification event in all cases as
5864 * explained below.
5865 *
5866 * Case 1: vcpu keeps in non-root mode. Sending a
5867 * notification event posts the interrupt to vcpu.
5868 *
5869 * Case 2: vcpu exits to root mode and is still
5870 * runnable. PIR will be synced to vIRR before the
5871 * next vcpu entry. Sending a notification event in
5872 * this case has no effect, as vcpu is not in root
5873 * mode.
5874 *
5875 * Case 3: vcpu exits to root mode and is blocked.
5876 * vcpu_block() has already synced PIR to vIRR and
5877 * never blocks vcpu if vIRR is not cleared. Therefore,
5878 * a blocked vcpu here does not wait for any requested
5879 * interrupts in PIR, and sending a notification event
5880 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005881 */
Feng Wu28b835d2015-09-18 22:29:54 +08005882
Wincy Van06a55242017-04-28 13:13:59 +08005883 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005884 return true;
5885 }
5886#endif
5887 return false;
5888}
5889
Wincy Van705699a2015-02-03 23:58:17 +08005890static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5891 int vector)
5892{
5893 struct vcpu_vmx *vmx = to_vmx(vcpu);
5894
5895 if (is_guest_mode(vcpu) &&
5896 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005897 /*
5898 * If a posted intr is not recognized by hardware,
5899 * we will accomplish it in the next vmentry.
5900 */
5901 vmx->nested.pi_pending = true;
5902 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005903 /* the PIR and ON have been set by L1. */
5904 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5905 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005906 return 0;
5907 }
5908 return -1;
5909}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005911 * Send interrupt to vcpu via posted interrupt way.
5912 * 1. If target vcpu is running(non-root mode), send posted interrupt
5913 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5914 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5915 * interrupt from PIR in next vmentry.
5916 */
5917static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5918{
5919 struct vcpu_vmx *vmx = to_vmx(vcpu);
5920 int r;
5921
Wincy Van705699a2015-02-03 23:58:17 +08005922 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5923 if (!r)
5924 return;
5925
Yang Zhanga20ed542013-04-11 19:25:15 +08005926 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5927 return;
5928
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005929 /* If a previous notification has sent the IPI, nothing to do. */
5930 if (pi_test_and_set_on(&vmx->pi_desc))
5931 return;
5932
Wincy Van06a55242017-04-28 13:13:59 +08005933 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005934 kvm_vcpu_kick(vcpu);
5935}
5936
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005938 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5939 * will not change in the lifetime of the guest.
5940 * Note that host-state that does change is set elsewhere. E.g., host-state
5941 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5942 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005943static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005944{
5945 u32 low32, high32;
5946 unsigned long tmpl;
5947 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005948 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005949
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005950 cr0 = read_cr0();
5951 WARN_ON(cr0 & X86_CR0_TS);
5952 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005953
5954 /*
5955 * Save the most likely value for this task's CR3 in the VMCS.
5956 * We can't use __get_current_cr3_fast() because we're not atomic.
5957 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005958 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005959 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005960 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005961
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005962 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005963 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005964 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005965 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005966
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005967 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005968#ifdef CONFIG_X86_64
5969 /*
5970 * Load null selectors, so we can avoid reloading them in
5971 * __vmx_load_host_state(), in case userspace uses the null selectors
5972 * too (the expected case).
5973 */
5974 vmcs_write16(HOST_DS_SELECTOR, 0);
5975 vmcs_write16(HOST_ES_SELECTOR, 0);
5976#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005977 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5978 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005979#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005980 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5981 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5982
Juergen Gross87930012017-09-04 12:25:27 +02005983 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005984 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005985 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005986
Avi Kivity83287ea422012-09-16 15:10:57 +03005987 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005988
5989 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5990 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5991 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5992 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5993
5994 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5995 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5996 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5997 }
5998}
5999
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006000static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6001{
6002 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6003 if (enable_ept)
6004 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006005 if (is_guest_mode(&vmx->vcpu))
6006 vmx->vcpu.arch.cr4_guest_owned_bits &=
6007 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006008 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6009}
6010
Yang Zhang01e439b2013-04-11 19:25:12 +08006011static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6012{
6013 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6014
Andrey Smetanind62caab2015-11-10 15:36:33 +03006015 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006016 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006017
6018 if (!enable_vnmi)
6019 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6020
Yunhong Jiang64672c92016-06-13 14:19:59 -07006021 /* Enable the preemption timer dynamically */
6022 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006023 return pin_based_exec_ctrl;
6024}
6025
Andrey Smetanind62caab2015-11-10 15:36:33 +03006026static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6027{
6028 struct vcpu_vmx *vmx = to_vmx(vcpu);
6029
6030 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006031 if (cpu_has_secondary_exec_ctrls()) {
6032 if (kvm_vcpu_apicv_active(vcpu))
6033 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6034 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6035 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6036 else
6037 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6038 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6039 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6040 }
6041
6042 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006043 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006044}
6045
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006046static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6047{
6048 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006049
6050 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6051 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6052
Paolo Bonzini35754c92015-07-29 12:05:37 +02006053 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006054 exec_control &= ~CPU_BASED_TPR_SHADOW;
6055#ifdef CONFIG_X86_64
6056 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6057 CPU_BASED_CR8_LOAD_EXITING;
6058#endif
6059 }
6060 if (!enable_ept)
6061 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6062 CPU_BASED_CR3_LOAD_EXITING |
6063 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006064 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6065 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6066 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006067 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6068 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006069 return exec_control;
6070}
6071
Jim Mattson45ec3682017-08-23 16:32:04 -07006072static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006073{
Jim Mattson45ec3682017-08-23 16:32:04 -07006074 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006075 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006076}
6077
Jim Mattson75f4fc82017-08-23 16:32:03 -07006078static bool vmx_rdseed_supported(void)
6079{
6080 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006081 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006082}
6083
Paolo Bonzini80154d72017-08-24 13:55:35 +02006084static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006085{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006086 struct kvm_vcpu *vcpu = &vmx->vcpu;
6087
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006088 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006089
Paolo Bonzini80154d72017-08-24 13:55:35 +02006090 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006091 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6092 if (vmx->vpid == 0)
6093 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6094 if (!enable_ept) {
6095 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6096 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006097 /* Enable INVPCID for non-ept guests may cause performance regression. */
6098 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006099 }
6100 if (!enable_unrestricted_guest)
6101 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006102 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006103 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006104 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006105 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6106 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006107 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006108
6109 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6110 * in vmx_set_cr4. */
6111 exec_control &= ~SECONDARY_EXEC_DESC;
6112
Abel Gordonabc4fc52013-04-18 14:35:25 +03006113 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6114 (handle_vmptrld).
6115 We can NOT enable shadow_vmcs here because we don't have yet
6116 a current VMCS12
6117 */
6118 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006119
6120 if (!enable_pml)
6121 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006122
Paolo Bonzini3db13482017-08-24 14:48:03 +02006123 if (vmx_xsaves_supported()) {
6124 /* Exposing XSAVES only when XSAVE is exposed */
6125 bool xsaves_enabled =
6126 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6127 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6128
6129 if (!xsaves_enabled)
6130 exec_control &= ~SECONDARY_EXEC_XSAVES;
6131
6132 if (nested) {
6133 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006134 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006135 SECONDARY_EXEC_XSAVES;
6136 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006137 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006138 ~SECONDARY_EXEC_XSAVES;
6139 }
6140 }
6141
Paolo Bonzini80154d72017-08-24 13:55:35 +02006142 if (vmx_rdtscp_supported()) {
6143 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6144 if (!rdtscp_enabled)
6145 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6146
6147 if (nested) {
6148 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006149 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006150 SECONDARY_EXEC_RDTSCP;
6151 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006152 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006153 ~SECONDARY_EXEC_RDTSCP;
6154 }
6155 }
6156
6157 if (vmx_invpcid_supported()) {
6158 /* Exposing INVPCID only when PCID is exposed */
6159 bool invpcid_enabled =
6160 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6161 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6162
6163 if (!invpcid_enabled) {
6164 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6165 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6166 }
6167
6168 if (nested) {
6169 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006170 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006171 SECONDARY_EXEC_ENABLE_INVPCID;
6172 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006173 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006174 ~SECONDARY_EXEC_ENABLE_INVPCID;
6175 }
6176 }
6177
Jim Mattson45ec3682017-08-23 16:32:04 -07006178 if (vmx_rdrand_supported()) {
6179 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6180 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006181 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006182
6183 if (nested) {
6184 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006185 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006186 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006187 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006188 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006189 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006190 }
6191 }
6192
Jim Mattson75f4fc82017-08-23 16:32:03 -07006193 if (vmx_rdseed_supported()) {
6194 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6195 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006196 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006197
6198 if (nested) {
6199 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006200 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006201 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006202 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006203 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006204 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006205 }
6206 }
6207
Paolo Bonzini80154d72017-08-24 13:55:35 +02006208 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006209}
6210
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006211static void ept_set_mmio_spte_mask(void)
6212{
6213 /*
6214 * EPT Misconfigurations can be generated if the value of bits 2:0
6215 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006216 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006217 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6218 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006219}
6220
Wanpeng Lif53cd632014-12-02 19:14:58 +08006221#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006222/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223 * Sets up the vmcs for emulated real mode.
6224 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006225static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006226{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006227#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006228 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006229#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006230 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231
Abel Gordon4607c2d2013-04-18 14:35:55 +03006232 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006233 /*
6234 * At vCPU creation, "VMWRITE to any supported field
6235 * in the VMCS" is supported, so use the more
6236 * permissive vmx_vmread_bitmap to specify both read
6237 * and write permissions for the shadow VMCS.
6238 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006239 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006240 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006241 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006242 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006243 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006244
Avi Kivity6aa8b732006-12-10 02:21:36 -08006245 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6246
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006248 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006249 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006250
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006251 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252
Dan Williamsdfa169b2016-06-02 11:17:24 -07006253 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006254 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006255 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006256 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006257 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006258
Andrey Smetanind62caab2015-11-10 15:36:33 +03006259 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006260 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6261 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6262 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6263 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6264
6265 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006266
Li RongQing0bcf2612015-12-03 13:29:34 +08006267 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006268 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006269 }
6270
Wanpeng Lib31c1142018-03-12 04:53:04 -07006271 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006272 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006273 vmx->ple_window = ple_window;
6274 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006275 }
6276
Xiao Guangrongc3707952011-07-12 03:28:04 +08006277 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6278 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006279 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6280
Avi Kivity9581d442010-10-19 16:46:55 +02006281 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6282 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006283 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006284#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006285 rdmsrl(MSR_FS_BASE, a);
6286 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6287 rdmsrl(MSR_GS_BASE, a);
6288 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6289#else
6290 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6291 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6292#endif
6293
Bandan Das2a499e42017-08-03 15:54:41 -04006294 if (cpu_has_vmx_vmfunc())
6295 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6296
Eddie Dong2cc51562007-05-21 07:28:09 +03006297 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6298 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006299 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006300 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006301 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006302
Radim Krčmář74545702015-04-27 15:11:25 +02006303 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6304 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006305
Paolo Bonzini03916db2014-07-24 14:21:57 +02006306 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006307 u32 index = vmx_msr_index[i];
6308 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006309 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310
6311 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6312 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006313 if (wrmsr_safe(index, data_low, data_high) < 0)
6314 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006315 vmx->guest_msrs[j].index = i;
6316 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006317 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006318 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006321 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6322 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006323
6324 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
6326 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006327 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006328
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006329 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6330 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6331
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006332 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006333
Wanpeng Lif53cd632014-12-02 19:14:58 +08006334 if (vmx_xsaves_supported())
6335 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6336
Peter Feiner4e595162016-07-07 14:49:58 -07006337 if (enable_pml) {
6338 ASSERT(vmx->pml_pg);
6339 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6340 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6341 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006342}
6343
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006344static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006345{
6346 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006347 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006348 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006349
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006350 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006351 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006352
Wanpeng Li518e7b92018-02-28 14:03:31 +08006353 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006354 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006355 kvm_set_cr8(vcpu, 0);
6356
6357 if (!init_event) {
6358 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6359 MSR_IA32_APICBASE_ENABLE;
6360 if (kvm_vcpu_is_reset_bsp(vcpu))
6361 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6362 apic_base_msr.host_initiated = true;
6363 kvm_set_apic_base(vcpu, &apic_base_msr);
6364 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006365
Avi Kivity2fb92db2011-04-27 19:42:18 +03006366 vmx_segment_cache_clear(vmx);
6367
Avi Kivity5706be02008-08-20 15:07:31 +03006368 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006369 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006370 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006371
6372 seg_setup(VCPU_SREG_DS);
6373 seg_setup(VCPU_SREG_ES);
6374 seg_setup(VCPU_SREG_FS);
6375 seg_setup(VCPU_SREG_GS);
6376 seg_setup(VCPU_SREG_SS);
6377
6378 vmcs_write16(GUEST_TR_SELECTOR, 0);
6379 vmcs_writel(GUEST_TR_BASE, 0);
6380 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6381 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6382
6383 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6384 vmcs_writel(GUEST_LDTR_BASE, 0);
6385 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6386 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6387
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006388 if (!init_event) {
6389 vmcs_write32(GUEST_SYSENTER_CS, 0);
6390 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6391 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6392 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6393 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006394
Wanpeng Lic37c2872017-11-20 14:52:21 -08006395 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006396 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006397
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006398 vmcs_writel(GUEST_GDTR_BASE, 0);
6399 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6400
6401 vmcs_writel(GUEST_IDTR_BASE, 0);
6402 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6403
Anthony Liguori443381a2010-12-06 10:53:38 -06006404 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006405 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006406 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006407 if (kvm_mpx_supported())
6408 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006409
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006410 setup_msrs(vmx);
6411
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6413
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006414 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006415 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006416 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006417 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006418 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006419 vmcs_write32(TPR_THRESHOLD, 0);
6420 }
6421
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006422 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006423
Sheng Yang2384d2b2008-01-17 15:14:33 +08006424 if (vmx->vpid != 0)
6425 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6426
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006427 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006428 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006429 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006430 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006431 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006432
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006433 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006434
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006435 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006436 if (init_event)
6437 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006438}
6439
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006440/*
6441 * In nested virtualization, check if L1 asked to exit on external interrupts.
6442 * For most existing hypervisors, this will always return true.
6443 */
6444static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6445{
6446 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6447 PIN_BASED_EXT_INTR_MASK;
6448}
6449
Bandan Das77b0f5d2014-04-19 18:17:45 -04006450/*
6451 * In nested virtualization, check if L1 has set
6452 * VM_EXIT_ACK_INTR_ON_EXIT
6453 */
6454static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6455{
6456 return get_vmcs12(vcpu)->vm_exit_controls &
6457 VM_EXIT_ACK_INTR_ON_EXIT;
6458}
6459
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006460static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6461{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006462 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006463}
6464
Jan Kiszkac9a79532014-03-07 20:03:15 +01006465static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006466{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006467 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6468 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006469}
6470
Jan Kiszkac9a79532014-03-07 20:03:15 +01006471static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006472{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006473 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006474 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006475 enable_irq_window(vcpu);
6476 return;
6477 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006478
Paolo Bonzini47c01522016-12-19 11:44:07 +01006479 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6480 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006481}
6482
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006483static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006484{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006485 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006486 uint32_t intr;
6487 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006488
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006489 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006490
Avi Kivityfa89a812008-09-01 15:57:51 +03006491 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006492 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006493 int inc_eip = 0;
6494 if (vcpu->arch.interrupt.soft)
6495 inc_eip = vcpu->arch.event_exit_inst_len;
6496 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006497 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006498 return;
6499 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006500 intr = irq | INTR_INFO_VALID_MASK;
6501 if (vcpu->arch.interrupt.soft) {
6502 intr |= INTR_TYPE_SOFT_INTR;
6503 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6504 vmx->vcpu.arch.event_exit_inst_len);
6505 } else
6506 intr |= INTR_TYPE_EXT_INTR;
6507 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006508
6509 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006510}
6511
Sheng Yangf08864b2008-05-15 18:23:25 +08006512static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6513{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006514 struct vcpu_vmx *vmx = to_vmx(vcpu);
6515
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006516 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006517 /*
6518 * Tracking the NMI-blocked state in software is built upon
6519 * finding the next open IRQ window. This, in turn, depends on
6520 * well-behaving guests: They have to keep IRQs disabled at
6521 * least as long as the NMI handler runs. Otherwise we may
6522 * cause NMI nesting, maybe breaking the guest. But as this is
6523 * highly unlikely, we can live with the residual risk.
6524 */
6525 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6526 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6527 }
6528
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006529 ++vcpu->stat.nmi_injections;
6530 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006531
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006532 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006533 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006534 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006535 return;
6536 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006537
Sheng Yangf08864b2008-05-15 18:23:25 +08006538 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6539 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006540
6541 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006542}
6543
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006544static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6545{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006546 struct vcpu_vmx *vmx = to_vmx(vcpu);
6547 bool masked;
6548
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006549 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006550 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006551 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006552 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006553 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6554 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6555 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006556}
6557
6558static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6559{
6560 struct vcpu_vmx *vmx = to_vmx(vcpu);
6561
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006562 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006563 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6564 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6565 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6566 }
6567 } else {
6568 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6569 if (masked)
6570 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6571 GUEST_INTR_STATE_NMI);
6572 else
6573 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6574 GUEST_INTR_STATE_NMI);
6575 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006576}
6577
Jan Kiszka2505dc92013-04-14 12:12:47 +02006578static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6579{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006580 if (to_vmx(vcpu)->nested.nested_run_pending)
6581 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006582
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006583 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006584 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6585 return 0;
6586
Jan Kiszka2505dc92013-04-14 12:12:47 +02006587 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6588 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6589 | GUEST_INTR_STATE_NMI));
6590}
6591
Gleb Natapov78646122009-03-23 12:12:11 +02006592static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6593{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006594 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6595 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006596 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6597 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006598}
6599
Izik Eiduscbc94022007-10-25 00:29:55 +02006600static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6601{
6602 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006603
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006604 if (enable_unrestricted_guest)
6605 return 0;
6606
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006607 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6608 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006609 if (ret)
6610 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006611 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006612 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006613}
6614
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006615static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6616{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006617 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006618 return 0;
6619}
6620
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006621static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006623 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006624 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006625 /*
6626 * Update instruction length as we may reinject the exception
6627 * from user space while in guest debugging mode.
6628 */
6629 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6630 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006631 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006632 return false;
6633 /* fall through */
6634 case DB_VECTOR:
6635 if (vcpu->guest_debug &
6636 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6637 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006638 /* fall through */
6639 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006640 case OF_VECTOR:
6641 case BR_VECTOR:
6642 case UD_VECTOR:
6643 case DF_VECTOR:
6644 case SS_VECTOR:
6645 case GP_VECTOR:
6646 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006647 return true;
6648 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006649 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006650 return false;
6651}
6652
6653static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6654 int vec, u32 err_code)
6655{
6656 /*
6657 * Instruction with address size override prefix opcode 0x67
6658 * Cause the #SS fault with 0 error code in VM86 mode.
6659 */
6660 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6661 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6662 if (vcpu->arch.halt_request) {
6663 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006664 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006665 }
6666 return 1;
6667 }
6668 return 0;
6669 }
6670
6671 /*
6672 * Forward all other exceptions that are valid in real mode.
6673 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6674 * the required debugging infrastructure rework.
6675 */
6676 kvm_queue_exception(vcpu, vec);
6677 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006678}
6679
Andi Kleena0861c02009-06-08 17:37:09 +08006680/*
6681 * Trigger machine check on the host. We assume all the MSRs are already set up
6682 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6683 * We pass a fake environment to the machine check handler because we want
6684 * the guest to be always treated like user space, no matter what context
6685 * it used internally.
6686 */
6687static void kvm_machine_check(void)
6688{
6689#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6690 struct pt_regs regs = {
6691 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6692 .flags = X86_EFLAGS_IF,
6693 };
6694
6695 do_machine_check(&regs, 0);
6696#endif
6697}
6698
Avi Kivity851ba692009-08-24 11:10:17 +03006699static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006700{
6701 /* already handled by vcpu_run */
6702 return 1;
6703}
6704
Avi Kivity851ba692009-08-24 11:10:17 +03006705static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706{
Avi Kivity1155f762007-11-22 11:30:47 +02006707 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006708 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006709 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006710 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006711 u32 vect_info;
6712 enum emulation_result er;
6713
Avi Kivity1155f762007-11-22 11:30:47 +02006714 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006715 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006716
Andi Kleena0861c02009-06-08 17:37:09 +08006717 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006718 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006719
Jim Mattsonef85b672016-12-12 11:01:37 -08006720 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006721 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006722
Wanpeng Li082d06e2018-04-03 16:28:48 -07006723 if (is_invalid_opcode(intr_info))
6724 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006725
Avi Kivity6aa8b732006-12-10 02:21:36 -08006726 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006727 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006728 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006729
Liran Alon9e869482018-03-12 13:12:51 +02006730 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6731 WARN_ON_ONCE(!enable_vmware_backdoor);
6732 er = emulate_instruction(vcpu,
6733 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6734 if (er == EMULATE_USER_EXIT)
6735 return 0;
6736 else if (er != EMULATE_DONE)
6737 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6738 return 1;
6739 }
6740
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006741 /*
6742 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6743 * MMIO, it is better to report an internal error.
6744 * See the comments in vmx_handle_exit.
6745 */
6746 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6747 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6748 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6749 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006750 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006751 vcpu->run->internal.data[0] = vect_info;
6752 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006753 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006754 return 0;
6755 }
6756
Avi Kivity6aa8b732006-12-10 02:21:36 -08006757 if (is_page_fault(intr_info)) {
6758 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006759 /* EPT won't cause page fault directly */
6760 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006761 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006762 }
6763
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006764 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006765
6766 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6767 return handle_rmode_exception(vcpu, ex_no, error_code);
6768
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006769 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006770 case AC_VECTOR:
6771 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6772 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006773 case DB_VECTOR:
6774 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6775 if (!(vcpu->guest_debug &
6776 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006777 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006778 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006779 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006780 skip_emulated_instruction(vcpu);
6781
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006782 kvm_queue_exception(vcpu, DB_VECTOR);
6783 return 1;
6784 }
6785 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6786 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6787 /* fall through */
6788 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006789 /*
6790 * Update instruction length as we may reinject #BP from
6791 * user space while in guest debugging mode. Reading it for
6792 * #DB as well causes no harm, it is not used in that case.
6793 */
6794 vmx->vcpu.arch.event_exit_inst_len =
6795 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006796 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006797 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006798 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6799 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006800 break;
6801 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006802 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6803 kvm_run->ex.exception = ex_no;
6804 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006805 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006806 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006807 return 0;
6808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006812 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006813 return 1;
6814}
6815
Avi Kivity851ba692009-08-24 11:10:17 +03006816static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006817{
Avi Kivity851ba692009-08-24 11:10:17 +03006818 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006819 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006820 return 0;
6821}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822
Avi Kivity851ba692009-08-24 11:10:17 +03006823static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006824{
He, Qingbfdaab02007-09-12 14:18:28 +08006825 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006826 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006827 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828
He, Qingbfdaab02007-09-12 14:18:28 +08006829 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006830 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006831
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006832 ++vcpu->stat.io_exits;
6833
Sean Christopherson432baf62018-03-08 08:57:26 -08006834 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006835 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006836
6837 port = exit_qualification >> 16;
6838 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006839 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006840
Sean Christophersondca7f122018-03-08 08:57:27 -08006841 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842}
6843
Ingo Molnar102d8322007-02-19 14:37:47 +02006844static void
6845vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6846{
6847 /*
6848 * Patch in the VMCALL instruction:
6849 */
6850 hypercall[0] = 0x0f;
6851 hypercall[1] = 0x01;
6852 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006853}
6854
Guo Chao0fa06072012-06-28 15:16:19 +08006855/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006856static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6857{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006858 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006859 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6860 unsigned long orig_val = val;
6861
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006862 /*
6863 * We get here when L2 changed cr0 in a way that did not change
6864 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006865 * but did change L0 shadowed bits. So we first calculate the
6866 * effective cr0 value that L1 would like to write into the
6867 * hardware. It consists of the L2-owned bits from the new
6868 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006869 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006870 val = (val & ~vmcs12->cr0_guest_host_mask) |
6871 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6872
David Matlack38991522016-11-29 18:14:08 -08006873 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006874 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006875
6876 if (kvm_set_cr0(vcpu, val))
6877 return 1;
6878 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006879 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006880 } else {
6881 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006882 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006883 return 1;
David Matlack38991522016-11-29 18:14:08 -08006884
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006885 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006886 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006887}
6888
6889static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6890{
6891 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6893 unsigned long orig_val = val;
6894
6895 /* analogously to handle_set_cr0 */
6896 val = (val & ~vmcs12->cr4_guest_host_mask) |
6897 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6898 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006899 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006900 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006901 return 0;
6902 } else
6903 return kvm_set_cr4(vcpu, val);
6904}
6905
Paolo Bonzini0367f202016-07-12 10:44:55 +02006906static int handle_desc(struct kvm_vcpu *vcpu)
6907{
6908 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6909 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6910}
6911
Avi Kivity851ba692009-08-24 11:10:17 +03006912static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006914 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006915 int cr;
6916 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006917 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006918 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919
He, Qingbfdaab02007-09-12 14:18:28 +08006920 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006921 cr = exit_qualification & 15;
6922 reg = (exit_qualification >> 8) & 15;
6923 switch ((exit_qualification >> 4) & 3) {
6924 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006925 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006926 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006927 switch (cr) {
6928 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006929 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006930 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006931 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006932 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006933 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006934 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006935 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006936 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006937 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006938 case 8: {
6939 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006940 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006941 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006942 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006943 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006944 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006945 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006946 return ret;
6947 /*
6948 * TODO: we might be squashing a
6949 * KVM_GUESTDBG_SINGLESTEP-triggered
6950 * KVM_EXIT_DEBUG here.
6951 */
Avi Kivity851ba692009-08-24 11:10:17 +03006952 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006953 return 0;
6954 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006955 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006956 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006957 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006958 WARN_ONCE(1, "Guest should always own CR0.TS");
6959 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006960 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006961 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006962 case 1: /*mov from cr*/
6963 switch (cr) {
6964 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006965 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006966 val = kvm_read_cr3(vcpu);
6967 kvm_register_write(vcpu, reg, val);
6968 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006969 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006971 val = kvm_get_cr8(vcpu);
6972 kvm_register_write(vcpu, reg, val);
6973 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975 }
6976 break;
6977 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006978 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006979 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006980 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006981
Kyle Huey6affcbe2016-11-29 12:40:40 -08006982 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006983 default:
6984 break;
6985 }
Avi Kivity851ba692009-08-24 11:10:17 +03006986 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006987 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988 (int)(exit_qualification >> 4) & 3, cr);
6989 return 0;
6990}
6991
Avi Kivity851ba692009-08-24 11:10:17 +03006992static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006993{
He, Qingbfdaab02007-09-12 14:18:28 +08006994 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006995 int dr, dr7, reg;
6996
6997 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6998 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6999
7000 /* First, if DR does not exist, trigger UD */
7001 if (!kvm_require_dr(vcpu, dr))
7002 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007003
Jan Kiszkaf2483412010-01-20 18:20:20 +01007004 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007005 if (!kvm_require_cpl(vcpu, 0))
7006 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007007 dr7 = vmcs_readl(GUEST_DR7);
7008 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007009 /*
7010 * As the vm-exit takes precedence over the debug trap, we
7011 * need to emulate the latter, either for the host or the
7012 * guest debugging itself.
7013 */
7014 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007015 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007016 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007017 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007018 vcpu->run->debug.arch.exception = DB_VECTOR;
7019 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007020 return 0;
7021 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007022 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007023 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007024 kvm_queue_exception(vcpu, DB_VECTOR);
7025 return 1;
7026 }
7027 }
7028
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007029 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007030 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7031 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007032
7033 /*
7034 * No more DR vmexits; force a reload of the debug registers
7035 * and reenter on this instruction. The next vmexit will
7036 * retrieve the full state of the debug registers.
7037 */
7038 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7039 return 1;
7040 }
7041
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007042 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7043 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007044 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007045
7046 if (kvm_get_dr(vcpu, dr, &val))
7047 return 1;
7048 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007049 } else
Nadav Amit57773922014-06-18 17:19:23 +03007050 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007051 return 1;
7052
Kyle Huey6affcbe2016-11-29 12:40:40 -08007053 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054}
7055
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007056static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7057{
7058 return vcpu->arch.dr6;
7059}
7060
7061static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7062{
7063}
7064
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007065static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7066{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007067 get_debugreg(vcpu->arch.db[0], 0);
7068 get_debugreg(vcpu->arch.db[1], 1);
7069 get_debugreg(vcpu->arch.db[2], 2);
7070 get_debugreg(vcpu->arch.db[3], 3);
7071 get_debugreg(vcpu->arch.dr6, 6);
7072 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7073
7074 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007075 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007076}
7077
Gleb Natapov020df072010-04-13 10:05:23 +03007078static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7079{
7080 vmcs_writel(GUEST_DR7, val);
7081}
7082
Avi Kivity851ba692009-08-24 11:10:17 +03007083static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084{
Kyle Huey6a908b62016-11-29 12:40:37 -08007085 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086}
7087
Avi Kivity851ba692009-08-24 11:10:17 +03007088static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007089{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007090 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007091 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007093 msr_info.index = ecx;
7094 msr_info.host_initiated = false;
7095 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007096 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007097 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007098 return 1;
7099 }
7100
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007101 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007102
Avi Kivity6aa8b732006-12-10 02:21:36 -08007103 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007104 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7105 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007106 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007107}
7108
Avi Kivity851ba692009-08-24 11:10:17 +03007109static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007110{
Will Auld8fe8ab42012-11-29 12:42:12 -08007111 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007112 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7113 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7114 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115
Will Auld8fe8ab42012-11-29 12:42:12 -08007116 msr.data = data;
7117 msr.index = ecx;
7118 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007119 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007120 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007121 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007122 return 1;
7123 }
7124
Avi Kivity59200272010-01-25 19:47:02 +02007125 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007126 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007130{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007131 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007132 return 1;
7133}
7134
Avi Kivity851ba692009-08-24 11:10:17 +03007135static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007137 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7138 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007139
Avi Kivity3842d132010-07-27 12:30:24 +03007140 kvm_make_request(KVM_REQ_EVENT, vcpu);
7141
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007142 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143 return 1;
7144}
7145
Avi Kivity851ba692009-08-24 11:10:17 +03007146static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147{
Avi Kivityd3bef152007-06-05 15:53:05 +03007148 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149}
7150
Avi Kivity851ba692009-08-24 11:10:17 +03007151static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007152{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007153 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007154}
7155
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007156static int handle_invd(struct kvm_vcpu *vcpu)
7157{
Andre Przywara51d8b662010-12-21 11:12:02 +01007158 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007159}
7160
Avi Kivity851ba692009-08-24 11:10:17 +03007161static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007162{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007163 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007164
7165 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007166 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007167}
7168
Avi Kivityfee84b02011-11-10 14:57:25 +02007169static int handle_rdpmc(struct kvm_vcpu *vcpu)
7170{
7171 int err;
7172
7173 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007174 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007175}
7176
Avi Kivity851ba692009-08-24 11:10:17 +03007177static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007178{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007179 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007180}
7181
Dexuan Cui2acf9232010-06-10 11:27:12 +08007182static int handle_xsetbv(struct kvm_vcpu *vcpu)
7183{
7184 u64 new_bv = kvm_read_edx_eax(vcpu);
7185 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7186
7187 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007188 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007189 return 1;
7190}
7191
Wanpeng Lif53cd632014-12-02 19:14:58 +08007192static int handle_xsaves(struct kvm_vcpu *vcpu)
7193{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007194 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007195 WARN(1, "this should never happen\n");
7196 return 1;
7197}
7198
7199static int handle_xrstors(struct kvm_vcpu *vcpu)
7200{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007201 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007202 WARN(1, "this should never happen\n");
7203 return 1;
7204}
7205
Avi Kivity851ba692009-08-24 11:10:17 +03007206static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007207{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007208 if (likely(fasteoi)) {
7209 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7210 int access_type, offset;
7211
7212 access_type = exit_qualification & APIC_ACCESS_TYPE;
7213 offset = exit_qualification & APIC_ACCESS_OFFSET;
7214 /*
7215 * Sane guest uses MOV to write EOI, with written value
7216 * not cared. So make a short-circuit here by avoiding
7217 * heavy instruction emulation.
7218 */
7219 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7220 (offset == APIC_EOI)) {
7221 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007222 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007223 }
7224 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007225 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007226}
7227
Yang Zhangc7c9c562013-01-25 10:18:51 +08007228static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7229{
7230 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7231 int vector = exit_qualification & 0xff;
7232
7233 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7234 kvm_apic_set_eoi_accelerated(vcpu, vector);
7235 return 1;
7236}
7237
Yang Zhang83d4c282013-01-25 10:18:49 +08007238static int handle_apic_write(struct kvm_vcpu *vcpu)
7239{
7240 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7241 u32 offset = exit_qualification & 0xfff;
7242
7243 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7244 kvm_apic_write_nodecode(vcpu, offset);
7245 return 1;
7246}
7247
Avi Kivity851ba692009-08-24 11:10:17 +03007248static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007249{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007250 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007251 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007252 bool has_error_code = false;
7253 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007254 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007255 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007256
7257 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007258 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007259 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007260
7261 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7262
7263 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007264 if (reason == TASK_SWITCH_GATE && idt_v) {
7265 switch (type) {
7266 case INTR_TYPE_NMI_INTR:
7267 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007268 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007269 break;
7270 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007271 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007272 kvm_clear_interrupt_queue(vcpu);
7273 break;
7274 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007275 if (vmx->idt_vectoring_info &
7276 VECTORING_INFO_DELIVER_CODE_MASK) {
7277 has_error_code = true;
7278 error_code =
7279 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7280 }
7281 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007282 case INTR_TYPE_SOFT_EXCEPTION:
7283 kvm_clear_exception_queue(vcpu);
7284 break;
7285 default:
7286 break;
7287 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007288 }
Izik Eidus37817f22008-03-24 23:14:53 +02007289 tss_selector = exit_qualification;
7290
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007291 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7292 type != INTR_TYPE_EXT_INTR &&
7293 type != INTR_TYPE_NMI_INTR))
7294 skip_emulated_instruction(vcpu);
7295
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007296 if (kvm_task_switch(vcpu, tss_selector,
7297 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7298 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007299 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7300 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7301 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007302 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007303 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007304
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007305 /*
7306 * TODO: What about debug traps on tss switch?
7307 * Are we supposed to inject them and update dr6?
7308 */
7309
7310 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007311}
7312
Avi Kivity851ba692009-08-24 11:10:17 +03007313static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007314{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007315 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007316 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007317 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007318
Sheng Yangf9c617f2009-03-25 10:08:52 +08007319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007320
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007321 /*
7322 * EPT violation happened while executing iret from NMI,
7323 * "blocked by NMI" bit has to be set before next VM entry.
7324 * There are errata that may cause this bit to not be set:
7325 * AAK134, BY25.
7326 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007327 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007328 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007329 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007330 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7331
Sheng Yang14394422008-04-28 12:24:45 +08007332 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007333 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007334
Junaid Shahid27959a42016-12-06 16:46:10 -08007335 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007336 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007337 ? PFERR_USER_MASK : 0;
7338 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007339 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007340 ? PFERR_WRITE_MASK : 0;
7341 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007342 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007343 ? PFERR_FETCH_MASK : 0;
7344 /* ept page table entry is present? */
7345 error_code |= (exit_qualification &
7346 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7347 EPT_VIOLATION_EXECUTABLE))
7348 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007349
Paolo Bonzinieebed242016-11-28 14:39:58 +01007350 error_code |= (exit_qualification & 0x100) != 0 ?
7351 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007352
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007353 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007354 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007355}
7356
Avi Kivity851ba692009-08-24 11:10:17 +03007357static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007358{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007359 gpa_t gpa;
7360
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007361 /*
7362 * A nested guest cannot optimize MMIO vmexits, because we have an
7363 * nGPA here instead of the required GPA.
7364 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007365 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007366 if (!is_guest_mode(vcpu) &&
7367 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007368 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007369 /*
7370 * Doing kvm_skip_emulated_instruction() depends on undefined
7371 * behavior: Intel's manual doesn't mandate
7372 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7373 * occurs and while on real hardware it was observed to be set,
7374 * other hypervisors (namely Hyper-V) don't set it, we end up
7375 * advancing IP with some random value. Disable fast mmio when
7376 * running nested and keep it for real hardware in hope that
7377 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7378 */
7379 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7380 return kvm_skip_emulated_instruction(vcpu);
7381 else
7382 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7383 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007384 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007385
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007386 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007387}
7388
Avi Kivity851ba692009-08-24 11:10:17 +03007389static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007390{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007391 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007392 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7393 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007394 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007395 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007396
7397 return 1;
7398}
7399
Mohammed Gamal80ced182009-09-01 12:48:18 +02007400static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007401{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007402 struct vcpu_vmx *vmx = to_vmx(vcpu);
7403 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007404 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007405 u32 cpu_exec_ctrl;
7406 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007407 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007408
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007409 /*
7410 * We should never reach the point where we are emulating L2
7411 * due to invalid guest state as that means we incorrectly
7412 * allowed a nested VMEntry with an invalid vmcs12.
7413 */
7414 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7415
Avi Kivity49e9d552010-09-19 14:34:08 +02007416 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7417 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007418
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007419 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007420 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007421 return handle_interrupt_window(&vmx->vcpu);
7422
Radim Krčmář72875d82017-04-26 22:32:19 +02007423 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007424 return 1;
7425
Liran Alon9b8ae632017-11-05 16:56:34 +02007426 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007427
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007428 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007429 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007430 ret = 0;
7431 goto out;
7432 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007433
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007434 if (err != EMULATE_DONE)
7435 goto emulation_error;
7436
7437 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7438 vcpu->arch.exception.pending)
7439 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007440
Gleb Natapov8d76c492013-05-08 18:38:44 +03007441 if (vcpu->arch.halt_request) {
7442 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007443 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007444 goto out;
7445 }
7446
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007447 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007448 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007449 if (need_resched())
7450 schedule();
7451 }
7452
Mohammed Gamal80ced182009-09-01 12:48:18 +02007453out:
7454 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007455
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007456emulation_error:
7457 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7458 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7459 vcpu->run->internal.ndata = 0;
7460 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007461}
7462
7463static void grow_ple_window(struct kvm_vcpu *vcpu)
7464{
7465 struct vcpu_vmx *vmx = to_vmx(vcpu);
7466 int old = vmx->ple_window;
7467
Babu Mogerc8e88712018-03-16 16:37:24 -04007468 vmx->ple_window = __grow_ple_window(old, ple_window,
7469 ple_window_grow,
7470 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007471
7472 if (vmx->ple_window != old)
7473 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007474
7475 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007476}
7477
7478static void shrink_ple_window(struct kvm_vcpu *vcpu)
7479{
7480 struct vcpu_vmx *vmx = to_vmx(vcpu);
7481 int old = vmx->ple_window;
7482
Babu Mogerc8e88712018-03-16 16:37:24 -04007483 vmx->ple_window = __shrink_ple_window(old, ple_window,
7484 ple_window_shrink,
7485 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007486
7487 if (vmx->ple_window != old)
7488 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007489
7490 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007491}
7492
7493/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007494 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7495 */
7496static void wakeup_handler(void)
7497{
7498 struct kvm_vcpu *vcpu;
7499 int cpu = smp_processor_id();
7500
7501 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7502 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7503 blocked_vcpu_list) {
7504 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7505
7506 if (pi_test_on(pi_desc) == 1)
7507 kvm_vcpu_kick(vcpu);
7508 }
7509 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7510}
7511
Peng Haoe01bca22018-04-07 05:47:32 +08007512static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007513{
7514 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7515 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7516 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7517 0ull, VMX_EPT_EXECUTABLE_MASK,
7518 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007519 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007520
7521 ept_set_mmio_spte_mask();
7522 kvm_enable_tdp();
7523}
7524
Tiejun Chenf2c76482014-10-28 10:14:47 +08007525static __init int hardware_setup(void)
7526{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007527 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007528 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007529
7530 rdmsrl_safe(MSR_EFER, &host_efer);
7531
7532 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7533 kvm_define_shared_msr(i, vmx_msr_index[i]);
7534
Radim Krčmář23611332016-09-29 22:41:33 +02007535 for (i = 0; i < VMX_BITMAP_NR; i++) {
7536 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7537 if (!vmx_bitmap[i])
7538 goto out;
7539 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007540
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007541 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7542 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7543
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007544 if (setup_vmcs_config(&vmcs_config) < 0) {
7545 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007546 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007547 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007548
7549 if (boot_cpu_has(X86_FEATURE_NX))
7550 kvm_enable_efer_bits(EFER_NX);
7551
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007552 if (boot_cpu_has(X86_FEATURE_MPX)) {
7553 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7554 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7555 }
7556
Wanpeng Li08d839c2017-03-23 05:30:08 -07007557 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7558 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007559 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007560
Tiejun Chenf2c76482014-10-28 10:14:47 +08007561 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007562 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007563 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007564 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007565 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007566
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007567 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007568 enable_ept_ad_bits = 0;
7569
Wanpeng Li8ad81822017-10-09 15:51:53 -07007570 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007571 enable_unrestricted_guest = 0;
7572
Paolo Bonziniad15a292015-01-30 16:18:49 +01007573 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007574 flexpriority_enabled = 0;
7575
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007576 if (!cpu_has_virtual_nmis())
7577 enable_vnmi = 0;
7578
Paolo Bonziniad15a292015-01-30 16:18:49 +01007579 /*
7580 * set_apic_access_page_addr() is used to reload apic access
7581 * page upon invalidation. No need to do anything if not
7582 * using the APIC_ACCESS_ADDR VMCS field.
7583 */
7584 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007585 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007586
7587 if (!cpu_has_vmx_tpr_shadow())
7588 kvm_x86_ops->update_cr8_intercept = NULL;
7589
7590 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7591 kvm_disable_largepages();
7592
Wanpeng Li0f107682017-09-28 18:06:24 -07007593 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007594 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007595 ple_window = 0;
7596 ple_window_grow = 0;
7597 ple_window_max = 0;
7598 ple_window_shrink = 0;
7599 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007600
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007601 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007602 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007603 kvm_x86_ops->sync_pir_to_irr = NULL;
7604 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007605
Haozhong Zhang64903d62015-10-20 15:39:09 +08007606 if (cpu_has_vmx_tsc_scaling()) {
7607 kvm_has_tsc_control = true;
7608 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7609 kvm_tsc_scaling_ratio_frac_bits = 48;
7610 }
7611
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007612 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7613
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007614 if (enable_ept)
7615 vmx_enable_tdp();
7616 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007617 kvm_disable_tdp();
7618
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007619 if (!nested) {
7620 kvm_x86_ops->get_nested_state = NULL;
7621 kvm_x86_ops->set_nested_state = NULL;
7622 }
7623
Kai Huang843e4332015-01-28 10:54:28 +08007624 /*
7625 * Only enable PML when hardware supports PML feature, and both EPT
7626 * and EPT A/D bit features are enabled -- PML depends on them to work.
7627 */
7628 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7629 enable_pml = 0;
7630
7631 if (!enable_pml) {
7632 kvm_x86_ops->slot_enable_log_dirty = NULL;
7633 kvm_x86_ops->slot_disable_log_dirty = NULL;
7634 kvm_x86_ops->flush_log_dirty = NULL;
7635 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7636 }
7637
Yunhong Jiang64672c92016-06-13 14:19:59 -07007638 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7639 u64 vmx_msr;
7640
7641 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7642 cpu_preemption_timer_multi =
7643 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7644 } else {
7645 kvm_x86_ops->set_hv_timer = NULL;
7646 kvm_x86_ops->cancel_hv_timer = NULL;
7647 }
7648
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007649 if (!cpu_has_vmx_shadow_vmcs())
7650 enable_shadow_vmcs = 0;
7651 if (enable_shadow_vmcs)
7652 init_vmcs_shadow_fields();
7653
Feng Wubf9f6ac2015-09-18 22:29:55 +08007654 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007655 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007656
Ashok Rajc45dcc72016-06-22 14:59:56 +08007657 kvm_mce_cap_supported |= MCG_LMCE_P;
7658
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007660
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007661out:
Radim Krčmář23611332016-09-29 22:41:33 +02007662 for (i = 0; i < VMX_BITMAP_NR; i++)
7663 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007664
7665 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007666}
7667
7668static __exit void hardware_unsetup(void)
7669{
Radim Krčmář23611332016-09-29 22:41:33 +02007670 int i;
7671
7672 for (i = 0; i < VMX_BITMAP_NR; i++)
7673 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007674
Tiejun Chenf2c76482014-10-28 10:14:47 +08007675 free_kvm_area();
7676}
7677
Avi Kivity6aa8b732006-12-10 02:21:36 -08007678/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007679 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7680 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7681 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007682static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007683{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007684 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007685 grow_ple_window(vcpu);
7686
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007687 /*
7688 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7689 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7690 * never set PAUSE_EXITING and just set PLE if supported,
7691 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7692 */
7693 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007694 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007695}
7696
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007697static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007698{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007699 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007700}
7701
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007702static int handle_mwait(struct kvm_vcpu *vcpu)
7703{
7704 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7705 return handle_nop(vcpu);
7706}
7707
Jim Mattson45ec3682017-08-23 16:32:04 -07007708static int handle_invalid_op(struct kvm_vcpu *vcpu)
7709{
7710 kvm_queue_exception(vcpu, UD_VECTOR);
7711 return 1;
7712}
7713
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007714static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7715{
7716 return 1;
7717}
7718
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007719static int handle_monitor(struct kvm_vcpu *vcpu)
7720{
7721 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7722 return handle_nop(vcpu);
7723}
7724
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007725/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007726 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7727 * set the success or error code of an emulated VMX instruction, as specified
7728 * by Vol 2B, VMX Instruction Reference, "Conventions".
7729 */
7730static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7731{
7732 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7733 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7734 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7735}
7736
7737static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7738{
7739 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7740 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7741 X86_EFLAGS_SF | X86_EFLAGS_OF))
7742 | X86_EFLAGS_CF);
7743}
7744
Abel Gordon145c28d2013-04-18 14:36:55 +03007745static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007746 u32 vm_instruction_error)
7747{
7748 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7749 /*
7750 * failValid writes the error number to the current VMCS, which
7751 * can't be done there isn't a current VMCS.
7752 */
7753 nested_vmx_failInvalid(vcpu);
7754 return;
7755 }
7756 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7757 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7758 X86_EFLAGS_SF | X86_EFLAGS_OF))
7759 | X86_EFLAGS_ZF);
7760 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7761 /*
7762 * We don't need to force a shadow sync because
7763 * VM_INSTRUCTION_ERROR is not shadowed
7764 */
7765}
Abel Gordon145c28d2013-04-18 14:36:55 +03007766
Wincy Vanff651cb2014-12-11 08:52:58 +03007767static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7768{
7769 /* TODO: not to reset guest simply here. */
7770 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007771 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007772}
7773
Jan Kiszkaf4124502014-03-07 20:03:13 +01007774static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7775{
7776 struct vcpu_vmx *vmx =
7777 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7778
7779 vmx->nested.preemption_timer_expired = true;
7780 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7781 kvm_vcpu_kick(&vmx->vcpu);
7782
7783 return HRTIMER_NORESTART;
7784}
7785
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007786/*
Bandan Das19677e32014-05-06 02:19:15 -04007787 * Decode the memory-address operand of a vmx instruction, as recorded on an
7788 * exit caused by such an instruction (run by a guest hypervisor).
7789 * On success, returns 0. When the operand is invalid, returns 1 and throws
7790 * #UD or #GP.
7791 */
7792static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7793 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007794 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007795{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007796 gva_t off;
7797 bool exn;
7798 struct kvm_segment s;
7799
Bandan Das19677e32014-05-06 02:19:15 -04007800 /*
7801 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7802 * Execution", on an exit, vmx_instruction_info holds most of the
7803 * addressing components of the operand. Only the displacement part
7804 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7805 * For how an actual address is calculated from all these components,
7806 * refer to Vol. 1, "Operand Addressing".
7807 */
7808 int scaling = vmx_instruction_info & 3;
7809 int addr_size = (vmx_instruction_info >> 7) & 7;
7810 bool is_reg = vmx_instruction_info & (1u << 10);
7811 int seg_reg = (vmx_instruction_info >> 15) & 7;
7812 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7813 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7814 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7815 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7816
7817 if (is_reg) {
7818 kvm_queue_exception(vcpu, UD_VECTOR);
7819 return 1;
7820 }
7821
7822 /* Addr = segment_base + offset */
7823 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007824 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007825 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007826 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007827 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007828 off += kvm_register_read(vcpu, index_reg)<<scaling;
7829 vmx_get_segment(vcpu, &s, seg_reg);
7830 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007831
7832 if (addr_size == 1) /* 32 bit */
7833 *ret &= 0xffffffff;
7834
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007835 /* Checks for #GP/#SS exceptions. */
7836 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007837 if (is_long_mode(vcpu)) {
7838 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7839 * non-canonical form. This is the only check on the memory
7840 * destination for long mode!
7841 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007842 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007843 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007844 /* Protected mode: apply checks for segment validity in the
7845 * following order:
7846 * - segment type check (#GP(0) may be thrown)
7847 * - usability check (#GP(0)/#SS(0))
7848 * - limit check (#GP(0)/#SS(0))
7849 */
7850 if (wr)
7851 /* #GP(0) if the destination operand is located in a
7852 * read-only data segment or any code segment.
7853 */
7854 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7855 else
7856 /* #GP(0) if the source operand is located in an
7857 * execute-only code segment
7858 */
7859 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007860 if (exn) {
7861 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7862 return 1;
7863 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007864 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7865 */
7866 exn = (s.unusable != 0);
7867 /* Protected mode: #GP(0)/#SS(0) if the memory
7868 * operand is outside the segment limit.
7869 */
7870 exn = exn || (off + sizeof(u64) > s.limit);
7871 }
7872 if (exn) {
7873 kvm_queue_exception_e(vcpu,
7874 seg_reg == VCPU_SREG_SS ?
7875 SS_VECTOR : GP_VECTOR,
7876 0);
7877 return 1;
7878 }
7879
Bandan Das19677e32014-05-06 02:19:15 -04007880 return 0;
7881}
7882
Radim Krčmářcbf71272017-05-19 15:48:51 +02007883static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007884{
7885 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007886 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007887
7888 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007889 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007890 return 1;
7891
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007892 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007893 kvm_inject_page_fault(vcpu, &e);
7894 return 1;
7895 }
7896
Bandan Das3573e222014-05-06 02:19:16 -04007897 return 0;
7898}
7899
Jim Mattsone29acc52016-11-30 12:03:43 -08007900static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7901{
7902 struct vcpu_vmx *vmx = to_vmx(vcpu);
7903 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007904 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007905
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007906 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7907 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007908 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007909
7910 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7911 if (!vmx->nested.cached_vmcs12)
7912 goto out_cached_vmcs12;
7913
Liran Alon61ada742018-06-23 02:35:08 +03007914 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7915 if (!vmx->nested.cached_shadow_vmcs12)
7916 goto out_cached_shadow_vmcs12;
7917
Jim Mattsone29acc52016-11-30 12:03:43 -08007918 if (enable_shadow_vmcs) {
7919 shadow_vmcs = alloc_vmcs();
7920 if (!shadow_vmcs)
7921 goto out_shadow_vmcs;
7922 /* mark vmcs as shadow */
Liran Alon392b2f22018-06-23 02:35:01 +03007923 shadow_vmcs->hdr.shadow_vmcs = 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007924 /* init shadow vmcs */
7925 vmcs_clear(shadow_vmcs);
7926 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7927 }
7928
Jim Mattsone29acc52016-11-30 12:03:43 -08007929 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7930 HRTIMER_MODE_REL_PINNED);
7931 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7932
7933 vmx->nested.vmxon = true;
7934 return 0;
7935
7936out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03007937 kfree(vmx->nested.cached_shadow_vmcs12);
7938
7939out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08007940 kfree(vmx->nested.cached_vmcs12);
7941
7942out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007943 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007944
Jim Mattsonde3a0022017-11-27 17:22:25 -06007945out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007946 return -ENOMEM;
7947}
7948
Bandan Das3573e222014-05-06 02:19:16 -04007949/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007950 * Emulate the VMXON instruction.
7951 * Currently, we just remember that VMX is active, and do not save or even
7952 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7953 * do not currently need to store anything in that guest-allocated memory
7954 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7955 * argument is different from the VMXON pointer (which the spec says they do).
7956 */
7957static int handle_vmon(struct kvm_vcpu *vcpu)
7958{
Jim Mattsone29acc52016-11-30 12:03:43 -08007959 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007960 gpa_t vmptr;
7961 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007962 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007963 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7964 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007965
Jim Mattson70f3aac2017-04-26 08:53:46 -07007966 /*
7967 * The Intel VMX Instruction Reference lists a bunch of bits that are
7968 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7969 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7970 * Otherwise, we should fail with #UD. But most faulting conditions
7971 * have already been checked by hardware, prior to the VM-exit for
7972 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7973 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007974 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007975 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007976 kvm_queue_exception(vcpu, UD_VECTOR);
7977 return 1;
7978 }
7979
Felix Wilhelm727ba742018-06-11 09:43:44 +02007980 /* CPL=0 must be checked manually. */
7981 if (vmx_get_cpl(vcpu)) {
7982 kvm_queue_exception(vcpu, UD_VECTOR);
7983 return 1;
7984 }
7985
Abel Gordon145c28d2013-04-18 14:36:55 +03007986 if (vmx->nested.vmxon) {
7987 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007988 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007989 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007990
Haozhong Zhang3b840802016-06-22 14:59:54 +08007991 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007992 != VMXON_NEEDED_FEATURES) {
7993 kvm_inject_gp(vcpu, 0);
7994 return 1;
7995 }
7996
Radim Krčmářcbf71272017-05-19 15:48:51 +02007997 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007998 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007999
8000 /*
8001 * SDM 3: 24.11.5
8002 * The first 4 bytes of VMXON region contain the supported
8003 * VMCS revision identifier
8004 *
8005 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8006 * which replaces physical address width with 32
8007 */
8008 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8009 nested_vmx_failInvalid(vcpu);
8010 return kvm_skip_emulated_instruction(vcpu);
8011 }
8012
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008013 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8014 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008015 nested_vmx_failInvalid(vcpu);
8016 return kvm_skip_emulated_instruction(vcpu);
8017 }
8018 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8019 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008020 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008021 nested_vmx_failInvalid(vcpu);
8022 return kvm_skip_emulated_instruction(vcpu);
8023 }
8024 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008025 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008026
8027 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008028 ret = enter_vmx_operation(vcpu);
8029 if (ret)
8030 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008031
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008032 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008033 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008034}
8035
8036/*
8037 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8038 * for running VMX instructions (except VMXON, whose prerequisites are
8039 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008040 * Note that many of these exceptions have priority over VM exits, so they
8041 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008042 */
8043static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8044{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008045 if (vmx_get_cpl(vcpu)) {
8046 kvm_queue_exception(vcpu, UD_VECTOR);
8047 return 0;
8048 }
8049
Jim Mattson70f3aac2017-04-26 08:53:46 -07008050 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008051 kvm_queue_exception(vcpu, UD_VECTOR);
8052 return 0;
8053 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008054 return 1;
8055}
8056
David Matlack8ca44e82017-08-01 14:00:39 -07008057static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8058{
8059 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8060 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8061}
8062
Abel Gordone7953d72013-04-18 14:37:55 +03008063static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8064{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008065 if (vmx->nested.current_vmptr == -1ull)
8066 return;
8067
Abel Gordon012f83c2013-04-18 14:39:25 +03008068 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008069 /* copy to memory all shadowed fields in case
8070 they were modified */
8071 copy_shadow_to_vmcs12(vmx);
8072 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008073 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008074 }
Wincy Van705699a2015-02-03 23:58:17 +08008075 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008076
8077 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008078 kvm_vcpu_write_guest_page(&vmx->vcpu,
8079 vmx->nested.current_vmptr >> PAGE_SHIFT,
8080 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008081
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008082 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008083}
8084
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008085/*
8086 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8087 * just stops using VMX.
8088 */
8089static void free_nested(struct vcpu_vmx *vmx)
8090{
Wanpeng Lib7455822017-11-22 14:04:00 -08008091 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008092 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008093
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008094 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008095 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008096 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008097 vmx->nested.posted_intr_nv = -1;
8098 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008099 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008100 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008101 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8102 free_vmcs(vmx->vmcs01.shadow_vmcs);
8103 vmx->vmcs01.shadow_vmcs = NULL;
8104 }
David Matlack4f2777b2016-07-13 17:16:37 -07008105 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008106 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008107 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008108 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008109 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008110 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008111 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008112 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008113 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008114 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008115 }
Wincy Van705699a2015-02-03 23:58:17 +08008116 if (vmx->nested.pi_desc_page) {
8117 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008118 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008119 vmx->nested.pi_desc_page = NULL;
8120 vmx->nested.pi_desc = NULL;
8121 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008122
Jim Mattsonde3a0022017-11-27 17:22:25 -06008123 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008124}
8125
8126/* Emulate the VMXOFF instruction */
8127static int handle_vmoff(struct kvm_vcpu *vcpu)
8128{
8129 if (!nested_vmx_check_permission(vcpu))
8130 return 1;
8131 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008132 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008133 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008134}
8135
Nadav Har'El27d6c862011-05-25 23:06:59 +03008136/* Emulate the VMCLEAR instruction */
8137static int handle_vmclear(struct kvm_vcpu *vcpu)
8138{
8139 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008140 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008141 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008142
8143 if (!nested_vmx_check_permission(vcpu))
8144 return 1;
8145
Radim Krčmářcbf71272017-05-19 15:48:51 +02008146 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008147 return 1;
8148
Radim Krčmářcbf71272017-05-19 15:48:51 +02008149 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8150 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8151 return kvm_skip_emulated_instruction(vcpu);
8152 }
8153
8154 if (vmptr == vmx->nested.vmxon_ptr) {
8155 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8156 return kvm_skip_emulated_instruction(vcpu);
8157 }
8158
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008159 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008160 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008161
Jim Mattson587d7e722017-03-02 12:41:48 -08008162 kvm_vcpu_write_guest(vcpu,
8163 vmptr + offsetof(struct vmcs12, launch_state),
8164 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008165
Nadav Har'El27d6c862011-05-25 23:06:59 +03008166 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008167 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008168}
8169
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008170static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8171
8172/* Emulate the VMLAUNCH instruction */
8173static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8174{
8175 return nested_vmx_run(vcpu, true);
8176}
8177
8178/* Emulate the VMRESUME instruction */
8179static int handle_vmresume(struct kvm_vcpu *vcpu)
8180{
8181
8182 return nested_vmx_run(vcpu, false);
8183}
8184
Nadav Har'El49f705c2011-05-25 23:08:30 +03008185/*
8186 * Read a vmcs12 field. Since these can have varying lengths and we return
8187 * one type, we chose the biggest type (u64) and zero-extend the return value
8188 * to that size. Note that the caller, handle_vmread, might need to use only
8189 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8190 * 64-bit fields are to be returned).
8191 */
Liran Alone2536742018-06-23 02:35:02 +03008192static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008194{
8195 short offset = vmcs_field_to_offset(field);
8196 char *p;
8197
8198 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008199 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008200
Liran Alone2536742018-06-23 02:35:02 +03008201 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008202
Jim Mattsond37f4262017-12-22 12:12:16 -08008203 switch (vmcs_field_width(field)) {
8204 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008205 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008206 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008207 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008208 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008210 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008211 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008212 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008213 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008214 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008215 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008216 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008217 WARN_ON(1);
8218 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008219 }
8220}
8221
Abel Gordon20b97fe2013-04-18 14:36:25 +03008222
Liran Alone2536742018-06-23 02:35:02 +03008223static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008224 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008225 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008226 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008227 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008228 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008229
Jim Mattsond37f4262017-12-22 12:12:16 -08008230 switch (vmcs_field_width(field)) {
8231 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008232 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008233 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008234 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008235 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008236 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008237 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008238 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008239 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008240 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008241 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008242 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008243 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008244 WARN_ON(1);
8245 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008246 }
8247
8248}
8249
Jim Mattsonf4160e42018-05-29 09:11:33 -07008250/*
8251 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8252 * they have been modified by the L1 guest. Note that the "read-only"
8253 * VM-exit information fields are actually writable if the vCPU is
8254 * configured to support "VMWRITE to any supported field in the VMCS."
8255 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008256static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8257{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008258 const u16 *fields[] = {
8259 shadow_read_write_fields,
8260 shadow_read_only_fields
8261 };
8262 const int max_fields[] = {
8263 max_shadow_read_write_fields,
8264 max_shadow_read_only_fields
8265 };
8266 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008267 unsigned long field;
8268 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008269 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008270
Jan Kiszka282da872014-10-08 18:05:39 +02008271 preempt_disable();
8272
Abel Gordon16f5b902013-04-18 14:38:25 +03008273 vmcs_load(shadow_vmcs);
8274
Jim Mattsonf4160e42018-05-29 09:11:33 -07008275 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8276 for (i = 0; i < max_fields[q]; i++) {
8277 field = fields[q][i];
8278 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008279 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008280 }
8281 /*
8282 * Skip the VM-exit information fields if they are read-only.
8283 */
8284 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8285 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008286 }
8287
8288 vmcs_clear(shadow_vmcs);
8289 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008290
8291 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008292}
8293
Abel Gordonc3114422013-04-18 14:38:55 +03008294static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8295{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008296 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008297 shadow_read_write_fields,
8298 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008299 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008300 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008301 max_shadow_read_write_fields,
8302 max_shadow_read_only_fields
8303 };
8304 int i, q;
8305 unsigned long field;
8306 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008307 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008308
8309 vmcs_load(shadow_vmcs);
8310
Mathias Krausec2bae892013-06-26 20:36:21 +02008311 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008312 for (i = 0; i < max_fields[q]; i++) {
8313 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008314 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008315 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008316 }
8317 }
8318
8319 vmcs_clear(shadow_vmcs);
8320 vmcs_load(vmx->loaded_vmcs->vmcs);
8321}
8322
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323/*
8324 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8325 * used before) all generate the same failure when it is missing.
8326 */
8327static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8328{
8329 struct vcpu_vmx *vmx = to_vmx(vcpu);
8330 if (vmx->nested.current_vmptr == -1ull) {
8331 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 return 0;
8333 }
8334 return 1;
8335}
8336
8337static int handle_vmread(struct kvm_vcpu *vcpu)
8338{
8339 unsigned long field;
8340 u64 field_value;
8341 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8342 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8343 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008344 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008345
Kyle Hueyeb277562016-11-29 12:40:39 -08008346 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008347 return 1;
8348
Kyle Huey6affcbe2016-11-29 12:40:40 -08008349 if (!nested_vmx_check_vmcs12(vcpu))
8350 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008351
Liran Alon6d894f42018-06-23 02:35:09 +03008352 if (!is_guest_mode(vcpu))
8353 vmcs12 = get_vmcs12(vcpu);
8354 else {
8355 /*
8356 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8357 * to shadowed-field sets the ALU flags for VMfailInvalid.
8358 */
8359 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8360 nested_vmx_failInvalid(vcpu);
8361 return kvm_skip_emulated_instruction(vcpu);
8362 }
8363 vmcs12 = get_shadow_vmcs12(vcpu);
8364 }
8365
Nadav Har'El49f705c2011-05-25 23:08:30 +03008366 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008367 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008368 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008369 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008370 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008371 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008372 }
8373 /*
8374 * Now copy part of this value to register or memory, as requested.
8375 * Note that the number of bits actually copied is 32 or 64 depending
8376 * on the guest's mode (32 or 64 bit), not on the given field's length.
8377 */
8378 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008379 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008380 field_value);
8381 } else {
8382 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008383 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008384 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008385 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008386 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8387 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008388 }
8389
8390 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008391 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008392}
8393
8394
8395static int handle_vmwrite(struct kvm_vcpu *vcpu)
8396{
8397 unsigned long field;
8398 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008399 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008400 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8401 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008402
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 /* The value to write might be 32 or 64 bits, depending on L1's long
8404 * mode, and eventually we need to write that into a field of several
8405 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008406 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008407 * bits into the vmcs12 field.
8408 */
8409 u64 field_value = 0;
8410 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008411 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008412
Kyle Hueyeb277562016-11-29 12:40:39 -08008413 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008414 return 1;
8415
Kyle Huey6affcbe2016-11-29 12:40:40 -08008416 if (!nested_vmx_check_vmcs12(vcpu))
8417 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008418
Nadav Har'El49f705c2011-05-25 23:08:30 +03008419 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008420 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008421 (((vmx_instruction_info) >> 3) & 0xf));
8422 else {
8423 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008424 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008426 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8427 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008428 kvm_inject_page_fault(vcpu, &e);
8429 return 1;
8430 }
8431 }
8432
8433
Nadav Amit27e6fb52014-06-18 17:19:26 +03008434 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008435 /*
8436 * If the vCPU supports "VMWRITE to any supported field in the
8437 * VMCS," then the "read-only" fields are actually read/write.
8438 */
8439 if (vmcs_field_readonly(field) &&
8440 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441 nested_vmx_failValid(vcpu,
8442 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008443 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444 }
8445
Liran Alon6d894f42018-06-23 02:35:09 +03008446 if (!is_guest_mode(vcpu))
8447 vmcs12 = get_vmcs12(vcpu);
8448 else {
8449 /*
8450 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8451 * to shadowed-field sets the ALU flags for VMfailInvalid.
8452 */
8453 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8454 nested_vmx_failInvalid(vcpu);
8455 return kvm_skip_emulated_instruction(vcpu);
8456 }
8457 vmcs12 = get_shadow_vmcs12(vcpu);
8458
8459 }
8460
8461 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008462 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008463 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008464 }
8465
Liran Alon6d894f42018-06-23 02:35:09 +03008466 /*
8467 * Do not track vmcs12 dirty-state if in guest-mode
8468 * as we actually dirty shadow vmcs12 instead of vmcs12.
8469 */
8470 if (!is_guest_mode(vcpu)) {
8471 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008472#define SHADOW_FIELD_RW(x) case x:
8473#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008474 /*
8475 * The fields that can be updated by L1 without a vmexit are
8476 * always updated in the vmcs02, the others go down the slow
8477 * path of prepare_vmcs02.
8478 */
8479 break;
8480 default:
8481 vmx->nested.dirty_vmcs12 = true;
8482 break;
8483 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008484 }
8485
Nadav Har'El49f705c2011-05-25 23:08:30 +03008486 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008487 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008488}
8489
Jim Mattsona8bc2842016-11-30 12:03:44 -08008490static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8491{
8492 vmx->nested.current_vmptr = vmptr;
8493 if (enable_shadow_vmcs) {
8494 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8495 SECONDARY_EXEC_SHADOW_VMCS);
8496 vmcs_write64(VMCS_LINK_POINTER,
8497 __pa(vmx->vmcs01.shadow_vmcs));
8498 vmx->nested.sync_shadow_vmcs = true;
8499 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008500 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008501}
8502
Nadav Har'El63846662011-05-25 23:07:29 +03008503/* Emulate the VMPTRLD instruction */
8504static int handle_vmptrld(struct kvm_vcpu *vcpu)
8505{
8506 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008507 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008508
8509 if (!nested_vmx_check_permission(vcpu))
8510 return 1;
8511
Radim Krčmářcbf71272017-05-19 15:48:51 +02008512 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008513 return 1;
8514
Radim Krčmářcbf71272017-05-19 15:48:51 +02008515 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8516 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8517 return kvm_skip_emulated_instruction(vcpu);
8518 }
8519
8520 if (vmptr == vmx->nested.vmxon_ptr) {
8521 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8522 return kvm_skip_emulated_instruction(vcpu);
8523 }
8524
Nadav Har'El63846662011-05-25 23:07:29 +03008525 if (vmx->nested.current_vmptr != vmptr) {
8526 struct vmcs12 *new_vmcs12;
8527 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008528 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8529 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008530 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008531 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008532 }
8533 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008534 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008535 (new_vmcs12->hdr.shadow_vmcs &&
8536 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008537 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008538 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008539 nested_vmx_failValid(vcpu,
8540 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008541 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008542 }
Nadav Har'El63846662011-05-25 23:07:29 +03008543
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008544 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008545 /*
8546 * Load VMCS12 from guest memory since it is not already
8547 * cached.
8548 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008549 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8550 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008551 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008552
Jim Mattsona8bc2842016-11-30 12:03:44 -08008553 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008554 }
8555
8556 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008557 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008558}
8559
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008560/* Emulate the VMPTRST instruction */
8561static int handle_vmptrst(struct kvm_vcpu *vcpu)
8562{
8563 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8564 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8565 gva_t vmcs_gva;
8566 struct x86_exception e;
8567
8568 if (!nested_vmx_check_permission(vcpu))
8569 return 1;
8570
8571 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008572 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008573 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008574 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008575 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8576 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8577 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008578 kvm_inject_page_fault(vcpu, &e);
8579 return 1;
8580 }
8581 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008582 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008583}
8584
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008585/* Emulate the INVEPT instruction */
8586static int handle_invept(struct kvm_vcpu *vcpu)
8587{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008588 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008589 u32 vmx_instruction_info, types;
8590 unsigned long type;
8591 gva_t gva;
8592 struct x86_exception e;
8593 struct {
8594 u64 eptp, gpa;
8595 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008596
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008597 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008598 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008599 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008600 kvm_queue_exception(vcpu, UD_VECTOR);
8601 return 1;
8602 }
8603
8604 if (!nested_vmx_check_permission(vcpu))
8605 return 1;
8606
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008607 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008608 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008609
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008610 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008611
Jim Mattson85c856b2016-10-26 08:38:38 -07008612 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008613 nested_vmx_failValid(vcpu,
8614 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008615 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008616 }
8617
8618 /* According to the Intel VMX instruction reference, the memory
8619 * operand is read even if it isn't needed (e.g., for type==global)
8620 */
8621 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008622 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008623 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008624 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008625 kvm_inject_page_fault(vcpu, &e);
8626 return 1;
8627 }
8628
8629 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008630 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008631 /*
8632 * TODO: track mappings and invalidate
8633 * single context requests appropriately
8634 */
8635 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008636 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008637 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 nested_vmx_succeed(vcpu);
8639 break;
8640 default:
8641 BUG_ON(1);
8642 break;
8643 }
8644
Kyle Huey6affcbe2016-11-29 12:40:40 -08008645 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008646}
8647
Petr Matouseka642fc32014-09-23 20:22:30 +02008648static int handle_invvpid(struct kvm_vcpu *vcpu)
8649{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008650 struct vcpu_vmx *vmx = to_vmx(vcpu);
8651 u32 vmx_instruction_info;
8652 unsigned long type, types;
8653 gva_t gva;
8654 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008655 struct {
8656 u64 vpid;
8657 u64 gla;
8658 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008659
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008660 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008661 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008662 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008663 kvm_queue_exception(vcpu, UD_VECTOR);
8664 return 1;
8665 }
8666
8667 if (!nested_vmx_check_permission(vcpu))
8668 return 1;
8669
8670 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8671 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8672
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008673 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008674 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008675
Jim Mattson85c856b2016-10-26 08:38:38 -07008676 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008677 nested_vmx_failValid(vcpu,
8678 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008679 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008680 }
8681
8682 /* according to the intel vmx instruction reference, the memory
8683 * operand is read even if it isn't needed (e.g., for type==global)
8684 */
8685 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8686 vmx_instruction_info, false, &gva))
8687 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008688 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008689 kvm_inject_page_fault(vcpu, &e);
8690 return 1;
8691 }
Jim Mattson40352602017-06-28 09:37:37 -07008692 if (operand.vpid >> 16) {
8693 nested_vmx_failValid(vcpu,
8694 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8695 return kvm_skip_emulated_instruction(vcpu);
8696 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008697
8698 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008699 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008700 if (!operand.vpid ||
8701 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008702 nested_vmx_failValid(vcpu,
8703 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8704 return kvm_skip_emulated_instruction(vcpu);
8705 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008706 if (cpu_has_vmx_invvpid_individual_addr() &&
8707 vmx->nested.vpid02) {
8708 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8709 vmx->nested.vpid02, operand.gla);
8710 } else
8711 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8712 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008713 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008714 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008715 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008716 nested_vmx_failValid(vcpu,
8717 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008718 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008719 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008720 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008721 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008722 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008723 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008724 break;
8725 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008726 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008727 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008728 }
8729
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008730 nested_vmx_succeed(vcpu);
8731
Kyle Huey6affcbe2016-11-29 12:40:40 -08008732 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008733}
8734
Kai Huang843e4332015-01-28 10:54:28 +08008735static int handle_pml_full(struct kvm_vcpu *vcpu)
8736{
8737 unsigned long exit_qualification;
8738
8739 trace_kvm_pml_full(vcpu->vcpu_id);
8740
8741 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8742
8743 /*
8744 * PML buffer FULL happened while executing iret from NMI,
8745 * "blocked by NMI" bit has to be set before next VM entry.
8746 */
8747 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008748 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008749 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8750 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8751 GUEST_INTR_STATE_NMI);
8752
8753 /*
8754 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8755 * here.., and there's no userspace involvement needed for PML.
8756 */
8757 return 1;
8758}
8759
Yunhong Jiang64672c92016-06-13 14:19:59 -07008760static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8761{
8762 kvm_lapic_expired_hv_timer(vcpu);
8763 return 1;
8764}
8765
Bandan Das41ab9372017-08-03 15:54:43 -04008766static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8767{
8768 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008769 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8770
8771 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008772 switch (address & VMX_EPTP_MT_MASK) {
8773 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008774 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008775 return false;
8776 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008777 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008778 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008779 return false;
8780 break;
8781 default:
8782 return false;
8783 }
8784
David Hildenbrandbb97a012017-08-10 23:15:28 +02008785 /* only 4 levels page-walk length are valid */
8786 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008787 return false;
8788
8789 /* Reserved bits should not be set */
8790 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8791 return false;
8792
8793 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008794 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008795 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008796 return false;
8797 }
8798
8799 return true;
8800}
8801
8802static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8803 struct vmcs12 *vmcs12)
8804{
8805 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8806 u64 address;
8807 bool accessed_dirty;
8808 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8809
8810 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8811 !nested_cpu_has_ept(vmcs12))
8812 return 1;
8813
8814 if (index >= VMFUNC_EPTP_ENTRIES)
8815 return 1;
8816
8817
8818 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8819 &address, index * 8, 8))
8820 return 1;
8821
David Hildenbrandbb97a012017-08-10 23:15:28 +02008822 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008823
8824 /*
8825 * If the (L2) guest does a vmfunc to the currently
8826 * active ept pointer, we don't have to do anything else
8827 */
8828 if (vmcs12->ept_pointer != address) {
8829 if (!valid_ept_address(vcpu, address))
8830 return 1;
8831
8832 kvm_mmu_unload(vcpu);
8833 mmu->ept_ad = accessed_dirty;
8834 mmu->base_role.ad_disabled = !accessed_dirty;
8835 vmcs12->ept_pointer = address;
8836 /*
8837 * TODO: Check what's the correct approach in case
8838 * mmu reload fails. Currently, we just let the next
8839 * reload potentially fail
8840 */
8841 kvm_mmu_reload(vcpu);
8842 }
8843
8844 return 0;
8845}
8846
Bandan Das2a499e42017-08-03 15:54:41 -04008847static int handle_vmfunc(struct kvm_vcpu *vcpu)
8848{
Bandan Das27c42a12017-08-03 15:54:42 -04008849 struct vcpu_vmx *vmx = to_vmx(vcpu);
8850 struct vmcs12 *vmcs12;
8851 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8852
8853 /*
8854 * VMFUNC is only supported for nested guests, but we always enable the
8855 * secondary control for simplicity; for non-nested mode, fake that we
8856 * didn't by injecting #UD.
8857 */
8858 if (!is_guest_mode(vcpu)) {
8859 kvm_queue_exception(vcpu, UD_VECTOR);
8860 return 1;
8861 }
8862
8863 vmcs12 = get_vmcs12(vcpu);
8864 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8865 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008866
8867 switch (function) {
8868 case 0:
8869 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8870 goto fail;
8871 break;
8872 default:
8873 goto fail;
8874 }
8875 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008876
8877fail:
8878 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8879 vmcs_read32(VM_EXIT_INTR_INFO),
8880 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008881 return 1;
8882}
8883
Nadav Har'El0140cae2011-05-25 23:06:28 +03008884/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008885 * The exit handlers return 1 if the exit was handled fully and guest execution
8886 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8887 * to be done to userspace and return 0.
8888 */
Mathias Krause772e0312012-08-30 01:30:19 +02008889static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008890 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8891 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008892 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008893 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008894 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008895 [EXIT_REASON_CR_ACCESS] = handle_cr,
8896 [EXIT_REASON_DR_ACCESS] = handle_dr,
8897 [EXIT_REASON_CPUID] = handle_cpuid,
8898 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8899 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8900 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8901 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008902 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008903 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008904 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008905 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008906 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008907 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008908 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008909 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008910 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008911 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008912 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008913 [EXIT_REASON_VMOFF] = handle_vmoff,
8914 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008915 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8916 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008917 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008918 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008919 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008920 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008921 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008922 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008923 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8924 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008925 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8926 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008927 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008928 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008929 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008930 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008931 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008932 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008933 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008934 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008935 [EXIT_REASON_XSAVES] = handle_xsaves,
8936 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008937 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008938 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008939 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008940};
8941
8942static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008943 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008944
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008945static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8946 struct vmcs12 *vmcs12)
8947{
8948 unsigned long exit_qualification;
8949 gpa_t bitmap, last_bitmap;
8950 unsigned int port;
8951 int size;
8952 u8 b;
8953
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008954 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008955 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008956
8957 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8958
8959 port = exit_qualification >> 16;
8960 size = (exit_qualification & 7) + 1;
8961
8962 last_bitmap = (gpa_t)-1;
8963 b = -1;
8964
8965 while (size > 0) {
8966 if (port < 0x8000)
8967 bitmap = vmcs12->io_bitmap_a;
8968 else if (port < 0x10000)
8969 bitmap = vmcs12->io_bitmap_b;
8970 else
Joe Perches1d804d02015-03-30 16:46:09 -07008971 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008972 bitmap += (port & 0x7fff) / 8;
8973
8974 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008975 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008976 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008977 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008978 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008979
8980 port++;
8981 size--;
8982 last_bitmap = bitmap;
8983 }
8984
Joe Perches1d804d02015-03-30 16:46:09 -07008985 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008986}
8987
Nadav Har'El644d7112011-05-25 23:12:35 +03008988/*
8989 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8990 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8991 * disinterest in the current event (read or write a specific MSR) by using an
8992 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8993 */
8994static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8995 struct vmcs12 *vmcs12, u32 exit_reason)
8996{
8997 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8998 gpa_t bitmap;
8999
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009000 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009001 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009002
9003 /*
9004 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9005 * for the four combinations of read/write and low/high MSR numbers.
9006 * First we need to figure out which of the four to use:
9007 */
9008 bitmap = vmcs12->msr_bitmap;
9009 if (exit_reason == EXIT_REASON_MSR_WRITE)
9010 bitmap += 2048;
9011 if (msr_index >= 0xc0000000) {
9012 msr_index -= 0xc0000000;
9013 bitmap += 1024;
9014 }
9015
9016 /* Then read the msr_index'th bit from this bitmap: */
9017 if (msr_index < 1024*8) {
9018 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009019 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009020 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009021 return 1 & (b >> (msr_index & 7));
9022 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009023 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009024}
9025
9026/*
9027 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9028 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9029 * intercept (via guest_host_mask etc.) the current event.
9030 */
9031static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9032 struct vmcs12 *vmcs12)
9033{
9034 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9035 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009036 int reg;
9037 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009038
9039 switch ((exit_qualification >> 4) & 3) {
9040 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009041 reg = (exit_qualification >> 8) & 15;
9042 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009043 switch (cr) {
9044 case 0:
9045 if (vmcs12->cr0_guest_host_mask &
9046 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009047 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009048 break;
9049 case 3:
9050 if ((vmcs12->cr3_target_count >= 1 &&
9051 vmcs12->cr3_target_value0 == val) ||
9052 (vmcs12->cr3_target_count >= 2 &&
9053 vmcs12->cr3_target_value1 == val) ||
9054 (vmcs12->cr3_target_count >= 3 &&
9055 vmcs12->cr3_target_value2 == val) ||
9056 (vmcs12->cr3_target_count >= 4 &&
9057 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009058 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009059 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009060 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009061 break;
9062 case 4:
9063 if (vmcs12->cr4_guest_host_mask &
9064 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009066 break;
9067 case 8:
9068 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009069 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009070 break;
9071 }
9072 break;
9073 case 2: /* clts */
9074 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9075 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009076 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009077 break;
9078 case 1: /* mov from cr */
9079 switch (cr) {
9080 case 3:
9081 if (vmcs12->cpu_based_vm_exec_control &
9082 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009083 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009084 break;
9085 case 8:
9086 if (vmcs12->cpu_based_vm_exec_control &
9087 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009088 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009089 break;
9090 }
9091 break;
9092 case 3: /* lmsw */
9093 /*
9094 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9095 * cr0. Other attempted changes are ignored, with no exit.
9096 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009097 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009098 if (vmcs12->cr0_guest_host_mask & 0xe &
9099 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009100 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009101 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9102 !(vmcs12->cr0_read_shadow & 0x1) &&
9103 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009104 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009105 break;
9106 }
Joe Perches1d804d02015-03-30 16:46:09 -07009107 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009108}
9109
9110/*
9111 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9112 * should handle it ourselves in L0 (and then continue L2). Only call this
9113 * when in is_guest_mode (L2).
9114 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009115static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009116{
Nadav Har'El644d7112011-05-25 23:12:35 +03009117 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9118 struct vcpu_vmx *vmx = to_vmx(vcpu);
9119 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9120
Jim Mattson4f350c62017-09-14 16:31:44 -07009121 if (vmx->nested.nested_run_pending)
9122 return false;
9123
9124 if (unlikely(vmx->fail)) {
9125 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9126 vmcs_read32(VM_INSTRUCTION_ERROR));
9127 return true;
9128 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009129
David Matlackc9f04402017-08-01 14:00:40 -07009130 /*
9131 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009132 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9133 * Page). The CPU may write to these pages via their host
9134 * physical address while L2 is running, bypassing any
9135 * address-translation-based dirty tracking (e.g. EPT write
9136 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009137 *
9138 * Mark them dirty on every exit from L2 to prevent them from
9139 * getting out of sync with dirty tracking.
9140 */
9141 nested_mark_vmcs12_pages_dirty(vcpu);
9142
Jim Mattson4f350c62017-09-14 16:31:44 -07009143 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9144 vmcs_readl(EXIT_QUALIFICATION),
9145 vmx->idt_vectoring_info,
9146 intr_info,
9147 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9148 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009149
9150 switch (exit_reason) {
9151 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009152 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009153 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009154 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009155 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009156 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009157 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009158 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009159 else if (is_debug(intr_info) &&
9160 vcpu->guest_debug &
9161 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9162 return false;
9163 else if (is_breakpoint(intr_info) &&
9164 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9165 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009166 return vmcs12->exception_bitmap &
9167 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9168 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009169 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009170 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009171 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009172 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009173 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009174 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009175 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009176 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009177 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009178 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009179 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009180 case EXIT_REASON_HLT:
9181 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9182 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009183 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009184 case EXIT_REASON_INVLPG:
9185 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9186 case EXIT_REASON_RDPMC:
9187 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009188 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009189 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009190 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009191 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009192 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009193 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9194 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9195 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9196 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9197 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9198 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009199 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009200 /*
9201 * VMX instructions trap unconditionally. This allows L1 to
9202 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9203 */
Joe Perches1d804d02015-03-30 16:46:09 -07009204 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009205 case EXIT_REASON_CR_ACCESS:
9206 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9207 case EXIT_REASON_DR_ACCESS:
9208 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9209 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009210 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009211 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9212 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009213 case EXIT_REASON_MSR_READ:
9214 case EXIT_REASON_MSR_WRITE:
9215 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9216 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009217 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009218 case EXIT_REASON_MWAIT_INSTRUCTION:
9219 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009220 case EXIT_REASON_MONITOR_TRAP_FLAG:
9221 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009222 case EXIT_REASON_MONITOR_INSTRUCTION:
9223 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9224 case EXIT_REASON_PAUSE_INSTRUCTION:
9225 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9226 nested_cpu_has2(vmcs12,
9227 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9228 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009229 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009230 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009231 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009232 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009233 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009234 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009235 /*
9236 * The controls for "virtualize APIC accesses," "APIC-
9237 * register virtualization," and "virtual-interrupt
9238 * delivery" only come from vmcs12.
9239 */
Joe Perches1d804d02015-03-30 16:46:09 -07009240 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009241 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009242 /*
9243 * L0 always deals with the EPT violation. If nested EPT is
9244 * used, and the nested mmu code discovers that the address is
9245 * missing in the guest EPT table (EPT12), the EPT violation
9246 * will be injected with nested_ept_inject_page_fault()
9247 */
Joe Perches1d804d02015-03-30 16:46:09 -07009248 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009249 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009250 /*
9251 * L2 never uses directly L1's EPT, but rather L0's own EPT
9252 * table (shadow on EPT) or a merged EPT table that L0 built
9253 * (EPT on EPT). So any problems with the structure of the
9254 * table is L0's fault.
9255 */
Joe Perches1d804d02015-03-30 16:46:09 -07009256 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009257 case EXIT_REASON_INVPCID:
9258 return
9259 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9260 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009261 case EXIT_REASON_WBINVD:
9262 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9263 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009264 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009265 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9266 /*
9267 * This should never happen, since it is not possible to
9268 * set XSS to a non-zero value---neither in L1 nor in L2.
9269 * If if it were, XSS would have to be checked against
9270 * the XSS exit bitmap in vmcs12.
9271 */
9272 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009273 case EXIT_REASON_PREEMPTION_TIMER:
9274 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009275 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009276 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009277 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009278 case EXIT_REASON_VMFUNC:
9279 /* VM functions are emulated through L2->L0 vmexits. */
9280 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009281 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009282 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009283 }
9284}
9285
Paolo Bonzini7313c692017-07-27 10:31:25 +02009286static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9287{
9288 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9289
9290 /*
9291 * At this point, the exit interruption info in exit_intr_info
9292 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9293 * we need to query the in-kernel LAPIC.
9294 */
9295 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9296 if ((exit_intr_info &
9297 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9298 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9299 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9300 vmcs12->vm_exit_intr_error_code =
9301 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9302 }
9303
9304 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9305 vmcs_readl(EXIT_QUALIFICATION));
9306 return 1;
9307}
9308
Avi Kivity586f9602010-11-18 13:09:54 +02009309static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9310{
9311 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9312 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9313}
9314
Kai Huanga3eaa862015-11-04 13:46:05 +08009315static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009316{
Kai Huanga3eaa862015-11-04 13:46:05 +08009317 if (vmx->pml_pg) {
9318 __free_page(vmx->pml_pg);
9319 vmx->pml_pg = NULL;
9320 }
Kai Huang843e4332015-01-28 10:54:28 +08009321}
9322
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009323static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009324{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009325 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009326 u64 *pml_buf;
9327 u16 pml_idx;
9328
9329 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9330
9331 /* Do nothing if PML buffer is empty */
9332 if (pml_idx == (PML_ENTITY_NUM - 1))
9333 return;
9334
9335 /* PML index always points to next available PML buffer entity */
9336 if (pml_idx >= PML_ENTITY_NUM)
9337 pml_idx = 0;
9338 else
9339 pml_idx++;
9340
9341 pml_buf = page_address(vmx->pml_pg);
9342 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9343 u64 gpa;
9344
9345 gpa = pml_buf[pml_idx];
9346 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009347 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009348 }
9349
9350 /* reset PML index */
9351 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9352}
9353
9354/*
9355 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9356 * Called before reporting dirty_bitmap to userspace.
9357 */
9358static void kvm_flush_pml_buffers(struct kvm *kvm)
9359{
9360 int i;
9361 struct kvm_vcpu *vcpu;
9362 /*
9363 * We only need to kick vcpu out of guest mode here, as PML buffer
9364 * is flushed at beginning of all VMEXITs, and it's obvious that only
9365 * vcpus running in guest are possible to have unflushed GPAs in PML
9366 * buffer.
9367 */
9368 kvm_for_each_vcpu(i, vcpu, kvm)
9369 kvm_vcpu_kick(vcpu);
9370}
9371
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009372static void vmx_dump_sel(char *name, uint32_t sel)
9373{
9374 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009375 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009376 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9377 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9378 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9379}
9380
9381static void vmx_dump_dtsel(char *name, uint32_t limit)
9382{
9383 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9384 name, vmcs_read32(limit),
9385 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9386}
9387
9388static void dump_vmcs(void)
9389{
9390 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9391 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9392 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9393 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9394 u32 secondary_exec_control = 0;
9395 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009396 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009397 int i, n;
9398
9399 if (cpu_has_secondary_exec_ctrls())
9400 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9401
9402 pr_err("*** Guest State ***\n");
9403 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9404 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9405 vmcs_readl(CR0_GUEST_HOST_MASK));
9406 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9407 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9408 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9409 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9410 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9411 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009412 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9413 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9414 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9415 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009416 }
9417 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9418 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9419 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9420 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9421 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9422 vmcs_readl(GUEST_SYSENTER_ESP),
9423 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9424 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9425 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9426 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9427 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9428 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9429 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9430 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9431 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9432 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9433 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9434 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9435 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009436 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9437 efer, vmcs_read64(GUEST_IA32_PAT));
9438 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9439 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009440 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009441 if (cpu_has_load_perf_global_ctrl &&
9442 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009443 pr_err("PerfGlobCtl = 0x%016llx\n",
9444 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009445 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009446 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009447 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9448 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9449 vmcs_read32(GUEST_ACTIVITY_STATE));
9450 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9451 pr_err("InterruptStatus = %04x\n",
9452 vmcs_read16(GUEST_INTR_STATUS));
9453
9454 pr_err("*** Host State ***\n");
9455 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9456 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9457 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9458 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9459 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9460 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9461 vmcs_read16(HOST_TR_SELECTOR));
9462 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9463 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9464 vmcs_readl(HOST_TR_BASE));
9465 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9466 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9467 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9468 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9469 vmcs_readl(HOST_CR4));
9470 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9471 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9472 vmcs_read32(HOST_IA32_SYSENTER_CS),
9473 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9474 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009475 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9476 vmcs_read64(HOST_IA32_EFER),
9477 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009478 if (cpu_has_load_perf_global_ctrl &&
9479 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009480 pr_err("PerfGlobCtl = 0x%016llx\n",
9481 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009482
9483 pr_err("*** Control State ***\n");
9484 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9485 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9486 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9487 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9488 vmcs_read32(EXCEPTION_BITMAP),
9489 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9490 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9491 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9492 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9493 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9494 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9495 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9496 vmcs_read32(VM_EXIT_INTR_INFO),
9497 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9498 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9499 pr_err(" reason=%08x qualification=%016lx\n",
9500 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9501 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9502 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9503 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009504 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009505 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009506 pr_err("TSC Multiplier = 0x%016llx\n",
9507 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009508 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9509 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9510 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9511 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9512 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009513 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009514 n = vmcs_read32(CR3_TARGET_COUNT);
9515 for (i = 0; i + 1 < n; i += 4)
9516 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9517 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9518 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9519 if (i < n)
9520 pr_err("CR3 target%u=%016lx\n",
9521 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9522 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9523 pr_err("PLE Gap=%08x Window=%08x\n",
9524 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9525 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9526 pr_err("Virtual processor ID = 0x%04x\n",
9527 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9528}
9529
Avi Kivity6aa8b732006-12-10 02:21:36 -08009530/*
9531 * The guest has exited. See if we can fix it or if we need userspace
9532 * assistance.
9533 */
Avi Kivity851ba692009-08-24 11:10:17 +03009534static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009535{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009536 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009537 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009538 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009539
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009540 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9541
Kai Huang843e4332015-01-28 10:54:28 +08009542 /*
9543 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9544 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9545 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9546 * mode as if vcpus is in root mode, the PML buffer must has been
9547 * flushed already.
9548 */
9549 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009550 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009551
Mohammed Gamal80ced182009-09-01 12:48:18 +02009552 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009553 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009554 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009555
Paolo Bonzini7313c692017-07-27 10:31:25 +02009556 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9557 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009558
Mohammed Gamal51207022010-05-31 22:40:54 +03009559 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009560 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009561 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9562 vcpu->run->fail_entry.hardware_entry_failure_reason
9563 = exit_reason;
9564 return 0;
9565 }
9566
Avi Kivity29bd8a72007-09-10 17:27:03 +03009567 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009568 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9569 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009570 = vmcs_read32(VM_INSTRUCTION_ERROR);
9571 return 0;
9572 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009573
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009574 /*
9575 * Note:
9576 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9577 * delivery event since it indicates guest is accessing MMIO.
9578 * The vm-exit can be triggered again after return to guest that
9579 * will cause infinite loop.
9580 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009581 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009582 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009583 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009584 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009585 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9586 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9587 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009588 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009589 vcpu->run->internal.data[0] = vectoring_info;
9590 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009591 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9592 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9593 vcpu->run->internal.ndata++;
9594 vcpu->run->internal.data[3] =
9595 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9596 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009597 return 0;
9598 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009599
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009600 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009601 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9602 if (vmx_interrupt_allowed(vcpu)) {
9603 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9604 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9605 vcpu->arch.nmi_pending) {
9606 /*
9607 * This CPU don't support us in finding the end of an
9608 * NMI-blocked window if the guest runs with IRQs
9609 * disabled. So we pull the trigger after 1 s of
9610 * futile waiting, but inform the user about this.
9611 */
9612 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9613 "state on VCPU %d after 1 s timeout\n",
9614 __func__, vcpu->vcpu_id);
9615 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9616 }
9617 }
9618
Avi Kivity6aa8b732006-12-10 02:21:36 -08009619 if (exit_reason < kvm_vmx_max_exit_handlers
9620 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009621 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009622 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009623 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9624 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009625 kvm_queue_exception(vcpu, UD_VECTOR);
9626 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009627 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009628}
9629
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009630static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009631{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009632 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9633
9634 if (is_guest_mode(vcpu) &&
9635 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9636 return;
9637
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009638 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009639 vmcs_write32(TPR_THRESHOLD, 0);
9640 return;
9641 }
9642
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009643 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009644}
9645
Jim Mattson8d860bb2018-05-09 16:56:05 -04009646static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009647{
9648 u32 sec_exec_control;
9649
Jim Mattson8d860bb2018-05-09 16:56:05 -04009650 if (!lapic_in_kernel(vcpu))
9651 return;
9652
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009653 /* Postpone execution until vmcs01 is the current VMCS. */
9654 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009655 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009656 return;
9657 }
9658
Paolo Bonzini35754c92015-07-29 12:05:37 +02009659 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009660 return;
9661
9662 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009663 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9664 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009665
Jim Mattson8d860bb2018-05-09 16:56:05 -04009666 switch (kvm_get_apic_mode(vcpu)) {
9667 case LAPIC_MODE_INVALID:
9668 WARN_ONCE(true, "Invalid local APIC state");
9669 case LAPIC_MODE_DISABLED:
9670 break;
9671 case LAPIC_MODE_XAPIC:
9672 if (flexpriority_enabled) {
9673 sec_exec_control |=
9674 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9675 vmx_flush_tlb(vcpu, true);
9676 }
9677 break;
9678 case LAPIC_MODE_X2APIC:
9679 if (cpu_has_vmx_virtualize_x2apic_mode())
9680 sec_exec_control |=
9681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9682 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009683 }
9684 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9685
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009686 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009687}
9688
Tang Chen38b99172014-09-24 15:57:54 +08009689static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9690{
Jim Mattsonab5df312018-05-09 17:02:03 -04009691 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009692 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009693 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009694 }
Tang Chen38b99172014-09-24 15:57:54 +08009695}
9696
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009697static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009698{
9699 u16 status;
9700 u8 old;
9701
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009702 if (max_isr == -1)
9703 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009704
9705 status = vmcs_read16(GUEST_INTR_STATUS);
9706 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009707 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009708 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009709 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009710 vmcs_write16(GUEST_INTR_STATUS, status);
9711 }
9712}
9713
9714static void vmx_set_rvi(int vector)
9715{
9716 u16 status;
9717 u8 old;
9718
Wei Wang4114c272014-11-05 10:53:43 +08009719 if (vector == -1)
9720 vector = 0;
9721
Yang Zhangc7c9c562013-01-25 10:18:51 +08009722 status = vmcs_read16(GUEST_INTR_STATUS);
9723 old = (u8)status & 0xff;
9724 if ((u8)vector != old) {
9725 status &= ~0xff;
9726 status |= (u8)vector;
9727 vmcs_write16(GUEST_INTR_STATUS, status);
9728 }
9729}
9730
9731static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9732{
Liran Alon851c1a182017-12-24 18:12:56 +02009733 /*
9734 * When running L2, updating RVI is only relevant when
9735 * vmcs12 virtual-interrupt-delivery enabled.
9736 * However, it can be enabled only when L1 also
9737 * intercepts external-interrupts and in that case
9738 * we should not update vmcs02 RVI but instead intercept
9739 * interrupt. Therefore, do nothing when running L2.
9740 */
9741 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009742 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009743}
9744
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009745static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009746{
9747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009748 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009749 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009750
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009751 WARN_ON(!vcpu->arch.apicv_active);
9752 if (pi_test_on(&vmx->pi_desc)) {
9753 pi_clear_on(&vmx->pi_desc);
9754 /*
9755 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9756 * But on x86 this is just a compiler barrier anyway.
9757 */
9758 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009759 max_irr_updated =
9760 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9761
9762 /*
9763 * If we are running L2 and L1 has a new pending interrupt
9764 * which can be injected, we should re-evaluate
9765 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009766 * If L1 intercepts external-interrupts, we should
9767 * exit from L2 to L1. Otherwise, interrupt should be
9768 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009769 */
Liran Alon851c1a182017-12-24 18:12:56 +02009770 if (is_guest_mode(vcpu) && max_irr_updated) {
9771 if (nested_exit_on_intr(vcpu))
9772 kvm_vcpu_exiting_guest_mode(vcpu);
9773 else
9774 kvm_make_request(KVM_REQ_EVENT, vcpu);
9775 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009776 } else {
9777 max_irr = kvm_lapic_find_highest_irr(vcpu);
9778 }
9779 vmx_hwapic_irr_update(vcpu, max_irr);
9780 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009781}
9782
Andrey Smetanin63086302015-11-10 15:36:32 +03009783static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009784{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009785 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009786 return;
9787
Yang Zhangc7c9c562013-01-25 10:18:51 +08009788 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9789 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9790 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9791 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9792}
9793
Paolo Bonzini967235d2016-12-19 14:03:45 +01009794static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9795{
9796 struct vcpu_vmx *vmx = to_vmx(vcpu);
9797
9798 pi_clear_on(&vmx->pi_desc);
9799 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9800}
9801
Avi Kivity51aa01d2010-07-20 14:31:20 +03009802static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009803{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009804 u32 exit_intr_info = 0;
9805 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009806
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009807 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9808 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009809 return;
9810
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009811 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9812 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9813 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009814
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009815 /* if exit due to PF check for async PF */
9816 if (is_page_fault(exit_intr_info))
9817 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9818
Andi Kleena0861c02009-06-08 17:37:09 +08009819 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009820 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9821 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009822 kvm_machine_check();
9823
Gleb Natapov20f65982009-05-11 13:35:55 +03009824 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009825 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009826 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009827 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009828 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009829 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009830}
Gleb Natapov20f65982009-05-11 13:35:55 +03009831
Yang Zhanga547c6d2013-04-11 19:25:10 +08009832static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9833{
9834 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9835
Yang Zhanga547c6d2013-04-11 19:25:10 +08009836 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9837 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9838 unsigned int vector;
9839 unsigned long entry;
9840 gate_desc *desc;
9841 struct vcpu_vmx *vmx = to_vmx(vcpu);
9842#ifdef CONFIG_X86_64
9843 unsigned long tmp;
9844#endif
9845
9846 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9847 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009848 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009849 asm volatile(
9850#ifdef CONFIG_X86_64
9851 "mov %%" _ASM_SP ", %[sp]\n\t"
9852 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9853 "push $%c[ss]\n\t"
9854 "push %[sp]\n\t"
9855#endif
9856 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009857 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009858 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009859 :
9860#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009861 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009862#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009863 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009864 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009865 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009866 [ss]"i"(__KERNEL_DS),
9867 [cs]"i"(__KERNEL_CS)
9868 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009869 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009870}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009871STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009872
Tom Lendackybc226f02018-05-10 22:06:39 +02009873static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009874{
Tom Lendackybc226f02018-05-10 22:06:39 +02009875 switch (index) {
9876 case MSR_IA32_SMBASE:
9877 /*
9878 * We cannot do SMM unless we can run the guest in big
9879 * real mode.
9880 */
9881 return enable_unrestricted_guest || emulate_invalid_guest_state;
9882 case MSR_AMD64_VIRT_SPEC_CTRL:
9883 /* This is AMD only. */
9884 return false;
9885 default:
9886 return true;
9887 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009888}
9889
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009890static bool vmx_mpx_supported(void)
9891{
9892 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9893 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9894}
9895
Wanpeng Li55412b22014-12-02 19:21:30 +08009896static bool vmx_xsaves_supported(void)
9897{
9898 return vmcs_config.cpu_based_2nd_exec_ctrl &
9899 SECONDARY_EXEC_XSAVES;
9900}
9901
Avi Kivity51aa01d2010-07-20 14:31:20 +03009902static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9903{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009904 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009905 bool unblock_nmi;
9906 u8 vector;
9907 bool idtv_info_valid;
9908
9909 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009910
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009911 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009912 if (vmx->loaded_vmcs->nmi_known_unmasked)
9913 return;
9914 /*
9915 * Can't use vmx->exit_intr_info since we're not sure what
9916 * the exit reason is.
9917 */
9918 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9919 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9920 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9921 /*
9922 * SDM 3: 27.7.1.2 (September 2008)
9923 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9924 * a guest IRET fault.
9925 * SDM 3: 23.2.2 (September 2008)
9926 * Bit 12 is undefined in any of the following cases:
9927 * If the VM exit sets the valid bit in the IDT-vectoring
9928 * information field.
9929 * If the VM exit is due to a double fault.
9930 */
9931 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9932 vector != DF_VECTOR && !idtv_info_valid)
9933 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9934 GUEST_INTR_STATE_NMI);
9935 else
9936 vmx->loaded_vmcs->nmi_known_unmasked =
9937 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9938 & GUEST_INTR_STATE_NMI);
9939 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9940 vmx->loaded_vmcs->vnmi_blocked_time +=
9941 ktime_to_ns(ktime_sub(ktime_get(),
9942 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009943}
9944
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009945static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009946 u32 idt_vectoring_info,
9947 int instr_len_field,
9948 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009949{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009950 u8 vector;
9951 int type;
9952 bool idtv_info_valid;
9953
9954 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009955
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009956 vcpu->arch.nmi_injected = false;
9957 kvm_clear_exception_queue(vcpu);
9958 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009959
9960 if (!idtv_info_valid)
9961 return;
9962
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009963 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009964
Avi Kivity668f6122008-07-02 09:28:55 +03009965 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9966 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009967
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009968 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009969 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009970 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009971 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009972 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009973 * Clear bit "block by NMI" before VM entry if a NMI
9974 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009975 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009976 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009977 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009978 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009979 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009980 /* fall through */
9981 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009982 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009983 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009984 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009985 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009986 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009987 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009988 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009989 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009990 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009991 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009992 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009993 break;
9994 default:
9995 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009996 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009997}
9998
Avi Kivity83422e12010-07-20 14:43:23 +03009999static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10000{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010001 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010002 VM_EXIT_INSTRUCTION_LEN,
10003 IDT_VECTORING_ERROR_CODE);
10004}
10005
Avi Kivityb463a6f2010-07-20 15:06:17 +030010006static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10007{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010008 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010009 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10010 VM_ENTRY_INSTRUCTION_LEN,
10011 VM_ENTRY_EXCEPTION_ERROR_CODE);
10012
10013 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10014}
10015
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010016static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10017{
10018 int i, nr_msrs;
10019 struct perf_guest_switch_msr *msrs;
10020
10021 msrs = perf_guest_get_msrs(&nr_msrs);
10022
10023 if (!msrs)
10024 return;
10025
10026 for (i = 0; i < nr_msrs; i++)
10027 if (msrs[i].host == msrs[i].guest)
10028 clear_atomic_switch_msr(vmx, msrs[i].msr);
10029 else
10030 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10031 msrs[i].host);
10032}
10033
Jiang Biao33365e72016-11-03 15:03:37 +080010034static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010035{
10036 struct vcpu_vmx *vmx = to_vmx(vcpu);
10037 u64 tscl;
10038 u32 delta_tsc;
10039
10040 if (vmx->hv_deadline_tsc == -1)
10041 return;
10042
10043 tscl = rdtsc();
10044 if (vmx->hv_deadline_tsc > tscl)
10045 /* sure to be 32 bit only because checked on set_hv_timer */
10046 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10047 cpu_preemption_timer_multi);
10048 else
10049 delta_tsc = 0;
10050
10051 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10052}
10053
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010054static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010055{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010057 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010058
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010059 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010060 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010061 vmx->loaded_vmcs->soft_vnmi_blocked))
10062 vmx->loaded_vmcs->entry_time = ktime_get();
10063
Avi Kivity104f2262010-11-18 13:12:52 +020010064 /* Don't enter VMX if guest state is invalid, let the exit handler
10065 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010066 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010067 return;
10068
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010069 if (vmx->ple_window_dirty) {
10070 vmx->ple_window_dirty = false;
10071 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10072 }
10073
Abel Gordon012f83c2013-04-18 14:39:25 +030010074 if (vmx->nested.sync_shadow_vmcs) {
10075 copy_vmcs12_to_shadow(vmx);
10076 vmx->nested.sync_shadow_vmcs = false;
10077 }
10078
Avi Kivity104f2262010-11-18 13:12:52 +020010079 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10080 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10081 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10082 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10083
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010084 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010085 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010086 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010087 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010088 }
10089
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010090 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010091 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010092 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010093 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010094 }
10095
Avi Kivity104f2262010-11-18 13:12:52 +020010096 /* When single-stepping over STI and MOV SS, we must clear the
10097 * corresponding interruptibility bits in the guest state. Otherwise
10098 * vmentry fails as it then expects bit 14 (BS) in pending debug
10099 * exceptions being set, but that's not correct for the guest debugging
10100 * case. */
10101 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10102 vmx_set_interrupt_shadow(vcpu, 0);
10103
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010104 if (static_cpu_has(X86_FEATURE_PKU) &&
10105 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10106 vcpu->arch.pkru != vmx->host_pkru)
10107 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010108
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010109 atomic_switch_perf_msrs(vmx);
10110
Yunhong Jiang64672c92016-06-13 14:19:59 -070010111 vmx_arm_hv_timer(vcpu);
10112
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010113 /*
10114 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10115 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10116 * is no need to worry about the conditional branch over the wrmsr
10117 * being speculatively taken.
10118 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010119 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010120
Nadav Har'Eld462b812011-05-24 15:26:10 +030010121 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010122
10123 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10124 (unsigned long)&current_evmcs->host_rsp : 0;
10125
Avi Kivity104f2262010-11-18 13:12:52 +020010126 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010127 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010128 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10129 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10130 "push %%" _ASM_CX " \n\t"
10131 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010132 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010133 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010134 /* Avoid VMWRITE when Enlightened VMCS is in use */
10135 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10136 "jz 2f \n\t"
10137 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10138 "jmp 1f \n\t"
10139 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010140 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010141 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010142 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010143 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10144 "mov %%cr2, %%" _ASM_DX " \n\t"
10145 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010146 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010147 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010148 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010149 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010150 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010151 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010152 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10153 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10154 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10155 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10156 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10157 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010158#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010159 "mov %c[r8](%0), %%r8 \n\t"
10160 "mov %c[r9](%0), %%r9 \n\t"
10161 "mov %c[r10](%0), %%r10 \n\t"
10162 "mov %c[r11](%0), %%r11 \n\t"
10163 "mov %c[r12](%0), %%r12 \n\t"
10164 "mov %c[r13](%0), %%r13 \n\t"
10165 "mov %c[r14](%0), %%r14 \n\t"
10166 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010167#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010168 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010169
Avi Kivity6aa8b732006-12-10 02:21:36 -080010170 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010171 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010172 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010173 "jmp 2f \n\t"
10174 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10175 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010176 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010177 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010178 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010179 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010180 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10181 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10182 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10183 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10184 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10185 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10186 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010187#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010188 "mov %%r8, %c[r8](%0) \n\t"
10189 "mov %%r9, %c[r9](%0) \n\t"
10190 "mov %%r10, %c[r10](%0) \n\t"
10191 "mov %%r11, %c[r11](%0) \n\t"
10192 "mov %%r12, %c[r12](%0) \n\t"
10193 "mov %%r13, %c[r13](%0) \n\t"
10194 "mov %%r14, %c[r14](%0) \n\t"
10195 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010196 "xor %%r8d, %%r8d \n\t"
10197 "xor %%r9d, %%r9d \n\t"
10198 "xor %%r10d, %%r10d \n\t"
10199 "xor %%r11d, %%r11d \n\t"
10200 "xor %%r12d, %%r12d \n\t"
10201 "xor %%r13d, %%r13d \n\t"
10202 "xor %%r14d, %%r14d \n\t"
10203 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010204#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010205 "mov %%cr2, %%" _ASM_AX " \n\t"
10206 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010207
Jim Mattson0cb5b302018-01-03 14:31:38 -080010208 "xor %%eax, %%eax \n\t"
10209 "xor %%ebx, %%ebx \n\t"
10210 "xor %%esi, %%esi \n\t"
10211 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010212 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010213 ".pushsection .rodata \n\t"
10214 ".global vmx_return \n\t"
10215 "vmx_return: " _ASM_PTR " 2b \n\t"
10216 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010217 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010218 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010219 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010220 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010221 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10222 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10223 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10224 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10225 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10226 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10227 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010228#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010229 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10230 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10231 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10232 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10233 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10234 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10235 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10236 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010237#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010238 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10239 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010240 : "cc", "memory"
10241#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010242 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010243 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010244#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010245 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010246#endif
10247 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010248
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010249 /*
10250 * We do not use IBRS in the kernel. If this vCPU has used the
10251 * SPEC_CTRL MSR it may have left it on; save the value and
10252 * turn it off. This is much more efficient than blindly adding
10253 * it to the atomic save/restore list. Especially as the former
10254 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10255 *
10256 * For non-nested case:
10257 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10258 * save it.
10259 *
10260 * For nested case:
10261 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10262 * save it.
10263 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010264 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010265 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010266
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010267 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010268
David Woodhouse117cc7a2018-01-12 11:11:27 +000010269 /* Eliminate branch target predictions from guest mode */
10270 vmexit_fill_RSB();
10271
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010272 /* All fields are clean at this point */
10273 if (static_branch_unlikely(&enable_evmcs))
10274 current_evmcs->hv_clean_fields |=
10275 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10276
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010277 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010278 if (vmx->host_debugctlmsr)
10279 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010280
Avi Kivityaa67f602012-08-01 16:48:03 +030010281#ifndef CONFIG_X86_64
10282 /*
10283 * The sysexit path does not restore ds/es, so we must set them to
10284 * a reasonable value ourselves.
10285 *
10286 * We can't defer this to vmx_load_host_state() since that function
10287 * may be executed in interrupt context, which saves and restore segments
10288 * around it, nullifying its effect.
10289 */
10290 loadsegment(ds, __USER_DS);
10291 loadsegment(es, __USER_DS);
10292#endif
10293
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010294 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010295 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010296 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010297 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010298 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010299 vcpu->arch.regs_dirty = 0;
10300
Gleb Natapove0b890d2013-09-25 12:51:33 +030010301 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010302 * eager fpu is enabled if PKEY is supported and CR4 is switched
10303 * back on host, so it is safe to read guest PKRU from current
10304 * XSAVE.
10305 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010306 if (static_cpu_has(X86_FEATURE_PKU) &&
10307 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10308 vcpu->arch.pkru = __read_pkru();
10309 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010310 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010311 }
10312
Gleb Natapove0b890d2013-09-25 12:51:33 +030010313 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010314 vmx->idt_vectoring_info = 0;
10315
10316 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10317 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10318 return;
10319
10320 vmx->loaded_vmcs->launched = 1;
10321 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010322
Avi Kivity51aa01d2010-07-20 14:31:20 +030010323 vmx_complete_atomic_exit(vmx);
10324 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010325 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010326}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010327STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010328
Sean Christopherson434a1e92018-03-20 12:17:18 -070010329static struct kvm *vmx_vm_alloc(void)
10330{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010331 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010332 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010333}
10334
10335static void vmx_vm_free(struct kvm *kvm)
10336{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010337 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010338}
10339
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010340static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010341{
10342 struct vcpu_vmx *vmx = to_vmx(vcpu);
10343 int cpu;
10344
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010345 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010346 return;
10347
10348 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010349 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010350 vmx_vcpu_put(vcpu);
10351 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010352 put_cpu();
10353}
10354
Jim Mattson2f1fe812016-07-08 15:36:06 -070010355/*
10356 * Ensure that the current vmcs of the logical processor is the
10357 * vmcs01 of the vcpu before calling free_nested().
10358 */
10359static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10360{
10361 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010362
Christoffer Dallec7660c2017-12-04 21:35:23 +010010363 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010364 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010365 free_nested(vmx);
10366 vcpu_put(vcpu);
10367}
10368
Avi Kivity6aa8b732006-12-10 02:21:36 -080010369static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10370{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010371 struct vcpu_vmx *vmx = to_vmx(vcpu);
10372
Kai Huang843e4332015-01-28 10:54:28 +080010373 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010374 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010375 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010376 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010377 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010378 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010379 kfree(vmx->guest_msrs);
10380 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010381 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010382}
10383
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010384static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010385{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010386 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010387 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010388 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010389 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010390
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010391 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010392 return ERR_PTR(-ENOMEM);
10393
Wanpeng Li991e7a02015-09-16 17:30:05 +080010394 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010395
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010396 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10397 if (err)
10398 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010399
Peter Feiner4e595162016-07-07 14:49:58 -070010400 err = -ENOMEM;
10401
10402 /*
10403 * If PML is turned on, failure on enabling PML just results in failure
10404 * of creating the vcpu, therefore we can simplify PML logic (by
10405 * avoiding dealing with cases, such as enabling PML partially on vcpus
10406 * for the guest, etc.
10407 */
10408 if (enable_pml) {
10409 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10410 if (!vmx->pml_pg)
10411 goto uninit_vcpu;
10412 }
10413
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010414 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010415 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10416 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010417
Peter Feiner4e595162016-07-07 14:49:58 -070010418 if (!vmx->guest_msrs)
10419 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010420
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010421 err = alloc_loaded_vmcs(&vmx->vmcs01);
10422 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010423 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010424
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010425 msr_bitmap = vmx->vmcs01.msr_bitmap;
10426 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10427 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10428 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10429 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10430 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10431 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10432 vmx->msr_bitmap_mode = 0;
10433
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010434 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010435 cpu = get_cpu();
10436 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010437 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010438 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010439 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010440 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010441 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010442 err = alloc_apic_access_page(kvm);
10443 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010444 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010445 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010446
Sean Christophersone90008d2018-03-05 12:04:37 -080010447 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010448 err = init_rmode_identity_map(kvm);
10449 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010450 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010451 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010452
Wanpeng Li5c614b32015-10-13 09:18:36 -070010453 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010454 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10455 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010456 vmx->nested.vpid02 = allocate_vpid();
10457 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010458
Wincy Van705699a2015-02-03 23:58:17 +080010459 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010460 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010461
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010462 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10463
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010464 /*
10465 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10466 * or POSTED_INTR_WAKEUP_VECTOR.
10467 */
10468 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10469 vmx->pi_desc.sn = 1;
10470
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010471 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010472
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010473free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010474 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010475 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010476free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010477 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010478free_pml:
10479 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010480uninit_vcpu:
10481 kvm_vcpu_uninit(&vmx->vcpu);
10482free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010483 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010484 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010485 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010486}
10487
Wanpeng Lib31c1142018-03-12 04:53:04 -070010488static int vmx_vm_init(struct kvm *kvm)
10489{
10490 if (!ple_gap)
10491 kvm->arch.pause_in_guest = true;
10492 return 0;
10493}
10494
Yang, Sheng002c7f72007-07-31 14:23:01 +030010495static void __init vmx_check_processor_compat(void *rtn)
10496{
10497 struct vmcs_config vmcs_conf;
10498
10499 *(int *)rtn = 0;
10500 if (setup_vmcs_config(&vmcs_conf) < 0)
10501 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010502 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010503 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10504 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10505 smp_processor_id());
10506 *(int *)rtn = -EIO;
10507 }
10508}
10509
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010510static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010511{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010512 u8 cache;
10513 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010514
Sheng Yang522c68c2009-04-27 20:35:43 +080010515 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010516 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010517 * 2. EPT with VT-d:
10518 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010519 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010520 * b. VT-d with snooping control feature: snooping control feature of
10521 * VT-d engine can guarantee the cache correctness. Just set it
10522 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010523 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010524 * consistent with host MTRR
10525 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010526 if (is_mmio) {
10527 cache = MTRR_TYPE_UNCACHABLE;
10528 goto exit;
10529 }
10530
10531 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010532 ipat = VMX_EPT_IPAT_BIT;
10533 cache = MTRR_TYPE_WRBACK;
10534 goto exit;
10535 }
10536
10537 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10538 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010539 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010540 cache = MTRR_TYPE_WRBACK;
10541 else
10542 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010543 goto exit;
10544 }
10545
Xiao Guangrongff536042015-06-15 16:55:22 +080010546 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010547
10548exit:
10549 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010550}
10551
Sheng Yang17cc3932010-01-05 19:02:27 +080010552static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010553{
Sheng Yang878403b2010-01-05 19:02:29 +080010554 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10555 return PT_DIRECTORY_LEVEL;
10556 else
10557 /* For shadow and EPT supported 1GB page */
10558 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010559}
10560
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010561static void vmcs_set_secondary_exec_control(u32 new_ctl)
10562{
10563 /*
10564 * These bits in the secondary execution controls field
10565 * are dynamic, the others are mostly based on the hypervisor
10566 * architecture and the guest's CPUID. Do not touch the
10567 * dynamic bits.
10568 */
10569 u32 mask =
10570 SECONDARY_EXEC_SHADOW_VMCS |
10571 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010572 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10573 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010574
10575 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10576
10577 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10578 (new_ctl & ~mask) | (cur_ctl & mask));
10579}
10580
David Matlack8322ebb2016-11-29 18:14:09 -080010581/*
10582 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10583 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10584 */
10585static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10586{
10587 struct vcpu_vmx *vmx = to_vmx(vcpu);
10588 struct kvm_cpuid_entry2 *entry;
10589
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010590 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10591 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010592
10593#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10594 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010595 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010596} while (0)
10597
10598 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10599 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10600 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10601 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10602 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10603 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10604 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10605 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10606 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10607 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10608 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10609 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10610 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10611 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10612 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10613
10614 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10615 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10616 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10617 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10618 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010619 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010620
10621#undef cr4_fixed1_update
10622}
10623
Sheng Yang0e851882009-12-18 16:48:46 +080010624static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10625{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010626 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010627
Paolo Bonzini80154d72017-08-24 13:55:35 +020010628 if (cpu_has_secondary_exec_ctrls()) {
10629 vmx_compute_secondary_exec_control(vmx);
10630 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010631 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010632
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010633 if (nested_vmx_allowed(vcpu))
10634 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10635 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10636 else
10637 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10638 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010639
10640 if (nested_vmx_allowed(vcpu))
10641 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010642}
10643
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010644static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10645{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010646 if (func == 1 && nested)
10647 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010648}
10649
Yang Zhang25d92082013-08-06 12:00:32 +030010650static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10651 struct x86_exception *fault)
10652{
Jan Kiszka533558b2014-01-04 18:47:20 +010010653 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010654 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010655 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010656 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010657
Bandan Dasc5f983f2017-05-05 15:25:14 -040010658 if (vmx->nested.pml_full) {
10659 exit_reason = EXIT_REASON_PML_FULL;
10660 vmx->nested.pml_full = false;
10661 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10662 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010663 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010664 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010665 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010666
10667 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010668 vmcs12->guest_physical_address = fault->address;
10669}
10670
Peter Feiner995f00a2017-06-30 17:26:32 -070010671static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10672{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010673 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010674}
10675
Nadav Har'El155a97a2013-08-05 11:07:16 +030010676/* Callbacks for nested_ept_init_mmu_context: */
10677
10678static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10679{
10680 /* return the page table to be shadowed - in our case, EPT12 */
10681 return get_vmcs12(vcpu)->ept_pointer;
10682}
10683
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010684static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010685{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010686 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010687 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010688 return 1;
10689
10690 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010691 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010692 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010693 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010694 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010695 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10696 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10697 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10698
10699 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010700 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010701}
10702
10703static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10704{
10705 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10706}
10707
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010708static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10709 u16 error_code)
10710{
10711 bool inequality, bit;
10712
10713 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10714 inequality =
10715 (error_code & vmcs12->page_fault_error_code_mask) !=
10716 vmcs12->page_fault_error_code_match;
10717 return inequality ^ bit;
10718}
10719
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010720static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10721 struct x86_exception *fault)
10722{
10723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10724
10725 WARN_ON(!is_guest_mode(vcpu));
10726
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010727 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10728 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010729 vmcs12->vm_exit_intr_error_code = fault->error_code;
10730 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10731 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10732 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10733 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010734 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010735 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010736 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010737}
10738
Paolo Bonzinic9923842017-12-13 14:16:30 +010010739static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10740 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010741
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010742static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010743{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010744 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010745 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010746 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010747 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010748
10749 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010750 /*
10751 * Translate L1 physical address to host physical
10752 * address for vmcs02. Keep the page pinned, so this
10753 * physical address remains valid. We keep a reference
10754 * to it so we can release it later.
10755 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010756 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010757 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010758 vmx->nested.apic_access_page = NULL;
10759 }
10760 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010761 /*
10762 * If translation failed, no matter: This feature asks
10763 * to exit when accessing the given address, and if it
10764 * can never be accessed, this feature won't do
10765 * anything anyway.
10766 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010767 if (!is_error_page(page)) {
10768 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010769 hpa = page_to_phys(vmx->nested.apic_access_page);
10770 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10771 } else {
10772 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10773 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10774 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010775 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010776
10777 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010778 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010779 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010780 vmx->nested.virtual_apic_page = NULL;
10781 }
10782 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010783
10784 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010785 * If translation failed, VM entry will fail because
10786 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10787 * Failing the vm entry is _not_ what the processor
10788 * does but it's basically the only possibility we
10789 * have. We could still enter the guest if CR8 load
10790 * exits are enabled, CR8 store exits are enabled, and
10791 * virtualize APIC access is disabled; in this case
10792 * the processor would never use the TPR shadow and we
10793 * could simply clear the bit from the execution
10794 * control. But such a configuration is useless, so
10795 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010796 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010797 if (!is_error_page(page)) {
10798 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010799 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10800 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10801 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010802 }
10803
Wincy Van705699a2015-02-03 23:58:17 +080010804 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010805 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10806 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010807 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010808 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010809 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010810 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10811 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010812 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010813 vmx->nested.pi_desc_page = page;
10814 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010815 vmx->nested.pi_desc =
10816 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10817 (unsigned long)(vmcs12->posted_intr_desc_addr &
10818 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010819 vmcs_write64(POSTED_INTR_DESC_ADDR,
10820 page_to_phys(vmx->nested.pi_desc_page) +
10821 (unsigned long)(vmcs12->posted_intr_desc_addr &
10822 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010823 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010824 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010825 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10826 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010827 else
10828 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10829 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010830}
10831
Jan Kiszkaf4124502014-03-07 20:03:13 +010010832static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10833{
10834 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10835 struct vcpu_vmx *vmx = to_vmx(vcpu);
10836
10837 if (vcpu->arch.virtual_tsc_khz == 0)
10838 return;
10839
10840 /* Make sure short timeouts reliably trigger an immediate vmexit.
10841 * hrtimer_start does not guarantee this. */
10842 if (preemption_timeout <= 1) {
10843 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10844 return;
10845 }
10846
10847 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10848 preemption_timeout *= 1000000;
10849 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10850 hrtimer_start(&vmx->nested.preemption_timer,
10851 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10852}
10853
Jim Mattson56a20512017-07-06 16:33:06 -070010854static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10855 struct vmcs12 *vmcs12)
10856{
10857 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10858 return 0;
10859
10860 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10861 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10862 return -EINVAL;
10863
10864 return 0;
10865}
10866
Wincy Van3af18d92015-02-03 23:49:31 +080010867static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10868 struct vmcs12 *vmcs12)
10869{
Wincy Van3af18d92015-02-03 23:49:31 +080010870 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10871 return 0;
10872
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010873 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010874 return -EINVAL;
10875
10876 return 0;
10877}
10878
Jim Mattson712b12d2017-08-24 13:24:47 -070010879static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10880 struct vmcs12 *vmcs12)
10881{
10882 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10883 return 0;
10884
10885 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10886 return -EINVAL;
10887
10888 return 0;
10889}
10890
Wincy Van3af18d92015-02-03 23:49:31 +080010891/*
10892 * Merge L0's and L1's MSR bitmap, return false to indicate that
10893 * we do not use the hardware.
10894 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010895static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10896 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010897{
Wincy Van82f0dd42015-02-03 23:57:18 +080010898 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010899 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010900 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010901 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010902 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010903 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010904 *
10905 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10906 * ensures that we do not accidentally generate an L02 MSR bitmap
10907 * from the L12 MSR bitmap that is too permissive.
10908 * 2. That L1 or L2s have actually used the MSR. This avoids
10909 * unnecessarily merging of the bitmap if the MSR is unused. This
10910 * works properly because we only update the L01 MSR bitmap lazily.
10911 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10912 * updated to reflect this when L1 (or its L2s) actually write to
10913 * the MSR.
10914 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010915 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10916 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010917
Paolo Bonzinic9923842017-12-13 14:16:30 +010010918 /* Nothing to do if the MSR bitmap is not in use. */
10919 if (!cpu_has_vmx_msr_bitmap() ||
10920 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10921 return false;
10922
Ashok Raj15d45072018-02-01 22:59:43 +010010923 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010924 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010925 return false;
10926
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010927 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10928 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010929 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010930
Radim Krčmářd048c092016-08-08 20:16:22 +020010931 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010932 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10933 /*
10934 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10935 * just lets the processor take the value from the virtual-APIC page;
10936 * take those 256 bits directly from the L1 bitmap.
10937 */
10938 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10939 unsigned word = msr / BITS_PER_LONG;
10940 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10941 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010942 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010943 } else {
10944 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10945 unsigned word = msr / BITS_PER_LONG;
10946 msr_bitmap_l0[word] = ~0;
10947 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10948 }
10949 }
10950
10951 nested_vmx_disable_intercept_for_msr(
10952 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010953 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010954 MSR_TYPE_W);
10955
10956 if (nested_cpu_has_vid(vmcs12)) {
10957 nested_vmx_disable_intercept_for_msr(
10958 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010959 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010960 MSR_TYPE_W);
10961 nested_vmx_disable_intercept_for_msr(
10962 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010963 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010964 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010965 }
Ashok Raj15d45072018-02-01 22:59:43 +010010966
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010967 if (spec_ctrl)
10968 nested_vmx_disable_intercept_for_msr(
10969 msr_bitmap_l1, msr_bitmap_l0,
10970 MSR_IA32_SPEC_CTRL,
10971 MSR_TYPE_R | MSR_TYPE_W);
10972
Ashok Raj15d45072018-02-01 22:59:43 +010010973 if (pred_cmd)
10974 nested_vmx_disable_intercept_for_msr(
10975 msr_bitmap_l1, msr_bitmap_l0,
10976 MSR_IA32_PRED_CMD,
10977 MSR_TYPE_W);
10978
Wincy Vanf2b93282015-02-03 23:56:03 +080010979 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010980 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010981
10982 return true;
10983}
10984
Liran Alon61ada742018-06-23 02:35:08 +030010985static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
10986 struct vmcs12 *vmcs12)
10987{
10988 struct vmcs12 *shadow;
10989 struct page *page;
10990
10991 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10992 vmcs12->vmcs_link_pointer == -1ull)
10993 return;
10994
10995 shadow = get_shadow_vmcs12(vcpu);
10996 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
10997
10998 memcpy(shadow, kmap(page), VMCS12_SIZE);
10999
11000 kunmap(page);
11001 kvm_release_page_clean(page);
11002}
11003
11004static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11005 struct vmcs12 *vmcs12)
11006{
11007 struct vcpu_vmx *vmx = to_vmx(vcpu);
11008
11009 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11010 vmcs12->vmcs_link_pointer == -1ull)
11011 return;
11012
11013 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11014 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11015}
11016
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011017static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11018 struct vmcs12 *vmcs12)
11019{
11020 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11021 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11022 return -EINVAL;
11023 else
11024 return 0;
11025}
11026
Wincy Vanf2b93282015-02-03 23:56:03 +080011027static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11028 struct vmcs12 *vmcs12)
11029{
Wincy Van82f0dd42015-02-03 23:57:18 +080011030 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011031 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011032 !nested_cpu_has_vid(vmcs12) &&
11033 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011034 return 0;
11035
11036 /*
11037 * If virtualize x2apic mode is enabled,
11038 * virtualize apic access must be disabled.
11039 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011040 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11041 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011042 return -EINVAL;
11043
Wincy Van608406e2015-02-03 23:57:51 +080011044 /*
11045 * If virtual interrupt delivery is enabled,
11046 * we must exit on external interrupts.
11047 */
11048 if (nested_cpu_has_vid(vmcs12) &&
11049 !nested_exit_on_intr(vcpu))
11050 return -EINVAL;
11051
Wincy Van705699a2015-02-03 23:58:17 +080011052 /*
11053 * bits 15:8 should be zero in posted_intr_nv,
11054 * the descriptor address has been already checked
11055 * in nested_get_vmcs12_pages.
11056 */
11057 if (nested_cpu_has_posted_intr(vmcs12) &&
11058 (!nested_cpu_has_vid(vmcs12) ||
11059 !nested_exit_intr_ack_set(vcpu) ||
11060 vmcs12->posted_intr_nv & 0xff00))
11061 return -EINVAL;
11062
Wincy Vanf2b93282015-02-03 23:56:03 +080011063 /* tpr shadow is needed by all apicv features. */
11064 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11065 return -EINVAL;
11066
11067 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011068}
11069
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011070static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11071 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011072 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011073{
Liran Alone2536742018-06-23 02:35:02 +030011074 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011075 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011076 u64 count, addr;
11077
Liran Alone2536742018-06-23 02:35:02 +030011078 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11079 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011080 WARN_ON(1);
11081 return -EINVAL;
11082 }
11083 if (count == 0)
11084 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011085 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011086 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11087 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011088 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011089 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11090 addr_field, maxphyaddr, count, addr);
11091 return -EINVAL;
11092 }
11093 return 0;
11094}
11095
11096static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11097 struct vmcs12 *vmcs12)
11098{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011099 if (vmcs12->vm_exit_msr_load_count == 0 &&
11100 vmcs12->vm_exit_msr_store_count == 0 &&
11101 vmcs12->vm_entry_msr_load_count == 0)
11102 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011103 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011104 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011105 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011106 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011107 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011108 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011109 return -EINVAL;
11110 return 0;
11111}
11112
Bandan Dasc5f983f2017-05-05 15:25:14 -040011113static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11114 struct vmcs12 *vmcs12)
11115{
11116 u64 address = vmcs12->pml_address;
11117 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11118
11119 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11120 if (!nested_cpu_has_ept(vmcs12) ||
11121 !IS_ALIGNED(address, 4096) ||
11122 address >> maxphyaddr)
11123 return -EINVAL;
11124 }
11125
11126 return 0;
11127}
11128
Liran Alona8a7c022018-06-23 02:35:06 +030011129static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11130 struct vmcs12 *vmcs12)
11131{
11132 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11133 return 0;
11134
11135 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11136 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11137 return -EINVAL;
11138
11139 return 0;
11140}
11141
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011142static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11143 struct vmx_msr_entry *e)
11144{
11145 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011146 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011147 return -EINVAL;
11148 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11149 e->index == MSR_IA32_UCODE_REV)
11150 return -EINVAL;
11151 if (e->reserved != 0)
11152 return -EINVAL;
11153 return 0;
11154}
11155
11156static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11157 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011158{
11159 if (e->index == MSR_FS_BASE ||
11160 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011161 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11162 nested_vmx_msr_check_common(vcpu, e))
11163 return -EINVAL;
11164 return 0;
11165}
11166
11167static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11168 struct vmx_msr_entry *e)
11169{
11170 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11171 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011172 return -EINVAL;
11173 return 0;
11174}
11175
11176/*
11177 * Load guest's/host's msr at nested entry/exit.
11178 * return 0 for success, entry index for failure.
11179 */
11180static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11181{
11182 u32 i;
11183 struct vmx_msr_entry e;
11184 struct msr_data msr;
11185
11186 msr.host_initiated = false;
11187 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011188 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11189 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011190 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011191 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11192 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011193 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011194 }
11195 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011196 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011197 "%s check failed (%u, 0x%x, 0x%x)\n",
11198 __func__, i, e.index, e.reserved);
11199 goto fail;
11200 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011201 msr.index = e.index;
11202 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011203 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011204 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011205 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11206 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011207 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011208 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011209 }
11210 return 0;
11211fail:
11212 return i + 1;
11213}
11214
11215static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11216{
11217 u32 i;
11218 struct vmx_msr_entry e;
11219
11220 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011221 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011222 if (kvm_vcpu_read_guest(vcpu,
11223 gpa + i * sizeof(e),
11224 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011225 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011226 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11227 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011228 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011229 }
11230 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011231 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011232 "%s check failed (%u, 0x%x, 0x%x)\n",
11233 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011234 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011235 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011236 msr_info.host_initiated = false;
11237 msr_info.index = e.index;
11238 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011239 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011240 "%s cannot read MSR (%u, 0x%x)\n",
11241 __func__, i, e.index);
11242 return -EINVAL;
11243 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011244 if (kvm_vcpu_write_guest(vcpu,
11245 gpa + i * sizeof(e) +
11246 offsetof(struct vmx_msr_entry, value),
11247 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011248 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011249 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011250 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011251 return -EINVAL;
11252 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011253 }
11254 return 0;
11255}
11256
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011257static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11258{
11259 unsigned long invalid_mask;
11260
11261 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11262 return (val & invalid_mask) == 0;
11263}
11264
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011265/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011266 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11267 * emulating VM entry into a guest with EPT enabled.
11268 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11269 * is assigned to entry_failure_code on failure.
11270 */
11271static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011272 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011273{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011274 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011275 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011276 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11277 return 1;
11278 }
11279
11280 /*
11281 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11282 * must not be dereferenced.
11283 */
11284 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11285 !nested_ept) {
11286 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11287 *entry_failure_code = ENTRY_FAIL_PDPTE;
11288 return 1;
11289 }
11290 }
11291
11292 vcpu->arch.cr3 = cr3;
11293 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11294 }
11295
11296 kvm_mmu_reset_context(vcpu);
11297 return 0;
11298}
11299
Jim Mattson6514dc32018-04-26 16:09:12 -070011300static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011301{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011302 struct vcpu_vmx *vmx = to_vmx(vcpu);
11303
11304 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11305 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11306 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11307 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11308 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11309 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11310 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11311 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11312 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11313 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11314 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11315 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11316 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11317 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11318 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11319 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11320 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11321 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11322 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11323 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11324 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11325 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11326 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11327 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11328 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11329 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11330 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11331 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11332 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11333 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11334 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011335
11336 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11337 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11338 vmcs12->guest_pending_dbg_exceptions);
11339 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11340 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11341
11342 if (nested_cpu_has_xsaves(vmcs12))
11343 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11344 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11345
11346 if (cpu_has_vmx_posted_intr())
11347 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11348
11349 /*
11350 * Whether page-faults are trapped is determined by a combination of
11351 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11352 * If enable_ept, L0 doesn't care about page faults and we should
11353 * set all of these to L1's desires. However, if !enable_ept, L0 does
11354 * care about (at least some) page faults, and because it is not easy
11355 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11356 * to exit on each and every L2 page fault. This is done by setting
11357 * MASK=MATCH=0 and (see below) EB.PF=1.
11358 * Note that below we don't need special code to set EB.PF beyond the
11359 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11360 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11361 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11362 */
11363 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11364 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11365 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11366 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11367
11368 /* All VMFUNCs are currently emulated through L0 vmexits. */
11369 if (cpu_has_vmx_vmfunc())
11370 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11371
11372 if (cpu_has_vmx_apicv()) {
11373 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11374 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11375 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11376 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11377 }
11378
11379 /*
11380 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11381 * Some constant fields are set here by vmx_set_constant_host_state().
11382 * Other fields are different per CPU, and will be set later when
11383 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11384 */
11385 vmx_set_constant_host_state(vmx);
11386
11387 /*
11388 * Set the MSR load/store lists to match L0's settings.
11389 */
11390 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11391 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11392 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11393 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11394 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11395
11396 set_cr4_guest_host_mask(vmx);
11397
11398 if (vmx_mpx_supported())
11399 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11400
11401 if (enable_vpid) {
11402 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11403 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11404 else
11405 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11406 }
11407
11408 /*
11409 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11410 */
11411 if (enable_ept) {
11412 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11413 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11414 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11415 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11416 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011417
11418 if (cpu_has_vmx_msr_bitmap())
11419 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011420}
11421
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011422/*
11423 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11424 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011425 * 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 +030011426 * guest in a way that will both be appropriate to L1's requests, and our
11427 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11428 * function also has additional necessary side-effects, like setting various
11429 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011430 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11431 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011432 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011433static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011434 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011435{
11436 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011437 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011438
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011439 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011440 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011441 vmx->nested.dirty_vmcs12 = false;
11442 }
11443
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011444 /*
11445 * First, the fields that are shadowed. This must be kept in sync
11446 * with vmx_shadow_fields.h.
11447 */
11448
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011449 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011450 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011451 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011452 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11453 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011454
11455 /*
11456 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11457 * HOST_FS_BASE, HOST_GS_BASE.
11458 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011459
Jim Mattson6514dc32018-04-26 16:09:12 -070011460 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011461 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011462 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11463 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11464 } else {
11465 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11466 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11467 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011468 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011469 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11470 vmcs12->vm_entry_intr_info_field);
11471 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11472 vmcs12->vm_entry_exception_error_code);
11473 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11474 vmcs12->vm_entry_instruction_len);
11475 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11476 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011477 vmx->loaded_vmcs->nmi_known_unmasked =
11478 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011479 } else {
11480 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11481 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011482 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011483
Jan Kiszkaf4124502014-03-07 20:03:13 +010011484 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011485
Paolo Bonzini93140062016-07-06 13:23:51 +020011486 /* Preemption timer setting is only taken from vmcs01. */
11487 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11488 exec_control |= vmcs_config.pin_based_exec_ctrl;
11489 if (vmx->hv_deadline_tsc == -1)
11490 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11491
11492 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011493 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011494 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11495 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011496 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011497 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011498 }
Wincy Van705699a2015-02-03 23:58:17 +080011499
Jan Kiszkaf4124502014-03-07 20:03:13 +010011500 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011501
Jan Kiszkaf4124502014-03-07 20:03:13 +010011502 vmx->nested.preemption_timer_expired = false;
11503 if (nested_cpu_has_preemption_timer(vmcs12))
11504 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011505
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011506 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011507 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011508
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011509 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011510 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011511 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011512 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011513 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011514 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011515 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11516 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011517 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011518 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11519 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11520 ~SECONDARY_EXEC_ENABLE_PML;
11521 exec_control |= vmcs12_exec_ctrl;
11522 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011523
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011524 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011525 vmcs_write16(GUEST_INTR_STATUS,
11526 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011527
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011528 /*
11529 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11530 * nested_get_vmcs12_pages will either fix it up or
11531 * remove the VM execution control.
11532 */
11533 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11534 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11535
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011536 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11537 }
11538
Jim Mattson83bafef2016-10-04 10:48:38 -070011539 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011540 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11541 * entry, but only if the current (host) sp changed from the value
11542 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11543 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11544 * here we just force the write to happen on entry.
11545 */
11546 vmx->host_rsp = 0;
11547
11548 exec_control = vmx_exec_control(vmx); /* L0's desires */
11549 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11550 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11551 exec_control &= ~CPU_BASED_TPR_SHADOW;
11552 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011553
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011554 /*
11555 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11556 * nested_get_vmcs12_pages can't fix it up, the illegal value
11557 * will result in a VM entry failure.
11558 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011559 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011560 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011561 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011562 } else {
11563#ifdef CONFIG_X86_64
11564 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11565 CPU_BASED_CR8_STORE_EXITING;
11566#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011567 }
11568
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011569 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011570 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11571 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011572 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011573 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11574 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11575
11576 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11577
11578 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11579 * bitwise-or of what L1 wants to trap for L2, and what we want to
11580 * trap. Note that CR0.TS also needs updating - we do this later.
11581 */
11582 update_exception_bitmap(vcpu);
11583 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11584 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11585
Nadav Har'El8049d652013-08-05 11:07:06 +030011586 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11587 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11588 * bits are further modified by vmx_set_efer() below.
11589 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011590 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011591
11592 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11593 * emulated by vmx_set_efer(), below.
11594 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011595 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011596 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11597 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011598 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11599
Jim Mattson6514dc32018-04-26 16:09:12 -070011600 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011601 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011602 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011603 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011604 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011605 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011606 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011607
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011608 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11609
Peter Feinerc95ba922016-08-17 09:36:47 -070011610 if (kvm_has_tsc_control)
11611 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011612
11613 if (enable_vpid) {
11614 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011615 * There is no direct mapping between vpid02 and vpid12, the
11616 * vpid02 is per-vCPU for L0 and reused while the value of
11617 * vpid12 is changed w/ one invvpid during nested vmentry.
11618 * The vpid12 is allocated by L1 for L2, so it will not
11619 * influence global bitmap(for vpid01 and vpid02 allocation)
11620 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011621 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011622 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011623 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11624 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011625 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011626 }
11627 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011628 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011629 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011630 }
11631
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011632 if (enable_pml) {
11633 /*
11634 * Conceptually we want to copy the PML address and index from
11635 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11636 * since we always flush the log on each vmexit, this happens
11637 * to be equivalent to simply resetting the fields in vmcs02.
11638 */
11639 ASSERT(vmx->pml_pg);
11640 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11641 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11642 }
11643
Nadav Har'El155a97a2013-08-05 11:07:16 +030011644 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011645 if (nested_ept_init_mmu_context(vcpu)) {
11646 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11647 return 1;
11648 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011649 } else if (nested_cpu_has2(vmcs12,
11650 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011651 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011652 }
11653
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011654 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011655 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11656 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011657 * The CR0_READ_SHADOW is what L2 should have expected to read given
11658 * the specifications by L1; It's not enough to take
11659 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11660 * have more bits than L1 expected.
11661 */
11662 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11663 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11664
11665 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11666 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11667
Jim Mattson6514dc32018-04-26 16:09:12 -070011668 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011669 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011670 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11671 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11672 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11673 else
11674 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11675 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11676 vmx_set_efer(vcpu, vcpu->arch.efer);
11677
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011678 /*
11679 * Guest state is invalid and unrestricted guest is disabled,
11680 * which means L1 attempted VMEntry to L2 with invalid state.
11681 * Fail the VMEntry.
11682 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011683 if (vmx->emulation_required) {
11684 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011685 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011686 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011687
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011688 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011689 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011690 entry_failure_code))
11691 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011692
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011693 if (!enable_ept)
11694 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11695
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011696 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11697 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011698 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011699}
11700
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011701static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11702{
11703 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11704 nested_cpu_has_virtual_nmis(vmcs12))
11705 return -EINVAL;
11706
11707 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11708 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11709 return -EINVAL;
11710
11711 return 0;
11712}
11713
Jim Mattsonca0bde22016-11-30 12:03:46 -080011714static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11715{
11716 struct vcpu_vmx *vmx = to_vmx(vcpu);
11717
11718 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11719 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11720 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11721
Jim Mattson56a20512017-07-06 16:33:06 -070011722 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11723 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11724
Jim Mattsonca0bde22016-11-30 12:03:46 -080011725 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11726 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11727
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011728 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11729 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11730
Jim Mattson712b12d2017-08-24 13:24:47 -070011731 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11732 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11733
Jim Mattsonca0bde22016-11-30 12:03:46 -080011734 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11735 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11736
11737 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11738 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11739
Bandan Dasc5f983f2017-05-05 15:25:14 -040011740 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11741 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11742
Liran Alona8a7c022018-06-23 02:35:06 +030011743 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11744 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11745
Jim Mattsonca0bde22016-11-30 12:03:46 -080011746 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011747 vmx->nested.msrs.procbased_ctls_low,
11748 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011749 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11750 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011751 vmx->nested.msrs.secondary_ctls_low,
11752 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011753 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011754 vmx->nested.msrs.pinbased_ctls_low,
11755 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011756 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011757 vmx->nested.msrs.exit_ctls_low,
11758 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011759 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011760 vmx->nested.msrs.entry_ctls_low,
11761 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011762 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11763
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011764 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011765 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11766
Bandan Das41ab9372017-08-03 15:54:43 -040011767 if (nested_cpu_has_vmfunc(vmcs12)) {
11768 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011769 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011770 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11771
11772 if (nested_cpu_has_eptp_switching(vmcs12)) {
11773 if (!nested_cpu_has_ept(vmcs12) ||
11774 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11775 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11776 }
11777 }
Bandan Das27c42a12017-08-03 15:54:42 -040011778
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011779 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11780 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11781
Jim Mattsonca0bde22016-11-30 12:03:46 -080011782 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11783 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11784 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11785 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11786
Marc Orr04473782018-06-20 17:21:29 -070011787 /*
11788 * From the Intel SDM, volume 3:
11789 * Fields relevant to VM-entry event injection must be set properly.
11790 * These fields are the VM-entry interruption-information field, the
11791 * VM-entry exception error code, and the VM-entry instruction length.
11792 */
11793 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11794 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11795 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11796 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11797 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11798 bool should_have_error_code;
11799 bool urg = nested_cpu_has2(vmcs12,
11800 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11801 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11802
11803 /* VM-entry interruption-info field: interruption type */
11804 if (intr_type == INTR_TYPE_RESERVED ||
11805 (intr_type == INTR_TYPE_OTHER_EVENT &&
11806 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11807 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11808
11809 /* VM-entry interruption-info field: vector */
11810 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11811 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11812 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11813 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11814
11815 /* VM-entry interruption-info field: deliver error code */
11816 should_have_error_code =
11817 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11818 x86_exception_has_error_code(vector);
11819 if (has_error_code != should_have_error_code)
11820 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11821
11822 /* VM-entry exception error code */
11823 if (has_error_code &&
11824 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11825 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11826
11827 /* VM-entry interruption-info field: reserved bits */
11828 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11829 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11830
11831 /* VM-entry instruction length */
11832 switch (intr_type) {
11833 case INTR_TYPE_SOFT_EXCEPTION:
11834 case INTR_TYPE_SOFT_INTR:
11835 case INTR_TYPE_PRIV_SW_EXCEPTION:
11836 if ((vmcs12->vm_entry_instruction_len > 15) ||
11837 (vmcs12->vm_entry_instruction_len == 0 &&
11838 !nested_cpu_has_zero_length_injection(vcpu)))
11839 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11840 }
11841 }
11842
Jim Mattsonca0bde22016-11-30 12:03:46 -080011843 return 0;
11844}
11845
Liran Alonf145d902018-06-23 02:35:07 +030011846static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11847 struct vmcs12 *vmcs12)
11848{
11849 int r;
11850 struct page *page;
11851 struct vmcs12 *shadow;
11852
11853 if (vmcs12->vmcs_link_pointer == -1ull)
11854 return 0;
11855
11856 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11857 return -EINVAL;
11858
11859 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11860 if (is_error_page(page))
11861 return -EINVAL;
11862
11863 r = 0;
11864 shadow = kmap(page);
11865 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11866 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11867 r = -EINVAL;
11868 kunmap(page);
11869 kvm_release_page_clean(page);
11870 return r;
11871}
11872
Jim Mattsonca0bde22016-11-30 12:03:46 -080011873static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11874 u32 *exit_qual)
11875{
11876 bool ia32e;
11877
11878 *exit_qual = ENTRY_FAIL_DEFAULT;
11879
11880 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11881 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11882 return 1;
11883
Liran Alonf145d902018-06-23 02:35:07 +030011884 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080011885 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11886 return 1;
11887 }
11888
11889 /*
11890 * If the load IA32_EFER VM-entry control is 1, the following checks
11891 * are performed on the field for the IA32_EFER MSR:
11892 * - Bits reserved in the IA32_EFER MSR must be 0.
11893 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11894 * the IA-32e mode guest VM-exit control. It must also be identical
11895 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11896 * CR0.PG) is 1.
11897 */
11898 if (to_vmx(vcpu)->nested.nested_run_pending &&
11899 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11900 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11901 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11902 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11903 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11904 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11905 return 1;
11906 }
11907
11908 /*
11909 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11910 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11911 * the values of the LMA and LME bits in the field must each be that of
11912 * the host address-space size VM-exit control.
11913 */
11914 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11915 ia32e = (vmcs12->vm_exit_controls &
11916 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11917 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11918 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11919 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11920 return 1;
11921 }
11922
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011923 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11924 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11925 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11926 return 1;
11927
Jim Mattsonca0bde22016-11-30 12:03:46 -080011928 return 0;
11929}
11930
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011931/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011932 * If exit_qual is NULL, this is being called from state restore (either RSM
11933 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011934 */
11935static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080011936{
11937 struct vcpu_vmx *vmx = to_vmx(vcpu);
11938 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011939 bool from_vmentry = !!exit_qual;
11940 u32 dummy_exit_qual;
11941 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011942
Jim Mattson858e25c2016-11-30 12:03:47 -080011943 enter_guest_mode(vcpu);
11944
11945 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11946 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11947
Jim Mattsonde3a0022017-11-27 17:22:25 -060011948 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011949 vmx_segment_cache_clear(vmx);
11950
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011951 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11952 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11953
11954 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011955 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011956 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011957
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011958 if (from_vmentry) {
11959 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011960
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011961 r = EXIT_REASON_MSR_LOAD_FAIL;
11962 *exit_qual = nested_vmx_load_msr(vcpu,
11963 vmcs12->vm_entry_msr_load_addr,
11964 vmcs12->vm_entry_msr_load_count);
11965 if (*exit_qual)
11966 goto fail;
11967 } else {
11968 /*
11969 * The MMU is not initialized to point at the right entities yet and
11970 * "get pages" would need to read data from the guest (i.e. we will
11971 * need to perform gpa to hpa translation). Request a call
11972 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11973 * have already been set at vmentry time and should not be reset.
11974 */
11975 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11976 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011977
Jim Mattson858e25c2016-11-30 12:03:47 -080011978 /*
11979 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11980 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11981 * returned as far as L1 is concerned. It will only return (and set
11982 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11983 */
11984 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011985
11986fail:
11987 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11988 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11989 leave_guest_mode(vcpu);
11990 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011991 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011992}
11993
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011994/*
11995 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11996 * for running an L2 nested guest.
11997 */
11998static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11999{
12000 struct vmcs12 *vmcs12;
12001 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012002 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012003 u32 exit_qual;
12004 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012005
Kyle Hueyeb277562016-11-29 12:40:39 -080012006 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012007 return 1;
12008
Kyle Hueyeb277562016-11-29 12:40:39 -080012009 if (!nested_vmx_check_vmcs12(vcpu))
12010 goto out;
12011
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012012 vmcs12 = get_vmcs12(vcpu);
12013
Liran Alona6192d42018-06-23 02:35:04 +030012014 /*
12015 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12016 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12017 * rather than RFLAGS.ZF, and no error number is stored to the
12018 * VM-instruction error field.
12019 */
12020 if (vmcs12->hdr.shadow_vmcs) {
12021 nested_vmx_failInvalid(vcpu);
12022 goto out;
12023 }
12024
Abel Gordon012f83c2013-04-18 14:39:25 +030012025 if (enable_shadow_vmcs)
12026 copy_shadow_to_vmcs12(vmx);
12027
Nadav Har'El7c177932011-05-25 23:12:04 +030012028 /*
12029 * The nested entry process starts with enforcing various prerequisites
12030 * on vmcs12 as required by the Intel SDM, and act appropriately when
12031 * they fail: As the SDM explains, some conditions should cause the
12032 * instruction to fail, while others will cause the instruction to seem
12033 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12034 * To speed up the normal (success) code path, we should avoid checking
12035 * for misconfigurations which will anyway be caught by the processor
12036 * when using the merged vmcs02.
12037 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012038 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12039 nested_vmx_failValid(vcpu,
12040 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12041 goto out;
12042 }
12043
Nadav Har'El7c177932011-05-25 23:12:04 +030012044 if (vmcs12->launch_state == launch) {
12045 nested_vmx_failValid(vcpu,
12046 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12047 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012048 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012049 }
12050
Jim Mattsonca0bde22016-11-30 12:03:46 -080012051 ret = check_vmentry_prereqs(vcpu, vmcs12);
12052 if (ret) {
12053 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012054 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012055 }
12056
Nadav Har'El7c177932011-05-25 23:12:04 +030012057 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012058 * After this point, the trap flag no longer triggers a singlestep trap
12059 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12060 * This is not 100% correct; for performance reasons, we delegate most
12061 * of the checks on host state to the processor. If those fail,
12062 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012063 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012064 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012065
Jim Mattsonca0bde22016-11-30 12:03:46 -080012066 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12067 if (ret) {
12068 nested_vmx_entry_failure(vcpu, vmcs12,
12069 EXIT_REASON_INVALID_STATE, exit_qual);
12070 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012071 }
12072
12073 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012074 * We're finally done with prerequisite checking, and can start with
12075 * the nested entry.
12076 */
12077
Jim Mattson6514dc32018-04-26 16:09:12 -070012078 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012079 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012080 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012081 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012082 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012083 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012084 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012085
Chao Gao135a06c2018-02-11 10:06:30 +080012086 /*
Liran Alon61ada742018-06-23 02:35:08 +030012087 * Must happen outside of enter_vmx_non_root_mode() as it will
12088 * also be used as part of restoring nVMX state for
12089 * snapshot restore (migration).
12090 *
12091 * In this flow, it is assumed that vmcs12 cache was
12092 * trasferred as part of captured nVMX state and should
12093 * therefore not be read from guest memory (which may not
12094 * exist on destination host yet).
12095 */
12096 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12097
12098 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012099 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12100 * by event injection, halt vcpu.
12101 */
12102 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012103 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12104 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012105 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012106 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012107 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012108
12109out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012110 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012111}
12112
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012113/*
12114 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12115 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12116 * This function returns the new value we should put in vmcs12.guest_cr0.
12117 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12118 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12119 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12120 * didn't trap the bit, because if L1 did, so would L0).
12121 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12122 * been modified by L2, and L1 knows it. So just leave the old value of
12123 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12124 * isn't relevant, because if L0 traps this bit it can set it to anything.
12125 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12126 * changed these bits, and therefore they need to be updated, but L0
12127 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12128 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12129 */
12130static inline unsigned long
12131vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12132{
12133 return
12134 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12135 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12136 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12137 vcpu->arch.cr0_guest_owned_bits));
12138}
12139
12140static inline unsigned long
12141vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12142{
12143 return
12144 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12145 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12146 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12147 vcpu->arch.cr4_guest_owned_bits));
12148}
12149
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012150static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12151 struct vmcs12 *vmcs12)
12152{
12153 u32 idt_vectoring;
12154 unsigned int nr;
12155
Wanpeng Li664f8e22017-08-24 03:35:09 -070012156 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012157 nr = vcpu->arch.exception.nr;
12158 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12159
12160 if (kvm_exception_is_soft(nr)) {
12161 vmcs12->vm_exit_instruction_len =
12162 vcpu->arch.event_exit_inst_len;
12163 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12164 } else
12165 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12166
12167 if (vcpu->arch.exception.has_error_code) {
12168 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12169 vmcs12->idt_vectoring_error_code =
12170 vcpu->arch.exception.error_code;
12171 }
12172
12173 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012174 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012175 vmcs12->idt_vectoring_info_field =
12176 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012177 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012178 nr = vcpu->arch.interrupt.nr;
12179 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12180
12181 if (vcpu->arch.interrupt.soft) {
12182 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12183 vmcs12->vm_entry_instruction_len =
12184 vcpu->arch.event_exit_inst_len;
12185 } else
12186 idt_vectoring |= INTR_TYPE_EXT_INTR;
12187
12188 vmcs12->idt_vectoring_info_field = idt_vectoring;
12189 }
12190}
12191
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012192static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12193{
12194 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012195 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012196 bool block_nested_events =
12197 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012198
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012199 if (vcpu->arch.exception.pending &&
12200 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012201 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012202 return -EBUSY;
12203 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012204 return 0;
12205 }
12206
Jan Kiszkaf4124502014-03-07 20:03:13 +010012207 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12208 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012209 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012210 return -EBUSY;
12211 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12212 return 0;
12213 }
12214
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012215 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012216 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012217 return -EBUSY;
12218 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12219 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12220 INTR_INFO_VALID_MASK, 0);
12221 /*
12222 * The NMI-triggered VM exit counts as injection:
12223 * clear this one and block further NMIs.
12224 */
12225 vcpu->arch.nmi_pending = 0;
12226 vmx_set_nmi_mask(vcpu, true);
12227 return 0;
12228 }
12229
12230 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12231 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012232 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012233 return -EBUSY;
12234 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012235 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012236 }
12237
David Hildenbrand6342c502017-01-25 11:58:58 +010012238 vmx_complete_nested_posted_interrupt(vcpu);
12239 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012240}
12241
Jan Kiszkaf4124502014-03-07 20:03:13 +010012242static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12243{
12244 ktime_t remaining =
12245 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12246 u64 value;
12247
12248 if (ktime_to_ns(remaining) <= 0)
12249 return 0;
12250
12251 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12252 do_div(value, 1000000);
12253 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12254}
12255
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012256/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012257 * Update the guest state fields of vmcs12 to reflect changes that
12258 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12259 * VM-entry controls is also updated, since this is really a guest
12260 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012261 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012262static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012263{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012264 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12265 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12266
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012267 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12268 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12269 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12270
12271 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12272 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12273 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12274 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12275 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12276 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12277 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12278 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12279 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12280 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12281 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12282 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12283 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12284 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12285 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12286 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12287 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12288 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12289 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12290 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12291 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12292 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12293 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12294 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12295 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12296 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12297 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12298 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12299 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12300 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12301 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12302 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12303 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12304 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12305 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12306 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12307
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012308 vmcs12->guest_interruptibility_info =
12309 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12310 vmcs12->guest_pending_dbg_exceptions =
12311 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012312 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12313 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12314 else
12315 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012316
Jan Kiszkaf4124502014-03-07 20:03:13 +010012317 if (nested_cpu_has_preemption_timer(vmcs12)) {
12318 if (vmcs12->vm_exit_controls &
12319 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12320 vmcs12->vmx_preemption_timer_value =
12321 vmx_get_preemption_timer_value(vcpu);
12322 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12323 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012324
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012325 /*
12326 * In some cases (usually, nested EPT), L2 is allowed to change its
12327 * own CR3 without exiting. If it has changed it, we must keep it.
12328 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12329 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12330 *
12331 * Additionally, restore L2's PDPTR to vmcs12.
12332 */
12333 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012334 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012335 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12336 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12337 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12338 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12339 }
12340
Jim Mattsond281e132017-06-01 12:44:46 -070012341 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012342
Wincy Van608406e2015-02-03 23:57:51 +080012343 if (nested_cpu_has_vid(vmcs12))
12344 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12345
Jan Kiszkac18911a2013-03-13 16:06:41 +010012346 vmcs12->vm_entry_controls =
12347 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012348 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012349
Jan Kiszka2996fca2014-06-16 13:59:43 +020012350 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12351 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12352 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12353 }
12354
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012355 /* TODO: These cannot have changed unless we have MSR bitmaps and
12356 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012357 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012358 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012359 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12360 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012361 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12362 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12363 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012364 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012365 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012366}
12367
12368/*
12369 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12370 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12371 * and this function updates it to reflect the changes to the guest state while
12372 * L2 was running (and perhaps made some exits which were handled directly by L0
12373 * without going back to L1), and to reflect the exit reason.
12374 * Note that we do not have to copy here all VMCS fields, just those that
12375 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12376 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12377 * which already writes to vmcs12 directly.
12378 */
12379static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12380 u32 exit_reason, u32 exit_intr_info,
12381 unsigned long exit_qualification)
12382{
12383 /* update guest state fields: */
12384 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012385
12386 /* update exit information fields: */
12387
Jan Kiszka533558b2014-01-04 18:47:20 +010012388 vmcs12->vm_exit_reason = exit_reason;
12389 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012390 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012391
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012392 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012393 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12394 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12395
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012396 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012397 vmcs12->launch_state = 1;
12398
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012399 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12400 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012401 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012402
12403 /*
12404 * Transfer the event that L0 or L1 may wanted to inject into
12405 * L2 to IDT_VECTORING_INFO_FIELD.
12406 */
12407 vmcs12_save_pending_event(vcpu, vmcs12);
12408 }
12409
12410 /*
12411 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12412 * preserved above and would only end up incorrectly in L1.
12413 */
12414 vcpu->arch.nmi_injected = false;
12415 kvm_clear_exception_queue(vcpu);
12416 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012417}
12418
Wanpeng Li5af41572017-11-05 16:54:49 -080012419static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12420 struct vmcs12 *vmcs12)
12421{
12422 u32 entry_failure_code;
12423
12424 nested_ept_uninit_mmu_context(vcpu);
12425
12426 /*
12427 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12428 * couldn't have changed.
12429 */
12430 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12431 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12432
12433 if (!enable_ept)
12434 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12435}
12436
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012437/*
12438 * A part of what we need to when the nested L2 guest exits and we want to
12439 * run its L1 parent, is to reset L1's guest state to the host state specified
12440 * in vmcs12.
12441 * This function is to be called not only on normal nested exit, but also on
12442 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12443 * Failures During or After Loading Guest State").
12444 * This function should be called when the active VMCS is L1's (vmcs01).
12445 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012446static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12447 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012448{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012449 struct kvm_segment seg;
12450
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012451 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12452 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012453 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012454 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12455 else
12456 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12457 vmx_set_efer(vcpu, vcpu->arch.efer);
12458
12459 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12460 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012461 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012462 /*
12463 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012464 * actually changed, because vmx_set_cr0 refers to efer set above.
12465 *
12466 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12467 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012468 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012469 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012470 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012471
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012472 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012473 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012474 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012475
Wanpeng Li5af41572017-11-05 16:54:49 -080012476 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012477
Liran Alon6f1e03b2018-05-22 17:16:14 +030012478 /*
12479 * If vmcs01 don't use VPID, CPU flushes TLB on every
12480 * VMEntry/VMExit. Thus, no need to flush TLB.
12481 *
12482 * If vmcs12 uses VPID, TLB entries populated by L2 are
12483 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12484 * with vmx->vpid. Thus, no need to flush TLB.
12485 *
12486 * Therefore, flush TLB only in case vmcs01 uses VPID and
12487 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12488 * are both tagged with vmx->vpid.
12489 */
12490 if (enable_vpid &&
12491 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012492 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012493 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012494
12495 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12496 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12497 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12498 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12499 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012500 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12501 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012502
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012503 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12504 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12505 vmcs_write64(GUEST_BNDCFGS, 0);
12506
Jan Kiszka44811c02013-08-04 17:17:27 +020012507 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012508 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012509 vcpu->arch.pat = vmcs12->host_ia32_pat;
12510 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012511 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12512 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12513 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012514
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012515 /* Set L1 segment info according to Intel SDM
12516 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12517 seg = (struct kvm_segment) {
12518 .base = 0,
12519 .limit = 0xFFFFFFFF,
12520 .selector = vmcs12->host_cs_selector,
12521 .type = 11,
12522 .present = 1,
12523 .s = 1,
12524 .g = 1
12525 };
12526 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12527 seg.l = 1;
12528 else
12529 seg.db = 1;
12530 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12531 seg = (struct kvm_segment) {
12532 .base = 0,
12533 .limit = 0xFFFFFFFF,
12534 .type = 3,
12535 .present = 1,
12536 .s = 1,
12537 .db = 1,
12538 .g = 1
12539 };
12540 seg.selector = vmcs12->host_ds_selector;
12541 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12542 seg.selector = vmcs12->host_es_selector;
12543 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12544 seg.selector = vmcs12->host_ss_selector;
12545 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12546 seg.selector = vmcs12->host_fs_selector;
12547 seg.base = vmcs12->host_fs_base;
12548 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12549 seg.selector = vmcs12->host_gs_selector;
12550 seg.base = vmcs12->host_gs_base;
12551 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12552 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012553 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012554 .limit = 0x67,
12555 .selector = vmcs12->host_tr_selector,
12556 .type = 11,
12557 .present = 1
12558 };
12559 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12560
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012561 kvm_set_dr(vcpu, 7, 0x400);
12562 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012563
Wincy Van3af18d92015-02-03 23:49:31 +080012564 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012565 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012566
Wincy Vanff651cb2014-12-11 08:52:58 +030012567 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12568 vmcs12->vm_exit_msr_load_count))
12569 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012570}
12571
12572/*
12573 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12574 * and modify vmcs12 to make it see what it would expect to see there if
12575 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12576 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012577static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12578 u32 exit_intr_info,
12579 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012580{
12581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012582 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12583
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012584 /* trying to cancel vmlaunch/vmresume is a bug */
12585 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12586
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012587 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012588 * The only expected VM-instruction error is "VM entry with
12589 * invalid control field(s)." Anything else indicates a
12590 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012591 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012592 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12593 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12594
12595 leave_guest_mode(vcpu);
12596
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012597 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12598 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12599
Jim Mattson4f350c62017-09-14 16:31:44 -070012600 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012601 if (exit_reason == -1)
12602 sync_vmcs12(vcpu, vmcs12);
12603 else
12604 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12605 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012606
Liran Alon61ada742018-06-23 02:35:08 +030012607 /*
12608 * Must happen outside of sync_vmcs12() as it will
12609 * also be used to capture vmcs12 cache as part of
12610 * capturing nVMX state for snapshot (migration).
12611 *
12612 * Otherwise, this flush will dirty guest memory at a
12613 * point it is already assumed by user-space to be
12614 * immutable.
12615 */
12616 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12617
Jim Mattson4f350c62017-09-14 16:31:44 -070012618 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12619 vmcs12->vm_exit_msr_store_count))
12620 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012621 }
12622
Jim Mattson4f350c62017-09-14 16:31:44 -070012623 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012624 vm_entry_controls_reset_shadow(vmx);
12625 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012626 vmx_segment_cache_clear(vmx);
12627
Paolo Bonzini93140062016-07-06 13:23:51 +020012628 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012629 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12630 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012631 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012632 if (vmx->hv_deadline_tsc == -1)
12633 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12634 PIN_BASED_VMX_PREEMPTION_TIMER);
12635 else
12636 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12637 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012638 if (kvm_has_tsc_control)
12639 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012640
Jim Mattson8d860bb2018-05-09 16:56:05 -040012641 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12642 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12643 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012644 } else if (!nested_cpu_has_ept(vmcs12) &&
12645 nested_cpu_has2(vmcs12,
12646 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012647 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012648 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012649
12650 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12651 vmx->host_rsp = 0;
12652
12653 /* Unpin physical memory we referred to in vmcs02 */
12654 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012655 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012656 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012657 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012658 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012659 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012660 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012661 }
Wincy Van705699a2015-02-03 23:58:17 +080012662 if (vmx->nested.pi_desc_page) {
12663 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012664 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012665 vmx->nested.pi_desc_page = NULL;
12666 vmx->nested.pi_desc = NULL;
12667 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012668
12669 /*
Tang Chen38b99172014-09-24 15:57:54 +080012670 * We are now running in L2, mmu_notifier will force to reload the
12671 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12672 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012673 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012674
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012675 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012676 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012677
12678 /* in case we halted in L2 */
12679 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012680
12681 if (likely(!vmx->fail)) {
12682 /*
12683 * TODO: SDM says that with acknowledge interrupt on
12684 * exit, bit 31 of the VM-exit interrupt information
12685 * (valid interrupt) is always set to 1 on
12686 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12687 * need kvm_cpu_has_interrupt(). See the commit
12688 * message for details.
12689 */
12690 if (nested_exit_intr_ack_set(vcpu) &&
12691 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12692 kvm_cpu_has_interrupt(vcpu)) {
12693 int irq = kvm_cpu_get_interrupt(vcpu);
12694 WARN_ON(irq < 0);
12695 vmcs12->vm_exit_intr_info = irq |
12696 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12697 }
12698
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012699 if (exit_reason != -1)
12700 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12701 vmcs12->exit_qualification,
12702 vmcs12->idt_vectoring_info_field,
12703 vmcs12->vm_exit_intr_info,
12704 vmcs12->vm_exit_intr_error_code,
12705 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012706
12707 load_vmcs12_host_state(vcpu, vmcs12);
12708
12709 return;
12710 }
12711
12712 /*
12713 * After an early L2 VM-entry failure, we're now back
12714 * in L1 which thinks it just finished a VMLAUNCH or
12715 * VMRESUME instruction, so we need to set the failure
12716 * flag and the VM-instruction error field of the VMCS
12717 * accordingly.
12718 */
12719 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012720
12721 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12722
Jim Mattson4f350c62017-09-14 16:31:44 -070012723 /*
12724 * The emulated instruction was already skipped in
12725 * nested_vmx_run, but the updated RIP was never
12726 * written back to the vmcs01.
12727 */
12728 skip_emulated_instruction(vcpu);
12729 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012730}
12731
Nadav Har'El7c177932011-05-25 23:12:04 +030012732/*
Jan Kiszka42124922014-01-04 18:47:19 +010012733 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12734 */
12735static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12736{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012737 if (is_guest_mode(vcpu)) {
12738 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012739 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012740 }
Jan Kiszka42124922014-01-04 18:47:19 +010012741 free_nested(to_vmx(vcpu));
12742}
12743
12744/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012745 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12746 * 23.7 "VM-entry failures during or after loading guest state" (this also
12747 * lists the acceptable exit-reason and exit-qualification parameters).
12748 * It should only be called before L2 actually succeeded to run, and when
12749 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12750 */
12751static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12752 struct vmcs12 *vmcs12,
12753 u32 reason, unsigned long qualification)
12754{
12755 load_vmcs12_host_state(vcpu, vmcs12);
12756 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12757 vmcs12->exit_qualification = qualification;
12758 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012759 if (enable_shadow_vmcs)
12760 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012761}
12762
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012763static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12764 struct x86_instruction_info *info,
12765 enum x86_intercept_stage stage)
12766{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012767 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12768 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12769
12770 /*
12771 * RDPID causes #UD if disabled through secondary execution controls.
12772 * Because it is marked as EmulateOnUD, we need to intercept it here.
12773 */
12774 if (info->intercept == x86_intercept_rdtscp &&
12775 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12776 ctxt->exception.vector = UD_VECTOR;
12777 ctxt->exception.error_code_valid = false;
12778 return X86EMUL_PROPAGATE_FAULT;
12779 }
12780
12781 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012782 return X86EMUL_CONTINUE;
12783}
12784
Yunhong Jiang64672c92016-06-13 14:19:59 -070012785#ifdef CONFIG_X86_64
12786/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12787static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12788 u64 divisor, u64 *result)
12789{
12790 u64 low = a << shift, high = a >> (64 - shift);
12791
12792 /* To avoid the overflow on divq */
12793 if (high >= divisor)
12794 return 1;
12795
12796 /* Low hold the result, high hold rem which is discarded */
12797 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12798 "rm" (divisor), "0" (low), "1" (high));
12799 *result = low;
12800
12801 return 0;
12802}
12803
12804static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12805{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012806 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012807 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012808
12809 if (kvm_mwait_in_guest(vcpu->kvm))
12810 return -EOPNOTSUPP;
12811
12812 vmx = to_vmx(vcpu);
12813 tscl = rdtsc();
12814 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12815 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012816 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12817
12818 if (delta_tsc > lapic_timer_advance_cycles)
12819 delta_tsc -= lapic_timer_advance_cycles;
12820 else
12821 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012822
12823 /* Convert to host delta tsc if tsc scaling is enabled */
12824 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12825 u64_shl_div_u64(delta_tsc,
12826 kvm_tsc_scaling_ratio_frac_bits,
12827 vcpu->arch.tsc_scaling_ratio,
12828 &delta_tsc))
12829 return -ERANGE;
12830
12831 /*
12832 * If the delta tsc can't fit in the 32 bit after the multi shift,
12833 * we can't use the preemption timer.
12834 * It's possible that it fits on later vmentries, but checking
12835 * on every vmentry is costly so we just use an hrtimer.
12836 */
12837 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12838 return -ERANGE;
12839
12840 vmx->hv_deadline_tsc = tscl + delta_tsc;
12841 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12842 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012843
12844 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012845}
12846
12847static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12848{
12849 struct vcpu_vmx *vmx = to_vmx(vcpu);
12850 vmx->hv_deadline_tsc = -1;
12851 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12852 PIN_BASED_VMX_PREEMPTION_TIMER);
12853}
12854#endif
12855
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012856static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012857{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012858 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012859 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012860}
12861
Kai Huang843e4332015-01-28 10:54:28 +080012862static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12863 struct kvm_memory_slot *slot)
12864{
12865 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12866 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12867}
12868
12869static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12870 struct kvm_memory_slot *slot)
12871{
12872 kvm_mmu_slot_set_dirty(kvm, slot);
12873}
12874
12875static void vmx_flush_log_dirty(struct kvm *kvm)
12876{
12877 kvm_flush_pml_buffers(kvm);
12878}
12879
Bandan Dasc5f983f2017-05-05 15:25:14 -040012880static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12881{
12882 struct vmcs12 *vmcs12;
12883 struct vcpu_vmx *vmx = to_vmx(vcpu);
12884 gpa_t gpa;
12885 struct page *page = NULL;
12886 u64 *pml_address;
12887
12888 if (is_guest_mode(vcpu)) {
12889 WARN_ON_ONCE(vmx->nested.pml_full);
12890
12891 /*
12892 * Check if PML is enabled for the nested guest.
12893 * Whether eptp bit 6 is set is already checked
12894 * as part of A/D emulation.
12895 */
12896 vmcs12 = get_vmcs12(vcpu);
12897 if (!nested_cpu_has_pml(vmcs12))
12898 return 0;
12899
Dan Carpenter47698862017-05-10 22:43:17 +030012900 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012901 vmx->nested.pml_full = true;
12902 return 1;
12903 }
12904
12905 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12906
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012907 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12908 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012909 return 0;
12910
12911 pml_address = kmap(page);
12912 pml_address[vmcs12->guest_pml_index--] = gpa;
12913 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012914 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012915 }
12916
12917 return 0;
12918}
12919
Kai Huang843e4332015-01-28 10:54:28 +080012920static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12921 struct kvm_memory_slot *memslot,
12922 gfn_t offset, unsigned long mask)
12923{
12924 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12925}
12926
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012927static void __pi_post_block(struct kvm_vcpu *vcpu)
12928{
12929 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12930 struct pi_desc old, new;
12931 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012932
12933 do {
12934 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012935 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12936 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012937
12938 dest = cpu_physical_id(vcpu->cpu);
12939
12940 if (x2apic_enabled())
12941 new.ndst = dest;
12942 else
12943 new.ndst = (dest << 8) & 0xFF00;
12944
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012945 /* set 'NV' to 'notification vector' */
12946 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012947 } while (cmpxchg64(&pi_desc->control, old.control,
12948 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012949
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012950 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12951 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012952 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012953 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012954 vcpu->pre_pcpu = -1;
12955 }
12956}
12957
Feng Wuefc64402015-09-18 22:29:51 +080012958/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012959 * This routine does the following things for vCPU which is going
12960 * to be blocked if VT-d PI is enabled.
12961 * - Store the vCPU to the wakeup list, so when interrupts happen
12962 * we can find the right vCPU to wake up.
12963 * - Change the Posted-interrupt descriptor as below:
12964 * 'NDST' <-- vcpu->pre_pcpu
12965 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12966 * - If 'ON' is set during this process, which means at least one
12967 * interrupt is posted for this vCPU, we cannot block it, in
12968 * this case, return 1, otherwise, return 0.
12969 *
12970 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012971static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012972{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012973 unsigned int dest;
12974 struct pi_desc old, new;
12975 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12976
12977 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012978 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12979 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012980 return 0;
12981
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012982 WARN_ON(irqs_disabled());
12983 local_irq_disable();
12984 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12985 vcpu->pre_pcpu = vcpu->cpu;
12986 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12987 list_add_tail(&vcpu->blocked_vcpu_list,
12988 &per_cpu(blocked_vcpu_on_cpu,
12989 vcpu->pre_pcpu));
12990 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12991 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012992
12993 do {
12994 old.control = new.control = pi_desc->control;
12995
Feng Wubf9f6ac2015-09-18 22:29:55 +080012996 WARN((pi_desc->sn == 1),
12997 "Warning: SN field of posted-interrupts "
12998 "is set before blocking\n");
12999
13000 /*
13001 * Since vCPU can be preempted during this process,
13002 * vcpu->cpu could be different with pre_pcpu, we
13003 * need to set pre_pcpu as the destination of wakeup
13004 * notification event, then we can find the right vCPU
13005 * to wakeup in wakeup handler if interrupts happen
13006 * when the vCPU is in blocked state.
13007 */
13008 dest = cpu_physical_id(vcpu->pre_pcpu);
13009
13010 if (x2apic_enabled())
13011 new.ndst = dest;
13012 else
13013 new.ndst = (dest << 8) & 0xFF00;
13014
13015 /* set 'NV' to 'wakeup vector' */
13016 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013017 } while (cmpxchg64(&pi_desc->control, old.control,
13018 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013019
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013020 /* We should not block the vCPU if an interrupt is posted for it. */
13021 if (pi_test_on(pi_desc) == 1)
13022 __pi_post_block(vcpu);
13023
13024 local_irq_enable();
13025 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013026}
13027
Yunhong Jiangbc225122016-06-13 14:19:58 -070013028static int vmx_pre_block(struct kvm_vcpu *vcpu)
13029{
13030 if (pi_pre_block(vcpu))
13031 return 1;
13032
Yunhong Jiang64672c92016-06-13 14:19:59 -070013033 if (kvm_lapic_hv_timer_in_use(vcpu))
13034 kvm_lapic_switch_to_sw_timer(vcpu);
13035
Yunhong Jiangbc225122016-06-13 14:19:58 -070013036 return 0;
13037}
13038
13039static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013040{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013041 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013042 return;
13043
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013044 WARN_ON(irqs_disabled());
13045 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013046 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013047 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013048}
13049
Yunhong Jiangbc225122016-06-13 14:19:58 -070013050static void vmx_post_block(struct kvm_vcpu *vcpu)
13051{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013052 if (kvm_x86_ops->set_hv_timer)
13053 kvm_lapic_switch_to_hv_timer(vcpu);
13054
Yunhong Jiangbc225122016-06-13 14:19:58 -070013055 pi_post_block(vcpu);
13056}
13057
Feng Wubf9f6ac2015-09-18 22:29:55 +080013058/*
Feng Wuefc64402015-09-18 22:29:51 +080013059 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13060 *
13061 * @kvm: kvm
13062 * @host_irq: host irq of the interrupt
13063 * @guest_irq: gsi of the interrupt
13064 * @set: set or unset PI
13065 * returns 0 on success, < 0 on failure
13066 */
13067static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13068 uint32_t guest_irq, bool set)
13069{
13070 struct kvm_kernel_irq_routing_entry *e;
13071 struct kvm_irq_routing_table *irq_rt;
13072 struct kvm_lapic_irq irq;
13073 struct kvm_vcpu *vcpu;
13074 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013075 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013076
13077 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013078 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13079 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013080 return 0;
13081
13082 idx = srcu_read_lock(&kvm->irq_srcu);
13083 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013084 if (guest_irq >= irq_rt->nr_rt_entries ||
13085 hlist_empty(&irq_rt->map[guest_irq])) {
13086 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13087 guest_irq, irq_rt->nr_rt_entries);
13088 goto out;
13089 }
Feng Wuefc64402015-09-18 22:29:51 +080013090
13091 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13092 if (e->type != KVM_IRQ_ROUTING_MSI)
13093 continue;
13094 /*
13095 * VT-d PI cannot support posting multicast/broadcast
13096 * interrupts to a vCPU, we still use interrupt remapping
13097 * for these kind of interrupts.
13098 *
13099 * For lowest-priority interrupts, we only support
13100 * those with single CPU as the destination, e.g. user
13101 * configures the interrupts via /proc/irq or uses
13102 * irqbalance to make the interrupts single-CPU.
13103 *
13104 * We will support full lowest-priority interrupt later.
13105 */
13106
Radim Krčmář371313132016-07-12 22:09:27 +020013107 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013108 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13109 /*
13110 * Make sure the IRTE is in remapped mode if
13111 * we don't handle it in posted mode.
13112 */
13113 ret = irq_set_vcpu_affinity(host_irq, NULL);
13114 if (ret < 0) {
13115 printk(KERN_INFO
13116 "failed to back to remapped mode, irq: %u\n",
13117 host_irq);
13118 goto out;
13119 }
13120
Feng Wuefc64402015-09-18 22:29:51 +080013121 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013122 }
Feng Wuefc64402015-09-18 22:29:51 +080013123
13124 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13125 vcpu_info.vector = irq.vector;
13126
hu huajun2698d822018-04-11 15:16:40 +080013127 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013128 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13129
13130 if (set)
13131 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013132 else
Feng Wuefc64402015-09-18 22:29:51 +080013133 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013134
13135 if (ret < 0) {
13136 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13137 __func__);
13138 goto out;
13139 }
13140 }
13141
13142 ret = 0;
13143out:
13144 srcu_read_unlock(&kvm->irq_srcu, idx);
13145 return ret;
13146}
13147
Ashok Rajc45dcc72016-06-22 14:59:56 +080013148static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13149{
13150 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13151 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13152 FEATURE_CONTROL_LMCE;
13153 else
13154 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13155 ~FEATURE_CONTROL_LMCE;
13156}
13157
Ladi Prosek72d7b372017-10-11 16:54:41 +020013158static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13159{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013160 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13161 if (to_vmx(vcpu)->nested.nested_run_pending)
13162 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013163 return 1;
13164}
13165
Ladi Prosek0234bf82017-10-11 16:54:40 +020013166static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13167{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013168 struct vcpu_vmx *vmx = to_vmx(vcpu);
13169
13170 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13171 if (vmx->nested.smm.guest_mode)
13172 nested_vmx_vmexit(vcpu, -1, 0, 0);
13173
13174 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13175 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013176 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013177 return 0;
13178}
13179
13180static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13181{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013182 struct vcpu_vmx *vmx = to_vmx(vcpu);
13183 int ret;
13184
13185 if (vmx->nested.smm.vmxon) {
13186 vmx->nested.vmxon = true;
13187 vmx->nested.smm.vmxon = false;
13188 }
13189
13190 if (vmx->nested.smm.guest_mode) {
13191 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013192 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013193 vcpu->arch.hflags |= HF_SMM_MASK;
13194 if (ret)
13195 return ret;
13196
13197 vmx->nested.smm.guest_mode = false;
13198 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013199 return 0;
13200}
13201
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013202static int enable_smi_window(struct kvm_vcpu *vcpu)
13203{
13204 return 0;
13205}
13206
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013207static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13208 struct kvm_nested_state __user *user_kvm_nested_state,
13209 u32 user_data_size)
13210{
13211 struct vcpu_vmx *vmx;
13212 struct vmcs12 *vmcs12;
13213 struct kvm_nested_state kvm_state = {
13214 .flags = 0,
13215 .format = 0,
13216 .size = sizeof(kvm_state),
13217 .vmx.vmxon_pa = -1ull,
13218 .vmx.vmcs_pa = -1ull,
13219 };
13220
13221 if (!vcpu)
13222 return kvm_state.size + 2 * VMCS12_SIZE;
13223
13224 vmx = to_vmx(vcpu);
13225 vmcs12 = get_vmcs12(vcpu);
13226 if (nested_vmx_allowed(vcpu) &&
13227 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13228 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13229 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13230
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013231 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013232 kvm_state.size += VMCS12_SIZE;
13233
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013234 if (is_guest_mode(vcpu) &&
13235 nested_cpu_has_shadow_vmcs(vmcs12) &&
13236 vmcs12->vmcs_link_pointer != -1ull)
13237 kvm_state.size += VMCS12_SIZE;
13238 }
13239
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013240 if (vmx->nested.smm.vmxon)
13241 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13242
13243 if (vmx->nested.smm.guest_mode)
13244 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13245
13246 if (is_guest_mode(vcpu)) {
13247 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13248
13249 if (vmx->nested.nested_run_pending)
13250 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13251 }
13252 }
13253
13254 if (user_data_size < kvm_state.size)
13255 goto out;
13256
13257 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13258 return -EFAULT;
13259
13260 if (vmx->nested.current_vmptr == -1ull)
13261 goto out;
13262
13263 /*
13264 * When running L2, the authoritative vmcs12 state is in the
13265 * vmcs02. When running L1, the authoritative vmcs12 state is
13266 * in the shadow vmcs linked to vmcs01, unless
13267 * sync_shadow_vmcs is set, in which case, the authoritative
13268 * vmcs12 state is in the vmcs12 already.
13269 */
13270 if (is_guest_mode(vcpu))
13271 sync_vmcs12(vcpu, vmcs12);
13272 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13273 copy_shadow_to_vmcs12(vmx);
13274
13275 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13276 return -EFAULT;
13277
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013278 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13279 vmcs12->vmcs_link_pointer != -1ull) {
13280 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13281 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13282 return -EFAULT;
13283 }
13284
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013285out:
13286 return kvm_state.size;
13287}
13288
13289static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13290 struct kvm_nested_state __user *user_kvm_nested_state,
13291 struct kvm_nested_state *kvm_state)
13292{
13293 struct vcpu_vmx *vmx = to_vmx(vcpu);
13294 struct vmcs12 *vmcs12;
13295 u32 exit_qual;
13296 int ret;
13297
13298 if (kvm_state->format != 0)
13299 return -EINVAL;
13300
13301 if (!nested_vmx_allowed(vcpu))
13302 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13303
13304 if (kvm_state->vmx.vmxon_pa == -1ull) {
13305 if (kvm_state->vmx.smm.flags)
13306 return -EINVAL;
13307
13308 if (kvm_state->vmx.vmcs_pa != -1ull)
13309 return -EINVAL;
13310
13311 vmx_leave_nested(vcpu);
13312 return 0;
13313 }
13314
13315 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13316 return -EINVAL;
13317
13318 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13319 return -EINVAL;
13320
13321 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13322 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13323 return -EINVAL;
13324
13325 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13326 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13327 return -EINVAL;
13328
13329 if (kvm_state->vmx.smm.flags &
13330 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13331 return -EINVAL;
13332
13333 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13334 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13335 return -EINVAL;
13336
13337 vmx_leave_nested(vcpu);
13338 if (kvm_state->vmx.vmxon_pa == -1ull)
13339 return 0;
13340
13341 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13342 ret = enter_vmx_operation(vcpu);
13343 if (ret)
13344 return ret;
13345
13346 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13347
13348 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13349 vmx->nested.smm.vmxon = true;
13350 vmx->nested.vmxon = false;
13351
13352 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13353 vmx->nested.smm.guest_mode = true;
13354 }
13355
13356 vmcs12 = get_vmcs12(vcpu);
13357 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13358 return -EFAULT;
13359
Liran Alon392b2f22018-06-23 02:35:01 +030013360 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013361 return -EINVAL;
13362
13363 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13364 return 0;
13365
13366 vmx->nested.nested_run_pending =
13367 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13368
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013369 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13370 vmcs12->vmcs_link_pointer != -1ull) {
13371 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13372 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13373 return -EINVAL;
13374
13375 if (copy_from_user(shadow_vmcs12,
13376 user_kvm_nested_state->data + VMCS12_SIZE,
13377 sizeof(*vmcs12)))
13378 return -EFAULT;
13379
13380 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13381 !shadow_vmcs12->hdr.shadow_vmcs)
13382 return -EINVAL;
13383 }
13384
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013385 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13386 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13387 return -EINVAL;
13388
13389 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13390 vmx->nested.nested_run_pending = 1;
13391
13392 vmx->nested.dirty_vmcs12 = true;
13393 ret = enter_vmx_non_root_mode(vcpu, NULL);
13394 if (ret)
13395 return -EINVAL;
13396
13397 return 0;
13398}
13399
Kees Cook404f6aa2016-08-08 16:29:06 -070013400static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013401 .cpu_has_kvm_support = cpu_has_kvm_support,
13402 .disabled_by_bios = vmx_disabled_by_bios,
13403 .hardware_setup = hardware_setup,
13404 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013405 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013406 .hardware_enable = hardware_enable,
13407 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013408 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013409 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013410
Wanpeng Lib31c1142018-03-12 04:53:04 -070013411 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013412 .vm_alloc = vmx_vm_alloc,
13413 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013414
Avi Kivity6aa8b732006-12-10 02:21:36 -080013415 .vcpu_create = vmx_create_vcpu,
13416 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013417 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013418
Avi Kivity04d2cc72007-09-10 18:10:54 +030013419 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013420 .vcpu_load = vmx_vcpu_load,
13421 .vcpu_put = vmx_vcpu_put,
13422
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013423 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013424 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013425 .get_msr = vmx_get_msr,
13426 .set_msr = vmx_set_msr,
13427 .get_segment_base = vmx_get_segment_base,
13428 .get_segment = vmx_get_segment,
13429 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013430 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013431 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013432 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013433 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013434 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013435 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013436 .set_cr3 = vmx_set_cr3,
13437 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013438 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013439 .get_idt = vmx_get_idt,
13440 .set_idt = vmx_set_idt,
13441 .get_gdt = vmx_get_gdt,
13442 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013443 .get_dr6 = vmx_get_dr6,
13444 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013445 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013446 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013447 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013448 .get_rflags = vmx_get_rflags,
13449 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013450
Avi Kivity6aa8b732006-12-10 02:21:36 -080013451 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013452
Avi Kivity6aa8b732006-12-10 02:21:36 -080013453 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013454 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013455 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013456 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13457 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013458 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013459 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013460 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013461 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013462 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013463 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013464 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013465 .get_nmi_mask = vmx_get_nmi_mask,
13466 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013467 .enable_nmi_window = enable_nmi_window,
13468 .enable_irq_window = enable_irq_window,
13469 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013470 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013471 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013472 .get_enable_apicv = vmx_get_enable_apicv,
13473 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013474 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013475 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013476 .hwapic_irr_update = vmx_hwapic_irr_update,
13477 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013478 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13479 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013480
Izik Eiduscbc94022007-10-25 00:29:55 +020013481 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013482 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013483 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013484 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013485
Avi Kivity586f9602010-11-18 13:09:54 +020013486 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013487
Sheng Yang17cc3932010-01-05 19:02:27 +080013488 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013489
13490 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013491
13492 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013493 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013494
13495 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013496
13497 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013498
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013499 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013500 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013501
13502 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013503
13504 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013505 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013506 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013507 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013508 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013509
13510 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013511
13512 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013513
13514 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13515 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13516 .flush_log_dirty = vmx_flush_log_dirty,
13517 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013518 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013519
Feng Wubf9f6ac2015-09-18 22:29:55 +080013520 .pre_block = vmx_pre_block,
13521 .post_block = vmx_post_block,
13522
Wei Huang25462f72015-06-19 15:45:05 +020013523 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013524
13525 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013526
13527#ifdef CONFIG_X86_64
13528 .set_hv_timer = vmx_set_hv_timer,
13529 .cancel_hv_timer = vmx_cancel_hv_timer,
13530#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013531
13532 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013533
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013534 .get_nested_state = vmx_get_nested_state,
13535 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013536 .get_vmcs12_pages = nested_get_vmcs12_pages,
13537
Ladi Prosek72d7b372017-10-11 16:54:41 +020013538 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013539 .pre_enter_smm = vmx_pre_enter_smm,
13540 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013541 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013542};
13543
13544static int __init vmx_init(void)
13545{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013546 int r;
13547
13548#if IS_ENABLED(CONFIG_HYPERV)
13549 /*
13550 * Enlightened VMCS usage should be recommended and the host needs
13551 * to support eVMCS v1 or above. We can also disable eVMCS support
13552 * with module parameter.
13553 */
13554 if (enlightened_vmcs &&
13555 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13556 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13557 KVM_EVMCS_VERSION) {
13558 int cpu;
13559
13560 /* Check that we have assist pages on all online CPUs */
13561 for_each_online_cpu(cpu) {
13562 if (!hv_get_vp_assist_page(cpu)) {
13563 enlightened_vmcs = false;
13564 break;
13565 }
13566 }
13567
13568 if (enlightened_vmcs) {
13569 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13570 static_branch_enable(&enable_evmcs);
13571 }
13572 } else {
13573 enlightened_vmcs = false;
13574 }
13575#endif
13576
13577 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013578 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013579 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013580 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013581
Dave Young2965faa2015-09-09 15:38:55 -070013582#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013583 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13584 crash_vmclear_local_loaded_vmcss);
13585#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013586 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013587
He, Qingfdef3ad2007-04-30 09:45:24 +030013588 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013589}
13590
13591static void __exit vmx_exit(void)
13592{
Dave Young2965faa2015-09-09 15:38:55 -070013593#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013594 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013595 synchronize_rcu();
13596#endif
13597
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013598 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013599
13600#if IS_ENABLED(CONFIG_HYPERV)
13601 if (static_branch_unlikely(&enable_evmcs)) {
13602 int cpu;
13603 struct hv_vp_assist_page *vp_ap;
13604 /*
13605 * Reset everything to support using non-enlightened VMCS
13606 * access later (e.g. when we reload the module with
13607 * enlightened_vmcs=0)
13608 */
13609 for_each_online_cpu(cpu) {
13610 vp_ap = hv_get_vp_assist_page(cpu);
13611
13612 if (!vp_ap)
13613 continue;
13614
13615 vp_ap->current_nested_vmcs = 0;
13616 vp_ap->enlighten_vmentry = 0;
13617 }
13618
13619 static_branch_disable(&enable_evmcs);
13620 }
13621#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013622}
13623
13624module_init(vmx_init)
13625module_exit(vmx_exit)