blob: 935b84f40f65d9198766ff4ff8eee8a4b4f53642 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf41245002014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Tianyu Lan877ad952018-07-19 08:40:23 +0000191enum ept_pointers_status {
192 EPT_POINTERS_CHECK = 0,
193 EPT_POINTERS_MATCH = 1,
194 EPT_POINTERS_MISMATCH = 2
195};
196
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700197struct kvm_vmx {
198 struct kvm kvm;
199
200 unsigned int tss_addr;
201 bool ept_identity_pagetable_done;
202 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000203
204 enum ept_pointers_status ept_pointers_match;
205 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700206};
207
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200208#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300209
Liran Alon392b2f22018-06-23 02:35:01 +0300210struct vmcs_hdr {
211 u32 revision_id:31;
212 u32 shadow_vmcs:1;
213};
214
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400215struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300216 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400217 u32 abort;
218 char data[0];
219};
220
Nadav Har'Eld462b812011-05-24 15:26:10 +0300221/*
222 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
223 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
224 * loaded on this CPU (so we can clear them if the CPU goes down).
225 */
226struct loaded_vmcs {
227 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700228 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300229 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200230 bool launched;
231 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200232 unsigned long vmcs_host_cr3; /* May not match real cr3 */
233 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100234 /* Support for vnmi-less CPUs */
235 int soft_vnmi_blocked;
236 ktime_t entry_time;
237 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100238 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300239 struct list_head loaded_vmcss_on_cpu_link;
240};
241
Avi Kivity26bb0982009-09-07 11:14:12 +0300242struct shared_msr_entry {
243 unsigned index;
244 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200245 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300246};
247
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300248/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300249 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
250 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
251 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
252 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
253 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
254 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600255 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300256 * underlying hardware which will be used to run L2.
257 * This structure is packed to ensure that its layout is identical across
258 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700259 *
260 * IMPORTANT: Changing the layout of existing fields in this structure
261 * will break save/restore compatibility with older kvm releases. When
262 * adding new fields, either use space in the reserved padding* arrays
263 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300264 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300266struct __packed vmcs12 {
267 /* According to the Intel spec, a VMCS region must start with the
268 * following two fields. Then follow implementation-specific data.
269 */
Liran Alon392b2f22018-06-23 02:35:01 +0300270 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300271 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300272
Nadav Har'El27d6c862011-05-25 23:06:59 +0300273 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
274 u32 padding[7]; /* room for future expansion */
275
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 io_bitmap_a;
277 u64 io_bitmap_b;
278 u64 msr_bitmap;
279 u64 vm_exit_msr_store_addr;
280 u64 vm_exit_msr_load_addr;
281 u64 vm_entry_msr_load_addr;
282 u64 tsc_offset;
283 u64 virtual_apic_page_addr;
284 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800285 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300286 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800287 u64 eoi_exit_bitmap0;
288 u64 eoi_exit_bitmap1;
289 u64 eoi_exit_bitmap2;
290 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800291 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300292 u64 guest_physical_address;
293 u64 vmcs_link_pointer;
294 u64 guest_ia32_debugctl;
295 u64 guest_ia32_pat;
296 u64 guest_ia32_efer;
297 u64 guest_ia32_perf_global_ctrl;
298 u64 guest_pdptr0;
299 u64 guest_pdptr1;
300 u64 guest_pdptr2;
301 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100302 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 u64 host_ia32_pat;
304 u64 host_ia32_efer;
305 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700306 u64 vmread_bitmap;
307 u64 vmwrite_bitmap;
308 u64 vm_function_control;
309 u64 eptp_list_address;
310 u64 pml_address;
311 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300312 /*
313 * To allow migration of L1 (complete with its L2 guests) between
314 * machines of different natural widths (32 or 64 bit), we cannot have
315 * unsigned long fields with no explict size. We use u64 (aliased
316 * natural_width) instead. Luckily, x86 is little-endian.
317 */
318 natural_width cr0_guest_host_mask;
319 natural_width cr4_guest_host_mask;
320 natural_width cr0_read_shadow;
321 natural_width cr4_read_shadow;
322 natural_width cr3_target_value0;
323 natural_width cr3_target_value1;
324 natural_width cr3_target_value2;
325 natural_width cr3_target_value3;
326 natural_width exit_qualification;
327 natural_width guest_linear_address;
328 natural_width guest_cr0;
329 natural_width guest_cr3;
330 natural_width guest_cr4;
331 natural_width guest_es_base;
332 natural_width guest_cs_base;
333 natural_width guest_ss_base;
334 natural_width guest_ds_base;
335 natural_width guest_fs_base;
336 natural_width guest_gs_base;
337 natural_width guest_ldtr_base;
338 natural_width guest_tr_base;
339 natural_width guest_gdtr_base;
340 natural_width guest_idtr_base;
341 natural_width guest_dr7;
342 natural_width guest_rsp;
343 natural_width guest_rip;
344 natural_width guest_rflags;
345 natural_width guest_pending_dbg_exceptions;
346 natural_width guest_sysenter_esp;
347 natural_width guest_sysenter_eip;
348 natural_width host_cr0;
349 natural_width host_cr3;
350 natural_width host_cr4;
351 natural_width host_fs_base;
352 natural_width host_gs_base;
353 natural_width host_tr_base;
354 natural_width host_gdtr_base;
355 natural_width host_idtr_base;
356 natural_width host_ia32_sysenter_esp;
357 natural_width host_ia32_sysenter_eip;
358 natural_width host_rsp;
359 natural_width host_rip;
360 natural_width paddingl[8]; /* room for future expansion */
361 u32 pin_based_vm_exec_control;
362 u32 cpu_based_vm_exec_control;
363 u32 exception_bitmap;
364 u32 page_fault_error_code_mask;
365 u32 page_fault_error_code_match;
366 u32 cr3_target_count;
367 u32 vm_exit_controls;
368 u32 vm_exit_msr_store_count;
369 u32 vm_exit_msr_load_count;
370 u32 vm_entry_controls;
371 u32 vm_entry_msr_load_count;
372 u32 vm_entry_intr_info_field;
373 u32 vm_entry_exception_error_code;
374 u32 vm_entry_instruction_len;
375 u32 tpr_threshold;
376 u32 secondary_vm_exec_control;
377 u32 vm_instruction_error;
378 u32 vm_exit_reason;
379 u32 vm_exit_intr_info;
380 u32 vm_exit_intr_error_code;
381 u32 idt_vectoring_info_field;
382 u32 idt_vectoring_error_code;
383 u32 vm_exit_instruction_len;
384 u32 vmx_instruction_info;
385 u32 guest_es_limit;
386 u32 guest_cs_limit;
387 u32 guest_ss_limit;
388 u32 guest_ds_limit;
389 u32 guest_fs_limit;
390 u32 guest_gs_limit;
391 u32 guest_ldtr_limit;
392 u32 guest_tr_limit;
393 u32 guest_gdtr_limit;
394 u32 guest_idtr_limit;
395 u32 guest_es_ar_bytes;
396 u32 guest_cs_ar_bytes;
397 u32 guest_ss_ar_bytes;
398 u32 guest_ds_ar_bytes;
399 u32 guest_fs_ar_bytes;
400 u32 guest_gs_ar_bytes;
401 u32 guest_ldtr_ar_bytes;
402 u32 guest_tr_ar_bytes;
403 u32 guest_interruptibility_info;
404 u32 guest_activity_state;
405 u32 guest_sysenter_cs;
406 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100407 u32 vmx_preemption_timer_value;
408 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300409 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800410 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 guest_es_selector;
412 u16 guest_cs_selector;
413 u16 guest_ss_selector;
414 u16 guest_ds_selector;
415 u16 guest_fs_selector;
416 u16 guest_gs_selector;
417 u16 guest_ldtr_selector;
418 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800419 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300420 u16 host_es_selector;
421 u16 host_cs_selector;
422 u16 host_ss_selector;
423 u16 host_ds_selector;
424 u16 host_fs_selector;
425 u16 host_gs_selector;
426 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700427 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300428};
429
430/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700431 * For save/restore compatibility, the vmcs12 field offsets must not change.
432 */
433#define CHECK_OFFSET(field, loc) \
434 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
435 "Offset of " #field " in struct vmcs12 has changed.")
436
437static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300438 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700439 CHECK_OFFSET(abort, 4);
440 CHECK_OFFSET(launch_state, 8);
441 CHECK_OFFSET(io_bitmap_a, 40);
442 CHECK_OFFSET(io_bitmap_b, 48);
443 CHECK_OFFSET(msr_bitmap, 56);
444 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
445 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
446 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
447 CHECK_OFFSET(tsc_offset, 88);
448 CHECK_OFFSET(virtual_apic_page_addr, 96);
449 CHECK_OFFSET(apic_access_addr, 104);
450 CHECK_OFFSET(posted_intr_desc_addr, 112);
451 CHECK_OFFSET(ept_pointer, 120);
452 CHECK_OFFSET(eoi_exit_bitmap0, 128);
453 CHECK_OFFSET(eoi_exit_bitmap1, 136);
454 CHECK_OFFSET(eoi_exit_bitmap2, 144);
455 CHECK_OFFSET(eoi_exit_bitmap3, 152);
456 CHECK_OFFSET(xss_exit_bitmap, 160);
457 CHECK_OFFSET(guest_physical_address, 168);
458 CHECK_OFFSET(vmcs_link_pointer, 176);
459 CHECK_OFFSET(guest_ia32_debugctl, 184);
460 CHECK_OFFSET(guest_ia32_pat, 192);
461 CHECK_OFFSET(guest_ia32_efer, 200);
462 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
463 CHECK_OFFSET(guest_pdptr0, 216);
464 CHECK_OFFSET(guest_pdptr1, 224);
465 CHECK_OFFSET(guest_pdptr2, 232);
466 CHECK_OFFSET(guest_pdptr3, 240);
467 CHECK_OFFSET(guest_bndcfgs, 248);
468 CHECK_OFFSET(host_ia32_pat, 256);
469 CHECK_OFFSET(host_ia32_efer, 264);
470 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
471 CHECK_OFFSET(vmread_bitmap, 280);
472 CHECK_OFFSET(vmwrite_bitmap, 288);
473 CHECK_OFFSET(vm_function_control, 296);
474 CHECK_OFFSET(eptp_list_address, 304);
475 CHECK_OFFSET(pml_address, 312);
476 CHECK_OFFSET(cr0_guest_host_mask, 344);
477 CHECK_OFFSET(cr4_guest_host_mask, 352);
478 CHECK_OFFSET(cr0_read_shadow, 360);
479 CHECK_OFFSET(cr4_read_shadow, 368);
480 CHECK_OFFSET(cr3_target_value0, 376);
481 CHECK_OFFSET(cr3_target_value1, 384);
482 CHECK_OFFSET(cr3_target_value2, 392);
483 CHECK_OFFSET(cr3_target_value3, 400);
484 CHECK_OFFSET(exit_qualification, 408);
485 CHECK_OFFSET(guest_linear_address, 416);
486 CHECK_OFFSET(guest_cr0, 424);
487 CHECK_OFFSET(guest_cr3, 432);
488 CHECK_OFFSET(guest_cr4, 440);
489 CHECK_OFFSET(guest_es_base, 448);
490 CHECK_OFFSET(guest_cs_base, 456);
491 CHECK_OFFSET(guest_ss_base, 464);
492 CHECK_OFFSET(guest_ds_base, 472);
493 CHECK_OFFSET(guest_fs_base, 480);
494 CHECK_OFFSET(guest_gs_base, 488);
495 CHECK_OFFSET(guest_ldtr_base, 496);
496 CHECK_OFFSET(guest_tr_base, 504);
497 CHECK_OFFSET(guest_gdtr_base, 512);
498 CHECK_OFFSET(guest_idtr_base, 520);
499 CHECK_OFFSET(guest_dr7, 528);
500 CHECK_OFFSET(guest_rsp, 536);
501 CHECK_OFFSET(guest_rip, 544);
502 CHECK_OFFSET(guest_rflags, 552);
503 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
504 CHECK_OFFSET(guest_sysenter_esp, 568);
505 CHECK_OFFSET(guest_sysenter_eip, 576);
506 CHECK_OFFSET(host_cr0, 584);
507 CHECK_OFFSET(host_cr3, 592);
508 CHECK_OFFSET(host_cr4, 600);
509 CHECK_OFFSET(host_fs_base, 608);
510 CHECK_OFFSET(host_gs_base, 616);
511 CHECK_OFFSET(host_tr_base, 624);
512 CHECK_OFFSET(host_gdtr_base, 632);
513 CHECK_OFFSET(host_idtr_base, 640);
514 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
515 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
516 CHECK_OFFSET(host_rsp, 664);
517 CHECK_OFFSET(host_rip, 672);
518 CHECK_OFFSET(pin_based_vm_exec_control, 744);
519 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
520 CHECK_OFFSET(exception_bitmap, 752);
521 CHECK_OFFSET(page_fault_error_code_mask, 756);
522 CHECK_OFFSET(page_fault_error_code_match, 760);
523 CHECK_OFFSET(cr3_target_count, 764);
524 CHECK_OFFSET(vm_exit_controls, 768);
525 CHECK_OFFSET(vm_exit_msr_store_count, 772);
526 CHECK_OFFSET(vm_exit_msr_load_count, 776);
527 CHECK_OFFSET(vm_entry_controls, 780);
528 CHECK_OFFSET(vm_entry_msr_load_count, 784);
529 CHECK_OFFSET(vm_entry_intr_info_field, 788);
530 CHECK_OFFSET(vm_entry_exception_error_code, 792);
531 CHECK_OFFSET(vm_entry_instruction_len, 796);
532 CHECK_OFFSET(tpr_threshold, 800);
533 CHECK_OFFSET(secondary_vm_exec_control, 804);
534 CHECK_OFFSET(vm_instruction_error, 808);
535 CHECK_OFFSET(vm_exit_reason, 812);
536 CHECK_OFFSET(vm_exit_intr_info, 816);
537 CHECK_OFFSET(vm_exit_intr_error_code, 820);
538 CHECK_OFFSET(idt_vectoring_info_field, 824);
539 CHECK_OFFSET(idt_vectoring_error_code, 828);
540 CHECK_OFFSET(vm_exit_instruction_len, 832);
541 CHECK_OFFSET(vmx_instruction_info, 836);
542 CHECK_OFFSET(guest_es_limit, 840);
543 CHECK_OFFSET(guest_cs_limit, 844);
544 CHECK_OFFSET(guest_ss_limit, 848);
545 CHECK_OFFSET(guest_ds_limit, 852);
546 CHECK_OFFSET(guest_fs_limit, 856);
547 CHECK_OFFSET(guest_gs_limit, 860);
548 CHECK_OFFSET(guest_ldtr_limit, 864);
549 CHECK_OFFSET(guest_tr_limit, 868);
550 CHECK_OFFSET(guest_gdtr_limit, 872);
551 CHECK_OFFSET(guest_idtr_limit, 876);
552 CHECK_OFFSET(guest_es_ar_bytes, 880);
553 CHECK_OFFSET(guest_cs_ar_bytes, 884);
554 CHECK_OFFSET(guest_ss_ar_bytes, 888);
555 CHECK_OFFSET(guest_ds_ar_bytes, 892);
556 CHECK_OFFSET(guest_fs_ar_bytes, 896);
557 CHECK_OFFSET(guest_gs_ar_bytes, 900);
558 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
559 CHECK_OFFSET(guest_tr_ar_bytes, 908);
560 CHECK_OFFSET(guest_interruptibility_info, 912);
561 CHECK_OFFSET(guest_activity_state, 916);
562 CHECK_OFFSET(guest_sysenter_cs, 920);
563 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
564 CHECK_OFFSET(vmx_preemption_timer_value, 928);
565 CHECK_OFFSET(virtual_processor_id, 960);
566 CHECK_OFFSET(posted_intr_nv, 962);
567 CHECK_OFFSET(guest_es_selector, 964);
568 CHECK_OFFSET(guest_cs_selector, 966);
569 CHECK_OFFSET(guest_ss_selector, 968);
570 CHECK_OFFSET(guest_ds_selector, 970);
571 CHECK_OFFSET(guest_fs_selector, 972);
572 CHECK_OFFSET(guest_gs_selector, 974);
573 CHECK_OFFSET(guest_ldtr_selector, 976);
574 CHECK_OFFSET(guest_tr_selector, 978);
575 CHECK_OFFSET(guest_intr_status, 980);
576 CHECK_OFFSET(host_es_selector, 982);
577 CHECK_OFFSET(host_cs_selector, 984);
578 CHECK_OFFSET(host_ss_selector, 986);
579 CHECK_OFFSET(host_ds_selector, 988);
580 CHECK_OFFSET(host_fs_selector, 990);
581 CHECK_OFFSET(host_gs_selector, 992);
582 CHECK_OFFSET(host_tr_selector, 994);
583 CHECK_OFFSET(guest_pml_index, 996);
584}
585
586/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300587 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
588 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
589 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700590 *
591 * IMPORTANT: Changing this value will break save/restore compatibility with
592 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300593 */
594#define VMCS12_REVISION 0x11e57ed0
595
596/*
597 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
598 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
599 * current implementation, 4K are reserved to avoid future complications.
600 */
601#define VMCS12_SIZE 0x1000
602
603/*
Jim Mattson5b157062017-12-22 12:11:12 -0800604 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
605 * supported VMCS12 field encoding.
606 */
607#define VMCS12_MAX_FIELD_INDEX 0x17
608
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100609struct nested_vmx_msrs {
610 /*
611 * We only store the "true" versions of the VMX capability MSRs. We
612 * generate the "non-true" versions by setting the must-be-1 bits
613 * according to the SDM.
614 */
615 u32 procbased_ctls_low;
616 u32 procbased_ctls_high;
617 u32 secondary_ctls_low;
618 u32 secondary_ctls_high;
619 u32 pinbased_ctls_low;
620 u32 pinbased_ctls_high;
621 u32 exit_ctls_low;
622 u32 exit_ctls_high;
623 u32 entry_ctls_low;
624 u32 entry_ctls_high;
625 u32 misc_low;
626 u32 misc_high;
627 u32 ept_caps;
628 u32 vpid_caps;
629 u64 basic;
630 u64 cr0_fixed0;
631 u64 cr0_fixed1;
632 u64 cr4_fixed0;
633 u64 cr4_fixed1;
634 u64 vmcs_enum;
635 u64 vmfunc_controls;
636};
637
Jim Mattson5b157062017-12-22 12:11:12 -0800638/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300639 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
640 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
641 */
642struct nested_vmx {
643 /* Has the level1 guest done vmxon? */
644 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400645 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400646 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300647
648 /* The guest-physical address of the current VMCS L1 keeps for L2 */
649 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700650 /*
651 * Cache of the guest's VMCS, existing outside of guest memory.
652 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700653 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700654 */
655 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300656 /*
Liran Alon61ada742018-06-23 02:35:08 +0300657 * Cache of the guest's shadow VMCS, existing outside of guest
658 * memory. Loaded from guest memory during VM entry. Flushed
659 * to guest memory during VM exit.
660 */
661 struct vmcs12 *cached_shadow_vmcs12;
662 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300663 * Indicates if the shadow vmcs must be updated with the
664 * data hold by vmcs12
665 */
666 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100667 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300668
Jim Mattson8d860bb2018-05-09 16:56:05 -0400669 bool change_vmcs01_virtual_apic_mode;
670
Nadav Har'El644d7112011-05-25 23:12:35 +0300671 /* L2 must run next, and mustn't decide to exit to L1. */
672 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600673
674 struct loaded_vmcs vmcs02;
675
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300676 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600677 * Guest pages referred to in the vmcs02 with host-physical
678 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300679 */
680 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800681 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800682 struct page *pi_desc_page;
683 struct pi_desc *pi_desc;
684 bool pi_pending;
685 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100686
687 struct hrtimer preemption_timer;
688 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200689
690 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
691 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800692
Wanpeng Li5c614b32015-10-13 09:18:36 -0700693 u16 vpid02;
694 u16 last_vpid;
695
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100696 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200697
698 /* SMM related state */
699 struct {
700 /* in VMX operation on SMM entry? */
701 bool vmxon;
702 /* in guest mode on SMM entry? */
703 bool guest_mode;
704 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300705};
706
Yang Zhang01e439b2013-04-11 19:25:12 +0800707#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800708#define POSTED_INTR_SN 1
709
Yang Zhang01e439b2013-04-11 19:25:12 +0800710/* Posted-Interrupt Descriptor */
711struct pi_desc {
712 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800713 union {
714 struct {
715 /* bit 256 - Outstanding Notification */
716 u16 on : 1,
717 /* bit 257 - Suppress Notification */
718 sn : 1,
719 /* bit 271:258 - Reserved */
720 rsvd_1 : 14;
721 /* bit 279:272 - Notification Vector */
722 u8 nv;
723 /* bit 287:280 - Reserved */
724 u8 rsvd_2;
725 /* bit 319:288 - Notification Destination */
726 u32 ndst;
727 };
728 u64 control;
729 };
730 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800731} __aligned(64);
732
Yang Zhanga20ed542013-04-11 19:25:15 +0800733static bool pi_test_and_set_on(struct pi_desc *pi_desc)
734{
735 return test_and_set_bit(POSTED_INTR_ON,
736 (unsigned long *)&pi_desc->control);
737}
738
739static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
740{
741 return test_and_clear_bit(POSTED_INTR_ON,
742 (unsigned long *)&pi_desc->control);
743}
744
745static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
746{
747 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
748}
749
Feng Wuebbfc762015-09-18 22:29:46 +0800750static inline void pi_clear_sn(struct pi_desc *pi_desc)
751{
752 return clear_bit(POSTED_INTR_SN,
753 (unsigned long *)&pi_desc->control);
754}
755
756static inline void pi_set_sn(struct pi_desc *pi_desc)
757{
758 return set_bit(POSTED_INTR_SN,
759 (unsigned long *)&pi_desc->control);
760}
761
Paolo Bonziniad361092016-09-20 16:15:05 +0200762static inline void pi_clear_on(struct pi_desc *pi_desc)
763{
764 clear_bit(POSTED_INTR_ON,
765 (unsigned long *)&pi_desc->control);
766}
767
Feng Wuebbfc762015-09-18 22:29:46 +0800768static inline int pi_test_on(struct pi_desc *pi_desc)
769{
770 return test_bit(POSTED_INTR_ON,
771 (unsigned long *)&pi_desc->control);
772}
773
774static inline int pi_test_sn(struct pi_desc *pi_desc)
775{
776 return test_bit(POSTED_INTR_SN,
777 (unsigned long *)&pi_desc->control);
778}
779
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300782 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300783 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100784 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300785 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200786 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200787 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300788 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400789 int nmsrs;
790 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800791 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300793 u64 msr_host_kernel_gs_base;
794 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100796
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100797 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100798 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100799
Gleb Natapov2961e8762013-11-25 15:37:13 +0200800 u32 vm_entry_controls_shadow;
801 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200802 u32 secondary_exec_control;
803
Nadav Har'Eld462b812011-05-24 15:26:10 +0300804 /*
805 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
806 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700807 * guest (L2), it points to a different VMCS. loaded_cpu_state points
808 * to the VMCS whose state is loaded into the CPU registers that only
809 * need to be switched when transitioning to/from the kernel; a NULL
810 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300811 */
812 struct loaded_vmcs vmcs01;
813 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700814 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300815 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300816 struct msr_autoload {
817 unsigned nr;
818 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
819 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
820 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700821
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400822 struct {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400823 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300824#ifdef CONFIG_X86_64
825 u16 ds_sel, es_sel;
826#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200827 int gs_ldt_reload_needed;
828 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400829 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200830 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300831 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300832 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300833 struct kvm_segment segs[8];
834 } rmode;
835 struct {
836 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300837 struct kvm_save_segment {
838 u16 selector;
839 unsigned long base;
840 u32 limit;
841 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300842 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300843 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800844 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300845 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200846
Andi Kleena0861c02009-06-08 17:37:09 +0800847 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800848
Yang Zhang01e439b2013-04-11 19:25:12 +0800849 /* Posted interrupt descriptor */
850 struct pi_desc pi_desc;
851
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300852 /* Support for a guest hypervisor (nested VMX) */
853 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200854
855 /* Dynamic PLE window. */
856 int ple_window;
857 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800858
859 /* Support for PML */
860#define PML_ENTITY_NUM 512
861 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800862
Yunhong Jiang64672c92016-06-13 14:19:59 -0700863 /* apic deadline value in host tsc */
864 u64 hv_deadline_tsc;
865
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800866 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800867
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800868 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800869
Wanpeng Li74c55932017-11-29 01:31:20 -0800870 unsigned long host_debugctlmsr;
871
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800872 /*
873 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
874 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
875 * in msr_ia32_feature_control_valid_bits.
876 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800877 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800878 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +0000879 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400880};
881
Avi Kivity2fb92db2011-04-27 19:42:18 +0300882enum segment_cache_field {
883 SEG_FIELD_SEL = 0,
884 SEG_FIELD_BASE = 1,
885 SEG_FIELD_LIMIT = 2,
886 SEG_FIELD_AR = 3,
887
888 SEG_FIELD_NR = 4
889};
890
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700891static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
892{
893 return container_of(kvm, struct kvm_vmx, kvm);
894}
895
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400896static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
897{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000898 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899}
900
Feng Wuefc64402015-09-18 22:29:51 +0800901static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
902{
903 return &(to_vmx(vcpu)->pi_desc);
904}
905
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800906#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300907#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800908#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
909#define FIELD64(number, name) \
910 FIELD(number, name), \
911 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300912
Abel Gordon4607c2d2013-04-18 14:35:55 +0300913
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100914static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100915#define SHADOW_FIELD_RO(x) x,
916#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300917};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400918static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300919 ARRAY_SIZE(shadow_read_only_fields);
920
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100921static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100922#define SHADOW_FIELD_RW(x) x,
923#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300924};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400925static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300926 ARRAY_SIZE(shadow_read_write_fields);
927
Mathias Krause772e0312012-08-30 01:30:19 +0200928static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300929 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800930 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300931 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
932 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
933 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
934 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
935 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
936 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
937 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
938 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800939 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400940 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300941 FIELD(HOST_ES_SELECTOR, host_es_selector),
942 FIELD(HOST_CS_SELECTOR, host_cs_selector),
943 FIELD(HOST_SS_SELECTOR, host_ss_selector),
944 FIELD(HOST_DS_SELECTOR, host_ds_selector),
945 FIELD(HOST_FS_SELECTOR, host_fs_selector),
946 FIELD(HOST_GS_SELECTOR, host_gs_selector),
947 FIELD(HOST_TR_SELECTOR, host_tr_selector),
948 FIELD64(IO_BITMAP_A, io_bitmap_a),
949 FIELD64(IO_BITMAP_B, io_bitmap_b),
950 FIELD64(MSR_BITMAP, msr_bitmap),
951 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
952 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
953 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700954 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300955 FIELD64(TSC_OFFSET, tsc_offset),
956 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
957 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800958 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400959 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300960 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800961 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
962 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
963 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
964 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400965 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700966 FIELD64(VMREAD_BITMAP, vmread_bitmap),
967 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800968 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300969 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
970 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
971 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
972 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
973 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
974 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
975 FIELD64(GUEST_PDPTR0, guest_pdptr0),
976 FIELD64(GUEST_PDPTR1, guest_pdptr1),
977 FIELD64(GUEST_PDPTR2, guest_pdptr2),
978 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100979 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300980 FIELD64(HOST_IA32_PAT, host_ia32_pat),
981 FIELD64(HOST_IA32_EFER, host_ia32_efer),
982 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
983 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
984 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
985 FIELD(EXCEPTION_BITMAP, exception_bitmap),
986 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
987 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
988 FIELD(CR3_TARGET_COUNT, cr3_target_count),
989 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
990 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
991 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
992 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
993 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
994 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
995 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
996 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
997 FIELD(TPR_THRESHOLD, tpr_threshold),
998 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
999 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1000 FIELD(VM_EXIT_REASON, vm_exit_reason),
1001 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1002 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1003 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1004 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1005 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1006 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1007 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1008 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1009 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1010 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1011 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1012 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1013 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1014 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1015 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1016 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1017 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1018 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1019 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1020 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1021 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1022 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1023 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1024 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1025 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1026 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1027 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1028 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001029 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001030 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1031 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1032 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1033 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1034 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1035 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1036 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1037 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1038 FIELD(EXIT_QUALIFICATION, exit_qualification),
1039 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1040 FIELD(GUEST_CR0, guest_cr0),
1041 FIELD(GUEST_CR3, guest_cr3),
1042 FIELD(GUEST_CR4, guest_cr4),
1043 FIELD(GUEST_ES_BASE, guest_es_base),
1044 FIELD(GUEST_CS_BASE, guest_cs_base),
1045 FIELD(GUEST_SS_BASE, guest_ss_base),
1046 FIELD(GUEST_DS_BASE, guest_ds_base),
1047 FIELD(GUEST_FS_BASE, guest_fs_base),
1048 FIELD(GUEST_GS_BASE, guest_gs_base),
1049 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1050 FIELD(GUEST_TR_BASE, guest_tr_base),
1051 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1052 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1053 FIELD(GUEST_DR7, guest_dr7),
1054 FIELD(GUEST_RSP, guest_rsp),
1055 FIELD(GUEST_RIP, guest_rip),
1056 FIELD(GUEST_RFLAGS, guest_rflags),
1057 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1058 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1059 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1060 FIELD(HOST_CR0, host_cr0),
1061 FIELD(HOST_CR3, host_cr3),
1062 FIELD(HOST_CR4, host_cr4),
1063 FIELD(HOST_FS_BASE, host_fs_base),
1064 FIELD(HOST_GS_BASE, host_gs_base),
1065 FIELD(HOST_TR_BASE, host_tr_base),
1066 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1067 FIELD(HOST_IDTR_BASE, host_idtr_base),
1068 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1069 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1070 FIELD(HOST_RSP, host_rsp),
1071 FIELD(HOST_RIP, host_rip),
1072};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001073
1074static inline short vmcs_field_to_offset(unsigned long field)
1075{
Dan Williams085331d2018-01-31 17:47:03 -08001076 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1077 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001078 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001079
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001080 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001081 return -ENOENT;
1082
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001083 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001084 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001085 return -ENOENT;
1086
Linus Torvalds15303ba2018-02-10 13:16:35 -08001087 index = array_index_nospec(index, size);
1088 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001089 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001090 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001091 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001092}
1093
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001094static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1095{
David Matlack4f2777b2016-07-13 17:16:37 -07001096 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001097}
1098
Liran Alon61ada742018-06-23 02:35:08 +03001099static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1100{
1101 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1102}
1103
Peter Feiner995f00a2017-06-30 17:26:32 -07001104static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001105static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001106static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001107static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001108static void vmx_set_segment(struct kvm_vcpu *vcpu,
1109 struct kvm_segment *var, int seg);
1110static void vmx_get_segment(struct kvm_vcpu *vcpu,
1111 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001112static bool guest_state_valid(struct kvm_vcpu *vcpu);
1113static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001114static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001115static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1116static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1117static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1118 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001119static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001120static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1121 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001122
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1124static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001125/*
1126 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1127 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1128 */
1129static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130
Feng Wubf9f6ac2015-09-18 22:29:55 +08001131/*
1132 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1133 * can find which vCPU should be waken up.
1134 */
1135static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1136static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1137
Radim Krčmář23611332016-09-29 22:41:33 +02001138enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001139 VMX_VMREAD_BITMAP,
1140 VMX_VMWRITE_BITMAP,
1141 VMX_BITMAP_NR
1142};
1143
1144static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1145
Radim Krčmář23611332016-09-29 22:41:33 +02001146#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1147#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001148
Avi Kivity110312c2010-12-21 12:54:20 +02001149static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001150static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001151
Sheng Yang2384d2b2008-01-17 15:14:33 +08001152static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1153static DEFINE_SPINLOCK(vmx_vpid_lock);
1154
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001155static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 int size;
1157 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001158 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001160 u32 pin_based_exec_ctrl;
1161 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001162 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001163 u32 vmexit_ctrl;
1164 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001165 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001166} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167
Hannes Ederefff9e52008-11-28 17:02:06 +01001168static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001169 u32 ept;
1170 u32 vpid;
1171} vmx_capability;
1172
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173#define VMX_SEGMENT_FIELD(seg) \
1174 [VCPU_SREG_##seg] = { \
1175 .selector = GUEST_##seg##_SELECTOR, \
1176 .base = GUEST_##seg##_BASE, \
1177 .limit = GUEST_##seg##_LIMIT, \
1178 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1179 }
1180
Mathias Krause772e0312012-08-30 01:30:19 +02001181static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 unsigned selector;
1183 unsigned base;
1184 unsigned limit;
1185 unsigned ar_bytes;
1186} kvm_vmx_segment_fields[] = {
1187 VMX_SEGMENT_FIELD(CS),
1188 VMX_SEGMENT_FIELD(DS),
1189 VMX_SEGMENT_FIELD(ES),
1190 VMX_SEGMENT_FIELD(FS),
1191 VMX_SEGMENT_FIELD(GS),
1192 VMX_SEGMENT_FIELD(SS),
1193 VMX_SEGMENT_FIELD(TR),
1194 VMX_SEGMENT_FIELD(LDTR),
1195};
1196
Avi Kivity26bb0982009-09-07 11:14:12 +03001197static u64 host_efer;
1198
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001199static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1200
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001201/*
Brian Gerst8c065852010-07-17 09:03:26 -04001202 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001203 * away by decrementing the array size.
1204 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001206#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001207 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001209 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001212DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1213
1214#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1215
1216#define KVM_EVMCS_VERSION 1
1217
1218#if IS_ENABLED(CONFIG_HYPERV)
1219static bool __read_mostly enlightened_vmcs = true;
1220module_param(enlightened_vmcs, bool, 0444);
1221
1222static inline void evmcs_write64(unsigned long field, u64 value)
1223{
1224 u16 clean_field;
1225 int offset = get_evmcs_offset(field, &clean_field);
1226
1227 if (offset < 0)
1228 return;
1229
1230 *(u64 *)((char *)current_evmcs + offset) = value;
1231
1232 current_evmcs->hv_clean_fields &= ~clean_field;
1233}
1234
1235static inline void evmcs_write32(unsigned long field, u32 value)
1236{
1237 u16 clean_field;
1238 int offset = get_evmcs_offset(field, &clean_field);
1239
1240 if (offset < 0)
1241 return;
1242
1243 *(u32 *)((char *)current_evmcs + offset) = value;
1244 current_evmcs->hv_clean_fields &= ~clean_field;
1245}
1246
1247static inline void evmcs_write16(unsigned long field, u16 value)
1248{
1249 u16 clean_field;
1250 int offset = get_evmcs_offset(field, &clean_field);
1251
1252 if (offset < 0)
1253 return;
1254
1255 *(u16 *)((char *)current_evmcs + offset) = value;
1256 current_evmcs->hv_clean_fields &= ~clean_field;
1257}
1258
1259static inline u64 evmcs_read64(unsigned long field)
1260{
1261 int offset = get_evmcs_offset(field, NULL);
1262
1263 if (offset < 0)
1264 return 0;
1265
1266 return *(u64 *)((char *)current_evmcs + offset);
1267}
1268
1269static inline u32 evmcs_read32(unsigned long field)
1270{
1271 int offset = get_evmcs_offset(field, NULL);
1272
1273 if (offset < 0)
1274 return 0;
1275
1276 return *(u32 *)((char *)current_evmcs + offset);
1277}
1278
1279static inline u16 evmcs_read16(unsigned long field)
1280{
1281 int offset = get_evmcs_offset(field, NULL);
1282
1283 if (offset < 0)
1284 return 0;
1285
1286 return *(u16 *)((char *)current_evmcs + offset);
1287}
1288
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001289static inline void evmcs_touch_msr_bitmap(void)
1290{
1291 if (unlikely(!current_evmcs))
1292 return;
1293
1294 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1295 current_evmcs->hv_clean_fields &=
1296 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1297}
1298
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001299static void evmcs_load(u64 phys_addr)
1300{
1301 struct hv_vp_assist_page *vp_ap =
1302 hv_get_vp_assist_page(smp_processor_id());
1303
1304 vp_ap->current_nested_vmcs = phys_addr;
1305 vp_ap->enlighten_vmentry = 1;
1306}
1307
1308static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1309{
1310 /*
1311 * Enlightened VMCSv1 doesn't support these:
1312 *
1313 * POSTED_INTR_NV = 0x00000002,
1314 * GUEST_INTR_STATUS = 0x00000810,
1315 * APIC_ACCESS_ADDR = 0x00002014,
1316 * POSTED_INTR_DESC_ADDR = 0x00002016,
1317 * EOI_EXIT_BITMAP0 = 0x0000201c,
1318 * EOI_EXIT_BITMAP1 = 0x0000201e,
1319 * EOI_EXIT_BITMAP2 = 0x00002020,
1320 * EOI_EXIT_BITMAP3 = 0x00002022,
1321 */
1322 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1324 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1325 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1326 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1327 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1328 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1329
1330 /*
1331 * GUEST_PML_INDEX = 0x00000812,
1332 * PML_ADDRESS = 0x0000200e,
1333 */
1334 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1335
1336 /* VM_FUNCTION_CONTROL = 0x00002018, */
1337 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1338
1339 /*
1340 * EPTP_LIST_ADDRESS = 0x00002024,
1341 * VMREAD_BITMAP = 0x00002026,
1342 * VMWRITE_BITMAP = 0x00002028,
1343 */
1344 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1345
1346 /*
1347 * TSC_MULTIPLIER = 0x00002032,
1348 */
1349 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1350
1351 /*
1352 * PLE_GAP = 0x00004020,
1353 * PLE_WINDOW = 0x00004022,
1354 */
1355 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1356
1357 /*
1358 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1359 */
1360 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1361
1362 /*
1363 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1364 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1365 */
1366 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1367 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1368
1369 /*
1370 * Currently unsupported in KVM:
1371 * GUEST_IA32_RTIT_CTL = 0x00002814,
1372 */
1373}
Tianyu Lan877ad952018-07-19 08:40:23 +00001374
1375/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1376static void check_ept_pointer_match(struct kvm *kvm)
1377{
1378 struct kvm_vcpu *vcpu;
1379 u64 tmp_eptp = INVALID_PAGE;
1380 int i;
1381
1382 kvm_for_each_vcpu(i, vcpu, kvm) {
1383 if (!VALID_PAGE(tmp_eptp)) {
1384 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1385 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1386 to_kvm_vmx(kvm)->ept_pointers_match
1387 = EPT_POINTERS_MISMATCH;
1388 return;
1389 }
1390 }
1391
1392 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1393}
1394
1395static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1396{
1397 int ret;
1398
1399 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1400
1401 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1402 check_ept_pointer_match(kvm);
1403
1404 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1405 ret = -ENOTSUPP;
1406 goto out;
1407 }
1408
1409 ret = hyperv_flush_guest_mapping(
1410 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1411
1412out:
1413 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1414 return ret;
1415}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001416#else /* !IS_ENABLED(CONFIG_HYPERV) */
1417static inline void evmcs_write64(unsigned long field, u64 value) {}
1418static inline void evmcs_write32(unsigned long field, u32 value) {}
1419static inline void evmcs_write16(unsigned long field, u16 value) {}
1420static inline u64 evmcs_read64(unsigned long field) { return 0; }
1421static inline u32 evmcs_read32(unsigned long field) { return 0; }
1422static inline u16 evmcs_read16(unsigned long field) { return 0; }
1423static inline void evmcs_load(u64 phys_addr) {}
1424static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001425static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001426#endif /* IS_ENABLED(CONFIG_HYPERV) */
1427
Jan Kiszka5bb16012016-02-09 20:14:21 +01001428static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429{
1430 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1431 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001432 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1433}
1434
Jan Kiszka6f054852016-02-09 20:15:18 +01001435static inline bool is_debug(u32 intr_info)
1436{
1437 return is_exception_n(intr_info, DB_VECTOR);
1438}
1439
1440static inline bool is_breakpoint(u32 intr_info)
1441{
1442 return is_exception_n(intr_info, BP_VECTOR);
1443}
1444
Jan Kiszka5bb16012016-02-09 20:14:21 +01001445static inline bool is_page_fault(u32 intr_info)
1446{
1447 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448}
1449
Gui Jianfeng31299942010-03-15 17:29:09 +08001450static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001451{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001452 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001453}
1454
Gui Jianfeng31299942010-03-15 17:29:09 +08001455static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001456{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001457 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001458}
1459
Liran Alon9e869482018-03-12 13:12:51 +02001460static inline bool is_gp_fault(u32 intr_info)
1461{
1462 return is_exception_n(intr_info, GP_VECTOR);
1463}
1464
Gui Jianfeng31299942010-03-15 17:29:09 +08001465static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466{
1467 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1468 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1469}
1470
Gui Jianfeng31299942010-03-15 17:29:09 +08001471static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001472{
1473 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1474 INTR_INFO_VALID_MASK)) ==
1475 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1476}
1477
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001478/* Undocumented: icebp/int1 */
1479static inline bool is_icebp(u32 intr_info)
1480{
1481 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1482 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1483}
1484
Gui Jianfeng31299942010-03-15 17:29:09 +08001485static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001486{
Sheng Yang04547152009-04-01 15:52:31 +08001487 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001488}
1489
Gui Jianfeng31299942010-03-15 17:29:09 +08001490static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001491{
Sheng Yang04547152009-04-01 15:52:31 +08001492 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001493}
1494
Paolo Bonzini35754c92015-07-29 12:05:37 +02001495static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001496{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001497 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001498}
1499
Gui Jianfeng31299942010-03-15 17:29:09 +08001500static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001501{
Sheng Yang04547152009-04-01 15:52:31 +08001502 return vmcs_config.cpu_based_exec_ctrl &
1503 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001504}
1505
Avi Kivity774ead32007-12-26 13:57:04 +02001506static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001507{
Sheng Yang04547152009-04-01 15:52:31 +08001508 return vmcs_config.cpu_based_2nd_exec_ctrl &
1509 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1510}
1511
Yang Zhang8d146952013-01-25 10:18:50 +08001512static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1513{
1514 return vmcs_config.cpu_based_2nd_exec_ctrl &
1515 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1516}
1517
Yang Zhang83d4c282013-01-25 10:18:49 +08001518static inline bool cpu_has_vmx_apic_register_virt(void)
1519{
1520 return vmcs_config.cpu_based_2nd_exec_ctrl &
1521 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1522}
1523
Yang Zhangc7c9c562013-01-25 10:18:51 +08001524static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1525{
1526 return vmcs_config.cpu_based_2nd_exec_ctrl &
1527 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1528}
1529
Yunhong Jiang64672c92016-06-13 14:19:59 -07001530/*
1531 * Comment's format: document - errata name - stepping - processor name.
1532 * Refer from
1533 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1534 */
1535static u32 vmx_preemption_cpu_tfms[] = {
1536/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15370x000206E6,
1538/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1539/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1540/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15410x00020652,
1542/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15430x00020655,
1544/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1545/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1546/*
1547 * 320767.pdf - AAP86 - B1 -
1548 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1549 */
15500x000106E5,
1551/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15520x000106A0,
1553/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15540x000106A1,
1555/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15560x000106A4,
1557 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1558 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1559 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15600x000106A5,
1561};
1562
1563static inline bool cpu_has_broken_vmx_preemption_timer(void)
1564{
1565 u32 eax = cpuid_eax(0x00000001), i;
1566
1567 /* Clear the reserved bits */
1568 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001569 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001570 if (eax == vmx_preemption_cpu_tfms[i])
1571 return true;
1572
1573 return false;
1574}
1575
1576static inline bool cpu_has_vmx_preemption_timer(void)
1577{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001578 return vmcs_config.pin_based_exec_ctrl &
1579 PIN_BASED_VMX_PREEMPTION_TIMER;
1580}
1581
Yang Zhang01e439b2013-04-11 19:25:12 +08001582static inline bool cpu_has_vmx_posted_intr(void)
1583{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001584 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1585 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001586}
1587
1588static inline bool cpu_has_vmx_apicv(void)
1589{
1590 return cpu_has_vmx_apic_register_virt() &&
1591 cpu_has_vmx_virtual_intr_delivery() &&
1592 cpu_has_vmx_posted_intr();
1593}
1594
Sheng Yang04547152009-04-01 15:52:31 +08001595static inline bool cpu_has_vmx_flexpriority(void)
1596{
1597 return cpu_has_vmx_tpr_shadow() &&
1598 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001599}
1600
Marcelo Tosattie7997942009-06-11 12:07:40 -03001601static inline bool cpu_has_vmx_ept_execute_only(void)
1602{
Gui Jianfeng31299942010-03-15 17:29:09 +08001603 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001604}
1605
Marcelo Tosattie7997942009-06-11 12:07:40 -03001606static inline bool cpu_has_vmx_ept_2m_page(void)
1607{
Gui Jianfeng31299942010-03-15 17:29:09 +08001608 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001609}
1610
Sheng Yang878403b2010-01-05 19:02:29 +08001611static inline bool cpu_has_vmx_ept_1g_page(void)
1612{
Gui Jianfeng31299942010-03-15 17:29:09 +08001613 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001614}
1615
Sheng Yang4bc9b982010-06-02 14:05:24 +08001616static inline bool cpu_has_vmx_ept_4levels(void)
1617{
1618 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1619}
1620
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001621static inline bool cpu_has_vmx_ept_mt_wb(void)
1622{
1623 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1624}
1625
Yu Zhang855feb62017-08-24 20:27:55 +08001626static inline bool cpu_has_vmx_ept_5levels(void)
1627{
1628 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1629}
1630
Xudong Hao83c3a332012-05-28 19:33:35 +08001631static inline bool cpu_has_vmx_ept_ad_bits(void)
1632{
1633 return vmx_capability.ept & VMX_EPT_AD_BIT;
1634}
1635
Gui Jianfeng31299942010-03-15 17:29:09 +08001636static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001637{
Gui Jianfeng31299942010-03-15 17:29:09 +08001638 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001639}
1640
Gui Jianfeng31299942010-03-15 17:29:09 +08001641static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001642{
Gui Jianfeng31299942010-03-15 17:29:09 +08001643 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001644}
1645
Liran Aloncd9a4912018-05-22 17:16:15 +03001646static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1647{
1648 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1649}
1650
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001651static inline bool cpu_has_vmx_invvpid_single(void)
1652{
1653 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1654}
1655
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001656static inline bool cpu_has_vmx_invvpid_global(void)
1657{
1658 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1659}
1660
Wanpeng Li08d839c2017-03-23 05:30:08 -07001661static inline bool cpu_has_vmx_invvpid(void)
1662{
1663 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1664}
1665
Gui Jianfeng31299942010-03-15 17:29:09 +08001666static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001667{
Sheng Yang04547152009-04-01 15:52:31 +08001668 return vmcs_config.cpu_based_2nd_exec_ctrl &
1669 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001670}
1671
Gui Jianfeng31299942010-03-15 17:29:09 +08001672static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001673{
1674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1676}
1677
Gui Jianfeng31299942010-03-15 17:29:09 +08001678static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1682}
1683
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001684static inline bool cpu_has_vmx_basic_inout(void)
1685{
1686 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1687}
1688
Paolo Bonzini35754c92015-07-29 12:05:37 +02001689static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001690{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001691 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001692}
1693
Gui Jianfeng31299942010-03-15 17:29:09 +08001694static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001695{
Sheng Yang04547152009-04-01 15:52:31 +08001696 return vmcs_config.cpu_based_2nd_exec_ctrl &
1697 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001698}
1699
Gui Jianfeng31299942010-03-15 17:29:09 +08001700static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001701{
1702 return vmcs_config.cpu_based_2nd_exec_ctrl &
1703 SECONDARY_EXEC_RDTSCP;
1704}
1705
Mao, Junjiead756a12012-07-02 01:18:48 +00001706static inline bool cpu_has_vmx_invpcid(void)
1707{
1708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_ENABLE_INVPCID;
1710}
1711
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001712static inline bool cpu_has_virtual_nmis(void)
1713{
1714 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1715}
1716
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001717static inline bool cpu_has_vmx_wbinvd_exit(void)
1718{
1719 return vmcs_config.cpu_based_2nd_exec_ctrl &
1720 SECONDARY_EXEC_WBINVD_EXITING;
1721}
1722
Abel Gordonabc4fc52013-04-18 14:35:25 +03001723static inline bool cpu_has_vmx_shadow_vmcs(void)
1724{
1725 u64 vmx_msr;
1726 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1727 /* check if the cpu supports writing r/o exit information fields */
1728 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1729 return false;
1730
1731 return vmcs_config.cpu_based_2nd_exec_ctrl &
1732 SECONDARY_EXEC_SHADOW_VMCS;
1733}
1734
Kai Huang843e4332015-01-28 10:54:28 +08001735static inline bool cpu_has_vmx_pml(void)
1736{
1737 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1738}
1739
Haozhong Zhang64903d62015-10-20 15:39:09 +08001740static inline bool cpu_has_vmx_tsc_scaling(void)
1741{
1742 return vmcs_config.cpu_based_2nd_exec_ctrl &
1743 SECONDARY_EXEC_TSC_SCALING;
1744}
1745
Bandan Das2a499e42017-08-03 15:54:41 -04001746static inline bool cpu_has_vmx_vmfunc(void)
1747{
1748 return vmcs_config.cpu_based_2nd_exec_ctrl &
1749 SECONDARY_EXEC_ENABLE_VMFUNC;
1750}
1751
Sean Christopherson64f7a112018-04-30 10:01:06 -07001752static bool vmx_umip_emulated(void)
1753{
1754 return vmcs_config.cpu_based_2nd_exec_ctrl &
1755 SECONDARY_EXEC_DESC;
1756}
1757
Sheng Yang04547152009-04-01 15:52:31 +08001758static inline bool report_flexpriority(void)
1759{
1760 return flexpriority_enabled;
1761}
1762
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001763static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1764{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001765 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001766}
1767
Jim Mattsonf4160e42018-05-29 09:11:33 -07001768/*
1769 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1770 * to modify any valid field of the VMCS, or are the VM-exit
1771 * information fields read-only?
1772 */
1773static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1774{
1775 return to_vmx(vcpu)->nested.msrs.misc_low &
1776 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1777}
1778
Marc Orr04473782018-06-20 17:21:29 -07001779static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1780{
1781 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1782}
1783
1784static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1785{
1786 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1787 CPU_BASED_MONITOR_TRAP_FLAG;
1788}
1789
Liran Alonfa97d7d2018-07-18 14:07:59 +02001790static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1791{
1792 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1793 SECONDARY_EXEC_SHADOW_VMCS;
1794}
1795
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001796static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1797{
1798 return vmcs12->cpu_based_vm_exec_control & bit;
1799}
1800
1801static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1802{
1803 return (vmcs12->cpu_based_vm_exec_control &
1804 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1805 (vmcs12->secondary_vm_exec_control & bit);
1806}
1807
Jan Kiszkaf41245002014-03-07 20:03:13 +01001808static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1809{
1810 return vmcs12->pin_based_vm_exec_control &
1811 PIN_BASED_VMX_PREEMPTION_TIMER;
1812}
1813
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001814static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1815{
1816 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1817}
1818
1819static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1820{
1821 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1822}
1823
Nadav Har'El155a97a2013-08-05 11:07:16 +03001824static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1825{
1826 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1827}
1828
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001829static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1830{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001831 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001832}
1833
Bandan Dasc5f983f2017-05-05 15:25:14 -04001834static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1835{
1836 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1837}
1838
Wincy Vanf2b93282015-02-03 23:56:03 +08001839static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1840{
1841 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1842}
1843
Wanpeng Li5c614b32015-10-13 09:18:36 -07001844static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1845{
1846 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1847}
1848
Wincy Van82f0dd42015-02-03 23:57:18 +08001849static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1850{
1851 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1852}
1853
Wincy Van608406e2015-02-03 23:57:51 +08001854static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1855{
1856 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1857}
1858
Wincy Van705699a2015-02-03 23:58:17 +08001859static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1860{
1861 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1862}
1863
Bandan Das27c42a12017-08-03 15:54:42 -04001864static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1865{
1866 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1867}
1868
Bandan Das41ab9372017-08-03 15:54:43 -04001869static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1870{
1871 return nested_cpu_has_vmfunc(vmcs12) &&
1872 (vmcs12->vm_function_control &
1873 VMX_VMFUNC_EPTP_SWITCHING);
1874}
1875
Liran Alonf792d272018-06-23 02:35:05 +03001876static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1877{
1878 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1879}
1880
Jim Mattsonef85b672016-12-12 11:01:37 -08001881static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001882{
1883 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001884 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001885}
1886
Jan Kiszka533558b2014-01-04 18:47:20 +01001887static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1888 u32 exit_intr_info,
1889 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001890static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1891 struct vmcs12 *vmcs12,
1892 u32 reason, unsigned long qualification);
1893
Rusty Russell8b9cf982007-07-30 16:31:43 +10001894static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001895{
1896 int i;
1897
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001898 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001899 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001900 return i;
1901 return -1;
1902}
1903
Sheng Yang2384d2b2008-01-17 15:14:33 +08001904static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1905{
1906 struct {
1907 u64 vpid : 16;
1908 u64 rsvd : 48;
1909 u64 gva;
1910 } operand = { vpid, 0, gva };
1911
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001912 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001913 /* CF==1 or ZF==1 --> rc = -1 */
1914 "; ja 1f ; ud2 ; 1:"
1915 : : "a"(&operand), "c"(ext) : "cc", "memory");
1916}
1917
Sheng Yang14394422008-04-28 12:24:45 +08001918static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1919{
1920 struct {
1921 u64 eptp, gpa;
1922 } operand = {eptp, gpa};
1923
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001924 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001925 /* CF==1 or ZF==1 --> rc = -1 */
1926 "; ja 1f ; ud2 ; 1:\n"
1927 : : "a" (&operand), "c" (ext) : "cc", "memory");
1928}
1929
Avi Kivity26bb0982009-09-07 11:14:12 +03001930static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001931{
1932 int i;
1933
Rusty Russell8b9cf982007-07-30 16:31:43 +10001934 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001935 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001936 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001937 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001938}
1939
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940static void vmcs_clear(struct vmcs *vmcs)
1941{
1942 u64 phys_addr = __pa(vmcs);
1943 u8 error;
1944
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001945 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001946 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 : "cc", "memory");
1948 if (error)
1949 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1950 vmcs, phys_addr);
1951}
1952
Nadav Har'Eld462b812011-05-24 15:26:10 +03001953static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1954{
1955 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001956 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1957 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001958 loaded_vmcs->cpu = -1;
1959 loaded_vmcs->launched = 0;
1960}
1961
Dongxiao Xu7725b892010-05-11 18:29:38 +08001962static void vmcs_load(struct vmcs *vmcs)
1963{
1964 u64 phys_addr = __pa(vmcs);
1965 u8 error;
1966
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001967 if (static_branch_unlikely(&enable_evmcs))
1968 return evmcs_load(phys_addr);
1969
Dongxiao Xu7725b892010-05-11 18:29:38 +08001970 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001971 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001972 : "cc", "memory");
1973 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001974 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001975 vmcs, phys_addr);
1976}
1977
Dave Young2965faa2015-09-09 15:38:55 -07001978#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001979/*
1980 * This bitmap is used to indicate whether the vmclear
1981 * operation is enabled on all cpus. All disabled by
1982 * default.
1983 */
1984static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1985
1986static inline void crash_enable_local_vmclear(int cpu)
1987{
1988 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1989}
1990
1991static inline void crash_disable_local_vmclear(int cpu)
1992{
1993 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1994}
1995
1996static inline int crash_local_vmclear_enabled(int cpu)
1997{
1998 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1999}
2000
2001static void crash_vmclear_local_loaded_vmcss(void)
2002{
2003 int cpu = raw_smp_processor_id();
2004 struct loaded_vmcs *v;
2005
2006 if (!crash_local_vmclear_enabled(cpu))
2007 return;
2008
2009 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2010 loaded_vmcss_on_cpu_link)
2011 vmcs_clear(v->vmcs);
2012}
2013#else
2014static inline void crash_enable_local_vmclear(int cpu) { }
2015static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002016#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002017
Nadav Har'Eld462b812011-05-24 15:26:10 +03002018static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002020 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002021 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022
Nadav Har'Eld462b812011-05-24 15:26:10 +03002023 if (loaded_vmcs->cpu != cpu)
2024 return; /* vcpu migration can race with cpu offline */
2025 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002027 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002028 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002029
2030 /*
2031 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2032 * is before setting loaded_vmcs->vcpu to -1 which is done in
2033 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2034 * then adds the vmcs into percpu list before it is deleted.
2035 */
2036 smp_wmb();
2037
Nadav Har'Eld462b812011-05-24 15:26:10 +03002038 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002039 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040}
2041
Nadav Har'Eld462b812011-05-24 15:26:10 +03002042static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002043{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002044 int cpu = loaded_vmcs->cpu;
2045
2046 if (cpu != -1)
2047 smp_call_function_single(cpu,
2048 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002049}
2050
Junaid Shahidfaff8752018-06-29 13:10:05 -07002051static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2052{
2053 if (vpid == 0)
2054 return true;
2055
2056 if (cpu_has_vmx_invvpid_individual_addr()) {
2057 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2058 return true;
2059 }
2060
2061 return false;
2062}
2063
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002064static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002065{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002066 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002067 return;
2068
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002069 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002070 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002071}
2072
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002073static inline void vpid_sync_vcpu_global(void)
2074{
2075 if (cpu_has_vmx_invvpid_global())
2076 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2077}
2078
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002079static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002080{
2081 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002082 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002083 else
2084 vpid_sync_vcpu_global();
2085}
2086
Sheng Yang14394422008-04-28 12:24:45 +08002087static inline void ept_sync_global(void)
2088{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002089 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002090}
2091
2092static inline void ept_sync_context(u64 eptp)
2093{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002094 if (cpu_has_vmx_invept_context())
2095 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2096 else
2097 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002098}
2099
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002100static __always_inline void vmcs_check16(unsigned long field)
2101{
2102 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2103 "16-bit accessor invalid for 64-bit field");
2104 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2105 "16-bit accessor invalid for 64-bit high field");
2106 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2107 "16-bit accessor invalid for 32-bit high field");
2108 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2109 "16-bit accessor invalid for natural width field");
2110}
2111
2112static __always_inline void vmcs_check32(unsigned long field)
2113{
2114 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2115 "32-bit accessor invalid for 16-bit field");
2116 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2117 "32-bit accessor invalid for natural width field");
2118}
2119
2120static __always_inline void vmcs_check64(unsigned long field)
2121{
2122 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2123 "64-bit accessor invalid for 16-bit field");
2124 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2125 "64-bit accessor invalid for 64-bit high field");
2126 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2127 "64-bit accessor invalid for 32-bit field");
2128 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2129 "64-bit accessor invalid for natural width field");
2130}
2131
2132static __always_inline void vmcs_checkl(unsigned long field)
2133{
2134 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2135 "Natural width accessor invalid for 16-bit field");
2136 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2137 "Natural width accessor invalid for 64-bit field");
2138 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2139 "Natural width accessor invalid for 64-bit high field");
2140 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2141 "Natural width accessor invalid for 32-bit field");
2142}
2143
2144static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145{
Avi Kivity5e520e62011-05-15 10:13:12 -04002146 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147
Avi Kivity5e520e62011-05-15 10:13:12 -04002148 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2149 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 return value;
2151}
2152
Avi Kivity96304212011-05-15 10:13:13 -04002153static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002156 if (static_branch_unlikely(&enable_evmcs))
2157 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002158 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159}
2160
Avi Kivity96304212011-05-15 10:13:13 -04002161static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002163 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002164 if (static_branch_unlikely(&enable_evmcs))
2165 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002166 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167}
2168
Avi Kivity96304212011-05-15 10:13:13 -04002169static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002171 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002172 if (static_branch_unlikely(&enable_evmcs))
2173 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002174#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002175 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178#endif
2179}
2180
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002181static __always_inline unsigned long vmcs_readl(unsigned long field)
2182{
2183 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002184 if (static_branch_unlikely(&enable_evmcs))
2185 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002186 return __vmcs_readl(field);
2187}
2188
Avi Kivitye52de1b2007-01-05 16:36:56 -08002189static noinline void vmwrite_error(unsigned long field, unsigned long value)
2190{
2191 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2192 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2193 dump_stack();
2194}
2195
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002196static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197{
2198 u8 error;
2199
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002200 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002201 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002202 if (unlikely(error))
2203 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204}
2205
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002206static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002208 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002209 if (static_branch_unlikely(&enable_evmcs))
2210 return evmcs_write16(field, value);
2211
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002212 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213}
2214
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002215static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002217 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002218 if (static_branch_unlikely(&enable_evmcs))
2219 return evmcs_write32(field, value);
2220
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002221 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222}
2223
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002224static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002226 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002227 if (static_branch_unlikely(&enable_evmcs))
2228 return evmcs_write64(field, value);
2229
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002230 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002231#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002233 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234#endif
2235}
2236
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002237static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002238{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002240 if (static_branch_unlikely(&enable_evmcs))
2241 return evmcs_write64(field, value);
2242
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002243 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002244}
2245
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002246static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002247{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002248 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2249 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002250 if (static_branch_unlikely(&enable_evmcs))
2251 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2252
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002253 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2254}
2255
2256static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2257{
2258 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2259 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002260 if (static_branch_unlikely(&enable_evmcs))
2261 return evmcs_write32(field, evmcs_read32(field) | mask);
2262
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002263 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002264}
2265
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002266static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2267{
2268 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2269}
2270
Gleb Natapov2961e8762013-11-25 15:37:13 +02002271static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2272{
2273 vmcs_write32(VM_ENTRY_CONTROLS, val);
2274 vmx->vm_entry_controls_shadow = val;
2275}
2276
2277static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2278{
2279 if (vmx->vm_entry_controls_shadow != val)
2280 vm_entry_controls_init(vmx, val);
2281}
2282
2283static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2284{
2285 return vmx->vm_entry_controls_shadow;
2286}
2287
2288
2289static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2290{
2291 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2292}
2293
2294static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2295{
2296 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2297}
2298
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002299static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2300{
2301 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2302}
2303
Gleb Natapov2961e8762013-11-25 15:37:13 +02002304static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2305{
2306 vmcs_write32(VM_EXIT_CONTROLS, val);
2307 vmx->vm_exit_controls_shadow = val;
2308}
2309
2310static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2311{
2312 if (vmx->vm_exit_controls_shadow != val)
2313 vm_exit_controls_init(vmx, val);
2314}
2315
2316static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2317{
2318 return vmx->vm_exit_controls_shadow;
2319}
2320
2321
2322static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2323{
2324 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2325}
2326
2327static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2328{
2329 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2330}
2331
Avi Kivity2fb92db2011-04-27 19:42:18 +03002332static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2333{
2334 vmx->segment_cache.bitmask = 0;
2335}
2336
2337static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2338 unsigned field)
2339{
2340 bool ret;
2341 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2342
2343 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2344 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2345 vmx->segment_cache.bitmask = 0;
2346 }
2347 ret = vmx->segment_cache.bitmask & mask;
2348 vmx->segment_cache.bitmask |= mask;
2349 return ret;
2350}
2351
2352static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2353{
2354 u16 *p = &vmx->segment_cache.seg[seg].selector;
2355
2356 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2357 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2358 return *p;
2359}
2360
2361static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2362{
2363 ulong *p = &vmx->segment_cache.seg[seg].base;
2364
2365 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2366 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2367 return *p;
2368}
2369
2370static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2371{
2372 u32 *p = &vmx->segment_cache.seg[seg].limit;
2373
2374 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2375 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2376 return *p;
2377}
2378
2379static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2380{
2381 u32 *p = &vmx->segment_cache.seg[seg].ar;
2382
2383 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2384 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2385 return *p;
2386}
2387
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002388static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2389{
2390 u32 eb;
2391
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002392 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002393 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002394 /*
2395 * Guest access to VMware backdoor ports could legitimately
2396 * trigger #GP because of TSS I/O permission bitmap.
2397 * We intercept those #GP and allow access to them anyway
2398 * as VMware does.
2399 */
2400 if (enable_vmware_backdoor)
2401 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002402 if ((vcpu->guest_debug &
2403 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2404 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2405 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002406 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002407 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002408 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002409 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002410
2411 /* When we are running a nested L2 guest and L1 specified for it a
2412 * certain exception bitmap, we must trap the same exceptions and pass
2413 * them to L1. When running L2, we will only handle the exceptions
2414 * specified above if L1 did not want them.
2415 */
2416 if (is_guest_mode(vcpu))
2417 eb |= get_vmcs12(vcpu)->exception_bitmap;
2418
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002419 vmcs_write32(EXCEPTION_BITMAP, eb);
2420}
2421
Ashok Raj15d45072018-02-01 22:59:43 +01002422/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002423 * Check if MSR is intercepted for currently loaded MSR bitmap.
2424 */
2425static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2426{
2427 unsigned long *msr_bitmap;
2428 int f = sizeof(unsigned long);
2429
2430 if (!cpu_has_vmx_msr_bitmap())
2431 return true;
2432
2433 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2434
2435 if (msr <= 0x1fff) {
2436 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2437 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2438 msr &= 0x1fff;
2439 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2440 }
2441
2442 return true;
2443}
2444
2445/*
Ashok Raj15d45072018-02-01 22:59:43 +01002446 * Check if MSR is intercepted for L01 MSR bitmap.
2447 */
2448static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2449{
2450 unsigned long *msr_bitmap;
2451 int f = sizeof(unsigned long);
2452
2453 if (!cpu_has_vmx_msr_bitmap())
2454 return true;
2455
2456 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2457
2458 if (msr <= 0x1fff) {
2459 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2460 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2461 msr &= 0x1fff;
2462 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2463 }
2464
2465 return true;
2466}
2467
Gleb Natapov2961e8762013-11-25 15:37:13 +02002468static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2469 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002470{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002471 vm_entry_controls_clearbit(vmx, entry);
2472 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002473}
2474
Avi Kivity61d2ef22010-04-28 16:40:38 +03002475static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2476{
2477 unsigned i;
2478 struct msr_autoload *m = &vmx->msr_autoload;
2479
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002480 switch (msr) {
2481 case MSR_EFER:
2482 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002483 clear_atomic_switch_msr_special(vmx,
2484 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002485 VM_EXIT_LOAD_IA32_EFER);
2486 return;
2487 }
2488 break;
2489 case MSR_CORE_PERF_GLOBAL_CTRL:
2490 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002491 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002492 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2493 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2494 return;
2495 }
2496 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002497 }
2498
Avi Kivity61d2ef22010-04-28 16:40:38 +03002499 for (i = 0; i < m->nr; ++i)
2500 if (m->guest[i].index == msr)
2501 break;
2502
2503 if (i == m->nr)
2504 return;
2505 --m->nr;
2506 m->guest[i] = m->guest[m->nr];
2507 m->host[i] = m->host[m->nr];
2508 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2509 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2510}
2511
Gleb Natapov2961e8762013-11-25 15:37:13 +02002512static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2513 unsigned long entry, unsigned long exit,
2514 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2515 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002516{
2517 vmcs_write64(guest_val_vmcs, guest_val);
2518 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002519 vm_entry_controls_setbit(vmx, entry);
2520 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002521}
2522
Avi Kivity61d2ef22010-04-28 16:40:38 +03002523static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2524 u64 guest_val, u64 host_val)
2525{
2526 unsigned i;
2527 struct msr_autoload *m = &vmx->msr_autoload;
2528
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002529 switch (msr) {
2530 case MSR_EFER:
2531 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002532 add_atomic_switch_msr_special(vmx,
2533 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002534 VM_EXIT_LOAD_IA32_EFER,
2535 GUEST_IA32_EFER,
2536 HOST_IA32_EFER,
2537 guest_val, host_val);
2538 return;
2539 }
2540 break;
2541 case MSR_CORE_PERF_GLOBAL_CTRL:
2542 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002543 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002544 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2545 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2546 GUEST_IA32_PERF_GLOBAL_CTRL,
2547 HOST_IA32_PERF_GLOBAL_CTRL,
2548 guest_val, host_val);
2549 return;
2550 }
2551 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002552 case MSR_IA32_PEBS_ENABLE:
2553 /* PEBS needs a quiescent period after being disabled (to write
2554 * a record). Disabling PEBS through VMX MSR swapping doesn't
2555 * provide that period, so a CPU could write host's record into
2556 * guest's memory.
2557 */
2558 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002559 }
2560
Avi Kivity61d2ef22010-04-28 16:40:38 +03002561 for (i = 0; i < m->nr; ++i)
2562 if (m->guest[i].index == msr)
2563 break;
2564
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002565 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002566 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002567 "Can't add msr %x\n", msr);
2568 return;
2569 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002570 ++m->nr;
2571 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2572 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2573 }
2574
2575 m->guest[i].index = msr;
2576 m->guest[i].value = guest_val;
2577 m->host[i].index = msr;
2578 m->host[i].value = host_val;
2579}
2580
Avi Kivity92c0d902009-10-29 11:00:16 +02002581static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002582{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002583 u64 guest_efer = vmx->vcpu.arch.efer;
2584 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002585
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002586 if (!enable_ept) {
2587 /*
2588 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2589 * host CPUID is more efficient than testing guest CPUID
2590 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2591 */
2592 if (boot_cpu_has(X86_FEATURE_SMEP))
2593 guest_efer |= EFER_NX;
2594 else if (!(guest_efer & EFER_NX))
2595 ignore_bits |= EFER_NX;
2596 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002597
Avi Kivity51c6cf62007-08-29 03:48:05 +03002598 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002599 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002600 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002601 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002602#ifdef CONFIG_X86_64
2603 ignore_bits |= EFER_LMA | EFER_LME;
2604 /* SCE is meaningful only in long mode on Intel */
2605 if (guest_efer & EFER_LMA)
2606 ignore_bits &= ~(u64)EFER_SCE;
2607#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002608
2609 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002610
2611 /*
2612 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2613 * On CPUs that support "load IA32_EFER", always switch EFER
2614 * atomically, since it's faster than switching it manually.
2615 */
2616 if (cpu_has_load_ia32_efer ||
2617 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002618 if (!(guest_efer & EFER_LMA))
2619 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002620 if (guest_efer != host_efer)
2621 add_atomic_switch_msr(vmx, MSR_EFER,
2622 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002623 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002624 } else {
2625 guest_efer &= ~ignore_bits;
2626 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002627
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002628 vmx->guest_msrs[efer_offset].data = guest_efer;
2629 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2630
2631 return true;
2632 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002633}
2634
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002635#ifdef CONFIG_X86_32
2636/*
2637 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2638 * VMCS rather than the segment table. KVM uses this helper to figure
2639 * out the current bases to poke them into the VMCS before entry.
2640 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002641static unsigned long segment_base(u16 selector)
2642{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002643 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002644 unsigned long v;
2645
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002646 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002647 return 0;
2648
Thomas Garnier45fc8752017-03-14 10:05:08 -07002649 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002650
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002651 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002652 u16 ldt_selector = kvm_read_ldt();
2653
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002654 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002655 return 0;
2656
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002657 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002658 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002659 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002660 return v;
2661}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002662#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002663
Avi Kivity04d2cc72007-09-10 18:10:54 +03002664static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002665{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002666 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002667#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002668 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002669#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002670 unsigned long fs_base, gs_base;
2671 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002672 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002673
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002674 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002675 return;
2676
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002677 vmx->loaded_cpu_state = vmx->loaded_vmcs;
2678
Avi Kivity33ed6322007-05-02 16:54:03 +03002679 /*
2680 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2681 * allow segment selectors with cpl > 0 or ti == 1.
2682 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002683 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002684 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002685
2686#ifdef CONFIG_X86_64
Sean Christophersone368b872018-07-23 12:32:41 -07002687 savesegment(ds, vmx->host_state.ds_sel);
2688 savesegment(es, vmx->host_state.es_sel);
2689
2690 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002691 if (likely(is_64bit_mm(current->mm))) {
2692 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002693 fs_sel = current->thread.fsindex;
2694 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002695 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002696 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002697 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002698 savesegment(fs, fs_sel);
2699 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002700 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002701 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002702 }
Sean Christophersone368b872018-07-23 12:32:41 -07002703
2704 if (is_long_mode(&vmx->vcpu))
2705 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2706#else
2707 savesegment(fs, fs_sel);
2708 savesegment(gs, gs_sel);
2709 fs_base = segment_base(fs_sel);
2710 gs_base = segment_base(gs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002711#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002712
2713 vmx->host_state.fs_sel = fs_sel;
2714 if (!(fs_sel & 7)) {
2715 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002716 vmx->host_state.fs_reload_needed = 0;
2717 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002718 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002719 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002720 }
Sean Christophersone368b872018-07-23 12:32:41 -07002721 vmx->host_state.gs_sel = gs_sel;
2722 if (!(gs_sel & 7))
2723 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002724 else {
2725 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002726 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002727 }
2728
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002729 vmcs_writel(HOST_FS_BASE, fs_base);
Sean Christophersone368b872018-07-23 12:32:41 -07002730 vmcs_writel(HOST_GS_BASE, gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002731
Avi Kivity26bb0982009-09-07 11:14:12 +03002732 for (i = 0; i < vmx->save_nmsrs; ++i)
2733 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002734 vmx->guest_msrs[i].data,
2735 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002736}
2737
Avi Kivitya9b21b62008-06-24 11:48:49 +03002738static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002739{
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002740 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002741 return;
2742
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002743 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
2744
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002745 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002746 vmx->loaded_cpu_state = NULL;
2747
Avi Kivityc8770e72010-11-11 12:37:26 +02002748#ifdef CONFIG_X86_64
2749 if (is_long_mode(&vmx->vcpu))
2750 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2751#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002752 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002753 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002754#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002755 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002756#else
2757 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002758#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002759 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002760 if (vmx->host_state.fs_reload_needed)
2761 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002762#ifdef CONFIG_X86_64
2763 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2764 loadsegment(ds, vmx->host_state.ds_sel);
2765 loadsegment(es, vmx->host_state.es_sel);
2766 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002767#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002768 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002769#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002770 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002771#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002772 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002773}
2774
Avi Kivitya9b21b62008-06-24 11:48:49 +03002775static void vmx_load_host_state(struct vcpu_vmx *vmx)
2776{
2777 preempt_disable();
2778 __vmx_load_host_state(vmx);
2779 preempt_enable();
2780}
2781
Feng Wu28b835d2015-09-18 22:29:54 +08002782static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2783{
2784 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2785 struct pi_desc old, new;
2786 unsigned int dest;
2787
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002788 /*
2789 * In case of hot-plug or hot-unplug, we may have to undo
2790 * vmx_vcpu_pi_put even if there is no assigned device. And we
2791 * always keep PI.NDST up to date for simplicity: it makes the
2792 * code easier, and CPU migration is not a fast path.
2793 */
2794 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002795 return;
2796
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002797 /*
2798 * First handle the simple case where no cmpxchg is necessary; just
2799 * allow posting non-urgent interrupts.
2800 *
2801 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2802 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2803 * expects the VCPU to be on the blocked_vcpu_list that matches
2804 * PI.NDST.
2805 */
2806 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2807 vcpu->cpu == cpu) {
2808 pi_clear_sn(pi_desc);
2809 return;
2810 }
2811
2812 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002813 do {
2814 old.control = new.control = pi_desc->control;
2815
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002816 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002817
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002818 if (x2apic_enabled())
2819 new.ndst = dest;
2820 else
2821 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002822
Feng Wu28b835d2015-09-18 22:29:54 +08002823 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002824 } while (cmpxchg64(&pi_desc->control, old.control,
2825 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002826}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002827
Peter Feinerc95ba922016-08-17 09:36:47 -07002828static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2829{
2830 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2831 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2832}
2833
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834/*
2835 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2836 * vcpu mutex is already taken.
2837 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002838static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002841 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002843 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002844 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002845 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002846 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002847
2848 /*
2849 * Read loaded_vmcs->cpu should be before fetching
2850 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2851 * See the comments in __loaded_vmcs_clear().
2852 */
2853 smp_rmb();
2854
Nadav Har'Eld462b812011-05-24 15:26:10 +03002855 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2856 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002857 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002858 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002859 }
2860
2861 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2862 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2863 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002864 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002865 }
2866
2867 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002868 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002869 unsigned long sysenter_esp;
2870
2871 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002872
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 /*
2874 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002875 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002877 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002878 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002879 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002881 /*
2882 * VM exits change the host TR limit to 0x67 after a VM
2883 * exit. This is okay, since 0x67 covers everything except
2884 * the IO bitmap and have have code to handle the IO bitmap
2885 * being lost after a VM exit.
2886 */
2887 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2888
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2890 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002891
Nadav Har'Eld462b812011-05-24 15:26:10 +03002892 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 }
Feng Wu28b835d2015-09-18 22:29:54 +08002894
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002895 /* Setup TSC multiplier */
2896 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002897 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2898 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002899
Feng Wu28b835d2015-09-18 22:29:54 +08002900 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002901 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002902 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002903}
2904
2905static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2906{
2907 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2908
2909 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002910 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2911 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002912 return;
2913
2914 /* Set SN when the vCPU is preempted */
2915 if (vcpu->preempted)
2916 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
2919static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2920{
Feng Wu28b835d2015-09-18 22:29:54 +08002921 vmx_vcpu_pi_put(vcpu);
2922
Avi Kivitya9b21b62008-06-24 11:48:49 +03002923 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924}
2925
Wanpeng Lif244dee2017-07-20 01:11:54 -07002926static bool emulation_required(struct kvm_vcpu *vcpu)
2927{
2928 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2929}
2930
Avi Kivityedcafe32009-12-30 18:07:40 +02002931static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2932
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002933/*
2934 * Return the cr0 value that a nested guest would read. This is a combination
2935 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2936 * its hypervisor (cr0_read_shadow).
2937 */
2938static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2939{
2940 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2941 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2942}
2943static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2944{
2945 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2946 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2947}
2948
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2950{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002951 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002952
Avi Kivity6de12732011-03-07 12:51:22 +02002953 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2954 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2955 rflags = vmcs_readl(GUEST_RFLAGS);
2956 if (to_vmx(vcpu)->rmode.vm86_active) {
2957 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2958 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2959 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2960 }
2961 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002962 }
Avi Kivity6de12732011-03-07 12:51:22 +02002963 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964}
2965
2966static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2967{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002968 unsigned long old_rflags = vmx_get_rflags(vcpu);
2969
Avi Kivity6de12732011-03-07 12:51:22 +02002970 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2971 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002972 if (to_vmx(vcpu)->rmode.vm86_active) {
2973 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002974 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002975 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002977
2978 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2979 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980}
2981
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002982static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002983{
2984 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2985 int ret = 0;
2986
2987 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002988 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002989 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002990 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002991
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002992 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002993}
2994
2995static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2996{
2997 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2998 u32 interruptibility = interruptibility_old;
2999
3000 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3001
Jan Kiszka48005f62010-02-19 19:38:07 +01003002 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003003 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003004 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003005 interruptibility |= GUEST_INTR_STATE_STI;
3006
3007 if ((interruptibility != interruptibility_old))
3008 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3009}
3010
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3012{
3013 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003015 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003017 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018
Glauber Costa2809f5d2009-05-12 16:21:05 -04003019 /* skipping an emulated instruction also counts */
3020 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021}
3022
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003023static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3024 unsigned long exit_qual)
3025{
3026 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3027 unsigned int nr = vcpu->arch.exception.nr;
3028 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3029
3030 if (vcpu->arch.exception.has_error_code) {
3031 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3032 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3033 }
3034
3035 if (kvm_exception_is_soft(nr))
3036 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3037 else
3038 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3039
3040 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3041 vmx_get_nmi_mask(vcpu))
3042 intr_info |= INTR_INFO_UNBLOCK_NMI;
3043
3044 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3045}
3046
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003047/*
3048 * KVM wants to inject page-faults which it got to the guest. This function
3049 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003050 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003051static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003052{
3053 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003054 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003055
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003056 if (nr == PF_VECTOR) {
3057 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003058 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003059 return 1;
3060 }
3061 /*
3062 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3063 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3064 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3065 * can be written only when inject_pending_event runs. This should be
3066 * conditional on a new capability---if the capability is disabled,
3067 * kvm_multiple_exception would write the ancillary information to
3068 * CR2 or DR6, for backwards ABI-compatibility.
3069 */
3070 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3071 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003072 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003073 return 1;
3074 }
3075 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003076 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003077 if (nr == DB_VECTOR)
3078 *exit_qual = vcpu->arch.dr6;
3079 else
3080 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003081 return 1;
3082 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003083 }
3084
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003085 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003086}
3087
Wanpeng Licaa057a2018-03-12 04:53:03 -07003088static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3089{
3090 /*
3091 * Ensure that we clear the HLT state in the VMCS. We don't need to
3092 * explicitly skip the instruction because if the HLT state is set,
3093 * then the instruction is already executing and RIP has already been
3094 * advanced.
3095 */
3096 if (kvm_hlt_in_guest(vcpu->kvm) &&
3097 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3098 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3099}
3100
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003101static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003102{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003103 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003104 unsigned nr = vcpu->arch.exception.nr;
3105 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003106 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003107 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003108
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003109 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003110 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003111 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3112 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003113
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003114 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003115 int inc_eip = 0;
3116 if (kvm_exception_is_soft(nr))
3117 inc_eip = vcpu->arch.event_exit_inst_len;
3118 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003119 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003120 return;
3121 }
3122
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003123 WARN_ON_ONCE(vmx->emulation_required);
3124
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003125 if (kvm_exception_is_soft(nr)) {
3126 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3127 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003128 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3129 } else
3130 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3131
3132 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003133
3134 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003135}
3136
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003137static bool vmx_rdtscp_supported(void)
3138{
3139 return cpu_has_vmx_rdtscp();
3140}
3141
Mao, Junjiead756a12012-07-02 01:18:48 +00003142static bool vmx_invpcid_supported(void)
3143{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003144 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003145}
3146
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147/*
Eddie Donga75beee2007-05-17 18:55:15 +03003148 * Swap MSR entry in host/guest MSR entry array.
3149 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003150static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003151{
Avi Kivity26bb0982009-09-07 11:14:12 +03003152 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003153
3154 tmp = vmx->guest_msrs[to];
3155 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3156 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003157}
3158
3159/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003160 * Set up the vmcs to automatically save and restore system
3161 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3162 * mode, as fiddling with msrs is very expensive.
3163 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003164static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003165{
Avi Kivity26bb0982009-09-07 11:14:12 +03003166 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003167
Eddie Donga75beee2007-05-17 18:55:15 +03003168 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003169#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003170 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003171 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003172 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003173 move_msr_up(vmx, index, save_nmsrs++);
3174 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003175 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003176 move_msr_up(vmx, index, save_nmsrs++);
3177 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003178 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003179 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003180 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003181 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003182 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003183 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003184 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003185 * if efer.sce is enabled.
3186 */
Brian Gerst8c065852010-07-17 09:03:26 -04003187 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003188 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003189 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003190 }
Eddie Donga75beee2007-05-17 18:55:15 +03003191#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003192 index = __find_msr_index(vmx, MSR_EFER);
3193 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003194 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003195
Avi Kivity26bb0982009-09-07 11:14:12 +03003196 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003197
Yang Zhang8d146952013-01-25 10:18:50 +08003198 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003199 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003200}
3201
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003202static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003204 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003206 if (is_guest_mode(vcpu) &&
3207 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3208 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3209
3210 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211}
3212
3213/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003214 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003216static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003218 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003219 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003220 * We're here if L1 chose not to trap WRMSR to TSC. According
3221 * to the spec, this should set L1's TSC; The offset that L1
3222 * set for L2 remains unchanged, and still needs to be added
3223 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003224 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003225 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003226 /* recalculate vmcs02.TSC_OFFSET: */
3227 vmcs12 = get_vmcs12(vcpu);
3228 vmcs_write64(TSC_OFFSET, offset +
3229 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3230 vmcs12->tsc_offset : 0));
3231 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003232 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3233 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003234 vmcs_write64(TSC_OFFSET, offset);
3235 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236}
3237
Nadav Har'El801d3422011-05-25 23:02:23 +03003238/*
3239 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3240 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3241 * all guests if the "nested" module option is off, and can also be disabled
3242 * for a single guest by disabling its VMX cpuid bit.
3243 */
3244static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3245{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003246 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003247}
3248
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003250 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3251 * returned for the various VMX controls MSRs when nested VMX is enabled.
3252 * The same values should also be used to verify that vmcs12 control fields are
3253 * valid during nested entry from L1 to L2.
3254 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3255 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3256 * bit in the high half is on if the corresponding bit in the control field
3257 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003258 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003259static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003260{
Paolo Bonzini13893092018-02-26 13:40:09 +01003261 if (!nested) {
3262 memset(msrs, 0, sizeof(*msrs));
3263 return;
3264 }
3265
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003266 /*
3267 * Note that as a general rule, the high half of the MSRs (bits in
3268 * the control fields which may be 1) should be initialized by the
3269 * intersection of the underlying hardware's MSR (i.e., features which
3270 * can be supported) and the list of features we want to expose -
3271 * because they are known to be properly supported in our code.
3272 * Also, usually, the low half of the MSRs (bits which must be 1) can
3273 * be set to 0, meaning that L1 may turn off any of these bits. The
3274 * reason is that if one of these bits is necessary, it will appear
3275 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3276 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003277 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003278 * These rules have exceptions below.
3279 */
3280
3281 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003282 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003283 msrs->pinbased_ctls_low,
3284 msrs->pinbased_ctls_high);
3285 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003286 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003287 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003288 PIN_BASED_EXT_INTR_MASK |
3289 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003290 PIN_BASED_VIRTUAL_NMIS |
3291 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003293 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003294 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003295
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003296 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003297 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003298 msrs->exit_ctls_low,
3299 msrs->exit_ctls_high);
3300 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003301 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003302
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003304#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003305 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003306#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003307 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003308 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003309 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003310 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003311 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3312
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003313 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003315
Jan Kiszka2996fca2014-06-16 13:59:43 +02003316 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003317 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003318
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003319 /* entry controls */
3320 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msrs->entry_ctls_low,
3322 msrs->entry_ctls_high);
3323 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003324 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003326#ifdef CONFIG_X86_64
3327 VM_ENTRY_IA32E_MODE |
3328#endif
3329 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003330 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003331 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003332 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003334
Jan Kiszka2996fca2014-06-16 13:59:43 +02003335 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003336 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003337
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003338 /* cpu-based controls */
3339 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msrs->procbased_ctls_low,
3341 msrs->procbased_ctls_high);
3342 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003343 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003345 CPU_BASED_VIRTUAL_INTR_PENDING |
3346 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003347 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3348 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3349 CPU_BASED_CR3_STORE_EXITING |
3350#ifdef CONFIG_X86_64
3351 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3352#endif
3353 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003354 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3355 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3356 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3357 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003358 /*
3359 * We can allow some features even when not supported by the
3360 * hardware. For example, L1 can specify an MSR bitmap - and we
3361 * can use it to avoid exits to L1 - even when L0 runs L2
3362 * without MSR bitmaps.
3363 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003364 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003365 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003366 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003367
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003368 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003370 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3371
Paolo Bonzini80154d72017-08-24 13:55:35 +02003372 /*
3373 * secondary cpu-based controls. Do not include those that
3374 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3375 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003376 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003377 msrs->secondary_ctls_low,
3378 msrs->secondary_ctls_high);
3379 msrs->secondary_ctls_low = 0;
3380 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003381 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003382 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003383 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003384 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003385 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003386 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003387 /*
3388 * We can emulate "VMCS shadowing," even if the hardware
3389 * doesn't support it.
3390 */
3391 msrs->secondary_ctls_high |=
3392 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003393
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003394 if (enable_ept) {
3395 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003396 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003397 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003398 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003399 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003400 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003401 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003402 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003403 msrs->ept_caps &= vmx_capability.ept;
3404 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003405 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3406 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003407 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003408 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003409 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003410 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003411 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003412 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003413
Bandan Das27c42a12017-08-03 15:54:42 -04003414 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003415 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003416 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003417 /*
3418 * Advertise EPTP switching unconditionally
3419 * since we emulate it
3420 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003421 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003423 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003424 }
3425
Paolo Bonzinief697a72016-03-18 16:58:38 +01003426 /*
3427 * Old versions of KVM use the single-context version without
3428 * checking for support, so declare that it is supported even
3429 * though it is treated as global context. The alternative is
3430 * not failing the single-context invvpid, and it is worse.
3431 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003432 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003433 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003434 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003435 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003436 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003437 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003438
Radim Krčmář0790ec12015-03-17 14:02:32 +01003439 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003440 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003441 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3442
Jan Kiszkac18911a2013-03-13 16:06:41 +01003443 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003444 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003445 msrs->misc_low,
3446 msrs->misc_high);
3447 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3448 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003449 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003450 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003451 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003452 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003453
3454 /*
3455 * This MSR reports some information about VMX support. We
3456 * should return information about the VMX we emulate for the
3457 * guest, and the VMCS structure we give it - not about the
3458 * VMX support of the underlying hardware.
3459 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003460 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003461 VMCS12_REVISION |
3462 VMX_BASIC_TRUE_CTLS |
3463 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3464 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3465
3466 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003468
3469 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003470 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003471 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3472 * We picked the standard core2 setting.
3473 */
3474#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3475#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3477 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003478
3479 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003480 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3481 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003482
3483 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003484 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003485}
3486
David Matlack38991522016-11-29 18:14:08 -08003487/*
3488 * if fixed0[i] == 1: val[i] must be 1
3489 * if fixed1[i] == 0: val[i] must be 0
3490 */
3491static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3492{
3493 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003494}
3495
3496static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3497{
David Matlack38991522016-11-29 18:14:08 -08003498 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003499}
3500
3501static inline u64 vmx_control_msr(u32 low, u32 high)
3502{
3503 return low | ((u64)high << 32);
3504}
3505
David Matlack62cc6b9d2016-11-29 18:14:07 -08003506static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3507{
3508 superset &= mask;
3509 subset &= mask;
3510
3511 return (superset | subset) == superset;
3512}
3513
3514static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3515{
3516 const u64 feature_and_reserved =
3517 /* feature (except bit 48; see below) */
3518 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3519 /* reserved */
3520 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003521 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003522
3523 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3524 return -EINVAL;
3525
3526 /*
3527 * KVM does not emulate a version of VMX that constrains physical
3528 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3529 */
3530 if (data & BIT_ULL(48))
3531 return -EINVAL;
3532
3533 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3534 vmx_basic_vmcs_revision_id(data))
3535 return -EINVAL;
3536
3537 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3538 return -EINVAL;
3539
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003540 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003541 return 0;
3542}
3543
3544static int
3545vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3546{
3547 u64 supported;
3548 u32 *lowp, *highp;
3549
3550 switch (msr_index) {
3551 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003552 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3553 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003554 break;
3555 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 lowp = &vmx->nested.msrs.procbased_ctls_low;
3557 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003558 break;
3559 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003560 lowp = &vmx->nested.msrs.exit_ctls_low;
3561 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003562 break;
3563 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 lowp = &vmx->nested.msrs.entry_ctls_low;
3565 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003566 break;
3567 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003568 lowp = &vmx->nested.msrs.secondary_ctls_low;
3569 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003570 break;
3571 default:
3572 BUG();
3573 }
3574
3575 supported = vmx_control_msr(*lowp, *highp);
3576
3577 /* Check must-be-1 bits are still 1. */
3578 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3579 return -EINVAL;
3580
3581 /* Check must-be-0 bits are still 0. */
3582 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3583 return -EINVAL;
3584
3585 *lowp = data;
3586 *highp = data >> 32;
3587 return 0;
3588}
3589
3590static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3591{
3592 const u64 feature_and_reserved_bits =
3593 /* feature */
3594 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3595 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3596 /* reserved */
3597 GENMASK_ULL(13, 9) | BIT_ULL(31);
3598 u64 vmx_misc;
3599
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003600 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3601 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003602
3603 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3604 return -EINVAL;
3605
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003606 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003607 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3608 vmx_misc_preemption_timer_rate(data) !=
3609 vmx_misc_preemption_timer_rate(vmx_misc))
3610 return -EINVAL;
3611
3612 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3613 return -EINVAL;
3614
3615 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3616 return -EINVAL;
3617
3618 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3619 return -EINVAL;
3620
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 vmx->nested.msrs.misc_low = data;
3622 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003623
3624 /*
3625 * If L1 has read-only VM-exit information fields, use the
3626 * less permissive vmx_vmwrite_bitmap to specify write
3627 * permissions for the shadow VMCS.
3628 */
3629 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3630 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3631
David Matlack62cc6b9d2016-11-29 18:14:07 -08003632 return 0;
3633}
3634
3635static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3636{
3637 u64 vmx_ept_vpid_cap;
3638
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003639 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3640 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003641
3642 /* Every bit is either reserved or a feature bit. */
3643 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3644 return -EINVAL;
3645
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 vmx->nested.msrs.ept_caps = data;
3647 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003648 return 0;
3649}
3650
3651static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3652{
3653 u64 *msr;
3654
3655 switch (msr_index) {
3656 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003657 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003658 break;
3659 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003661 break;
3662 default:
3663 BUG();
3664 }
3665
3666 /*
3667 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3668 * must be 1 in the restored value.
3669 */
3670 if (!is_bitwise_subset(data, *msr, -1ULL))
3671 return -EINVAL;
3672
3673 *msr = data;
3674 return 0;
3675}
3676
3677/*
3678 * Called when userspace is restoring VMX MSRs.
3679 *
3680 * Returns 0 on success, non-0 otherwise.
3681 */
3682static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3683{
3684 struct vcpu_vmx *vmx = to_vmx(vcpu);
3685
Jim Mattsona943ac52018-05-29 09:11:32 -07003686 /*
3687 * Don't allow changes to the VMX capability MSRs while the vCPU
3688 * is in VMX operation.
3689 */
3690 if (vmx->nested.vmxon)
3691 return -EBUSY;
3692
David Matlack62cc6b9d2016-11-29 18:14:07 -08003693 switch (msr_index) {
3694 case MSR_IA32_VMX_BASIC:
3695 return vmx_restore_vmx_basic(vmx, data);
3696 case MSR_IA32_VMX_PINBASED_CTLS:
3697 case MSR_IA32_VMX_PROCBASED_CTLS:
3698 case MSR_IA32_VMX_EXIT_CTLS:
3699 case MSR_IA32_VMX_ENTRY_CTLS:
3700 /*
3701 * The "non-true" VMX capability MSRs are generated from the
3702 * "true" MSRs, so we do not support restoring them directly.
3703 *
3704 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3705 * should restore the "true" MSRs with the must-be-1 bits
3706 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3707 * DEFAULT SETTINGS".
3708 */
3709 return -EINVAL;
3710 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3711 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3712 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3713 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3714 case MSR_IA32_VMX_PROCBASED_CTLS2:
3715 return vmx_restore_control_msr(vmx, msr_index, data);
3716 case MSR_IA32_VMX_MISC:
3717 return vmx_restore_vmx_misc(vmx, data);
3718 case MSR_IA32_VMX_CR0_FIXED0:
3719 case MSR_IA32_VMX_CR4_FIXED0:
3720 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3721 case MSR_IA32_VMX_CR0_FIXED1:
3722 case MSR_IA32_VMX_CR4_FIXED1:
3723 /*
3724 * These MSRs are generated based on the vCPU's CPUID, so we
3725 * do not support restoring them directly.
3726 */
3727 return -EINVAL;
3728 case MSR_IA32_VMX_EPT_VPID_CAP:
3729 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3730 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003731 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003732 return 0;
3733 default:
3734 /*
3735 * The rest of the VMX capability MSRs do not support restore.
3736 */
3737 return -EINVAL;
3738 }
3739}
3740
Jan Kiszkacae50132014-01-04 18:47:22 +01003741/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003742static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003743{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003744 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003745 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003746 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003747 break;
3748 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3749 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003750 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003751 msrs->pinbased_ctls_low,
3752 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003753 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3754 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003755 break;
3756 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3757 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003758 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003759 msrs->procbased_ctls_low,
3760 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003761 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3762 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003763 break;
3764 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3765 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003766 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 msrs->exit_ctls_low,
3768 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003769 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3770 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003771 break;
3772 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3773 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003774 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 msrs->entry_ctls_low,
3776 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003777 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3778 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003781 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003782 msrs->misc_low,
3783 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003784 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003785 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003787 break;
3788 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003789 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003790 break;
3791 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003792 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003793 break;
3794 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003795 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003796 break;
3797 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003798 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003799 break;
3800 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003801 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 msrs->secondary_ctls_low,
3803 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003804 break;
3805 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 *pdata = msrs->ept_caps |
3807 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003808 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003809 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003811 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003813 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003814 }
3815
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003816 return 0;
3817}
3818
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003819static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3820 uint64_t val)
3821{
3822 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3823
3824 return !(val & ~valid_bits);
3825}
3826
Tom Lendacky801e4592018-02-21 13:39:51 -06003827static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3828{
Paolo Bonzini13893092018-02-26 13:40:09 +01003829 switch (msr->index) {
3830 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3831 if (!nested)
3832 return 1;
3833 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3834 default:
3835 return 1;
3836 }
3837
3838 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003839}
3840
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003841/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 * Reads an msr value (of 'msr_index') into 'pdata'.
3843 * Returns 0 on success, non-0 otherwise.
3844 * Assumes vcpu_load() was already called.
3845 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003846static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003849 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003851 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003852#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003854 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 break;
3856 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003857 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003859 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003860 vmx_load_host_state(vmx);
3861 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003862 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003863#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003865 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003866 case MSR_IA32_SPEC_CTRL:
3867 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003868 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3869 return 1;
3870
3871 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3872 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003873 case MSR_IA32_ARCH_CAPABILITIES:
3874 if (!msr_info->host_initiated &&
3875 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3876 return 1;
3877 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3878 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003880 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 break;
3882 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003883 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884 break;
3885 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003886 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003888 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003889 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003890 (!msr_info->host_initiated &&
3891 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003892 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003893 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003894 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003895 case MSR_IA32_MCG_EXT_CTL:
3896 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003897 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003898 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003899 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003900 msr_info->data = vcpu->arch.mcg_ext_ctl;
3901 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003902 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003903 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003904 break;
3905 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3906 if (!nested_vmx_allowed(vcpu))
3907 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003908 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3909 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003910 case MSR_IA32_XSS:
3911 if (!vmx_xsaves_supported())
3912 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003913 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003914 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003915 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003916 if (!msr_info->host_initiated &&
3917 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003918 return 1;
3919 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003921 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003922 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003923 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003924 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003926 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927 }
3928
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929 return 0;
3930}
3931
Jan Kiszkacae50132014-01-04 18:47:22 +01003932static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3933
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934/*
3935 * Writes msr value into into the appropriate "register".
3936 * Returns 0 on success, non-0 otherwise.
3937 * Assumes vcpu_load() was already called.
3938 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003939static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003942 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003943 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003944 u32 msr_index = msr_info->index;
3945 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003946
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003948 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003949 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003950 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003951#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003953 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954 vmcs_writel(GUEST_FS_BASE, data);
3955 break;
3956 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003957 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 vmcs_writel(GUEST_GS_BASE, data);
3959 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003960 case MSR_KERNEL_GS_BASE:
3961 vmx_load_host_state(vmx);
3962 vmx->msr_guest_kernel_gs_base = data;
3963 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964#endif
3965 case MSR_IA32_SYSENTER_CS:
3966 vmcs_write32(GUEST_SYSENTER_CS, data);
3967 break;
3968 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003969 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970 break;
3971 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003972 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003974 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003975 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003976 (!msr_info->host_initiated &&
3977 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003978 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003979 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003980 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003982 vmcs_write64(GUEST_BNDCFGS, data);
3983 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003984 case MSR_IA32_SPEC_CTRL:
3985 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003986 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3987 return 1;
3988
3989 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003990 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003991 return 1;
3992
3993 vmx->spec_ctrl = data;
3994
3995 if (!data)
3996 break;
3997
3998 /*
3999 * For non-nested:
4000 * When it's written (to non-zero) for the first time, pass
4001 * it through.
4002 *
4003 * For nested:
4004 * The handling of the MSR bitmap for L2 guests is done in
4005 * nested_vmx_merge_msr_bitmap. We should not touch the
4006 * vmcs02.msr_bitmap here since it gets completely overwritten
4007 * in the merging. We update the vmcs01 here for L1 as well
4008 * since it will end up touching the MSR anyway now.
4009 */
4010 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4011 MSR_IA32_SPEC_CTRL,
4012 MSR_TYPE_RW);
4013 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004014 case MSR_IA32_PRED_CMD:
4015 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004016 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4017 return 1;
4018
4019 if (data & ~PRED_CMD_IBPB)
4020 return 1;
4021
4022 if (!data)
4023 break;
4024
4025 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4026
4027 /*
4028 * For non-nested:
4029 * When it's written (to non-zero) for the first time, pass
4030 * it through.
4031 *
4032 * For nested:
4033 * The handling of the MSR bitmap for L2 guests is done in
4034 * nested_vmx_merge_msr_bitmap. We should not touch the
4035 * vmcs02.msr_bitmap here since it gets completely overwritten
4036 * in the merging.
4037 */
4038 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4039 MSR_TYPE_W);
4040 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004041 case MSR_IA32_ARCH_CAPABILITIES:
4042 if (!msr_info->host_initiated)
4043 return 1;
4044 vmx->arch_capabilities = data;
4045 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004046 case MSR_IA32_CR_PAT:
4047 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004048 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4049 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004050 vmcs_write64(GUEST_IA32_PAT, data);
4051 vcpu->arch.pat = data;
4052 break;
4053 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004054 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004055 break;
Will Auldba904632012-11-29 12:42:50 -08004056 case MSR_IA32_TSC_ADJUST:
4057 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004058 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004059 case MSR_IA32_MCG_EXT_CTL:
4060 if ((!msr_info->host_initiated &&
4061 !(to_vmx(vcpu)->msr_ia32_feature_control &
4062 FEATURE_CONTROL_LMCE)) ||
4063 (data & ~MCG_EXT_CTL_LMCE_EN))
4064 return 1;
4065 vcpu->arch.mcg_ext_ctl = data;
4066 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004067 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004068 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004069 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004070 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4071 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004072 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004073 if (msr_info->host_initiated && data == 0)
4074 vmx_leave_nested(vcpu);
4075 break;
4076 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004077 if (!msr_info->host_initiated)
4078 return 1; /* they are read-only */
4079 if (!nested_vmx_allowed(vcpu))
4080 return 1;
4081 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004082 case MSR_IA32_XSS:
4083 if (!vmx_xsaves_supported())
4084 return 1;
4085 /*
4086 * The only supported bit as of Skylake is bit 8, but
4087 * it is not supported on KVM.
4088 */
4089 if (data != 0)
4090 return 1;
4091 vcpu->arch.ia32_xss = data;
4092 if (vcpu->arch.ia32_xss != host_xss)
4093 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4094 vcpu->arch.ia32_xss, host_xss);
4095 else
4096 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4097 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004098 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004099 if (!msr_info->host_initiated &&
4100 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004101 return 1;
4102 /* Check reserved bit, higher 32 bits should be zero */
4103 if ((data >> 32) != 0)
4104 return 1;
4105 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004107 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004108 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004109 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004110 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004111 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4112 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004113 ret = kvm_set_shared_msr(msr->index, msr->data,
4114 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004115 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004116 if (ret)
4117 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004118 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004119 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004121 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122 }
4123
Eddie Dong2cc51562007-05-21 07:28:09 +03004124 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125}
4126
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004127static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004129 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4130 switch (reg) {
4131 case VCPU_REGS_RSP:
4132 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4133 break;
4134 case VCPU_REGS_RIP:
4135 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4136 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004137 case VCPU_EXREG_PDPTR:
4138 if (enable_ept)
4139 ept_save_pdptrs(vcpu);
4140 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004141 default:
4142 break;
4143 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144}
4145
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146static __init int cpu_has_kvm_support(void)
4147{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004148 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149}
4150
4151static __init int vmx_disabled_by_bios(void)
4152{
4153 u64 msr;
4154
4155 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004156 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004157 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004158 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4159 && tboot_enabled())
4160 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004161 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004162 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004163 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004164 && !tboot_enabled()) {
4165 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004166 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004167 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004168 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004169 /* launched w/o TXT and VMX disabled */
4170 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4171 && !tboot_enabled())
4172 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004173 }
4174
4175 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176}
4177
Dongxiao Xu7725b892010-05-11 18:29:38 +08004178static void kvm_cpu_vmxon(u64 addr)
4179{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004180 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004181 intel_pt_handle_vmx(1);
4182
Dongxiao Xu7725b892010-05-11 18:29:38 +08004183 asm volatile (ASM_VMX_VMXON_RAX
4184 : : "a"(&addr), "m"(addr)
4185 : "memory", "cc");
4186}
4187
Radim Krčmář13a34e02014-08-28 15:13:03 +02004188static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189{
4190 int cpu = raw_smp_processor_id();
4191 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004192 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004194 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004195 return -EBUSY;
4196
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004197 /*
4198 * This can happen if we hot-added a CPU but failed to allocate
4199 * VP assist page for it.
4200 */
4201 if (static_branch_unlikely(&enable_evmcs) &&
4202 !hv_get_vp_assist_page(cpu))
4203 return -EFAULT;
4204
Nadav Har'Eld462b812011-05-24 15:26:10 +03004205 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004206 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4207 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004208
4209 /*
4210 * Now we can enable the vmclear operation in kdump
4211 * since the loaded_vmcss_on_cpu list on this cpu
4212 * has been initialized.
4213 *
4214 * Though the cpu is not in VMX operation now, there
4215 * is no problem to enable the vmclear operation
4216 * for the loaded_vmcss_on_cpu list is empty!
4217 */
4218 crash_enable_local_vmclear(cpu);
4219
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004221
4222 test_bits = FEATURE_CONTROL_LOCKED;
4223 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4224 if (tboot_enabled())
4225 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4226
4227 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004229 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4230 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004231 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004232 if (enable_ept)
4233 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004234
4235 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004236}
4237
Nadav Har'Eld462b812011-05-24 15:26:10 +03004238static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004239{
4240 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004241 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004242
Nadav Har'Eld462b812011-05-24 15:26:10 +03004243 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4244 loaded_vmcss_on_cpu_link)
4245 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004246}
4247
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004248
4249/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4250 * tricks.
4251 */
4252static void kvm_cpu_vmxoff(void)
4253{
4254 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004255
4256 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004257 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004258}
4259
Radim Krčmář13a34e02014-08-28 15:13:03 +02004260static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004262 vmclear_local_loaded_vmcss();
4263 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004264}
4265
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004266static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004267 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268{
4269 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004270 u32 ctl = ctl_min | ctl_opt;
4271
4272 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4273
4274 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4275 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4276
4277 /* Ensure minimum (required) set of control bits are supported. */
4278 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004279 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004280
4281 *result = ctl;
4282 return 0;
4283}
4284
Avi Kivity110312c2010-12-21 12:54:20 +02004285static __init bool allow_1_setting(u32 msr, u32 ctl)
4286{
4287 u32 vmx_msr_low, vmx_msr_high;
4288
4289 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4290 return vmx_msr_high & ctl;
4291}
4292
Yang, Sheng002c7f72007-07-31 14:23:01 +03004293static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004294{
4295 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004296 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004297 u32 _pin_based_exec_control = 0;
4298 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004299 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004300 u32 _vmexit_control = 0;
4301 u32 _vmentry_control = 0;
4302
Paolo Bonzini13893092018-02-26 13:40:09 +01004303 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304304 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004305#ifdef CONFIG_X86_64
4306 CPU_BASED_CR8_LOAD_EXITING |
4307 CPU_BASED_CR8_STORE_EXITING |
4308#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004309 CPU_BASED_CR3_LOAD_EXITING |
4310 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004311 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004312 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004313 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004314 CPU_BASED_MWAIT_EXITING |
4315 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004316 CPU_BASED_INVLPG_EXITING |
4317 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004318
Sheng Yangf78e0e22007-10-29 09:40:42 +08004319 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004320 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004321 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004322 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4323 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004324 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004325#ifdef CONFIG_X86_64
4326 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4327 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4328 ~CPU_BASED_CR8_STORE_EXITING;
4329#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004330 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004331 min2 = 0;
4332 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004333 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004334 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004335 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004336 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004337 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004338 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004339 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004340 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004341 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004342 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004343 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004344 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004345 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004346 SECONDARY_EXEC_RDSEED_EXITING |
4347 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004348 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004349 SECONDARY_EXEC_TSC_SCALING |
4350 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004351 if (adjust_vmx_controls(min2, opt2,
4352 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004353 &_cpu_based_2nd_exec_control) < 0)
4354 return -EIO;
4355 }
4356#ifndef CONFIG_X86_64
4357 if (!(_cpu_based_2nd_exec_control &
4358 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4359 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4360#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004361
4362 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4363 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004364 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004365 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4366 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004367
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004368 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4369 &vmx_capability.ept, &vmx_capability.vpid);
4370
Sheng Yangd56f5462008-04-25 10:13:16 +08004371 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004372 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4373 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004374 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4375 CPU_BASED_CR3_STORE_EXITING |
4376 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004377 } else if (vmx_capability.ept) {
4378 vmx_capability.ept = 0;
4379 pr_warn_once("EPT CAP should not exist if not support "
4380 "1-setting enable EPT VM-execution control\n");
4381 }
4382 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4383 vmx_capability.vpid) {
4384 vmx_capability.vpid = 0;
4385 pr_warn_once("VPID CAP should not exist if not support "
4386 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004387 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004388
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004389 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004390#ifdef CONFIG_X86_64
4391 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4392#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004393 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004394 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004395 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4396 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004397 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004398
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004399 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4400 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4401 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004402 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4403 &_pin_based_exec_control) < 0)
4404 return -EIO;
4405
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004406 if (cpu_has_broken_vmx_preemption_timer())
4407 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004408 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004409 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004410 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4411
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004412 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004413 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004414 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4415 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004416 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004418 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004419
4420 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4421 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004422 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004423
4424#ifdef CONFIG_X86_64
4425 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4426 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004427 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004428#endif
4429
4430 /* Require Write-Back (WB) memory type for VMCS accesses. */
4431 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004432 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004433
Yang, Sheng002c7f72007-07-31 14:23:01 +03004434 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004435 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004436 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004437
Liran Alon2307af12018-06-29 22:59:04 +03004438 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004439
Yang, Sheng002c7f72007-07-31 14:23:01 +03004440 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4441 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004442 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004443 vmcs_conf->vmexit_ctrl = _vmexit_control;
4444 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004445
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004446 if (static_branch_unlikely(&enable_evmcs))
4447 evmcs_sanitize_exec_ctrls(vmcs_conf);
4448
Avi Kivity110312c2010-12-21 12:54:20 +02004449 cpu_has_load_ia32_efer =
4450 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4451 VM_ENTRY_LOAD_IA32_EFER)
4452 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4453 VM_EXIT_LOAD_IA32_EFER);
4454
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004455 cpu_has_load_perf_global_ctrl =
4456 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4457 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4458 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4459 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4460
4461 /*
4462 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004463 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004464 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4465 *
4466 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4467 *
4468 * AAK155 (model 26)
4469 * AAP115 (model 30)
4470 * AAT100 (model 37)
4471 * BC86,AAY89,BD102 (model 44)
4472 * BA97 (model 46)
4473 *
4474 */
4475 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4476 switch (boot_cpu_data.x86_model) {
4477 case 26:
4478 case 30:
4479 case 37:
4480 case 44:
4481 case 46:
4482 cpu_has_load_perf_global_ctrl = false;
4483 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4484 "does not work properly. Using workaround\n");
4485 break;
4486 default:
4487 break;
4488 }
4489 }
4490
Borislav Petkov782511b2016-04-04 22:25:03 +02004491 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004492 rdmsrl(MSR_IA32_XSS, host_xss);
4493
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004494 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004495}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496
Liran Alon491a6032018-06-23 02:35:12 +03004497static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004498{
4499 int node = cpu_to_node(cpu);
4500 struct page *pages;
4501 struct vmcs *vmcs;
4502
Vlastimil Babka96db8002015-09-08 15:03:50 -07004503 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504 if (!pages)
4505 return NULL;
4506 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004507 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004508
4509 /* KVM supports Enlightened VMCS v1 only */
4510 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004511 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004512 else
Liran Alon392b2f22018-06-23 02:35:01 +03004513 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004514
Liran Alon491a6032018-06-23 02:35:12 +03004515 if (shadow)
4516 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517 return vmcs;
4518}
4519
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520static void free_vmcs(struct vmcs *vmcs)
4521{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004522 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523}
4524
Nadav Har'Eld462b812011-05-24 15:26:10 +03004525/*
4526 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4527 */
4528static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4529{
4530 if (!loaded_vmcs->vmcs)
4531 return;
4532 loaded_vmcs_clear(loaded_vmcs);
4533 free_vmcs(loaded_vmcs->vmcs);
4534 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004535 if (loaded_vmcs->msr_bitmap)
4536 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004537 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004538}
4539
Liran Alon491a6032018-06-23 02:35:12 +03004540static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004541{
Liran Alon491a6032018-06-23 02:35:12 +03004542 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004543}
4544
4545static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4546{
Liran Alon491a6032018-06-23 02:35:12 +03004547 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004548 if (!loaded_vmcs->vmcs)
4549 return -ENOMEM;
4550
4551 loaded_vmcs->shadow_vmcs = NULL;
4552 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004553
4554 if (cpu_has_vmx_msr_bitmap()) {
4555 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4556 if (!loaded_vmcs->msr_bitmap)
4557 goto out_vmcs;
4558 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004559
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004560 if (IS_ENABLED(CONFIG_HYPERV) &&
4561 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004562 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4563 struct hv_enlightened_vmcs *evmcs =
4564 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4565
4566 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4567 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004568 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004569 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004570
4571out_vmcs:
4572 free_loaded_vmcs(loaded_vmcs);
4573 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004574}
4575
Sam Ravnborg39959582007-06-01 00:47:13 -07004576static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577{
4578 int cpu;
4579
Zachary Amsden3230bb42009-09-29 11:38:37 -10004580 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004582 per_cpu(vmxarea, cpu) = NULL;
4583 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004584}
4585
Jim Mattsond37f4262017-12-22 12:12:16 -08004586enum vmcs_field_width {
4587 VMCS_FIELD_WIDTH_U16 = 0,
4588 VMCS_FIELD_WIDTH_U64 = 1,
4589 VMCS_FIELD_WIDTH_U32 = 2,
4590 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004591};
4592
Jim Mattsond37f4262017-12-22 12:12:16 -08004593static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004594{
4595 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004596 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004597 return (field >> 13) & 0x3 ;
4598}
4599
4600static inline int vmcs_field_readonly(unsigned long field)
4601{
4602 return (((field >> 10) & 0x3) == 1);
4603}
4604
Bandan Dasfe2b2012014-04-21 15:20:14 -04004605static void init_vmcs_shadow_fields(void)
4606{
4607 int i, j;
4608
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004609 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4610 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004611 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004612 (i + 1 == max_shadow_read_only_fields ||
4613 shadow_read_only_fields[i + 1] != field + 1))
4614 pr_err("Missing field from shadow_read_only_field %x\n",
4615 field + 1);
4616
4617 clear_bit(field, vmx_vmread_bitmap);
4618#ifdef CONFIG_X86_64
4619 if (field & 1)
4620 continue;
4621#endif
4622 if (j < i)
4623 shadow_read_only_fields[j] = field;
4624 j++;
4625 }
4626 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004627
4628 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004629 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004630 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004631 (i + 1 == max_shadow_read_write_fields ||
4632 shadow_read_write_fields[i + 1] != field + 1))
4633 pr_err("Missing field from shadow_read_write_field %x\n",
4634 field + 1);
4635
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004636 /*
4637 * PML and the preemption timer can be emulated, but the
4638 * processor cannot vmwrite to fields that don't exist
4639 * on bare metal.
4640 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004641 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004642 case GUEST_PML_INDEX:
4643 if (!cpu_has_vmx_pml())
4644 continue;
4645 break;
4646 case VMX_PREEMPTION_TIMER_VALUE:
4647 if (!cpu_has_vmx_preemption_timer())
4648 continue;
4649 break;
4650 case GUEST_INTR_STATUS:
4651 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004652 continue;
4653 break;
4654 default:
4655 break;
4656 }
4657
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004658 clear_bit(field, vmx_vmwrite_bitmap);
4659 clear_bit(field, vmx_vmread_bitmap);
4660#ifdef CONFIG_X86_64
4661 if (field & 1)
4662 continue;
4663#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004664 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004665 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004666 j++;
4667 }
4668 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004669}
4670
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671static __init int alloc_kvm_area(void)
4672{
4673 int cpu;
4674
Zachary Amsden3230bb42009-09-29 11:38:37 -10004675 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004676 struct vmcs *vmcs;
4677
Liran Alon491a6032018-06-23 02:35:12 +03004678 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679 if (!vmcs) {
4680 free_kvm_area();
4681 return -ENOMEM;
4682 }
4683
Liran Alon2307af12018-06-29 22:59:04 +03004684 /*
4685 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4686 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4687 * revision_id reported by MSR_IA32_VMX_BASIC.
4688 *
4689 * However, even though not explictly documented by
4690 * TLFS, VMXArea passed as VMXON argument should
4691 * still be marked with revision_id reported by
4692 * physical CPU.
4693 */
4694 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004695 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004696
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697 per_cpu(vmxarea, cpu) = vmcs;
4698 }
4699 return 0;
4700}
4701
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004702static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004703 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004705 if (!emulate_invalid_guest_state) {
4706 /*
4707 * CS and SS RPL should be equal during guest entry according
4708 * to VMX spec, but in reality it is not always so. Since vcpu
4709 * is in the middle of the transition from real mode to
4710 * protected mode it is safe to assume that RPL 0 is a good
4711 * default value.
4712 */
4713 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004714 save->selector &= ~SEGMENT_RPL_MASK;
4715 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004716 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004717 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004718 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719}
4720
4721static void enter_pmode(struct kvm_vcpu *vcpu)
4722{
4723 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004724 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725
Gleb Natapovd99e4152012-12-20 16:57:45 +02004726 /*
4727 * Update real mode segment cache. It may be not up-to-date if sement
4728 * register was written while vcpu was in a guest mode.
4729 */
4730 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4731 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4732 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4733 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4734 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4735 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4736
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004737 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738
Avi Kivity2fb92db2011-04-27 19:42:18 +03004739 vmx_segment_cache_clear(vmx);
4740
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004741 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742
4743 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004744 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4745 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 vmcs_writel(GUEST_RFLAGS, flags);
4747
Rusty Russell66aee912007-07-17 23:34:16 +10004748 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4749 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750
4751 update_exception_bitmap(vcpu);
4752
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004753 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4754 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4755 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4756 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4757 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4758 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759}
4760
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004761static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762{
Mathias Krause772e0312012-08-30 01:30:19 +02004763 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004764 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765
Gleb Natapovd99e4152012-12-20 16:57:45 +02004766 var.dpl = 0x3;
4767 if (seg == VCPU_SREG_CS)
4768 var.type = 0x3;
4769
4770 if (!emulate_invalid_guest_state) {
4771 var.selector = var.base >> 4;
4772 var.base = var.base & 0xffff0;
4773 var.limit = 0xffff;
4774 var.g = 0;
4775 var.db = 0;
4776 var.present = 1;
4777 var.s = 1;
4778 var.l = 0;
4779 var.unusable = 0;
4780 var.type = 0x3;
4781 var.avl = 0;
4782 if (save->base & 0xf)
4783 printk_once(KERN_WARNING "kvm: segment base is not "
4784 "paragraph aligned when entering "
4785 "protected mode (seg=%d)", seg);
4786 }
4787
4788 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004789 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004790 vmcs_write32(sf->limit, var.limit);
4791 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792}
4793
4794static void enter_rmode(struct kvm_vcpu *vcpu)
4795{
4796 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004797 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004798 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004800 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4801 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4802 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4803 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4804 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004805 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4806 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004807
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004808 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809
Gleb Natapov776e58e2011-03-13 12:34:27 +02004810 /*
4811 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004812 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004813 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004814 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004815 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4816 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004817
Avi Kivity2fb92db2011-04-27 19:42:18 +03004818 vmx_segment_cache_clear(vmx);
4819
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004820 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004821 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4823
4824 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004825 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004827 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828
4829 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004830 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831 update_exception_bitmap(vcpu);
4832
Gleb Natapovd99e4152012-12-20 16:57:45 +02004833 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4834 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4835 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4836 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4837 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4838 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004839
Eddie Dong8668a3c2007-10-10 14:26:45 +08004840 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841}
4842
Amit Shah401d10d2009-02-20 22:53:37 +05304843static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4844{
4845 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004846 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4847
4848 if (!msr)
4849 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304850
Avi Kivity44ea2b12009-09-06 15:55:37 +03004851 /*
4852 * Force kernel_gs_base reloading before EFER changes, as control
4853 * of this msr depends on is_long_mode().
4854 */
4855 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004856 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304857 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004858 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304859 msr->data = efer;
4860 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004861 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304862
4863 msr->data = efer & ~EFER_LME;
4864 }
4865 setup_msrs(vmx);
4866}
4867
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004868#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869
4870static void enter_lmode(struct kvm_vcpu *vcpu)
4871{
4872 u32 guest_tr_ar;
4873
Avi Kivity2fb92db2011-04-27 19:42:18 +03004874 vmx_segment_cache_clear(to_vmx(vcpu));
4875
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004877 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004878 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4879 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004881 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4882 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 }
Avi Kivityda38f432010-07-06 11:30:49 +03004884 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885}
4886
4887static void exit_lmode(struct kvm_vcpu *vcpu)
4888{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004889 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004890 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891}
4892
4893#endif
4894
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004895static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4896 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004897{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004898 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004899 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4900 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004901 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004902 } else {
4903 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004904 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004905}
4906
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004907static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004908{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004909 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004910}
4911
Junaid Shahidfaff8752018-06-29 13:10:05 -07004912static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4913{
4914 int vpid = to_vmx(vcpu)->vpid;
4915
4916 if (!vpid_sync_vcpu_addr(vpid, addr))
4917 vpid_sync_context(vpid);
4918
4919 /*
4920 * If VPIDs are not supported or enabled, then the above is a no-op.
4921 * But we don't really need a TLB flush in that case anyway, because
4922 * each VM entry/exit includes an implicit flush when VPID is 0.
4923 */
4924}
4925
Avi Kivitye8467fd2009-12-29 18:43:06 +02004926static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4927{
4928 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4929
4930 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4931 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4932}
4933
Avi Kivityaff48ba2010-12-05 18:56:11 +02004934static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4935{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004936 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004937 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4938 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4939}
4940
Anthony Liguori25c4c272007-04-27 09:29:21 +03004941static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004942{
Avi Kivityfc78f512009-12-07 12:16:48 +02004943 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4944
4945 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4946 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004947}
4948
Sheng Yang14394422008-04-28 12:24:45 +08004949static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4950{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004951 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4952
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004953 if (!test_bit(VCPU_EXREG_PDPTR,
4954 (unsigned long *)&vcpu->arch.regs_dirty))
4955 return;
4956
Sheng Yang14394422008-04-28 12:24:45 +08004957 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004958 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4959 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4960 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4961 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004962 }
4963}
4964
Avi Kivity8f5d5492009-05-31 18:41:29 +03004965static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4966{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004967 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4968
Avi Kivity8f5d5492009-05-31 18:41:29 +03004969 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004970 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4971 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4972 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4973 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004974 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004975
4976 __set_bit(VCPU_EXREG_PDPTR,
4977 (unsigned long *)&vcpu->arch.regs_avail);
4978 __set_bit(VCPU_EXREG_PDPTR,
4979 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004980}
4981
David Matlack38991522016-11-29 18:14:08 -08004982static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4983{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004984 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4985 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4987
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004988 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004989 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4990 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4991 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4992
4993 return fixed_bits_valid(val, fixed0, fixed1);
4994}
4995
4996static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4997{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004998 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4999 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005000
5001 return fixed_bits_valid(val, fixed0, fixed1);
5002}
5003
5004static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5005{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005006 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5007 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005008
5009 return fixed_bits_valid(val, fixed0, fixed1);
5010}
5011
5012/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5013#define nested_guest_cr4_valid nested_cr4_valid
5014#define nested_host_cr4_valid nested_cr4_valid
5015
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005016static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005017
5018static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5019 unsigned long cr0,
5020 struct kvm_vcpu *vcpu)
5021{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005022 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5023 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005024 if (!(cr0 & X86_CR0_PG)) {
5025 /* From paging/starting to nonpaging */
5026 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005027 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005028 (CPU_BASED_CR3_LOAD_EXITING |
5029 CPU_BASED_CR3_STORE_EXITING));
5030 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005031 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005032 } else if (!is_paging(vcpu)) {
5033 /* From nonpaging to paging */
5034 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005035 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005036 ~(CPU_BASED_CR3_LOAD_EXITING |
5037 CPU_BASED_CR3_STORE_EXITING));
5038 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005039 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005040 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005041
5042 if (!(cr0 & X86_CR0_WP))
5043 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005044}
5045
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5047{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005048 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005049 unsigned long hw_cr0;
5050
Gleb Natapov50378782013-02-04 16:00:28 +02005051 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005052 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005053 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005054 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005055 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005056
Gleb Natapov218e7632013-01-21 15:36:45 +02005057 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5058 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059
Gleb Natapov218e7632013-01-21 15:36:45 +02005060 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5061 enter_rmode(vcpu);
5062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005064#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005065 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005066 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005068 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069 exit_lmode(vcpu);
5070 }
5071#endif
5072
Sean Christophersonb4d18512018-03-05 12:04:40 -08005073 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005074 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5075
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005077 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005078 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005079
5080 /* depends on vcpu->arch.cr0 to be set to a new value */
5081 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082}
5083
Yu Zhang855feb62017-08-24 20:27:55 +08005084static int get_ept_level(struct kvm_vcpu *vcpu)
5085{
5086 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5087 return 5;
5088 return 4;
5089}
5090
Peter Feiner995f00a2017-06-30 17:26:32 -07005091static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005092{
Yu Zhang855feb62017-08-24 20:27:55 +08005093 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005094
Yu Zhang855feb62017-08-24 20:27:55 +08005095 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005096
Peter Feiner995f00a2017-06-30 17:26:32 -07005097 if (enable_ept_ad_bits &&
5098 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005099 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005100 eptp |= (root_hpa & PAGE_MASK);
5101
5102 return eptp;
5103}
5104
Avi Kivity6aa8b732006-12-10 02:21:36 -08005105static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5106{
Tianyu Lan877ad952018-07-19 08:40:23 +00005107 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005108 unsigned long guest_cr3;
5109 u64 eptp;
5110
5111 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005112 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005113 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005114 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005115
5116 if (kvm_x86_ops->tlb_remote_flush) {
5117 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5118 to_vmx(vcpu)->ept_pointer = eptp;
5119 to_kvm_vmx(kvm)->ept_pointers_match
5120 = EPT_POINTERS_CHECK;
5121 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5122 }
5123
Sean Christophersone90008d2018-03-05 12:04:37 -08005124 if (enable_unrestricted_guest || is_paging(vcpu) ||
5125 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005126 guest_cr3 = kvm_read_cr3(vcpu);
5127 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005128 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005129 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005130 }
5131
Sheng Yang14394422008-04-28 12:24:45 +08005132 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133}
5134
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005135static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005137 /*
5138 * Pass through host's Machine Check Enable value to hw_cr4, which
5139 * is in force while we are in guest mode. Do not let guests control
5140 * this bit, even if host CR4.MCE == 0.
5141 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005142 unsigned long hw_cr4;
5143
5144 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5145 if (enable_unrestricted_guest)
5146 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5147 else if (to_vmx(vcpu)->rmode.vm86_active)
5148 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5149 else
5150 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005151
Sean Christopherson64f7a112018-04-30 10:01:06 -07005152 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5153 if (cr4 & X86_CR4_UMIP) {
5154 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005155 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005156 hw_cr4 &= ~X86_CR4_UMIP;
5157 } else if (!is_guest_mode(vcpu) ||
5158 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5159 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5160 SECONDARY_EXEC_DESC);
5161 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005162
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005163 if (cr4 & X86_CR4_VMXE) {
5164 /*
5165 * To use VMXON (and later other VMX instructions), a guest
5166 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5167 * So basically the check on whether to allow nested VMX
5168 * is here.
5169 */
5170 if (!nested_vmx_allowed(vcpu))
5171 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005172 }
David Matlack38991522016-11-29 18:14:08 -08005173
5174 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005175 return 1;
5176
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005177 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005178
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005179 if (!enable_unrestricted_guest) {
5180 if (enable_ept) {
5181 if (!is_paging(vcpu)) {
5182 hw_cr4 &= ~X86_CR4_PAE;
5183 hw_cr4 |= X86_CR4_PSE;
5184 } else if (!(cr4 & X86_CR4_PAE)) {
5185 hw_cr4 &= ~X86_CR4_PAE;
5186 }
5187 }
5188
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005189 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005190 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5191 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5192 * to be manually disabled when guest switches to non-paging
5193 * mode.
5194 *
5195 * If !enable_unrestricted_guest, the CPU is always running
5196 * with CR0.PG=1 and CR4 needs to be modified.
5197 * If enable_unrestricted_guest, the CPU automatically
5198 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005199 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005200 if (!is_paging(vcpu))
5201 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5202 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005203
Sheng Yang14394422008-04-28 12:24:45 +08005204 vmcs_writel(CR4_READ_SHADOW, cr4);
5205 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005206 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005207}
5208
Avi Kivity6aa8b732006-12-10 02:21:36 -08005209static void vmx_get_segment(struct kvm_vcpu *vcpu,
5210 struct kvm_segment *var, int seg)
5211{
Avi Kivitya9179492011-01-03 14:28:52 +02005212 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213 u32 ar;
5214
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005215 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005216 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005217 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005218 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005219 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005220 var->base = vmx_read_guest_seg_base(vmx, seg);
5221 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5222 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005223 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005224 var->base = vmx_read_guest_seg_base(vmx, seg);
5225 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5226 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5227 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005228 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229 var->type = ar & 15;
5230 var->s = (ar >> 4) & 1;
5231 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005232 /*
5233 * Some userspaces do not preserve unusable property. Since usable
5234 * segment has to be present according to VMX spec we can use present
5235 * property to amend userspace bug by making unusable segment always
5236 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5237 * segment as unusable.
5238 */
5239 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005240 var->avl = (ar >> 12) & 1;
5241 var->l = (ar >> 13) & 1;
5242 var->db = (ar >> 14) & 1;
5243 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244}
5245
Avi Kivitya9179492011-01-03 14:28:52 +02005246static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5247{
Avi Kivitya9179492011-01-03 14:28:52 +02005248 struct kvm_segment s;
5249
5250 if (to_vmx(vcpu)->rmode.vm86_active) {
5251 vmx_get_segment(vcpu, &s, seg);
5252 return s.base;
5253 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005254 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005255}
5256
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005257static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005258{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005259 struct vcpu_vmx *vmx = to_vmx(vcpu);
5260
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005261 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005262 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005263 else {
5264 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005265 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005266 }
Avi Kivity69c73022011-03-07 15:26:44 +02005267}
5268
Avi Kivity653e3102007-05-07 10:55:37 +03005269static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005271 u32 ar;
5272
Avi Kivityf0495f92012-06-07 17:06:10 +03005273 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274 ar = 1 << 16;
5275 else {
5276 ar = var->type & 15;
5277 ar |= (var->s & 1) << 4;
5278 ar |= (var->dpl & 3) << 5;
5279 ar |= (var->present & 1) << 7;
5280 ar |= (var->avl & 1) << 12;
5281 ar |= (var->l & 1) << 13;
5282 ar |= (var->db & 1) << 14;
5283 ar |= (var->g & 1) << 15;
5284 }
Avi Kivity653e3102007-05-07 10:55:37 +03005285
5286 return ar;
5287}
5288
5289static void vmx_set_segment(struct kvm_vcpu *vcpu,
5290 struct kvm_segment *var, int seg)
5291{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005292 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005293 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005294
Avi Kivity2fb92db2011-04-27 19:42:18 +03005295 vmx_segment_cache_clear(vmx);
5296
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005297 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5298 vmx->rmode.segs[seg] = *var;
5299 if (seg == VCPU_SREG_TR)
5300 vmcs_write16(sf->selector, var->selector);
5301 else if (var->s)
5302 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005303 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005304 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005305
Avi Kivity653e3102007-05-07 10:55:37 +03005306 vmcs_writel(sf->base, var->base);
5307 vmcs_write32(sf->limit, var->limit);
5308 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005309
5310 /*
5311 * Fix the "Accessed" bit in AR field of segment registers for older
5312 * qemu binaries.
5313 * IA32 arch specifies that at the time of processor reset the
5314 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005315 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005316 * state vmexit when "unrestricted guest" mode is turned on.
5317 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5318 * tree. Newer qemu binaries with that qemu fix would not need this
5319 * kvm hack.
5320 */
5321 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005322 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005323
Gleb Natapovf924d662012-12-12 19:10:55 +02005324 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005325
5326out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005327 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005328}
5329
Avi Kivity6aa8b732006-12-10 02:21:36 -08005330static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5331{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005332 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333
5334 *db = (ar >> 14) & 1;
5335 *l = (ar >> 13) & 1;
5336}
5337
Gleb Natapov89a27f42010-02-16 10:51:48 +02005338static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005339{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005340 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5341 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005342}
5343
Gleb Natapov89a27f42010-02-16 10:51:48 +02005344static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005345{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005346 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5347 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348}
5349
Gleb Natapov89a27f42010-02-16 10:51:48 +02005350static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005351{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005352 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5353 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005354}
5355
Gleb Natapov89a27f42010-02-16 10:51:48 +02005356static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005357{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005358 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5359 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005360}
5361
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005362static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5363{
5364 struct kvm_segment var;
5365 u32 ar;
5366
5367 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005368 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005369 if (seg == VCPU_SREG_CS)
5370 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005371 ar = vmx_segment_access_rights(&var);
5372
5373 if (var.base != (var.selector << 4))
5374 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005375 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005376 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005377 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005378 return false;
5379
5380 return true;
5381}
5382
5383static bool code_segment_valid(struct kvm_vcpu *vcpu)
5384{
5385 struct kvm_segment cs;
5386 unsigned int cs_rpl;
5387
5388 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005389 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390
Avi Kivity1872a3f2009-01-04 23:26:52 +02005391 if (cs.unusable)
5392 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005393 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005394 return false;
5395 if (!cs.s)
5396 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005397 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005398 if (cs.dpl > cs_rpl)
5399 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005400 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005401 if (cs.dpl != cs_rpl)
5402 return false;
5403 }
5404 if (!cs.present)
5405 return false;
5406
5407 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5408 return true;
5409}
5410
5411static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5412{
5413 struct kvm_segment ss;
5414 unsigned int ss_rpl;
5415
5416 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005417 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005418
Avi Kivity1872a3f2009-01-04 23:26:52 +02005419 if (ss.unusable)
5420 return true;
5421 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005422 return false;
5423 if (!ss.s)
5424 return false;
5425 if (ss.dpl != ss_rpl) /* DPL != RPL */
5426 return false;
5427 if (!ss.present)
5428 return false;
5429
5430 return true;
5431}
5432
5433static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5434{
5435 struct kvm_segment var;
5436 unsigned int rpl;
5437
5438 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005439 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005440
Avi Kivity1872a3f2009-01-04 23:26:52 +02005441 if (var.unusable)
5442 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005443 if (!var.s)
5444 return false;
5445 if (!var.present)
5446 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005447 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005448 if (var.dpl < rpl) /* DPL < RPL */
5449 return false;
5450 }
5451
5452 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5453 * rights flags
5454 */
5455 return true;
5456}
5457
5458static bool tr_valid(struct kvm_vcpu *vcpu)
5459{
5460 struct kvm_segment tr;
5461
5462 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5463
Avi Kivity1872a3f2009-01-04 23:26:52 +02005464 if (tr.unusable)
5465 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005466 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005467 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005468 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005469 return false;
5470 if (!tr.present)
5471 return false;
5472
5473 return true;
5474}
5475
5476static bool ldtr_valid(struct kvm_vcpu *vcpu)
5477{
5478 struct kvm_segment ldtr;
5479
5480 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5481
Avi Kivity1872a3f2009-01-04 23:26:52 +02005482 if (ldtr.unusable)
5483 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005484 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005485 return false;
5486 if (ldtr.type != 2)
5487 return false;
5488 if (!ldtr.present)
5489 return false;
5490
5491 return true;
5492}
5493
5494static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5495{
5496 struct kvm_segment cs, ss;
5497
5498 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5499 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5500
Nadav Amitb32a9912015-03-29 16:33:04 +03005501 return ((cs.selector & SEGMENT_RPL_MASK) ==
5502 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005503}
5504
5505/*
5506 * Check if guest state is valid. Returns true if valid, false if
5507 * not.
5508 * We assume that registers are always usable
5509 */
5510static bool guest_state_valid(struct kvm_vcpu *vcpu)
5511{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005512 if (enable_unrestricted_guest)
5513 return true;
5514
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005515 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005516 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005517 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5518 return false;
5519 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5520 return false;
5521 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5522 return false;
5523 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5524 return false;
5525 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5526 return false;
5527 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5528 return false;
5529 } else {
5530 /* protected mode guest state checks */
5531 if (!cs_ss_rpl_check(vcpu))
5532 return false;
5533 if (!code_segment_valid(vcpu))
5534 return false;
5535 if (!stack_segment_valid(vcpu))
5536 return false;
5537 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5538 return false;
5539 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5540 return false;
5541 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5542 return false;
5543 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5544 return false;
5545 if (!tr_valid(vcpu))
5546 return false;
5547 if (!ldtr_valid(vcpu))
5548 return false;
5549 }
5550 /* TODO:
5551 * - Add checks on RIP
5552 * - Add checks on RFLAGS
5553 */
5554
5555 return true;
5556}
5557
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005558static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5559{
5560 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5561}
5562
Mike Dayd77c26f2007-10-08 09:02:08 -04005563static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005565 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005566 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005567 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005568
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005569 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005570 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005571 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5572 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005573 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005574 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005575 r = kvm_write_guest_page(kvm, fn++, &data,
5576 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005577 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005578 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005579 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5580 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005581 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005582 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5583 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005584 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005585 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005586 r = kvm_write_guest_page(kvm, fn, &data,
5587 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5588 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005589out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005590 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005591 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005592}
5593
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005594static int init_rmode_identity_map(struct kvm *kvm)
5595{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005596 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005597 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005598 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005599 u32 tmp;
5600
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005601 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005602 mutex_lock(&kvm->slots_lock);
5603
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005604 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005605 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005606
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005607 if (!kvm_vmx->ept_identity_map_addr)
5608 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5609 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005610
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005611 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005612 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005613 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005614 goto out2;
5615
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005616 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005617 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5618 if (r < 0)
5619 goto out;
5620 /* Set up identity-mapping pagetable for EPT in real mode */
5621 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5622 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5623 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5624 r = kvm_write_guest_page(kvm, identity_map_pfn,
5625 &tmp, i * sizeof(tmp), sizeof(tmp));
5626 if (r < 0)
5627 goto out;
5628 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005629 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005630
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005631out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005632 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005633
5634out2:
5635 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005636 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005637}
5638
Avi Kivity6aa8b732006-12-10 02:21:36 -08005639static void seg_setup(int seg)
5640{
Mathias Krause772e0312012-08-30 01:30:19 +02005641 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005642 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005643
5644 vmcs_write16(sf->selector, 0);
5645 vmcs_writel(sf->base, 0);
5646 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005647 ar = 0x93;
5648 if (seg == VCPU_SREG_CS)
5649 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005650
5651 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652}
5653
Sheng Yangf78e0e22007-10-29 09:40:42 +08005654static int alloc_apic_access_page(struct kvm *kvm)
5655{
Xiao Guangrong44841412012-09-07 14:14:20 +08005656 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005657 int r = 0;
5658
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005659 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005660 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005661 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005662 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5663 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005664 if (r)
5665 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005666
Tang Chen73a6d942014-09-11 13:38:00 +08005667 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005668 if (is_error_page(page)) {
5669 r = -EFAULT;
5670 goto out;
5671 }
5672
Tang Chenc24ae0d2014-09-24 15:57:58 +08005673 /*
5674 * Do not pin the page in memory, so that memory hot-unplug
5675 * is able to migrate it.
5676 */
5677 put_page(page);
5678 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005679out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005680 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005681 return r;
5682}
5683
Wanpeng Li991e7a02015-09-16 17:30:05 +08005684static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005685{
5686 int vpid;
5687
Avi Kivity919818a2009-03-23 18:01:29 +02005688 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005689 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005690 spin_lock(&vmx_vpid_lock);
5691 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005692 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005693 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005694 else
5695 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005696 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005697 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005698}
5699
Wanpeng Li991e7a02015-09-16 17:30:05 +08005700static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005701{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005702 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005703 return;
5704 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005705 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005706 spin_unlock(&vmx_vpid_lock);
5707}
5708
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005709static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5710 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005711{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005712 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005713
5714 if (!cpu_has_vmx_msr_bitmap())
5715 return;
5716
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005717 if (static_branch_unlikely(&enable_evmcs))
5718 evmcs_touch_msr_bitmap();
5719
Sheng Yang25c5f222008-03-28 13:18:56 +08005720 /*
5721 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5722 * have the write-low and read-high bitmap offsets the wrong way round.
5723 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5724 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005725 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005726 if (type & MSR_TYPE_R)
5727 /* read-low */
5728 __clear_bit(msr, msr_bitmap + 0x000 / f);
5729
5730 if (type & MSR_TYPE_W)
5731 /* write-low */
5732 __clear_bit(msr, msr_bitmap + 0x800 / f);
5733
Sheng Yang25c5f222008-03-28 13:18:56 +08005734 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5735 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005736 if (type & MSR_TYPE_R)
5737 /* read-high */
5738 __clear_bit(msr, msr_bitmap + 0x400 / f);
5739
5740 if (type & MSR_TYPE_W)
5741 /* write-high */
5742 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5743
5744 }
5745}
5746
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005747static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5748 u32 msr, int type)
5749{
5750 int f = sizeof(unsigned long);
5751
5752 if (!cpu_has_vmx_msr_bitmap())
5753 return;
5754
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005755 if (static_branch_unlikely(&enable_evmcs))
5756 evmcs_touch_msr_bitmap();
5757
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005758 /*
5759 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5760 * have the write-low and read-high bitmap offsets the wrong way round.
5761 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5762 */
5763 if (msr <= 0x1fff) {
5764 if (type & MSR_TYPE_R)
5765 /* read-low */
5766 __set_bit(msr, msr_bitmap + 0x000 / f);
5767
5768 if (type & MSR_TYPE_W)
5769 /* write-low */
5770 __set_bit(msr, msr_bitmap + 0x800 / f);
5771
5772 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5773 msr &= 0x1fff;
5774 if (type & MSR_TYPE_R)
5775 /* read-high */
5776 __set_bit(msr, msr_bitmap + 0x400 / f);
5777
5778 if (type & MSR_TYPE_W)
5779 /* write-high */
5780 __set_bit(msr, msr_bitmap + 0xc00 / f);
5781
5782 }
5783}
5784
5785static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5786 u32 msr, int type, bool value)
5787{
5788 if (value)
5789 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5790 else
5791 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5792}
5793
Wincy Vanf2b93282015-02-03 23:56:03 +08005794/*
5795 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5796 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5797 */
5798static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5799 unsigned long *msr_bitmap_nested,
5800 u32 msr, int type)
5801{
5802 int f = sizeof(unsigned long);
5803
Wincy Vanf2b93282015-02-03 23:56:03 +08005804 /*
5805 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5806 * have the write-low and read-high bitmap offsets the wrong way round.
5807 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5808 */
5809 if (msr <= 0x1fff) {
5810 if (type & MSR_TYPE_R &&
5811 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5812 /* read-low */
5813 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5814
5815 if (type & MSR_TYPE_W &&
5816 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5817 /* write-low */
5818 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5819
5820 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5821 msr &= 0x1fff;
5822 if (type & MSR_TYPE_R &&
5823 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5824 /* read-high */
5825 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5826
5827 if (type & MSR_TYPE_W &&
5828 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5829 /* write-high */
5830 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5831
5832 }
5833}
5834
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005835static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005836{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005837 u8 mode = 0;
5838
5839 if (cpu_has_secondary_exec_ctrls() &&
5840 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5841 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5842 mode |= MSR_BITMAP_MODE_X2APIC;
5843 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5844 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5845 }
5846
5847 if (is_long_mode(vcpu))
5848 mode |= MSR_BITMAP_MODE_LM;
5849
5850 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005851}
5852
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005853#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5854
5855static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5856 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005857{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005858 int msr;
5859
5860 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5861 unsigned word = msr / BITS_PER_LONG;
5862 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5863 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005864 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005865
5866 if (mode & MSR_BITMAP_MODE_X2APIC) {
5867 /*
5868 * TPR reads and writes can be virtualized even if virtual interrupt
5869 * delivery is not in use.
5870 */
5871 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5872 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5873 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5874 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5875 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5876 }
5877 }
5878}
5879
5880static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5881{
5882 struct vcpu_vmx *vmx = to_vmx(vcpu);
5883 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5884 u8 mode = vmx_msr_bitmap_mode(vcpu);
5885 u8 changed = mode ^ vmx->msr_bitmap_mode;
5886
5887 if (!changed)
5888 return;
5889
5890 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5891 !(mode & MSR_BITMAP_MODE_LM));
5892
5893 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5894 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5895
5896 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005897}
5898
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005899static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005900{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005901 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005902}
5903
David Matlackc9f04402017-08-01 14:00:40 -07005904static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5905{
5906 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5907 gfn_t gfn;
5908
5909 /*
5910 * Don't need to mark the APIC access page dirty; it is never
5911 * written to by the CPU during APIC virtualization.
5912 */
5913
5914 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5915 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5916 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5917 }
5918
5919 if (nested_cpu_has_posted_intr(vmcs12)) {
5920 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5921 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5922 }
5923}
5924
5925
David Hildenbrand6342c502017-01-25 11:58:58 +01005926static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005927{
5928 struct vcpu_vmx *vmx = to_vmx(vcpu);
5929 int max_irr;
5930 void *vapic_page;
5931 u16 status;
5932
David Matlackc9f04402017-08-01 14:00:40 -07005933 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5934 return;
Wincy Van705699a2015-02-03 23:58:17 +08005935
David Matlackc9f04402017-08-01 14:00:40 -07005936 vmx->nested.pi_pending = false;
5937 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5938 return;
Wincy Van705699a2015-02-03 23:58:17 +08005939
David Matlackc9f04402017-08-01 14:00:40 -07005940 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5941 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005942 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005943 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5944 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005945 kunmap(vmx->nested.virtual_apic_page);
5946
5947 status = vmcs_read16(GUEST_INTR_STATUS);
5948 if ((u8)max_irr > ((u8)status & 0xff)) {
5949 status &= ~0xff;
5950 status |= (u8)max_irr;
5951 vmcs_write16(GUEST_INTR_STATUS, status);
5952 }
5953 }
David Matlackc9f04402017-08-01 14:00:40 -07005954
5955 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005956}
5957
Wincy Van06a55242017-04-28 13:13:59 +08005958static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5959 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005960{
5961#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005962 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5963
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005964 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005965 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005966 * The vector of interrupt to be delivered to vcpu had
5967 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005968 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005969 * Following cases will be reached in this block, and
5970 * we always send a notification event in all cases as
5971 * explained below.
5972 *
5973 * Case 1: vcpu keeps in non-root mode. Sending a
5974 * notification event posts the interrupt to vcpu.
5975 *
5976 * Case 2: vcpu exits to root mode and is still
5977 * runnable. PIR will be synced to vIRR before the
5978 * next vcpu entry. Sending a notification event in
5979 * this case has no effect, as vcpu is not in root
5980 * mode.
5981 *
5982 * Case 3: vcpu exits to root mode and is blocked.
5983 * vcpu_block() has already synced PIR to vIRR and
5984 * never blocks vcpu if vIRR is not cleared. Therefore,
5985 * a blocked vcpu here does not wait for any requested
5986 * interrupts in PIR, and sending a notification event
5987 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005988 */
Feng Wu28b835d2015-09-18 22:29:54 +08005989
Wincy Van06a55242017-04-28 13:13:59 +08005990 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005991 return true;
5992 }
5993#endif
5994 return false;
5995}
5996
Wincy Van705699a2015-02-03 23:58:17 +08005997static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5998 int vector)
5999{
6000 struct vcpu_vmx *vmx = to_vmx(vcpu);
6001
6002 if (is_guest_mode(vcpu) &&
6003 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006004 /*
6005 * If a posted intr is not recognized by hardware,
6006 * we will accomplish it in the next vmentry.
6007 */
6008 vmx->nested.pi_pending = true;
6009 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006010 /* the PIR and ON have been set by L1. */
6011 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6012 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006013 return 0;
6014 }
6015 return -1;
6016}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006017/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006018 * Send interrupt to vcpu via posted interrupt way.
6019 * 1. If target vcpu is running(non-root mode), send posted interrupt
6020 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6021 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6022 * interrupt from PIR in next vmentry.
6023 */
6024static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6025{
6026 struct vcpu_vmx *vmx = to_vmx(vcpu);
6027 int r;
6028
Wincy Van705699a2015-02-03 23:58:17 +08006029 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6030 if (!r)
6031 return;
6032
Yang Zhanga20ed542013-04-11 19:25:15 +08006033 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6034 return;
6035
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006036 /* If a previous notification has sent the IPI, nothing to do. */
6037 if (pi_test_and_set_on(&vmx->pi_desc))
6038 return;
6039
Wincy Van06a55242017-04-28 13:13:59 +08006040 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006041 kvm_vcpu_kick(vcpu);
6042}
6043
Avi Kivity6aa8b732006-12-10 02:21:36 -08006044/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006045 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6046 * will not change in the lifetime of the guest.
6047 * Note that host-state that does change is set elsewhere. E.g., host-state
6048 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6049 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006050static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006051{
6052 u32 low32, high32;
6053 unsigned long tmpl;
6054 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006055 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006056
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006057 cr0 = read_cr0();
6058 WARN_ON(cr0 & X86_CR0_TS);
6059 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006060
6061 /*
6062 * Save the most likely value for this task's CR3 in the VMCS.
6063 * We can't use __get_current_cr3_fast() because we're not atomic.
6064 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006065 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006066 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006067 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006068
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006069 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006070 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006071 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006072 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006073
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006074 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006075#ifdef CONFIG_X86_64
6076 /*
6077 * Load null selectors, so we can avoid reloading them in
6078 * __vmx_load_host_state(), in case userspace uses the null selectors
6079 * too (the expected case).
6080 */
6081 vmcs_write16(HOST_DS_SELECTOR, 0);
6082 vmcs_write16(HOST_ES_SELECTOR, 0);
6083#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006084 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6085 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006086#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006087 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6088 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6089
Juergen Gross87930012017-09-04 12:25:27 +02006090 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006091 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006092 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006093
Avi Kivity83287ea422012-09-16 15:10:57 +03006094 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006095
6096 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6097 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6098 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6099 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6100
6101 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6102 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6103 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6104 }
6105}
6106
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6108{
6109 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6110 if (enable_ept)
6111 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006112 if (is_guest_mode(&vmx->vcpu))
6113 vmx->vcpu.arch.cr4_guest_owned_bits &=
6114 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006115 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6116}
6117
Yang Zhang01e439b2013-04-11 19:25:12 +08006118static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6119{
6120 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6121
Andrey Smetanind62caab2015-11-10 15:36:33 +03006122 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006123 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006124
6125 if (!enable_vnmi)
6126 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6127
Yunhong Jiang64672c92016-06-13 14:19:59 -07006128 /* Enable the preemption timer dynamically */
6129 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006130 return pin_based_exec_ctrl;
6131}
6132
Andrey Smetanind62caab2015-11-10 15:36:33 +03006133static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6134{
6135 struct vcpu_vmx *vmx = to_vmx(vcpu);
6136
6137 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006138 if (cpu_has_secondary_exec_ctrls()) {
6139 if (kvm_vcpu_apicv_active(vcpu))
6140 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6141 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6142 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6143 else
6144 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6145 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6146 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6147 }
6148
6149 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006150 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006151}
6152
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006153static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6154{
6155 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006156
6157 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6158 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6159
Paolo Bonzini35754c92015-07-29 12:05:37 +02006160 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006161 exec_control &= ~CPU_BASED_TPR_SHADOW;
6162#ifdef CONFIG_X86_64
6163 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6164 CPU_BASED_CR8_LOAD_EXITING;
6165#endif
6166 }
6167 if (!enable_ept)
6168 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6169 CPU_BASED_CR3_LOAD_EXITING |
6170 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006171 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6172 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6173 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006174 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6175 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006176 return exec_control;
6177}
6178
Jim Mattson45ec3682017-08-23 16:32:04 -07006179static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006180{
Jim Mattson45ec3682017-08-23 16:32:04 -07006181 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006182 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006183}
6184
Jim Mattson75f4fc82017-08-23 16:32:03 -07006185static bool vmx_rdseed_supported(void)
6186{
6187 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006188 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006189}
6190
Paolo Bonzini80154d72017-08-24 13:55:35 +02006191static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006192{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006193 struct kvm_vcpu *vcpu = &vmx->vcpu;
6194
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006195 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006196
Paolo Bonzini80154d72017-08-24 13:55:35 +02006197 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006198 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6199 if (vmx->vpid == 0)
6200 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6201 if (!enable_ept) {
6202 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6203 enable_unrestricted_guest = 0;
6204 }
6205 if (!enable_unrestricted_guest)
6206 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006207 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006208 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006209 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006210 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6211 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006212 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006213
6214 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6215 * in vmx_set_cr4. */
6216 exec_control &= ~SECONDARY_EXEC_DESC;
6217
Abel Gordonabc4fc52013-04-18 14:35:25 +03006218 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6219 (handle_vmptrld).
6220 We can NOT enable shadow_vmcs here because we don't have yet
6221 a current VMCS12
6222 */
6223 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006224
6225 if (!enable_pml)
6226 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006227
Paolo Bonzini3db13482017-08-24 14:48:03 +02006228 if (vmx_xsaves_supported()) {
6229 /* Exposing XSAVES only when XSAVE is exposed */
6230 bool xsaves_enabled =
6231 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6232 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6233
6234 if (!xsaves_enabled)
6235 exec_control &= ~SECONDARY_EXEC_XSAVES;
6236
6237 if (nested) {
6238 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006239 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006240 SECONDARY_EXEC_XSAVES;
6241 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006242 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006243 ~SECONDARY_EXEC_XSAVES;
6244 }
6245 }
6246
Paolo Bonzini80154d72017-08-24 13:55:35 +02006247 if (vmx_rdtscp_supported()) {
6248 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6249 if (!rdtscp_enabled)
6250 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6251
6252 if (nested) {
6253 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006254 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006255 SECONDARY_EXEC_RDTSCP;
6256 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006257 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006258 ~SECONDARY_EXEC_RDTSCP;
6259 }
6260 }
6261
6262 if (vmx_invpcid_supported()) {
6263 /* Exposing INVPCID only when PCID is exposed */
6264 bool invpcid_enabled =
6265 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6266 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6267
6268 if (!invpcid_enabled) {
6269 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6270 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6271 }
6272
6273 if (nested) {
6274 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006275 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006276 SECONDARY_EXEC_ENABLE_INVPCID;
6277 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006278 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006279 ~SECONDARY_EXEC_ENABLE_INVPCID;
6280 }
6281 }
6282
Jim Mattson45ec3682017-08-23 16:32:04 -07006283 if (vmx_rdrand_supported()) {
6284 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6285 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006286 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006287
6288 if (nested) {
6289 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006290 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006291 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006292 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006293 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006294 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006295 }
6296 }
6297
Jim Mattson75f4fc82017-08-23 16:32:03 -07006298 if (vmx_rdseed_supported()) {
6299 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6300 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006301 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006302
6303 if (nested) {
6304 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006305 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006306 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006307 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006308 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006309 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006310 }
6311 }
6312
Paolo Bonzini80154d72017-08-24 13:55:35 +02006313 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006314}
6315
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006316static void ept_set_mmio_spte_mask(void)
6317{
6318 /*
6319 * EPT Misconfigurations can be generated if the value of bits 2:0
6320 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006321 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006322 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6323 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006324}
6325
Wanpeng Lif53cd632014-12-02 19:14:58 +08006326#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006327/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328 * Sets up the vmcs for emulated real mode.
6329 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006330static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006331{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006332#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006333 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006334#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006335 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006336
Abel Gordon4607c2d2013-04-18 14:35:55 +03006337 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006338 /*
6339 * At vCPU creation, "VMWRITE to any supported field
6340 * in the VMCS" is supported, so use the more
6341 * permissive vmx_vmread_bitmap to specify both read
6342 * and write permissions for the shadow VMCS.
6343 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006344 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006345 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006346 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006347 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006348 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006349
Avi Kivity6aa8b732006-12-10 02:21:36 -08006350 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6351
Avi Kivity6aa8b732006-12-10 02:21:36 -08006352 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006353 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006354 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006355
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006356 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006357
Dan Williamsdfa169b2016-06-02 11:17:24 -07006358 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006359 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006360 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006361 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006362 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006363
Andrey Smetanind62caab2015-11-10 15:36:33 +03006364 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006365 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6366 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6367 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6368 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6369
6370 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006371
Li RongQing0bcf2612015-12-03 13:29:34 +08006372 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006373 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006374 }
6375
Wanpeng Lib31c1142018-03-12 04:53:04 -07006376 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006377 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006378 vmx->ple_window = ple_window;
6379 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006380 }
6381
Xiao Guangrongc3707952011-07-12 03:28:04 +08006382 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6383 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006384 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6385
Avi Kivity9581d442010-10-19 16:46:55 +02006386 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6387 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006388 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006389#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006390 rdmsrl(MSR_FS_BASE, a);
6391 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6392 rdmsrl(MSR_GS_BASE, a);
6393 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6394#else
6395 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6396 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6397#endif
6398
Bandan Das2a499e42017-08-03 15:54:41 -04006399 if (cpu_has_vmx_vmfunc())
6400 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6401
Eddie Dong2cc51562007-05-21 07:28:09 +03006402 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6403 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006404 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006405 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006406 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006407
Radim Krčmář74545702015-04-27 15:11:25 +02006408 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6409 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006410
Paolo Bonzini03916db2014-07-24 14:21:57 +02006411 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412 u32 index = vmx_msr_index[i];
6413 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006414 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006415
6416 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6417 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006418 if (wrmsr_safe(index, data_low, data_high) < 0)
6419 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006420 vmx->guest_msrs[j].index = i;
6421 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006422 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006423 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006424 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006425
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006426 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6427 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006428
6429 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006430
6431 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006432 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006433
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006434 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6435 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6436
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006437 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006438
Wanpeng Lif53cd632014-12-02 19:14:58 +08006439 if (vmx_xsaves_supported())
6440 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6441
Peter Feiner4e595162016-07-07 14:49:58 -07006442 if (enable_pml) {
6443 ASSERT(vmx->pml_pg);
6444 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6445 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6446 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006447}
6448
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006449static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006450{
6451 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006452 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006453 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006454
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006455 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006456 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006457
Wanpeng Li518e7b92018-02-28 14:03:31 +08006458 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006459 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006460 kvm_set_cr8(vcpu, 0);
6461
6462 if (!init_event) {
6463 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6464 MSR_IA32_APICBASE_ENABLE;
6465 if (kvm_vcpu_is_reset_bsp(vcpu))
6466 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6467 apic_base_msr.host_initiated = true;
6468 kvm_set_apic_base(vcpu, &apic_base_msr);
6469 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006470
Avi Kivity2fb92db2011-04-27 19:42:18 +03006471 vmx_segment_cache_clear(vmx);
6472
Avi Kivity5706be02008-08-20 15:07:31 +03006473 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006474 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006475 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006476
6477 seg_setup(VCPU_SREG_DS);
6478 seg_setup(VCPU_SREG_ES);
6479 seg_setup(VCPU_SREG_FS);
6480 seg_setup(VCPU_SREG_GS);
6481 seg_setup(VCPU_SREG_SS);
6482
6483 vmcs_write16(GUEST_TR_SELECTOR, 0);
6484 vmcs_writel(GUEST_TR_BASE, 0);
6485 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6486 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6487
6488 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6489 vmcs_writel(GUEST_LDTR_BASE, 0);
6490 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6491 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6492
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006493 if (!init_event) {
6494 vmcs_write32(GUEST_SYSENTER_CS, 0);
6495 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6496 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6497 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6498 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006499
Wanpeng Lic37c2872017-11-20 14:52:21 -08006500 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006501 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006502
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006503 vmcs_writel(GUEST_GDTR_BASE, 0);
6504 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6505
6506 vmcs_writel(GUEST_IDTR_BASE, 0);
6507 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6508
Anthony Liguori443381a2010-12-06 10:53:38 -06006509 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006510 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006511 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006512 if (kvm_mpx_supported())
6513 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006514
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006515 setup_msrs(vmx);
6516
Avi Kivity6aa8b732006-12-10 02:21:36 -08006517 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6518
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006519 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006520 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006521 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006522 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006523 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006524 vmcs_write32(TPR_THRESHOLD, 0);
6525 }
6526
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006527 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006528
Sheng Yang2384d2b2008-01-17 15:14:33 +08006529 if (vmx->vpid != 0)
6530 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6531
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006532 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006533 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006534 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006535 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006536 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006537
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006538 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006539
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006540 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006541 if (init_event)
6542 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006543}
6544
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006545/*
6546 * In nested virtualization, check if L1 asked to exit on external interrupts.
6547 * For most existing hypervisors, this will always return true.
6548 */
6549static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6550{
6551 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6552 PIN_BASED_EXT_INTR_MASK;
6553}
6554
Bandan Das77b0f5d2014-04-19 18:17:45 -04006555/*
6556 * In nested virtualization, check if L1 has set
6557 * VM_EXIT_ACK_INTR_ON_EXIT
6558 */
6559static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6560{
6561 return get_vmcs12(vcpu)->vm_exit_controls &
6562 VM_EXIT_ACK_INTR_ON_EXIT;
6563}
6564
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006565static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6566{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006567 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006568}
6569
Jan Kiszkac9a79532014-03-07 20:03:15 +01006570static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006571{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006572 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6573 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006574}
6575
Jan Kiszkac9a79532014-03-07 20:03:15 +01006576static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006577{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006578 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006579 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006580 enable_irq_window(vcpu);
6581 return;
6582 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006583
Paolo Bonzini47c01522016-12-19 11:44:07 +01006584 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6585 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006586}
6587
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006588static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006589{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006591 uint32_t intr;
6592 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006593
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006594 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006595
Avi Kivityfa89a812008-09-01 15:57:51 +03006596 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006597 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006598 int inc_eip = 0;
6599 if (vcpu->arch.interrupt.soft)
6600 inc_eip = vcpu->arch.event_exit_inst_len;
6601 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006602 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006603 return;
6604 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006605 intr = irq | INTR_INFO_VALID_MASK;
6606 if (vcpu->arch.interrupt.soft) {
6607 intr |= INTR_TYPE_SOFT_INTR;
6608 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6609 vmx->vcpu.arch.event_exit_inst_len);
6610 } else
6611 intr |= INTR_TYPE_EXT_INTR;
6612 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006613
6614 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006615}
6616
Sheng Yangf08864b2008-05-15 18:23:25 +08006617static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6618{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006619 struct vcpu_vmx *vmx = to_vmx(vcpu);
6620
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006621 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006622 /*
6623 * Tracking the NMI-blocked state in software is built upon
6624 * finding the next open IRQ window. This, in turn, depends on
6625 * well-behaving guests: They have to keep IRQs disabled at
6626 * least as long as the NMI handler runs. Otherwise we may
6627 * cause NMI nesting, maybe breaking the guest. But as this is
6628 * highly unlikely, we can live with the residual risk.
6629 */
6630 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6631 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6632 }
6633
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006634 ++vcpu->stat.nmi_injections;
6635 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006636
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006637 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006638 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006639 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006640 return;
6641 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006642
Sheng Yangf08864b2008-05-15 18:23:25 +08006643 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6644 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006645
6646 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006647}
6648
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006649static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6650{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006651 struct vcpu_vmx *vmx = to_vmx(vcpu);
6652 bool masked;
6653
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006654 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006655 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006656 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006657 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006658 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6659 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6660 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006661}
6662
6663static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6664{
6665 struct vcpu_vmx *vmx = to_vmx(vcpu);
6666
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006667 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006668 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6669 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6670 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6671 }
6672 } else {
6673 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6674 if (masked)
6675 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6676 GUEST_INTR_STATE_NMI);
6677 else
6678 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6679 GUEST_INTR_STATE_NMI);
6680 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006681}
6682
Jan Kiszka2505dc92013-04-14 12:12:47 +02006683static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6684{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006685 if (to_vmx(vcpu)->nested.nested_run_pending)
6686 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006687
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006688 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006689 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6690 return 0;
6691
Jan Kiszka2505dc92013-04-14 12:12:47 +02006692 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6693 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6694 | GUEST_INTR_STATE_NMI));
6695}
6696
Gleb Natapov78646122009-03-23 12:12:11 +02006697static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6698{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006699 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6700 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006701 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6702 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006703}
6704
Izik Eiduscbc94022007-10-25 00:29:55 +02006705static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6706{
6707 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006708
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006709 if (enable_unrestricted_guest)
6710 return 0;
6711
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006712 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6713 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006714 if (ret)
6715 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006716 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006717 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006718}
6719
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006720static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6721{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006722 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006723 return 0;
6724}
6725
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006726static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006727{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006728 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006729 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006730 /*
6731 * Update instruction length as we may reinject the exception
6732 * from user space while in guest debugging mode.
6733 */
6734 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6735 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006736 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006737 return false;
6738 /* fall through */
6739 case DB_VECTOR:
6740 if (vcpu->guest_debug &
6741 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6742 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006743 /* fall through */
6744 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006745 case OF_VECTOR:
6746 case BR_VECTOR:
6747 case UD_VECTOR:
6748 case DF_VECTOR:
6749 case SS_VECTOR:
6750 case GP_VECTOR:
6751 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006752 return true;
6753 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006754 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006755 return false;
6756}
6757
6758static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6759 int vec, u32 err_code)
6760{
6761 /*
6762 * Instruction with address size override prefix opcode 0x67
6763 * Cause the #SS fault with 0 error code in VM86 mode.
6764 */
6765 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6766 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6767 if (vcpu->arch.halt_request) {
6768 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006769 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006770 }
6771 return 1;
6772 }
6773 return 0;
6774 }
6775
6776 /*
6777 * Forward all other exceptions that are valid in real mode.
6778 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6779 * the required debugging infrastructure rework.
6780 */
6781 kvm_queue_exception(vcpu, vec);
6782 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006783}
6784
Andi Kleena0861c02009-06-08 17:37:09 +08006785/*
6786 * Trigger machine check on the host. We assume all the MSRs are already set up
6787 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6788 * We pass a fake environment to the machine check handler because we want
6789 * the guest to be always treated like user space, no matter what context
6790 * it used internally.
6791 */
6792static void kvm_machine_check(void)
6793{
6794#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6795 struct pt_regs regs = {
6796 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6797 .flags = X86_EFLAGS_IF,
6798 };
6799
6800 do_machine_check(&regs, 0);
6801#endif
6802}
6803
Avi Kivity851ba692009-08-24 11:10:17 +03006804static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006805{
6806 /* already handled by vcpu_run */
6807 return 1;
6808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811{
Avi Kivity1155f762007-11-22 11:30:47 +02006812 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006813 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006814 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006815 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816 u32 vect_info;
6817 enum emulation_result er;
6818
Avi Kivity1155f762007-11-22 11:30:47 +02006819 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006820 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006821
Andi Kleena0861c02009-06-08 17:37:09 +08006822 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006823 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006824
Jim Mattsonef85b672016-12-12 11:01:37 -08006825 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006826 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006827
Wanpeng Li082d06e2018-04-03 16:28:48 -07006828 if (is_invalid_opcode(intr_info))
6829 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006830
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006832 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006833 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006834
Liran Alon9e869482018-03-12 13:12:51 +02006835 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6836 WARN_ON_ONCE(!enable_vmware_backdoor);
6837 er = emulate_instruction(vcpu,
6838 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6839 if (er == EMULATE_USER_EXIT)
6840 return 0;
6841 else if (er != EMULATE_DONE)
6842 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6843 return 1;
6844 }
6845
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006846 /*
6847 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6848 * MMIO, it is better to report an internal error.
6849 * See the comments in vmx_handle_exit.
6850 */
6851 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6852 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6853 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6854 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006855 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006856 vcpu->run->internal.data[0] = vect_info;
6857 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006858 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006859 return 0;
6860 }
6861
Avi Kivity6aa8b732006-12-10 02:21:36 -08006862 if (is_page_fault(intr_info)) {
6863 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006864 /* EPT won't cause page fault directly */
6865 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006866 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006867 }
6868
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006869 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006870
6871 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6872 return handle_rmode_exception(vcpu, ex_no, error_code);
6873
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006874 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006875 case AC_VECTOR:
6876 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6877 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006878 case DB_VECTOR:
6879 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6880 if (!(vcpu->guest_debug &
6881 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006882 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006883 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006884 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006885 skip_emulated_instruction(vcpu);
6886
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006887 kvm_queue_exception(vcpu, DB_VECTOR);
6888 return 1;
6889 }
6890 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6891 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6892 /* fall through */
6893 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006894 /*
6895 * Update instruction length as we may reinject #BP from
6896 * user space while in guest debugging mode. Reading it for
6897 * #DB as well causes no harm, it is not used in that case.
6898 */
6899 vmx->vcpu.arch.event_exit_inst_len =
6900 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006902 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006903 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6904 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006905 break;
6906 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006907 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6908 kvm_run->ex.exception = ex_no;
6909 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006911 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006912 return 0;
6913}
6914
Avi Kivity851ba692009-08-24 11:10:17 +03006915static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006916{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006917 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006918 return 1;
6919}
6920
Avi Kivity851ba692009-08-24 11:10:17 +03006921static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006922{
Avi Kivity851ba692009-08-24 11:10:17 +03006923 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006924 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006925 return 0;
6926}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006927
Avi Kivity851ba692009-08-24 11:10:17 +03006928static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929{
He, Qingbfdaab02007-09-12 14:18:28 +08006930 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006931 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006932 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006933
He, Qingbfdaab02007-09-12 14:18:28 +08006934 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006935 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006936
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006937 ++vcpu->stat.io_exits;
6938
Sean Christopherson432baf62018-03-08 08:57:26 -08006939 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006940 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006941
6942 port = exit_qualification >> 16;
6943 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006944 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006945
Sean Christophersondca7f122018-03-08 08:57:27 -08006946 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006947}
6948
Ingo Molnar102d8322007-02-19 14:37:47 +02006949static void
6950vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6951{
6952 /*
6953 * Patch in the VMCALL instruction:
6954 */
6955 hypercall[0] = 0x0f;
6956 hypercall[1] = 0x01;
6957 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006958}
6959
Guo Chao0fa06072012-06-28 15:16:19 +08006960/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006961static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6962{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006963 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006964 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6965 unsigned long orig_val = val;
6966
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006967 /*
6968 * We get here when L2 changed cr0 in a way that did not change
6969 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006970 * but did change L0 shadowed bits. So we first calculate the
6971 * effective cr0 value that L1 would like to write into the
6972 * hardware. It consists of the L2-owned bits from the new
6973 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006974 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006975 val = (val & ~vmcs12->cr0_guest_host_mask) |
6976 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6977
David Matlack38991522016-11-29 18:14:08 -08006978 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006979 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006980
6981 if (kvm_set_cr0(vcpu, val))
6982 return 1;
6983 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006984 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006985 } else {
6986 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006987 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006988 return 1;
David Matlack38991522016-11-29 18:14:08 -08006989
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006990 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006991 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006992}
6993
6994static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6995{
6996 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006997 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6998 unsigned long orig_val = val;
6999
7000 /* analogously to handle_set_cr0 */
7001 val = (val & ~vmcs12->cr4_guest_host_mask) |
7002 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7003 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007004 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007005 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007006 return 0;
7007 } else
7008 return kvm_set_cr4(vcpu, val);
7009}
7010
Paolo Bonzini0367f202016-07-12 10:44:55 +02007011static int handle_desc(struct kvm_vcpu *vcpu)
7012{
7013 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7014 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7015}
7016
Avi Kivity851ba692009-08-24 11:10:17 +03007017static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007018{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007019 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007020 int cr;
7021 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007022 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007023 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007024
He, Qingbfdaab02007-09-12 14:18:28 +08007025 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026 cr = exit_qualification & 15;
7027 reg = (exit_qualification >> 8) & 15;
7028 switch ((exit_qualification >> 4) & 3) {
7029 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007030 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007031 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007032 switch (cr) {
7033 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007034 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007035 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007037 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007038 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007039 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007040 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007041 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007042 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007043 case 8: {
7044 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007045 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007046 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007047 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007048 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007049 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007050 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007051 return ret;
7052 /*
7053 * TODO: we might be squashing a
7054 * KVM_GUESTDBG_SINGLESTEP-triggered
7055 * KVM_EXIT_DEBUG here.
7056 */
Avi Kivity851ba692009-08-24 11:10:17 +03007057 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007058 return 0;
7059 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007060 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007061 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007062 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007063 WARN_ONCE(1, "Guest should always own CR0.TS");
7064 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007065 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007066 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067 case 1: /*mov from cr*/
7068 switch (cr) {
7069 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007070 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007071 val = kvm_read_cr3(vcpu);
7072 kvm_register_write(vcpu, reg, val);
7073 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007074 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007075 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007076 val = kvm_get_cr8(vcpu);
7077 kvm_register_write(vcpu, reg, val);
7078 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007079 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080 }
7081 break;
7082 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007083 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007084 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007085 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086
Kyle Huey6affcbe2016-11-29 12:40:40 -08007087 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088 default:
7089 break;
7090 }
Avi Kivity851ba692009-08-24 11:10:17 +03007091 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007092 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093 (int)(exit_qualification >> 4) & 3, cr);
7094 return 0;
7095}
7096
Avi Kivity851ba692009-08-24 11:10:17 +03007097static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007098{
He, Qingbfdaab02007-09-12 14:18:28 +08007099 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007100 int dr, dr7, reg;
7101
7102 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7103 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7104
7105 /* First, if DR does not exist, trigger UD */
7106 if (!kvm_require_dr(vcpu, dr))
7107 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007108
Jan Kiszkaf2483412010-01-20 18:20:20 +01007109 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007110 if (!kvm_require_cpl(vcpu, 0))
7111 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007112 dr7 = vmcs_readl(GUEST_DR7);
7113 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007114 /*
7115 * As the vm-exit takes precedence over the debug trap, we
7116 * need to emulate the latter, either for the host or the
7117 * guest debugging itself.
7118 */
7119 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007120 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007121 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007122 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007123 vcpu->run->debug.arch.exception = DB_VECTOR;
7124 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 return 0;
7126 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007127 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007128 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007129 kvm_queue_exception(vcpu, DB_VECTOR);
7130 return 1;
7131 }
7132 }
7133
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007134 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007135 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7136 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007137
7138 /*
7139 * No more DR vmexits; force a reload of the debug registers
7140 * and reenter on this instruction. The next vmexit will
7141 * retrieve the full state of the debug registers.
7142 */
7143 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7144 return 1;
7145 }
7146
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007147 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7148 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007149 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007150
7151 if (kvm_get_dr(vcpu, dr, &val))
7152 return 1;
7153 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007154 } else
Nadav Amit57773922014-06-18 17:19:23 +03007155 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007156 return 1;
7157
Kyle Huey6affcbe2016-11-29 12:40:40 -08007158 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159}
7160
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007161static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7162{
7163 return vcpu->arch.dr6;
7164}
7165
7166static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7167{
7168}
7169
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007170static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7171{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007172 get_debugreg(vcpu->arch.db[0], 0);
7173 get_debugreg(vcpu->arch.db[1], 1);
7174 get_debugreg(vcpu->arch.db[2], 2);
7175 get_debugreg(vcpu->arch.db[3], 3);
7176 get_debugreg(vcpu->arch.dr6, 6);
7177 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7178
7179 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007180 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007181}
7182
Gleb Natapov020df072010-04-13 10:05:23 +03007183static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7184{
7185 vmcs_writel(GUEST_DR7, val);
7186}
7187
Avi Kivity851ba692009-08-24 11:10:17 +03007188static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007189{
Kyle Huey6a908b62016-11-29 12:40:37 -08007190 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007191}
7192
Avi Kivity851ba692009-08-24 11:10:17 +03007193static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007194{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007195 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007196 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007197
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007198 msr_info.index = ecx;
7199 msr_info.host_initiated = false;
7200 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007201 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007202 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007203 return 1;
7204 }
7205
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007206 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007207
Avi Kivity6aa8b732006-12-10 02:21:36 -08007208 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007209 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7210 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007211 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007212}
7213
Avi Kivity851ba692009-08-24 11:10:17 +03007214static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007215{
Will Auld8fe8ab42012-11-29 12:42:12 -08007216 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007217 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7218 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7219 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007220
Will Auld8fe8ab42012-11-29 12:42:12 -08007221 msr.data = data;
7222 msr.index = ecx;
7223 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007224 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007225 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007226 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007227 return 1;
7228 }
7229
Avi Kivity59200272010-01-25 19:47:02 +02007230 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007231 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007232}
7233
Avi Kivity851ba692009-08-24 11:10:17 +03007234static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007235{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007236 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007237 return 1;
7238}
7239
Avi Kivity851ba692009-08-24 11:10:17 +03007240static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007242 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7243 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007244
Avi Kivity3842d132010-07-27 12:30:24 +03007245 kvm_make_request(KVM_REQ_EVENT, vcpu);
7246
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007247 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007248 return 1;
7249}
7250
Avi Kivity851ba692009-08-24 11:10:17 +03007251static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252{
Avi Kivityd3bef152007-06-05 15:53:05 +03007253 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254}
7255
Avi Kivity851ba692009-08-24 11:10:17 +03007256static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007257{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007258 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007259}
7260
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007261static int handle_invd(struct kvm_vcpu *vcpu)
7262{
Andre Przywara51d8b662010-12-21 11:12:02 +01007263 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007264}
7265
Avi Kivity851ba692009-08-24 11:10:17 +03007266static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007267{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007268 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007269
7270 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007271 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007272}
7273
Avi Kivityfee84b02011-11-10 14:57:25 +02007274static int handle_rdpmc(struct kvm_vcpu *vcpu)
7275{
7276 int err;
7277
7278 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007279 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007280}
7281
Avi Kivity851ba692009-08-24 11:10:17 +03007282static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007283{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007284 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007285}
7286
Dexuan Cui2acf9232010-06-10 11:27:12 +08007287static int handle_xsetbv(struct kvm_vcpu *vcpu)
7288{
7289 u64 new_bv = kvm_read_edx_eax(vcpu);
7290 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7291
7292 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007293 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007294 return 1;
7295}
7296
Wanpeng Lif53cd632014-12-02 19:14:58 +08007297static int handle_xsaves(struct kvm_vcpu *vcpu)
7298{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007299 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007300 WARN(1, "this should never happen\n");
7301 return 1;
7302}
7303
7304static int handle_xrstors(struct kvm_vcpu *vcpu)
7305{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007306 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007307 WARN(1, "this should never happen\n");
7308 return 1;
7309}
7310
Avi Kivity851ba692009-08-24 11:10:17 +03007311static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007312{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007313 if (likely(fasteoi)) {
7314 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7315 int access_type, offset;
7316
7317 access_type = exit_qualification & APIC_ACCESS_TYPE;
7318 offset = exit_qualification & APIC_ACCESS_OFFSET;
7319 /*
7320 * Sane guest uses MOV to write EOI, with written value
7321 * not cared. So make a short-circuit here by avoiding
7322 * heavy instruction emulation.
7323 */
7324 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7325 (offset == APIC_EOI)) {
7326 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007327 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007328 }
7329 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007330 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007331}
7332
Yang Zhangc7c9c562013-01-25 10:18:51 +08007333static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7334{
7335 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7336 int vector = exit_qualification & 0xff;
7337
7338 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7339 kvm_apic_set_eoi_accelerated(vcpu, vector);
7340 return 1;
7341}
7342
Yang Zhang83d4c282013-01-25 10:18:49 +08007343static int handle_apic_write(struct kvm_vcpu *vcpu)
7344{
7345 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7346 u32 offset = exit_qualification & 0xfff;
7347
7348 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7349 kvm_apic_write_nodecode(vcpu, offset);
7350 return 1;
7351}
7352
Avi Kivity851ba692009-08-24 11:10:17 +03007353static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007354{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007355 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007356 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007357 bool has_error_code = false;
7358 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007359 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007360 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007361
7362 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007363 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007364 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007365
7366 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7367
7368 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007369 if (reason == TASK_SWITCH_GATE && idt_v) {
7370 switch (type) {
7371 case INTR_TYPE_NMI_INTR:
7372 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007373 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007374 break;
7375 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007376 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007377 kvm_clear_interrupt_queue(vcpu);
7378 break;
7379 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007380 if (vmx->idt_vectoring_info &
7381 VECTORING_INFO_DELIVER_CODE_MASK) {
7382 has_error_code = true;
7383 error_code =
7384 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7385 }
7386 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007387 case INTR_TYPE_SOFT_EXCEPTION:
7388 kvm_clear_exception_queue(vcpu);
7389 break;
7390 default:
7391 break;
7392 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007393 }
Izik Eidus37817f22008-03-24 23:14:53 +02007394 tss_selector = exit_qualification;
7395
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007396 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7397 type != INTR_TYPE_EXT_INTR &&
7398 type != INTR_TYPE_NMI_INTR))
7399 skip_emulated_instruction(vcpu);
7400
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007401 if (kvm_task_switch(vcpu, tss_selector,
7402 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7403 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007404 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7405 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7406 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007407 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007408 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007409
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007410 /*
7411 * TODO: What about debug traps on tss switch?
7412 * Are we supposed to inject them and update dr6?
7413 */
7414
7415 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007416}
7417
Avi Kivity851ba692009-08-24 11:10:17 +03007418static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007419{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007420 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007421 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007422 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007423
Sheng Yangf9c617f2009-03-25 10:08:52 +08007424 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007425
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007426 /*
7427 * EPT violation happened while executing iret from NMI,
7428 * "blocked by NMI" bit has to be set before next VM entry.
7429 * There are errata that may cause this bit to not be set:
7430 * AAK134, BY25.
7431 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007432 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007433 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007434 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007435 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7436
Sheng Yang14394422008-04-28 12:24:45 +08007437 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007438 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007439
Junaid Shahid27959a42016-12-06 16:46:10 -08007440 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007441 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007442 ? PFERR_USER_MASK : 0;
7443 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007444 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007445 ? PFERR_WRITE_MASK : 0;
7446 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007447 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007448 ? PFERR_FETCH_MASK : 0;
7449 /* ept page table entry is present? */
7450 error_code |= (exit_qualification &
7451 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7452 EPT_VIOLATION_EXECUTABLE))
7453 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007454
Paolo Bonzinieebed242016-11-28 14:39:58 +01007455 error_code |= (exit_qualification & 0x100) != 0 ?
7456 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007457
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007458 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007459 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007460}
7461
Avi Kivity851ba692009-08-24 11:10:17 +03007462static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007463{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007464 gpa_t gpa;
7465
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007466 /*
7467 * A nested guest cannot optimize MMIO vmexits, because we have an
7468 * nGPA here instead of the required GPA.
7469 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007470 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007471 if (!is_guest_mode(vcpu) &&
7472 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007473 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007474 /*
7475 * Doing kvm_skip_emulated_instruction() depends on undefined
7476 * behavior: Intel's manual doesn't mandate
7477 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7478 * occurs and while on real hardware it was observed to be set,
7479 * other hypervisors (namely Hyper-V) don't set it, we end up
7480 * advancing IP with some random value. Disable fast mmio when
7481 * running nested and keep it for real hardware in hope that
7482 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7483 */
7484 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7485 return kvm_skip_emulated_instruction(vcpu);
7486 else
7487 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7488 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007489 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007490
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007491 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007492}
7493
Avi Kivity851ba692009-08-24 11:10:17 +03007494static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007495{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007496 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007497 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7498 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007499 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007500 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007501
7502 return 1;
7503}
7504
Mohammed Gamal80ced182009-09-01 12:48:18 +02007505static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007506{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007507 struct vcpu_vmx *vmx = to_vmx(vcpu);
7508 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007509 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007510 u32 cpu_exec_ctrl;
7511 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007512 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007513
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007514 /*
7515 * We should never reach the point where we are emulating L2
7516 * due to invalid guest state as that means we incorrectly
7517 * allowed a nested VMEntry with an invalid vmcs12.
7518 */
7519 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7520
Avi Kivity49e9d552010-09-19 14:34:08 +02007521 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7522 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007523
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007524 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007525 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007526 return handle_interrupt_window(&vmx->vcpu);
7527
Radim Krčmář72875d82017-04-26 22:32:19 +02007528 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007529 return 1;
7530
Liran Alon9b8ae632017-11-05 16:56:34 +02007531 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007532
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007533 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007534 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007535 ret = 0;
7536 goto out;
7537 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007538
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007539 if (err != EMULATE_DONE)
7540 goto emulation_error;
7541
7542 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7543 vcpu->arch.exception.pending)
7544 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007545
Gleb Natapov8d76c492013-05-08 18:38:44 +03007546 if (vcpu->arch.halt_request) {
7547 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007548 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007549 goto out;
7550 }
7551
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007552 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007553 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007554 if (need_resched())
7555 schedule();
7556 }
7557
Mohammed Gamal80ced182009-09-01 12:48:18 +02007558out:
7559 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007560
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007561emulation_error:
7562 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7563 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7564 vcpu->run->internal.ndata = 0;
7565 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007566}
7567
7568static void grow_ple_window(struct kvm_vcpu *vcpu)
7569{
7570 struct vcpu_vmx *vmx = to_vmx(vcpu);
7571 int old = vmx->ple_window;
7572
Babu Mogerc8e88712018-03-16 16:37:24 -04007573 vmx->ple_window = __grow_ple_window(old, ple_window,
7574 ple_window_grow,
7575 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007576
7577 if (vmx->ple_window != old)
7578 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007579
7580 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007581}
7582
7583static void shrink_ple_window(struct kvm_vcpu *vcpu)
7584{
7585 struct vcpu_vmx *vmx = to_vmx(vcpu);
7586 int old = vmx->ple_window;
7587
Babu Mogerc8e88712018-03-16 16:37:24 -04007588 vmx->ple_window = __shrink_ple_window(old, ple_window,
7589 ple_window_shrink,
7590 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007591
7592 if (vmx->ple_window != old)
7593 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007594
7595 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007596}
7597
7598/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007599 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7600 */
7601static void wakeup_handler(void)
7602{
7603 struct kvm_vcpu *vcpu;
7604 int cpu = smp_processor_id();
7605
7606 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7607 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7608 blocked_vcpu_list) {
7609 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7610
7611 if (pi_test_on(pi_desc) == 1)
7612 kvm_vcpu_kick(vcpu);
7613 }
7614 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7615}
7616
Peng Haoe01bca22018-04-07 05:47:32 +08007617static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007618{
7619 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7620 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7621 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7622 0ull, VMX_EPT_EXECUTABLE_MASK,
7623 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007624 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007625
7626 ept_set_mmio_spte_mask();
7627 kvm_enable_tdp();
7628}
7629
Tiejun Chenf2c76482014-10-28 10:14:47 +08007630static __init int hardware_setup(void)
7631{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007632 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007633 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007634
7635 rdmsrl_safe(MSR_EFER, &host_efer);
7636
7637 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7638 kvm_define_shared_msr(i, vmx_msr_index[i]);
7639
Radim Krčmář23611332016-09-29 22:41:33 +02007640 for (i = 0; i < VMX_BITMAP_NR; i++) {
7641 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7642 if (!vmx_bitmap[i])
7643 goto out;
7644 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007645
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007646 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7647 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7648
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007649 if (setup_vmcs_config(&vmcs_config) < 0) {
7650 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007651 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007652 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007653
7654 if (boot_cpu_has(X86_FEATURE_NX))
7655 kvm_enable_efer_bits(EFER_NX);
7656
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007657 if (boot_cpu_has(X86_FEATURE_MPX)) {
7658 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7659 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7660 }
7661
Wanpeng Li08d839c2017-03-23 05:30:08 -07007662 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7663 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007664 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007665
Tiejun Chenf2c76482014-10-28 10:14:47 +08007666 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007667 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007668 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007669 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007670 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007671
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007672 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007673 enable_ept_ad_bits = 0;
7674
Wanpeng Li8ad81822017-10-09 15:51:53 -07007675 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007676 enable_unrestricted_guest = 0;
7677
Paolo Bonziniad15a292015-01-30 16:18:49 +01007678 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007679 flexpriority_enabled = 0;
7680
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007681 if (!cpu_has_virtual_nmis())
7682 enable_vnmi = 0;
7683
Paolo Bonziniad15a292015-01-30 16:18:49 +01007684 /*
7685 * set_apic_access_page_addr() is used to reload apic access
7686 * page upon invalidation. No need to do anything if not
7687 * using the APIC_ACCESS_ADDR VMCS field.
7688 */
7689 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007690 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007691
7692 if (!cpu_has_vmx_tpr_shadow())
7693 kvm_x86_ops->update_cr8_intercept = NULL;
7694
7695 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7696 kvm_disable_largepages();
7697
Tianyu Lan877ad952018-07-19 08:40:23 +00007698#if IS_ENABLED(CONFIG_HYPERV)
7699 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7700 && enable_ept)
7701 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7702#endif
7703
Wanpeng Li0f107682017-09-28 18:06:24 -07007704 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007706 ple_window = 0;
7707 ple_window_grow = 0;
7708 ple_window_max = 0;
7709 ple_window_shrink = 0;
7710 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007711
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007712 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007713 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007714 kvm_x86_ops->sync_pir_to_irr = NULL;
7715 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007716
Haozhong Zhang64903d62015-10-20 15:39:09 +08007717 if (cpu_has_vmx_tsc_scaling()) {
7718 kvm_has_tsc_control = true;
7719 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7720 kvm_tsc_scaling_ratio_frac_bits = 48;
7721 }
7722
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007723 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7724
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007725 if (enable_ept)
7726 vmx_enable_tdp();
7727 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007728 kvm_disable_tdp();
7729
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007730 if (!nested) {
7731 kvm_x86_ops->get_nested_state = NULL;
7732 kvm_x86_ops->set_nested_state = NULL;
7733 }
7734
Kai Huang843e4332015-01-28 10:54:28 +08007735 /*
7736 * Only enable PML when hardware supports PML feature, and both EPT
7737 * and EPT A/D bit features are enabled -- PML depends on them to work.
7738 */
7739 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7740 enable_pml = 0;
7741
7742 if (!enable_pml) {
7743 kvm_x86_ops->slot_enable_log_dirty = NULL;
7744 kvm_x86_ops->slot_disable_log_dirty = NULL;
7745 kvm_x86_ops->flush_log_dirty = NULL;
7746 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7747 }
7748
Yunhong Jiang64672c92016-06-13 14:19:59 -07007749 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7750 u64 vmx_msr;
7751
7752 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7753 cpu_preemption_timer_multi =
7754 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7755 } else {
7756 kvm_x86_ops->set_hv_timer = NULL;
7757 kvm_x86_ops->cancel_hv_timer = NULL;
7758 }
7759
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007760 if (!cpu_has_vmx_shadow_vmcs())
7761 enable_shadow_vmcs = 0;
7762 if (enable_shadow_vmcs)
7763 init_vmcs_shadow_fields();
7764
Feng Wubf9f6ac2015-09-18 22:29:55 +08007765 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007766 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007767
Ashok Rajc45dcc72016-06-22 14:59:56 +08007768 kvm_mce_cap_supported |= MCG_LMCE_P;
7769
Tiejun Chenf2c76482014-10-28 10:14:47 +08007770 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007771
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007772out:
Radim Krčmář23611332016-09-29 22:41:33 +02007773 for (i = 0; i < VMX_BITMAP_NR; i++)
7774 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007775
7776 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007777}
7778
7779static __exit void hardware_unsetup(void)
7780{
Radim Krčmář23611332016-09-29 22:41:33 +02007781 int i;
7782
7783 for (i = 0; i < VMX_BITMAP_NR; i++)
7784 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007785
Tiejun Chenf2c76482014-10-28 10:14:47 +08007786 free_kvm_area();
7787}
7788
Avi Kivity6aa8b732006-12-10 02:21:36 -08007789/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007790 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7791 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7792 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007793static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007794{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007795 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007796 grow_ple_window(vcpu);
7797
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007798 /*
7799 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7800 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7801 * never set PAUSE_EXITING and just set PLE if supported,
7802 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7803 */
7804 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007805 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007806}
7807
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007808static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007809{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007810 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007811}
7812
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007813static int handle_mwait(struct kvm_vcpu *vcpu)
7814{
7815 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7816 return handle_nop(vcpu);
7817}
7818
Jim Mattson45ec3682017-08-23 16:32:04 -07007819static int handle_invalid_op(struct kvm_vcpu *vcpu)
7820{
7821 kvm_queue_exception(vcpu, UD_VECTOR);
7822 return 1;
7823}
7824
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007825static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7826{
7827 return 1;
7828}
7829
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007830static int handle_monitor(struct kvm_vcpu *vcpu)
7831{
7832 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7833 return handle_nop(vcpu);
7834}
7835
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007836/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007837 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7838 * set the success or error code of an emulated VMX instruction, as specified
7839 * by Vol 2B, VMX Instruction Reference, "Conventions".
7840 */
7841static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7842{
7843 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7844 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7845 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7846}
7847
7848static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7849{
7850 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7851 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7852 X86_EFLAGS_SF | X86_EFLAGS_OF))
7853 | X86_EFLAGS_CF);
7854}
7855
Abel Gordon145c28d2013-04-18 14:36:55 +03007856static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007857 u32 vm_instruction_error)
7858{
7859 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7860 /*
7861 * failValid writes the error number to the current VMCS, which
7862 * can't be done there isn't a current VMCS.
7863 */
7864 nested_vmx_failInvalid(vcpu);
7865 return;
7866 }
7867 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7868 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7869 X86_EFLAGS_SF | X86_EFLAGS_OF))
7870 | X86_EFLAGS_ZF);
7871 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7872 /*
7873 * We don't need to force a shadow sync because
7874 * VM_INSTRUCTION_ERROR is not shadowed
7875 */
7876}
Abel Gordon145c28d2013-04-18 14:36:55 +03007877
Wincy Vanff651cb2014-12-11 08:52:58 +03007878static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7879{
7880 /* TODO: not to reset guest simply here. */
7881 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007882 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007883}
7884
Jan Kiszkaf41245002014-03-07 20:03:13 +01007885static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7886{
7887 struct vcpu_vmx *vmx =
7888 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7889
7890 vmx->nested.preemption_timer_expired = true;
7891 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7892 kvm_vcpu_kick(&vmx->vcpu);
7893
7894 return HRTIMER_NORESTART;
7895}
7896
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007897/*
Bandan Das19677e32014-05-06 02:19:15 -04007898 * Decode the memory-address operand of a vmx instruction, as recorded on an
7899 * exit caused by such an instruction (run by a guest hypervisor).
7900 * On success, returns 0. When the operand is invalid, returns 1 and throws
7901 * #UD or #GP.
7902 */
7903static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7904 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007905 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007906{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007907 gva_t off;
7908 bool exn;
7909 struct kvm_segment s;
7910
Bandan Das19677e32014-05-06 02:19:15 -04007911 /*
7912 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7913 * Execution", on an exit, vmx_instruction_info holds most of the
7914 * addressing components of the operand. Only the displacement part
7915 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7916 * For how an actual address is calculated from all these components,
7917 * refer to Vol. 1, "Operand Addressing".
7918 */
7919 int scaling = vmx_instruction_info & 3;
7920 int addr_size = (vmx_instruction_info >> 7) & 7;
7921 bool is_reg = vmx_instruction_info & (1u << 10);
7922 int seg_reg = (vmx_instruction_info >> 15) & 7;
7923 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7924 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7925 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7926 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7927
7928 if (is_reg) {
7929 kvm_queue_exception(vcpu, UD_VECTOR);
7930 return 1;
7931 }
7932
7933 /* Addr = segment_base + offset */
7934 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007935 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007936 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007937 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007938 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007939 off += kvm_register_read(vcpu, index_reg)<<scaling;
7940 vmx_get_segment(vcpu, &s, seg_reg);
7941 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007942
7943 if (addr_size == 1) /* 32 bit */
7944 *ret &= 0xffffffff;
7945
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007946 /* Checks for #GP/#SS exceptions. */
7947 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007948 if (is_long_mode(vcpu)) {
7949 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7950 * non-canonical form. This is the only check on the memory
7951 * destination for long mode!
7952 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007953 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007954 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007955 /* Protected mode: apply checks for segment validity in the
7956 * following order:
7957 * - segment type check (#GP(0) may be thrown)
7958 * - usability check (#GP(0)/#SS(0))
7959 * - limit check (#GP(0)/#SS(0))
7960 */
7961 if (wr)
7962 /* #GP(0) if the destination operand is located in a
7963 * read-only data segment or any code segment.
7964 */
7965 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7966 else
7967 /* #GP(0) if the source operand is located in an
7968 * execute-only code segment
7969 */
7970 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007971 if (exn) {
7972 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7973 return 1;
7974 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007975 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7976 */
7977 exn = (s.unusable != 0);
7978 /* Protected mode: #GP(0)/#SS(0) if the memory
7979 * operand is outside the segment limit.
7980 */
7981 exn = exn || (off + sizeof(u64) > s.limit);
7982 }
7983 if (exn) {
7984 kvm_queue_exception_e(vcpu,
7985 seg_reg == VCPU_SREG_SS ?
7986 SS_VECTOR : GP_VECTOR,
7987 0);
7988 return 1;
7989 }
7990
Bandan Das19677e32014-05-06 02:19:15 -04007991 return 0;
7992}
7993
Radim Krčmářcbf71272017-05-19 15:48:51 +02007994static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007995{
7996 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007997 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007998
7999 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008000 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008001 return 1;
8002
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008003 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008004 kvm_inject_page_fault(vcpu, &e);
8005 return 1;
8006 }
8007
Bandan Das3573e222014-05-06 02:19:16 -04008008 return 0;
8009}
8010
Liran Alonabfc52c2018-06-23 02:35:13 +03008011/*
8012 * Allocate a shadow VMCS and associate it with the currently loaded
8013 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8014 * VMCS is also VMCLEARed, so that it is ready for use.
8015 */
8016static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8017{
8018 struct vcpu_vmx *vmx = to_vmx(vcpu);
8019 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8020
8021 /*
8022 * We should allocate a shadow vmcs for vmcs01 only when L1
8023 * executes VMXON and free it when L1 executes VMXOFF.
8024 * As it is invalid to execute VMXON twice, we shouldn't reach
8025 * here when vmcs01 already have an allocated shadow vmcs.
8026 */
8027 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8028
8029 if (!loaded_vmcs->shadow_vmcs) {
8030 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8031 if (loaded_vmcs->shadow_vmcs)
8032 vmcs_clear(loaded_vmcs->shadow_vmcs);
8033 }
8034 return loaded_vmcs->shadow_vmcs;
8035}
8036
Jim Mattsone29acc52016-11-30 12:03:43 -08008037static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8038{
8039 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008040 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008041
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008042 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8043 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008044 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008045
8046 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8047 if (!vmx->nested.cached_vmcs12)
8048 goto out_cached_vmcs12;
8049
Liran Alon61ada742018-06-23 02:35:08 +03008050 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8051 if (!vmx->nested.cached_shadow_vmcs12)
8052 goto out_cached_shadow_vmcs12;
8053
Liran Alonabfc52c2018-06-23 02:35:13 +03008054 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8055 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008056
Jim Mattsone29acc52016-11-30 12:03:43 -08008057 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8058 HRTIMER_MODE_REL_PINNED);
8059 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8060
8061 vmx->nested.vmxon = true;
8062 return 0;
8063
8064out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008065 kfree(vmx->nested.cached_shadow_vmcs12);
8066
8067out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008068 kfree(vmx->nested.cached_vmcs12);
8069
8070out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008071 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008072
Jim Mattsonde3a0022017-11-27 17:22:25 -06008073out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008074 return -ENOMEM;
8075}
8076
Bandan Das3573e222014-05-06 02:19:16 -04008077/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008078 * Emulate the VMXON instruction.
8079 * Currently, we just remember that VMX is active, and do not save or even
8080 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8081 * do not currently need to store anything in that guest-allocated memory
8082 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8083 * argument is different from the VMXON pointer (which the spec says they do).
8084 */
8085static int handle_vmon(struct kvm_vcpu *vcpu)
8086{
Jim Mattsone29acc52016-11-30 12:03:43 -08008087 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008088 gpa_t vmptr;
8089 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008090 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008091 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8092 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008093
Jim Mattson70f3aac2017-04-26 08:53:46 -07008094 /*
8095 * The Intel VMX Instruction Reference lists a bunch of bits that are
8096 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8097 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8098 * Otherwise, we should fail with #UD. But most faulting conditions
8099 * have already been checked by hardware, prior to the VM-exit for
8100 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8101 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008102 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008103 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008104 kvm_queue_exception(vcpu, UD_VECTOR);
8105 return 1;
8106 }
8107
Felix Wilhelm727ba742018-06-11 09:43:44 +02008108 /* CPL=0 must be checked manually. */
8109 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008110 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008111 return 1;
8112 }
8113
Abel Gordon145c28d2013-04-18 14:36:55 +03008114 if (vmx->nested.vmxon) {
8115 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008116 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008117 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008118
Haozhong Zhang3b840802016-06-22 14:59:54 +08008119 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008120 != VMXON_NEEDED_FEATURES) {
8121 kvm_inject_gp(vcpu, 0);
8122 return 1;
8123 }
8124
Radim Krčmářcbf71272017-05-19 15:48:51 +02008125 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008126 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008127
8128 /*
8129 * SDM 3: 24.11.5
8130 * The first 4 bytes of VMXON region contain the supported
8131 * VMCS revision identifier
8132 *
8133 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8134 * which replaces physical address width with 32
8135 */
8136 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8137 nested_vmx_failInvalid(vcpu);
8138 return kvm_skip_emulated_instruction(vcpu);
8139 }
8140
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008141 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8142 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008143 nested_vmx_failInvalid(vcpu);
8144 return kvm_skip_emulated_instruction(vcpu);
8145 }
8146 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8147 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008148 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008149 nested_vmx_failInvalid(vcpu);
8150 return kvm_skip_emulated_instruction(vcpu);
8151 }
8152 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008153 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008154
8155 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008156 ret = enter_vmx_operation(vcpu);
8157 if (ret)
8158 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008159
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008160 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008161 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008162}
8163
8164/*
8165 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8166 * for running VMX instructions (except VMXON, whose prerequisites are
8167 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008168 * Note that many of these exceptions have priority over VM exits, so they
8169 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008170 */
8171static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8172{
Jim Mattsone49fcb82018-07-27 13:44:45 -07008173 if (!to_vmx(vcpu)->nested.vmxon) {
8174 kvm_queue_exception(vcpu, UD_VECTOR);
8175 return 0;
8176 }
8177
Felix Wilhelm727ba742018-06-11 09:43:44 +02008178 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008179 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008180 return 0;
8181 }
8182
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008183 return 1;
8184}
8185
David Matlack8ca44e82017-08-01 14:00:39 -07008186static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8187{
8188 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8189 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8190}
8191
Abel Gordone7953d72013-04-18 14:37:55 +03008192static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8193{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008194 if (vmx->nested.current_vmptr == -1ull)
8195 return;
8196
Abel Gordon012f83c2013-04-18 14:39:25 +03008197 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008198 /* copy to memory all shadowed fields in case
8199 they were modified */
8200 copy_shadow_to_vmcs12(vmx);
8201 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008202 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008203 }
Wincy Van705699a2015-02-03 23:58:17 +08008204 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008205
8206 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008207 kvm_vcpu_write_guest_page(&vmx->vcpu,
8208 vmx->nested.current_vmptr >> PAGE_SHIFT,
8209 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008210
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008211 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008212}
8213
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008214/*
8215 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8216 * just stops using VMX.
8217 */
8218static void free_nested(struct vcpu_vmx *vmx)
8219{
Wanpeng Lib7455822017-11-22 14:04:00 -08008220 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008221 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008222
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008223 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008224 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008225 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008226 vmx->nested.posted_intr_nv = -1;
8227 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008228 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008229 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008230 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8231 free_vmcs(vmx->vmcs01.shadow_vmcs);
8232 vmx->vmcs01.shadow_vmcs = NULL;
8233 }
David Matlack4f2777b2016-07-13 17:16:37 -07008234 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008235 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008236 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008237 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008238 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008239 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008240 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008241 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008242 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008243 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008244 }
Wincy Van705699a2015-02-03 23:58:17 +08008245 if (vmx->nested.pi_desc_page) {
8246 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008247 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008248 vmx->nested.pi_desc_page = NULL;
8249 vmx->nested.pi_desc = NULL;
8250 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008251
Jim Mattsonde3a0022017-11-27 17:22:25 -06008252 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008253}
8254
8255/* Emulate the VMXOFF instruction */
8256static int handle_vmoff(struct kvm_vcpu *vcpu)
8257{
8258 if (!nested_vmx_check_permission(vcpu))
8259 return 1;
8260 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008261 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008262 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008263}
8264
Nadav Har'El27d6c862011-05-25 23:06:59 +03008265/* Emulate the VMCLEAR instruction */
8266static int handle_vmclear(struct kvm_vcpu *vcpu)
8267{
8268 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008269 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008270 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008271
8272 if (!nested_vmx_check_permission(vcpu))
8273 return 1;
8274
Radim Krčmářcbf71272017-05-19 15:48:51 +02008275 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008276 return 1;
8277
Radim Krčmářcbf71272017-05-19 15:48:51 +02008278 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8279 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8280 return kvm_skip_emulated_instruction(vcpu);
8281 }
8282
8283 if (vmptr == vmx->nested.vmxon_ptr) {
8284 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8285 return kvm_skip_emulated_instruction(vcpu);
8286 }
8287
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008288 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008289 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008290
Jim Mattson587d7e722017-03-02 12:41:48 -08008291 kvm_vcpu_write_guest(vcpu,
8292 vmptr + offsetof(struct vmcs12, launch_state),
8293 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008294
Nadav Har'El27d6c862011-05-25 23:06:59 +03008295 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008296 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008297}
8298
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008299static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8300
8301/* Emulate the VMLAUNCH instruction */
8302static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8303{
8304 return nested_vmx_run(vcpu, true);
8305}
8306
8307/* Emulate the VMRESUME instruction */
8308static int handle_vmresume(struct kvm_vcpu *vcpu)
8309{
8310
8311 return nested_vmx_run(vcpu, false);
8312}
8313
Nadav Har'El49f705c2011-05-25 23:08:30 +03008314/*
8315 * Read a vmcs12 field. Since these can have varying lengths and we return
8316 * one type, we chose the biggest type (u64) and zero-extend the return value
8317 * to that size. Note that the caller, handle_vmread, might need to use only
8318 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8319 * 64-bit fields are to be returned).
8320 */
Liran Alone2536742018-06-23 02:35:02 +03008321static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008322 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323{
8324 short offset = vmcs_field_to_offset(field);
8325 char *p;
8326
8327 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008328 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008329
Liran Alone2536742018-06-23 02:35:02 +03008330 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008331
Jim Mattsond37f4262017-12-22 12:12:16 -08008332 switch (vmcs_field_width(field)) {
8333 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008334 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008335 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008336 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008337 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008338 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008339 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008341 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008342 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008343 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008344 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008345 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008346 WARN_ON(1);
8347 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008348 }
8349}
8350
Abel Gordon20b97fe2013-04-18 14:36:25 +03008351
Liran Alone2536742018-06-23 02:35:02 +03008352static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008353 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008354 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008355 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008356 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008357 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008358
Jim Mattsond37f4262017-12-22 12:12:16 -08008359 switch (vmcs_field_width(field)) {
8360 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008361 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008362 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008363 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008364 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008365 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008366 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008367 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008368 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008369 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008370 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008371 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008372 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008373 WARN_ON(1);
8374 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008375 }
8376
8377}
8378
Jim Mattsonf4160e42018-05-29 09:11:33 -07008379/*
8380 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8381 * they have been modified by the L1 guest. Note that the "read-only"
8382 * VM-exit information fields are actually writable if the vCPU is
8383 * configured to support "VMWRITE to any supported field in the VMCS."
8384 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008385static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8386{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008387 const u16 *fields[] = {
8388 shadow_read_write_fields,
8389 shadow_read_only_fields
8390 };
8391 const int max_fields[] = {
8392 max_shadow_read_write_fields,
8393 max_shadow_read_only_fields
8394 };
8395 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008396 unsigned long field;
8397 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008398 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008399
Jan Kiszka282da872014-10-08 18:05:39 +02008400 preempt_disable();
8401
Abel Gordon16f5b902013-04-18 14:38:25 +03008402 vmcs_load(shadow_vmcs);
8403
Jim Mattsonf4160e42018-05-29 09:11:33 -07008404 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8405 for (i = 0; i < max_fields[q]; i++) {
8406 field = fields[q][i];
8407 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008408 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008409 }
8410 /*
8411 * Skip the VM-exit information fields if they are read-only.
8412 */
8413 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8414 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008415 }
8416
8417 vmcs_clear(shadow_vmcs);
8418 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008419
8420 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008421}
8422
Abel Gordonc3114422013-04-18 14:38:55 +03008423static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8424{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008425 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008426 shadow_read_write_fields,
8427 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008428 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008429 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008430 max_shadow_read_write_fields,
8431 max_shadow_read_only_fields
8432 };
8433 int i, q;
8434 unsigned long field;
8435 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008436 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008437
8438 vmcs_load(shadow_vmcs);
8439
Mathias Krausec2bae892013-06-26 20:36:21 +02008440 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008441 for (i = 0; i < max_fields[q]; i++) {
8442 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008443 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008444 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008445 }
8446 }
8447
8448 vmcs_clear(shadow_vmcs);
8449 vmcs_load(vmx->loaded_vmcs->vmcs);
8450}
8451
Nadav Har'El49f705c2011-05-25 23:08:30 +03008452/*
8453 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8454 * used before) all generate the same failure when it is missing.
8455 */
8456static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8457{
8458 struct vcpu_vmx *vmx = to_vmx(vcpu);
8459 if (vmx->nested.current_vmptr == -1ull) {
8460 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008461 return 0;
8462 }
8463 return 1;
8464}
8465
8466static int handle_vmread(struct kvm_vcpu *vcpu)
8467{
8468 unsigned long field;
8469 u64 field_value;
8470 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8471 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8472 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008473 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008474
Kyle Hueyeb277562016-11-29 12:40:39 -08008475 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008476 return 1;
8477
Kyle Huey6affcbe2016-11-29 12:40:40 -08008478 if (!nested_vmx_check_vmcs12(vcpu))
8479 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008480
Liran Alon6d894f42018-06-23 02:35:09 +03008481 if (!is_guest_mode(vcpu))
8482 vmcs12 = get_vmcs12(vcpu);
8483 else {
8484 /*
8485 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8486 * to shadowed-field sets the ALU flags for VMfailInvalid.
8487 */
8488 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8489 nested_vmx_failInvalid(vcpu);
8490 return kvm_skip_emulated_instruction(vcpu);
8491 }
8492 vmcs12 = get_shadow_vmcs12(vcpu);
8493 }
8494
Nadav Har'El49f705c2011-05-25 23:08:30 +03008495 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008496 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008497 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008498 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008499 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008500 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008501 }
8502 /*
8503 * Now copy part of this value to register or memory, as requested.
8504 * Note that the number of bits actually copied is 32 or 64 depending
8505 * on the guest's mode (32 or 64 bit), not on the given field's length.
8506 */
8507 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008508 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008509 field_value);
8510 } else {
8511 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008512 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008513 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008514 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008515 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8516 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008517 }
8518
8519 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008520 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521}
8522
8523
8524static int handle_vmwrite(struct kvm_vcpu *vcpu)
8525{
8526 unsigned long field;
8527 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008528 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008529 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8530 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008531
Nadav Har'El49f705c2011-05-25 23:08:30 +03008532 /* The value to write might be 32 or 64 bits, depending on L1's long
8533 * mode, and eventually we need to write that into a field of several
8534 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008535 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536 * bits into the vmcs12 field.
8537 */
8538 u64 field_value = 0;
8539 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008540 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008541
Kyle Hueyeb277562016-11-29 12:40:39 -08008542 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543 return 1;
8544
Kyle Huey6affcbe2016-11-29 12:40:40 -08008545 if (!nested_vmx_check_vmcs12(vcpu))
8546 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008547
Nadav Har'El49f705c2011-05-25 23:08:30 +03008548 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008549 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008550 (((vmx_instruction_info) >> 3) & 0xf));
8551 else {
8552 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008553 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008554 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008555 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8556 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008557 kvm_inject_page_fault(vcpu, &e);
8558 return 1;
8559 }
8560 }
8561
8562
Nadav Amit27e6fb52014-06-18 17:19:26 +03008563 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008564 /*
8565 * If the vCPU supports "VMWRITE to any supported field in the
8566 * VMCS," then the "read-only" fields are actually read/write.
8567 */
8568 if (vmcs_field_readonly(field) &&
8569 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 nested_vmx_failValid(vcpu,
8571 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008572 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 }
8574
Liran Alon6d894f42018-06-23 02:35:09 +03008575 if (!is_guest_mode(vcpu))
8576 vmcs12 = get_vmcs12(vcpu);
8577 else {
8578 /*
8579 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8580 * to shadowed-field sets the ALU flags for VMfailInvalid.
8581 */
8582 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8583 nested_vmx_failInvalid(vcpu);
8584 return kvm_skip_emulated_instruction(vcpu);
8585 }
8586 vmcs12 = get_shadow_vmcs12(vcpu);
8587
8588 }
8589
8590 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008591 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008592 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008593 }
8594
Liran Alon6d894f42018-06-23 02:35:09 +03008595 /*
8596 * Do not track vmcs12 dirty-state if in guest-mode
8597 * as we actually dirty shadow vmcs12 instead of vmcs12.
8598 */
8599 if (!is_guest_mode(vcpu)) {
8600 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008601#define SHADOW_FIELD_RW(x) case x:
8602#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008603 /*
8604 * The fields that can be updated by L1 without a vmexit are
8605 * always updated in the vmcs02, the others go down the slow
8606 * path of prepare_vmcs02.
8607 */
8608 break;
8609 default:
8610 vmx->nested.dirty_vmcs12 = true;
8611 break;
8612 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008613 }
8614
Nadav Har'El49f705c2011-05-25 23:08:30 +03008615 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008616 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008617}
8618
Jim Mattsona8bc2842016-11-30 12:03:44 -08008619static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8620{
8621 vmx->nested.current_vmptr = vmptr;
8622 if (enable_shadow_vmcs) {
8623 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8624 SECONDARY_EXEC_SHADOW_VMCS);
8625 vmcs_write64(VMCS_LINK_POINTER,
8626 __pa(vmx->vmcs01.shadow_vmcs));
8627 vmx->nested.sync_shadow_vmcs = true;
8628 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008629 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008630}
8631
Nadav Har'El63846662011-05-25 23:07:29 +03008632/* Emulate the VMPTRLD instruction */
8633static int handle_vmptrld(struct kvm_vcpu *vcpu)
8634{
8635 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008636 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008637
8638 if (!nested_vmx_check_permission(vcpu))
8639 return 1;
8640
Radim Krčmářcbf71272017-05-19 15:48:51 +02008641 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008642 return 1;
8643
Radim Krčmářcbf71272017-05-19 15:48:51 +02008644 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8645 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8646 return kvm_skip_emulated_instruction(vcpu);
8647 }
8648
8649 if (vmptr == vmx->nested.vmxon_ptr) {
8650 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8651 return kvm_skip_emulated_instruction(vcpu);
8652 }
8653
Nadav Har'El63846662011-05-25 23:07:29 +03008654 if (vmx->nested.current_vmptr != vmptr) {
8655 struct vmcs12 *new_vmcs12;
8656 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008657 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8658 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008659 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008660 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008661 }
8662 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008663 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008664 (new_vmcs12->hdr.shadow_vmcs &&
8665 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008666 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008667 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008668 nested_vmx_failValid(vcpu,
8669 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008670 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008671 }
Nadav Har'El63846662011-05-25 23:07:29 +03008672
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008673 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008674 /*
8675 * Load VMCS12 from guest memory since it is not already
8676 * cached.
8677 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008678 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8679 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008680 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008681
Jim Mattsona8bc2842016-11-30 12:03:44 -08008682 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008683 }
8684
8685 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008686 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008687}
8688
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008689/* Emulate the VMPTRST instruction */
8690static int handle_vmptrst(struct kvm_vcpu *vcpu)
8691{
8692 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8693 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8694 gva_t vmcs_gva;
8695 struct x86_exception e;
8696
8697 if (!nested_vmx_check_permission(vcpu))
8698 return 1;
8699
8700 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008701 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008702 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008703 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008704 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8705 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8706 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008707 kvm_inject_page_fault(vcpu, &e);
8708 return 1;
8709 }
8710 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008711 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008712}
8713
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008714/* Emulate the INVEPT instruction */
8715static int handle_invept(struct kvm_vcpu *vcpu)
8716{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008717 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008718 u32 vmx_instruction_info, types;
8719 unsigned long type;
8720 gva_t gva;
8721 struct x86_exception e;
8722 struct {
8723 u64 eptp, gpa;
8724 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008725
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008726 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008727 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008728 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008729 kvm_queue_exception(vcpu, UD_VECTOR);
8730 return 1;
8731 }
8732
8733 if (!nested_vmx_check_permission(vcpu))
8734 return 1;
8735
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008736 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008737 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008738
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008739 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008740
Jim Mattson85c856b2016-10-26 08:38:38 -07008741 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008742 nested_vmx_failValid(vcpu,
8743 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008744 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008745 }
8746
8747 /* According to the Intel VMX instruction reference, the memory
8748 * operand is read even if it isn't needed (e.g., for type==global)
8749 */
8750 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008751 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008752 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008753 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008754 kvm_inject_page_fault(vcpu, &e);
8755 return 1;
8756 }
8757
8758 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008759 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008760 /*
8761 * TODO: track mappings and invalidate
8762 * single context requests appropriately
8763 */
8764 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008765 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008766 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008767 nested_vmx_succeed(vcpu);
8768 break;
8769 default:
8770 BUG_ON(1);
8771 break;
8772 }
8773
Kyle Huey6affcbe2016-11-29 12:40:40 -08008774 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008775}
8776
Petr Matouseka642fc32014-09-23 20:22:30 +02008777static int handle_invvpid(struct kvm_vcpu *vcpu)
8778{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008779 struct vcpu_vmx *vmx = to_vmx(vcpu);
8780 u32 vmx_instruction_info;
8781 unsigned long type, types;
8782 gva_t gva;
8783 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008784 struct {
8785 u64 vpid;
8786 u64 gla;
8787 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008788
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008789 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008790 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008791 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008792 kvm_queue_exception(vcpu, UD_VECTOR);
8793 return 1;
8794 }
8795
8796 if (!nested_vmx_check_permission(vcpu))
8797 return 1;
8798
8799 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8800 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8801
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008802 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008803 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008804
Jim Mattson85c856b2016-10-26 08:38:38 -07008805 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008806 nested_vmx_failValid(vcpu,
8807 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008808 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008809 }
8810
8811 /* according to the intel vmx instruction reference, the memory
8812 * operand is read even if it isn't needed (e.g., for type==global)
8813 */
8814 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8815 vmx_instruction_info, false, &gva))
8816 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008817 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008818 kvm_inject_page_fault(vcpu, &e);
8819 return 1;
8820 }
Jim Mattson40352602017-06-28 09:37:37 -07008821 if (operand.vpid >> 16) {
8822 nested_vmx_failValid(vcpu,
8823 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8824 return kvm_skip_emulated_instruction(vcpu);
8825 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008826
8827 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008828 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008829 if (!operand.vpid ||
8830 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008831 nested_vmx_failValid(vcpu,
8832 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8833 return kvm_skip_emulated_instruction(vcpu);
8834 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008835 if (cpu_has_vmx_invvpid_individual_addr() &&
8836 vmx->nested.vpid02) {
8837 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8838 vmx->nested.vpid02, operand.gla);
8839 } else
8840 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8841 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008842 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008843 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008844 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008845 nested_vmx_failValid(vcpu,
8846 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008847 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008848 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008849 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008850 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008851 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008852 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008853 break;
8854 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008855 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008856 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008857 }
8858
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008859 nested_vmx_succeed(vcpu);
8860
Kyle Huey6affcbe2016-11-29 12:40:40 -08008861 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008862}
8863
Junaid Shahideb4b2482018-06-27 14:59:14 -07008864static int handle_invpcid(struct kvm_vcpu *vcpu)
8865{
8866 u32 vmx_instruction_info;
8867 unsigned long type;
8868 bool pcid_enabled;
8869 gva_t gva;
8870 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07008871 unsigned i;
8872 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07008873 struct {
8874 u64 pcid;
8875 u64 gla;
8876 } operand;
8877
8878 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8879 kvm_queue_exception(vcpu, UD_VECTOR);
8880 return 1;
8881 }
8882
8883 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8884 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8885
8886 if (type > 3) {
8887 kvm_inject_gp(vcpu, 0);
8888 return 1;
8889 }
8890
8891 /* According to the Intel instruction reference, the memory operand
8892 * is read even if it isn't needed (e.g., for type==all)
8893 */
8894 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8895 vmx_instruction_info, false, &gva))
8896 return 1;
8897
8898 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8899 kvm_inject_page_fault(vcpu, &e);
8900 return 1;
8901 }
8902
8903 if (operand.pcid >> 12 != 0) {
8904 kvm_inject_gp(vcpu, 0);
8905 return 1;
8906 }
8907
8908 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8909
8910 switch (type) {
8911 case INVPCID_TYPE_INDIV_ADDR:
8912 if ((!pcid_enabled && (operand.pcid != 0)) ||
8913 is_noncanonical_address(operand.gla, vcpu)) {
8914 kvm_inject_gp(vcpu, 0);
8915 return 1;
8916 }
8917 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8918 return kvm_skip_emulated_instruction(vcpu);
8919
8920 case INVPCID_TYPE_SINGLE_CTXT:
8921 if (!pcid_enabled && (operand.pcid != 0)) {
8922 kvm_inject_gp(vcpu, 0);
8923 return 1;
8924 }
8925
8926 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8927 kvm_mmu_sync_roots(vcpu);
8928 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8929 }
8930
Junaid Shahidb94742c2018-06-27 14:59:20 -07008931 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8932 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8933 == operand.pcid)
8934 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07008935
Junaid Shahidb94742c2018-06-27 14:59:20 -07008936 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07008937 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07008938 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07008939 * given PCID, then nothing needs to be done here because a
8940 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008941 */
8942
8943 return kvm_skip_emulated_instruction(vcpu);
8944
8945 case INVPCID_TYPE_ALL_NON_GLOBAL:
8946 /*
8947 * Currently, KVM doesn't mark global entries in the shadow
8948 * page tables, so a non-global flush just degenerates to a
8949 * global flush. If needed, we could optimize this later by
8950 * keeping track of global entries in shadow page tables.
8951 */
8952
8953 /* fall-through */
8954 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8955 kvm_mmu_unload(vcpu);
8956 return kvm_skip_emulated_instruction(vcpu);
8957
8958 default:
8959 BUG(); /* We have already checked above that type <= 3 */
8960 }
8961}
8962
Kai Huang843e4332015-01-28 10:54:28 +08008963static int handle_pml_full(struct kvm_vcpu *vcpu)
8964{
8965 unsigned long exit_qualification;
8966
8967 trace_kvm_pml_full(vcpu->vcpu_id);
8968
8969 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8970
8971 /*
8972 * PML buffer FULL happened while executing iret from NMI,
8973 * "blocked by NMI" bit has to be set before next VM entry.
8974 */
8975 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008976 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008977 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8978 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8979 GUEST_INTR_STATE_NMI);
8980
8981 /*
8982 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8983 * here.., and there's no userspace involvement needed for PML.
8984 */
8985 return 1;
8986}
8987
Yunhong Jiang64672c92016-06-13 14:19:59 -07008988static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8989{
8990 kvm_lapic_expired_hv_timer(vcpu);
8991 return 1;
8992}
8993
Bandan Das41ab9372017-08-03 15:54:43 -04008994static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8995{
8996 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008997 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8998
8999 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009000 switch (address & VMX_EPTP_MT_MASK) {
9001 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009002 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009003 return false;
9004 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009005 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009006 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009007 return false;
9008 break;
9009 default:
9010 return false;
9011 }
9012
David Hildenbrandbb97a012017-08-10 23:15:28 +02009013 /* only 4 levels page-walk length are valid */
9014 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009015 return false;
9016
9017 /* Reserved bits should not be set */
9018 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9019 return false;
9020
9021 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009022 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009023 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009024 return false;
9025 }
9026
9027 return true;
9028}
9029
9030static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9031 struct vmcs12 *vmcs12)
9032{
9033 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9034 u64 address;
9035 bool accessed_dirty;
9036 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9037
9038 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9039 !nested_cpu_has_ept(vmcs12))
9040 return 1;
9041
9042 if (index >= VMFUNC_EPTP_ENTRIES)
9043 return 1;
9044
9045
9046 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9047 &address, index * 8, 8))
9048 return 1;
9049
David Hildenbrandbb97a012017-08-10 23:15:28 +02009050 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009051
9052 /*
9053 * If the (L2) guest does a vmfunc to the currently
9054 * active ept pointer, we don't have to do anything else
9055 */
9056 if (vmcs12->ept_pointer != address) {
9057 if (!valid_ept_address(vcpu, address))
9058 return 1;
9059
9060 kvm_mmu_unload(vcpu);
9061 mmu->ept_ad = accessed_dirty;
9062 mmu->base_role.ad_disabled = !accessed_dirty;
9063 vmcs12->ept_pointer = address;
9064 /*
9065 * TODO: Check what's the correct approach in case
9066 * mmu reload fails. Currently, we just let the next
9067 * reload potentially fail
9068 */
9069 kvm_mmu_reload(vcpu);
9070 }
9071
9072 return 0;
9073}
9074
Bandan Das2a499e42017-08-03 15:54:41 -04009075static int handle_vmfunc(struct kvm_vcpu *vcpu)
9076{
Bandan Das27c42a12017-08-03 15:54:42 -04009077 struct vcpu_vmx *vmx = to_vmx(vcpu);
9078 struct vmcs12 *vmcs12;
9079 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9080
9081 /*
9082 * VMFUNC is only supported for nested guests, but we always enable the
9083 * secondary control for simplicity; for non-nested mode, fake that we
9084 * didn't by injecting #UD.
9085 */
9086 if (!is_guest_mode(vcpu)) {
9087 kvm_queue_exception(vcpu, UD_VECTOR);
9088 return 1;
9089 }
9090
9091 vmcs12 = get_vmcs12(vcpu);
9092 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9093 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009094
9095 switch (function) {
9096 case 0:
9097 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9098 goto fail;
9099 break;
9100 default:
9101 goto fail;
9102 }
9103 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009104
9105fail:
9106 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9107 vmcs_read32(VM_EXIT_INTR_INFO),
9108 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009109 return 1;
9110}
9111
Nadav Har'El0140cae2011-05-25 23:06:28 +03009112/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009113 * The exit handlers return 1 if the exit was handled fully and guest execution
9114 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9115 * to be done to userspace and return 0.
9116 */
Mathias Krause772e0312012-08-30 01:30:19 +02009117static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009118 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9119 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009120 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009121 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009122 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009123 [EXIT_REASON_CR_ACCESS] = handle_cr,
9124 [EXIT_REASON_DR_ACCESS] = handle_dr,
9125 [EXIT_REASON_CPUID] = handle_cpuid,
9126 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9127 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9128 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9129 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009130 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009131 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009132 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009133 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009134 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009135 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009136 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009137 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009138 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009139 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009140 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009141 [EXIT_REASON_VMOFF] = handle_vmoff,
9142 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009143 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9144 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009145 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009146 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009147 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009148 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009149 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009150 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009151 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9152 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009153 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9154 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009155 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009156 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009157 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009158 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009159 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009160 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009161 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009162 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009163 [EXIT_REASON_XSAVES] = handle_xsaves,
9164 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009165 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009166 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009167 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009168 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009169};
9170
9171static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009172 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009173
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009174static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9175 struct vmcs12 *vmcs12)
9176{
9177 unsigned long exit_qualification;
9178 gpa_t bitmap, last_bitmap;
9179 unsigned int port;
9180 int size;
9181 u8 b;
9182
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009183 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009184 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009185
9186 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9187
9188 port = exit_qualification >> 16;
9189 size = (exit_qualification & 7) + 1;
9190
9191 last_bitmap = (gpa_t)-1;
9192 b = -1;
9193
9194 while (size > 0) {
9195 if (port < 0x8000)
9196 bitmap = vmcs12->io_bitmap_a;
9197 else if (port < 0x10000)
9198 bitmap = vmcs12->io_bitmap_b;
9199 else
Joe Perches1d804d02015-03-30 16:46:09 -07009200 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009201 bitmap += (port & 0x7fff) / 8;
9202
9203 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009204 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009205 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009206 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009207 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009208
9209 port++;
9210 size--;
9211 last_bitmap = bitmap;
9212 }
9213
Joe Perches1d804d02015-03-30 16:46:09 -07009214 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009215}
9216
Nadav Har'El644d7112011-05-25 23:12:35 +03009217/*
9218 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9219 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9220 * disinterest in the current event (read or write a specific MSR) by using an
9221 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9222 */
9223static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9224 struct vmcs12 *vmcs12, u32 exit_reason)
9225{
9226 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9227 gpa_t bitmap;
9228
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009229 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009230 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009231
9232 /*
9233 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9234 * for the four combinations of read/write and low/high MSR numbers.
9235 * First we need to figure out which of the four to use:
9236 */
9237 bitmap = vmcs12->msr_bitmap;
9238 if (exit_reason == EXIT_REASON_MSR_WRITE)
9239 bitmap += 2048;
9240 if (msr_index >= 0xc0000000) {
9241 msr_index -= 0xc0000000;
9242 bitmap += 1024;
9243 }
9244
9245 /* Then read the msr_index'th bit from this bitmap: */
9246 if (msr_index < 1024*8) {
9247 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009248 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009249 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009250 return 1 & (b >> (msr_index & 7));
9251 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009252 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009253}
9254
9255/*
9256 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9257 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9258 * intercept (via guest_host_mask etc.) the current event.
9259 */
9260static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9261 struct vmcs12 *vmcs12)
9262{
9263 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9264 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009265 int reg;
9266 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009267
9268 switch ((exit_qualification >> 4) & 3) {
9269 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009270 reg = (exit_qualification >> 8) & 15;
9271 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009272 switch (cr) {
9273 case 0:
9274 if (vmcs12->cr0_guest_host_mask &
9275 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009276 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009277 break;
9278 case 3:
9279 if ((vmcs12->cr3_target_count >= 1 &&
9280 vmcs12->cr3_target_value0 == val) ||
9281 (vmcs12->cr3_target_count >= 2 &&
9282 vmcs12->cr3_target_value1 == val) ||
9283 (vmcs12->cr3_target_count >= 3 &&
9284 vmcs12->cr3_target_value2 == val) ||
9285 (vmcs12->cr3_target_count >= 4 &&
9286 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009287 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009288 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009289 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009290 break;
9291 case 4:
9292 if (vmcs12->cr4_guest_host_mask &
9293 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009294 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009295 break;
9296 case 8:
9297 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009298 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009299 break;
9300 }
9301 break;
9302 case 2: /* clts */
9303 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9304 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009305 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009306 break;
9307 case 1: /* mov from cr */
9308 switch (cr) {
9309 case 3:
9310 if (vmcs12->cpu_based_vm_exec_control &
9311 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009312 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009313 break;
9314 case 8:
9315 if (vmcs12->cpu_based_vm_exec_control &
9316 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009317 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009318 break;
9319 }
9320 break;
9321 case 3: /* lmsw */
9322 /*
9323 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9324 * cr0. Other attempted changes are ignored, with no exit.
9325 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009326 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009327 if (vmcs12->cr0_guest_host_mask & 0xe &
9328 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009329 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009330 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9331 !(vmcs12->cr0_read_shadow & 0x1) &&
9332 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009333 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009334 break;
9335 }
Joe Perches1d804d02015-03-30 16:46:09 -07009336 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009337}
9338
Liran Alona7cde482018-06-23 02:35:10 +03009339static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9340 struct vmcs12 *vmcs12, gpa_t bitmap)
9341{
9342 u32 vmx_instruction_info;
9343 unsigned long field;
9344 u8 b;
9345
9346 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9347 return true;
9348
9349 /* Decode instruction info and find the field to access */
9350 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9351 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9352
9353 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9354 if (field >> 15)
9355 return true;
9356
9357 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9358 return true;
9359
9360 return 1 & (b >> (field & 7));
9361}
9362
Nadav Har'El644d7112011-05-25 23:12:35 +03009363/*
9364 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9365 * should handle it ourselves in L0 (and then continue L2). Only call this
9366 * when in is_guest_mode (L2).
9367 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009368static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009369{
Nadav Har'El644d7112011-05-25 23:12:35 +03009370 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9371 struct vcpu_vmx *vmx = to_vmx(vcpu);
9372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9373
Jim Mattson4f350c62017-09-14 16:31:44 -07009374 if (vmx->nested.nested_run_pending)
9375 return false;
9376
9377 if (unlikely(vmx->fail)) {
9378 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9379 vmcs_read32(VM_INSTRUCTION_ERROR));
9380 return true;
9381 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009382
David Matlackc9f04402017-08-01 14:00:40 -07009383 /*
9384 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009385 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9386 * Page). The CPU may write to these pages via their host
9387 * physical address while L2 is running, bypassing any
9388 * address-translation-based dirty tracking (e.g. EPT write
9389 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009390 *
9391 * Mark them dirty on every exit from L2 to prevent them from
9392 * getting out of sync with dirty tracking.
9393 */
9394 nested_mark_vmcs12_pages_dirty(vcpu);
9395
Jim Mattson4f350c62017-09-14 16:31:44 -07009396 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9397 vmcs_readl(EXIT_QUALIFICATION),
9398 vmx->idt_vectoring_info,
9399 intr_info,
9400 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9401 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009402
9403 switch (exit_reason) {
9404 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009405 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009406 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009407 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009408 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009409 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009410 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009411 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009412 else if (is_debug(intr_info) &&
9413 vcpu->guest_debug &
9414 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9415 return false;
9416 else if (is_breakpoint(intr_info) &&
9417 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9418 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009419 return vmcs12->exception_bitmap &
9420 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9421 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009422 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009423 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009424 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009425 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009426 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009427 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009428 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009429 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009430 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009431 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009432 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009433 case EXIT_REASON_HLT:
9434 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9435 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009436 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009437 case EXIT_REASON_INVLPG:
9438 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9439 case EXIT_REASON_RDPMC:
9440 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009441 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009442 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009443 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009444 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009445 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009446 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009447 case EXIT_REASON_VMREAD:
9448 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9449 vmcs12->vmread_bitmap);
9450 case EXIT_REASON_VMWRITE:
9451 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9452 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009453 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9454 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009455 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009456 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009457 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009458 /*
9459 * VMX instructions trap unconditionally. This allows L1 to
9460 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9461 */
Joe Perches1d804d02015-03-30 16:46:09 -07009462 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009463 case EXIT_REASON_CR_ACCESS:
9464 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9465 case EXIT_REASON_DR_ACCESS:
9466 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9467 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009468 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009469 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9470 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009471 case EXIT_REASON_MSR_READ:
9472 case EXIT_REASON_MSR_WRITE:
9473 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9474 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009475 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009476 case EXIT_REASON_MWAIT_INSTRUCTION:
9477 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009478 case EXIT_REASON_MONITOR_TRAP_FLAG:
9479 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009480 case EXIT_REASON_MONITOR_INSTRUCTION:
9481 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9482 case EXIT_REASON_PAUSE_INSTRUCTION:
9483 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9484 nested_cpu_has2(vmcs12,
9485 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9486 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009487 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009488 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009489 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009490 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009491 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009492 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009493 /*
9494 * The controls for "virtualize APIC accesses," "APIC-
9495 * register virtualization," and "virtual-interrupt
9496 * delivery" only come from vmcs12.
9497 */
Joe Perches1d804d02015-03-30 16:46:09 -07009498 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009499 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009500 /*
9501 * L0 always deals with the EPT violation. If nested EPT is
9502 * used, and the nested mmu code discovers that the address is
9503 * missing in the guest EPT table (EPT12), the EPT violation
9504 * will be injected with nested_ept_inject_page_fault()
9505 */
Joe Perches1d804d02015-03-30 16:46:09 -07009506 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009507 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009508 /*
9509 * L2 never uses directly L1's EPT, but rather L0's own EPT
9510 * table (shadow on EPT) or a merged EPT table that L0 built
9511 * (EPT on EPT). So any problems with the structure of the
9512 * table is L0's fault.
9513 */
Joe Perches1d804d02015-03-30 16:46:09 -07009514 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009515 case EXIT_REASON_INVPCID:
9516 return
9517 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9518 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009519 case EXIT_REASON_WBINVD:
9520 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9521 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009522 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009523 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9524 /*
9525 * This should never happen, since it is not possible to
9526 * set XSS to a non-zero value---neither in L1 nor in L2.
9527 * If if it were, XSS would have to be checked against
9528 * the XSS exit bitmap in vmcs12.
9529 */
9530 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009531 case EXIT_REASON_PREEMPTION_TIMER:
9532 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009533 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009534 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009535 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009536 case EXIT_REASON_VMFUNC:
9537 /* VM functions are emulated through L2->L0 vmexits. */
9538 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009539 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 }
9542}
9543
Paolo Bonzini7313c692017-07-27 10:31:25 +02009544static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9545{
9546 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9547
9548 /*
9549 * At this point, the exit interruption info in exit_intr_info
9550 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9551 * we need to query the in-kernel LAPIC.
9552 */
9553 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9554 if ((exit_intr_info &
9555 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9556 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9557 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9558 vmcs12->vm_exit_intr_error_code =
9559 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9560 }
9561
9562 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9563 vmcs_readl(EXIT_QUALIFICATION));
9564 return 1;
9565}
9566
Avi Kivity586f9602010-11-18 13:09:54 +02009567static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9568{
9569 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9570 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9571}
9572
Kai Huanga3eaa862015-11-04 13:46:05 +08009573static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009574{
Kai Huanga3eaa862015-11-04 13:46:05 +08009575 if (vmx->pml_pg) {
9576 __free_page(vmx->pml_pg);
9577 vmx->pml_pg = NULL;
9578 }
Kai Huang843e4332015-01-28 10:54:28 +08009579}
9580
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009581static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009582{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009584 u64 *pml_buf;
9585 u16 pml_idx;
9586
9587 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9588
9589 /* Do nothing if PML buffer is empty */
9590 if (pml_idx == (PML_ENTITY_NUM - 1))
9591 return;
9592
9593 /* PML index always points to next available PML buffer entity */
9594 if (pml_idx >= PML_ENTITY_NUM)
9595 pml_idx = 0;
9596 else
9597 pml_idx++;
9598
9599 pml_buf = page_address(vmx->pml_pg);
9600 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9601 u64 gpa;
9602
9603 gpa = pml_buf[pml_idx];
9604 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009605 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009606 }
9607
9608 /* reset PML index */
9609 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9610}
9611
9612/*
9613 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9614 * Called before reporting dirty_bitmap to userspace.
9615 */
9616static void kvm_flush_pml_buffers(struct kvm *kvm)
9617{
9618 int i;
9619 struct kvm_vcpu *vcpu;
9620 /*
9621 * We only need to kick vcpu out of guest mode here, as PML buffer
9622 * is flushed at beginning of all VMEXITs, and it's obvious that only
9623 * vcpus running in guest are possible to have unflushed GPAs in PML
9624 * buffer.
9625 */
9626 kvm_for_each_vcpu(i, vcpu, kvm)
9627 kvm_vcpu_kick(vcpu);
9628}
9629
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009630static void vmx_dump_sel(char *name, uint32_t sel)
9631{
9632 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009633 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009634 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9635 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9636 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9637}
9638
9639static void vmx_dump_dtsel(char *name, uint32_t limit)
9640{
9641 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9642 name, vmcs_read32(limit),
9643 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9644}
9645
9646static void dump_vmcs(void)
9647{
9648 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9649 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9650 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9651 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9652 u32 secondary_exec_control = 0;
9653 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009654 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009655 int i, n;
9656
9657 if (cpu_has_secondary_exec_ctrls())
9658 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9659
9660 pr_err("*** Guest State ***\n");
9661 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9662 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9663 vmcs_readl(CR0_GUEST_HOST_MASK));
9664 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9665 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9666 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9667 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9668 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9669 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009670 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9671 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9672 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9673 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009674 }
9675 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9676 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9677 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9678 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9679 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9680 vmcs_readl(GUEST_SYSENTER_ESP),
9681 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9682 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9683 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9684 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9685 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9686 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9687 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9688 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9689 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9690 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9691 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9692 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9693 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009694 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9695 efer, vmcs_read64(GUEST_IA32_PAT));
9696 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9697 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009698 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009699 if (cpu_has_load_perf_global_ctrl &&
9700 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009701 pr_err("PerfGlobCtl = 0x%016llx\n",
9702 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009703 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009704 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009705 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9706 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9707 vmcs_read32(GUEST_ACTIVITY_STATE));
9708 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9709 pr_err("InterruptStatus = %04x\n",
9710 vmcs_read16(GUEST_INTR_STATUS));
9711
9712 pr_err("*** Host State ***\n");
9713 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9714 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9715 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9716 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9717 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9718 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9719 vmcs_read16(HOST_TR_SELECTOR));
9720 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9721 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9722 vmcs_readl(HOST_TR_BASE));
9723 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9724 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9725 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9726 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9727 vmcs_readl(HOST_CR4));
9728 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9729 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9730 vmcs_read32(HOST_IA32_SYSENTER_CS),
9731 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9732 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009733 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9734 vmcs_read64(HOST_IA32_EFER),
9735 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009736 if (cpu_has_load_perf_global_ctrl &&
9737 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009738 pr_err("PerfGlobCtl = 0x%016llx\n",
9739 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009740
9741 pr_err("*** Control State ***\n");
9742 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9743 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9744 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9745 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9746 vmcs_read32(EXCEPTION_BITMAP),
9747 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9748 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9749 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9750 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9751 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9752 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9753 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9754 vmcs_read32(VM_EXIT_INTR_INFO),
9755 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9756 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9757 pr_err(" reason=%08x qualification=%016lx\n",
9758 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9759 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9760 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9761 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009762 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009763 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009764 pr_err("TSC Multiplier = 0x%016llx\n",
9765 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009766 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9767 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9768 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9769 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9770 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009771 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009772 n = vmcs_read32(CR3_TARGET_COUNT);
9773 for (i = 0; i + 1 < n; i += 4)
9774 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9775 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9776 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9777 if (i < n)
9778 pr_err("CR3 target%u=%016lx\n",
9779 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9780 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9781 pr_err("PLE Gap=%08x Window=%08x\n",
9782 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9783 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9784 pr_err("Virtual processor ID = 0x%04x\n",
9785 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9786}
9787
Avi Kivity6aa8b732006-12-10 02:21:36 -08009788/*
9789 * The guest has exited. See if we can fix it or if we need userspace
9790 * assistance.
9791 */
Avi Kivity851ba692009-08-24 11:10:17 +03009792static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009793{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009795 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009796 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009797
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009798 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9799
Kai Huang843e4332015-01-28 10:54:28 +08009800 /*
9801 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9802 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9803 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9804 * mode as if vcpus is in root mode, the PML buffer must has been
9805 * flushed already.
9806 */
9807 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009808 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009809
Mohammed Gamal80ced182009-09-01 12:48:18 +02009810 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009811 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009812 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009813
Paolo Bonzini7313c692017-07-27 10:31:25 +02009814 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9815 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009816
Mohammed Gamal51207022010-05-31 22:40:54 +03009817 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009818 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009819 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9820 vcpu->run->fail_entry.hardware_entry_failure_reason
9821 = exit_reason;
9822 return 0;
9823 }
9824
Avi Kivity29bd8a72007-09-10 17:27:03 +03009825 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009826 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9827 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009828 = vmcs_read32(VM_INSTRUCTION_ERROR);
9829 return 0;
9830 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009831
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009832 /*
9833 * Note:
9834 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9835 * delivery event since it indicates guest is accessing MMIO.
9836 * The vm-exit can be triggered again after return to guest that
9837 * will cause infinite loop.
9838 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009839 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009840 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009841 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009842 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009843 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9844 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9845 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009846 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009847 vcpu->run->internal.data[0] = vectoring_info;
9848 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009849 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9850 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9851 vcpu->run->internal.ndata++;
9852 vcpu->run->internal.data[3] =
9853 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9854 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009855 return 0;
9856 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009857
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009858 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009859 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9860 if (vmx_interrupt_allowed(vcpu)) {
9861 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9862 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9863 vcpu->arch.nmi_pending) {
9864 /*
9865 * This CPU don't support us in finding the end of an
9866 * NMI-blocked window if the guest runs with IRQs
9867 * disabled. So we pull the trigger after 1 s of
9868 * futile waiting, but inform the user about this.
9869 */
9870 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9871 "state on VCPU %d after 1 s timeout\n",
9872 __func__, vcpu->vcpu_id);
9873 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9874 }
9875 }
9876
Avi Kivity6aa8b732006-12-10 02:21:36 -08009877 if (exit_reason < kvm_vmx_max_exit_handlers
9878 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009879 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009880 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009881 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9882 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009883 kvm_queue_exception(vcpu, UD_VECTOR);
9884 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009885 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009886}
9887
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009888static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009889{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009890 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9891
9892 if (is_guest_mode(vcpu) &&
9893 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9894 return;
9895
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009896 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009897 vmcs_write32(TPR_THRESHOLD, 0);
9898 return;
9899 }
9900
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009901 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009902}
9903
Jim Mattson8d860bb2018-05-09 16:56:05 -04009904static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009905{
9906 u32 sec_exec_control;
9907
Jim Mattson8d860bb2018-05-09 16:56:05 -04009908 if (!lapic_in_kernel(vcpu))
9909 return;
9910
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009911 /* Postpone execution until vmcs01 is the current VMCS. */
9912 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009913 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009914 return;
9915 }
9916
Paolo Bonzini35754c92015-07-29 12:05:37 +02009917 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009918 return;
9919
9920 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009921 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9922 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009923
Jim Mattson8d860bb2018-05-09 16:56:05 -04009924 switch (kvm_get_apic_mode(vcpu)) {
9925 case LAPIC_MODE_INVALID:
9926 WARN_ONCE(true, "Invalid local APIC state");
9927 case LAPIC_MODE_DISABLED:
9928 break;
9929 case LAPIC_MODE_XAPIC:
9930 if (flexpriority_enabled) {
9931 sec_exec_control |=
9932 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9933 vmx_flush_tlb(vcpu, true);
9934 }
9935 break;
9936 case LAPIC_MODE_X2APIC:
9937 if (cpu_has_vmx_virtualize_x2apic_mode())
9938 sec_exec_control |=
9939 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9940 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009941 }
9942 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9943
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009944 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009945}
9946
Tang Chen38b99172014-09-24 15:57:54 +08009947static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9948{
Jim Mattsonab5df312018-05-09 17:02:03 -04009949 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009950 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009951 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009952 }
Tang Chen38b99172014-09-24 15:57:54 +08009953}
9954
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009955static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009956{
9957 u16 status;
9958 u8 old;
9959
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009960 if (max_isr == -1)
9961 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009962
9963 status = vmcs_read16(GUEST_INTR_STATUS);
9964 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009965 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009966 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009967 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009968 vmcs_write16(GUEST_INTR_STATUS, status);
9969 }
9970}
9971
9972static void vmx_set_rvi(int vector)
9973{
9974 u16 status;
9975 u8 old;
9976
Wei Wang4114c272014-11-05 10:53:43 +08009977 if (vector == -1)
9978 vector = 0;
9979
Yang Zhangc7c9c562013-01-25 10:18:51 +08009980 status = vmcs_read16(GUEST_INTR_STATUS);
9981 old = (u8)status & 0xff;
9982 if ((u8)vector != old) {
9983 status &= ~0xff;
9984 status |= (u8)vector;
9985 vmcs_write16(GUEST_INTR_STATUS, status);
9986 }
9987}
9988
9989static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9990{
Liran Alon851c1a182017-12-24 18:12:56 +02009991 /*
9992 * When running L2, updating RVI is only relevant when
9993 * vmcs12 virtual-interrupt-delivery enabled.
9994 * However, it can be enabled only when L1 also
9995 * intercepts external-interrupts and in that case
9996 * we should not update vmcs02 RVI but instead intercept
9997 * interrupt. Therefore, do nothing when running L2.
9998 */
9999 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010000 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010001}
10002
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010003static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010004{
10005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010006 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010007 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010008
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010009 WARN_ON(!vcpu->arch.apicv_active);
10010 if (pi_test_on(&vmx->pi_desc)) {
10011 pi_clear_on(&vmx->pi_desc);
10012 /*
10013 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10014 * But on x86 this is just a compiler barrier anyway.
10015 */
10016 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010017 max_irr_updated =
10018 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10019
10020 /*
10021 * If we are running L2 and L1 has a new pending interrupt
10022 * which can be injected, we should re-evaluate
10023 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010024 * If L1 intercepts external-interrupts, we should
10025 * exit from L2 to L1. Otherwise, interrupt should be
10026 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010027 */
Liran Alon851c1a182017-12-24 18:12:56 +020010028 if (is_guest_mode(vcpu) && max_irr_updated) {
10029 if (nested_exit_on_intr(vcpu))
10030 kvm_vcpu_exiting_guest_mode(vcpu);
10031 else
10032 kvm_make_request(KVM_REQ_EVENT, vcpu);
10033 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010034 } else {
10035 max_irr = kvm_lapic_find_highest_irr(vcpu);
10036 }
10037 vmx_hwapic_irr_update(vcpu, max_irr);
10038 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010039}
10040
Andrey Smetanin63086302015-11-10 15:36:32 +030010041static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010042{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010043 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010044 return;
10045
Yang Zhangc7c9c562013-01-25 10:18:51 +080010046 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10047 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10048 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10049 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10050}
10051
Paolo Bonzini967235d2016-12-19 14:03:45 +010010052static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10053{
10054 struct vcpu_vmx *vmx = to_vmx(vcpu);
10055
10056 pi_clear_on(&vmx->pi_desc);
10057 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10058}
10059
Avi Kivity51aa01d2010-07-20 14:31:20 +030010060static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010061{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010062 u32 exit_intr_info = 0;
10063 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010064
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010065 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10066 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010067 return;
10068
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010069 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10070 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10071 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010072
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010073 /* if exit due to PF check for async PF */
10074 if (is_page_fault(exit_intr_info))
10075 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10076
Andi Kleena0861c02009-06-08 17:37:09 +080010077 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010078 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10079 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010080 kvm_machine_check();
10081
Gleb Natapov20f65982009-05-11 13:35:55 +030010082 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010083 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010084 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010085 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010086 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010087 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010088}
Gleb Natapov20f65982009-05-11 13:35:55 +030010089
Yang Zhanga547c6d2013-04-11 19:25:10 +080010090static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10091{
10092 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10093
Yang Zhanga547c6d2013-04-11 19:25:10 +080010094 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10095 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10096 unsigned int vector;
10097 unsigned long entry;
10098 gate_desc *desc;
10099 struct vcpu_vmx *vmx = to_vmx(vcpu);
10100#ifdef CONFIG_X86_64
10101 unsigned long tmp;
10102#endif
10103
10104 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10105 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010106 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010107 asm volatile(
10108#ifdef CONFIG_X86_64
10109 "mov %%" _ASM_SP ", %[sp]\n\t"
10110 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10111 "push $%c[ss]\n\t"
10112 "push %[sp]\n\t"
10113#endif
10114 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010115 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010116 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010117 :
10118#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010119 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010120#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010121 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010122 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010123 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010124 [ss]"i"(__KERNEL_DS),
10125 [cs]"i"(__KERNEL_CS)
10126 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010127 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010128}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010129STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010130
Tom Lendackybc226f02018-05-10 22:06:39 +020010131static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010132{
Tom Lendackybc226f02018-05-10 22:06:39 +020010133 switch (index) {
10134 case MSR_IA32_SMBASE:
10135 /*
10136 * We cannot do SMM unless we can run the guest in big
10137 * real mode.
10138 */
10139 return enable_unrestricted_guest || emulate_invalid_guest_state;
10140 case MSR_AMD64_VIRT_SPEC_CTRL:
10141 /* This is AMD only. */
10142 return false;
10143 default:
10144 return true;
10145 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010146}
10147
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010148static bool vmx_mpx_supported(void)
10149{
10150 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10151 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10152}
10153
Wanpeng Li55412b22014-12-02 19:21:30 +080010154static bool vmx_xsaves_supported(void)
10155{
10156 return vmcs_config.cpu_based_2nd_exec_ctrl &
10157 SECONDARY_EXEC_XSAVES;
10158}
10159
Avi Kivity51aa01d2010-07-20 14:31:20 +030010160static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10161{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010162 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010163 bool unblock_nmi;
10164 u8 vector;
10165 bool idtv_info_valid;
10166
10167 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010168
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010169 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010170 if (vmx->loaded_vmcs->nmi_known_unmasked)
10171 return;
10172 /*
10173 * Can't use vmx->exit_intr_info since we're not sure what
10174 * the exit reason is.
10175 */
10176 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10177 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10178 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10179 /*
10180 * SDM 3: 27.7.1.2 (September 2008)
10181 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10182 * a guest IRET fault.
10183 * SDM 3: 23.2.2 (September 2008)
10184 * Bit 12 is undefined in any of the following cases:
10185 * If the VM exit sets the valid bit in the IDT-vectoring
10186 * information field.
10187 * If the VM exit is due to a double fault.
10188 */
10189 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10190 vector != DF_VECTOR && !idtv_info_valid)
10191 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10192 GUEST_INTR_STATE_NMI);
10193 else
10194 vmx->loaded_vmcs->nmi_known_unmasked =
10195 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10196 & GUEST_INTR_STATE_NMI);
10197 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10198 vmx->loaded_vmcs->vnmi_blocked_time +=
10199 ktime_to_ns(ktime_sub(ktime_get(),
10200 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010201}
10202
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010203static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010204 u32 idt_vectoring_info,
10205 int instr_len_field,
10206 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010207{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010208 u8 vector;
10209 int type;
10210 bool idtv_info_valid;
10211
10212 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010213
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010214 vcpu->arch.nmi_injected = false;
10215 kvm_clear_exception_queue(vcpu);
10216 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010217
10218 if (!idtv_info_valid)
10219 return;
10220
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010221 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010222
Avi Kivity668f6122008-07-02 09:28:55 +030010223 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10224 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010225
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010226 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010227 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010228 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010229 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010230 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010231 * Clear bit "block by NMI" before VM entry if a NMI
10232 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010233 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010234 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010235 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010236 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010237 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010238 /* fall through */
10239 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010240 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010241 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010242 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010243 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010244 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010245 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010246 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010247 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010248 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010249 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010250 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010251 break;
10252 default:
10253 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010254 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010255}
10256
Avi Kivity83422e12010-07-20 14:43:23 +030010257static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10258{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010259 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010260 VM_EXIT_INSTRUCTION_LEN,
10261 IDT_VECTORING_ERROR_CODE);
10262}
10263
Avi Kivityb463a6f2010-07-20 15:06:17 +030010264static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10265{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010266 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010267 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10268 VM_ENTRY_INSTRUCTION_LEN,
10269 VM_ENTRY_EXCEPTION_ERROR_CODE);
10270
10271 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10272}
10273
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010274static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10275{
10276 int i, nr_msrs;
10277 struct perf_guest_switch_msr *msrs;
10278
10279 msrs = perf_guest_get_msrs(&nr_msrs);
10280
10281 if (!msrs)
10282 return;
10283
10284 for (i = 0; i < nr_msrs; i++)
10285 if (msrs[i].host == msrs[i].guest)
10286 clear_atomic_switch_msr(vmx, msrs[i].msr);
10287 else
10288 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10289 msrs[i].host);
10290}
10291
Jiang Biao33365e72016-11-03 15:03:37 +080010292static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010293{
10294 struct vcpu_vmx *vmx = to_vmx(vcpu);
10295 u64 tscl;
10296 u32 delta_tsc;
10297
10298 if (vmx->hv_deadline_tsc == -1)
10299 return;
10300
10301 tscl = rdtsc();
10302 if (vmx->hv_deadline_tsc > tscl)
10303 /* sure to be 32 bit only because checked on set_hv_timer */
10304 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10305 cpu_preemption_timer_multi);
10306 else
10307 delta_tsc = 0;
10308
10309 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10310}
10311
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010312static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010313{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010315 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010316
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010317 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010318 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010319 vmx->loaded_vmcs->soft_vnmi_blocked))
10320 vmx->loaded_vmcs->entry_time = ktime_get();
10321
Avi Kivity104f2262010-11-18 13:12:52 +020010322 /* Don't enter VMX if guest state is invalid, let the exit handler
10323 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010324 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010325 return;
10326
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010327 if (vmx->ple_window_dirty) {
10328 vmx->ple_window_dirty = false;
10329 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10330 }
10331
Abel Gordon012f83c2013-04-18 14:39:25 +030010332 if (vmx->nested.sync_shadow_vmcs) {
10333 copy_vmcs12_to_shadow(vmx);
10334 vmx->nested.sync_shadow_vmcs = false;
10335 }
10336
Avi Kivity104f2262010-11-18 13:12:52 +020010337 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10338 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10339 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10340 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10341
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010342 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010343 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010344 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010345 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010346 }
10347
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010348 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010349 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010350 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010351 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010352 }
10353
Avi Kivity104f2262010-11-18 13:12:52 +020010354 /* When single-stepping over STI and MOV SS, we must clear the
10355 * corresponding interruptibility bits in the guest state. Otherwise
10356 * vmentry fails as it then expects bit 14 (BS) in pending debug
10357 * exceptions being set, but that's not correct for the guest debugging
10358 * case. */
10359 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10360 vmx_set_interrupt_shadow(vcpu, 0);
10361
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010362 if (static_cpu_has(X86_FEATURE_PKU) &&
10363 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10364 vcpu->arch.pkru != vmx->host_pkru)
10365 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010366
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010367 atomic_switch_perf_msrs(vmx);
10368
Yunhong Jiang64672c92016-06-13 14:19:59 -070010369 vmx_arm_hv_timer(vcpu);
10370
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010371 /*
10372 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10373 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10374 * is no need to worry about the conditional branch over the wrmsr
10375 * being speculatively taken.
10376 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010377 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010378
Nadav Har'Eld462b812011-05-24 15:26:10 +030010379 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010380
10381 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10382 (unsigned long)&current_evmcs->host_rsp : 0;
10383
Avi Kivity104f2262010-11-18 13:12:52 +020010384 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010385 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010386 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10387 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10388 "push %%" _ASM_CX " \n\t"
10389 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010390 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010391 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010392 /* Avoid VMWRITE when Enlightened VMCS is in use */
10393 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10394 "jz 2f \n\t"
10395 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10396 "jmp 1f \n\t"
10397 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010398 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010399 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010400 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010401 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10402 "mov %%cr2, %%" _ASM_DX " \n\t"
10403 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010404 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010405 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010406 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010407 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010408 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010409 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010410 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10411 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10412 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10413 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10414 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10415 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010416#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010417 "mov %c[r8](%0), %%r8 \n\t"
10418 "mov %c[r9](%0), %%r9 \n\t"
10419 "mov %c[r10](%0), %%r10 \n\t"
10420 "mov %c[r11](%0), %%r11 \n\t"
10421 "mov %c[r12](%0), %%r12 \n\t"
10422 "mov %c[r13](%0), %%r13 \n\t"
10423 "mov %c[r14](%0), %%r14 \n\t"
10424 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010425#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010426 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010427
Avi Kivity6aa8b732006-12-10 02:21:36 -080010428 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010429 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010430 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010431 "jmp 2f \n\t"
10432 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10433 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010434 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010435 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010436 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010437 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010438 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10439 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10440 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10441 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10442 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10443 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10444 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010445#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010446 "mov %%r8, %c[r8](%0) \n\t"
10447 "mov %%r9, %c[r9](%0) \n\t"
10448 "mov %%r10, %c[r10](%0) \n\t"
10449 "mov %%r11, %c[r11](%0) \n\t"
10450 "mov %%r12, %c[r12](%0) \n\t"
10451 "mov %%r13, %c[r13](%0) \n\t"
10452 "mov %%r14, %c[r14](%0) \n\t"
10453 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010454 "xor %%r8d, %%r8d \n\t"
10455 "xor %%r9d, %%r9d \n\t"
10456 "xor %%r10d, %%r10d \n\t"
10457 "xor %%r11d, %%r11d \n\t"
10458 "xor %%r12d, %%r12d \n\t"
10459 "xor %%r13d, %%r13d \n\t"
10460 "xor %%r14d, %%r14d \n\t"
10461 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010462#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010463 "mov %%cr2, %%" _ASM_AX " \n\t"
10464 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010465
Jim Mattson0cb5b302018-01-03 14:31:38 -080010466 "xor %%eax, %%eax \n\t"
10467 "xor %%ebx, %%ebx \n\t"
10468 "xor %%esi, %%esi \n\t"
10469 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010470 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010471 ".pushsection .rodata \n\t"
10472 ".global vmx_return \n\t"
10473 "vmx_return: " _ASM_PTR " 2b \n\t"
10474 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010475 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010476 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010477 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010478 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010479 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10480 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10481 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10482 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10483 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10484 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10485 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010486#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010487 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10488 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10489 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10490 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10491 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10492 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10493 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10494 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010495#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010496 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10497 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010498 : "cc", "memory"
10499#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010500 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010501 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010502#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010503 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010504#endif
10505 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010506
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010507 /*
10508 * We do not use IBRS in the kernel. If this vCPU has used the
10509 * SPEC_CTRL MSR it may have left it on; save the value and
10510 * turn it off. This is much more efficient than blindly adding
10511 * it to the atomic save/restore list. Especially as the former
10512 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10513 *
10514 * For non-nested case:
10515 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10516 * save it.
10517 *
10518 * For nested case:
10519 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10520 * save it.
10521 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010522 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010523 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010524
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010525 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010526
David Woodhouse117cc7a2018-01-12 11:11:27 +000010527 /* Eliminate branch target predictions from guest mode */
10528 vmexit_fill_RSB();
10529
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010530 /* All fields are clean at this point */
10531 if (static_branch_unlikely(&enable_evmcs))
10532 current_evmcs->hv_clean_fields |=
10533 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10534
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010535 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010536 if (vmx->host_debugctlmsr)
10537 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010538
Avi Kivityaa67f602012-08-01 16:48:03 +030010539#ifndef CONFIG_X86_64
10540 /*
10541 * The sysexit path does not restore ds/es, so we must set them to
10542 * a reasonable value ourselves.
10543 *
10544 * We can't defer this to vmx_load_host_state() since that function
10545 * may be executed in interrupt context, which saves and restore segments
10546 * around it, nullifying its effect.
10547 */
10548 loadsegment(ds, __USER_DS);
10549 loadsegment(es, __USER_DS);
10550#endif
10551
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010552 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010553 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010554 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010555 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010556 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010557 vcpu->arch.regs_dirty = 0;
10558
Gleb Natapove0b890d2013-09-25 12:51:33 +030010559 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010560 * eager fpu is enabled if PKEY is supported and CR4 is switched
10561 * back on host, so it is safe to read guest PKRU from current
10562 * XSAVE.
10563 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010564 if (static_cpu_has(X86_FEATURE_PKU) &&
10565 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10566 vcpu->arch.pkru = __read_pkru();
10567 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010568 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010569 }
10570
Gleb Natapove0b890d2013-09-25 12:51:33 +030010571 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010572 vmx->idt_vectoring_info = 0;
10573
10574 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10575 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10576 return;
10577
10578 vmx->loaded_vmcs->launched = 1;
10579 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010580
Avi Kivity51aa01d2010-07-20 14:31:20 +030010581 vmx_complete_atomic_exit(vmx);
10582 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010583 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010584}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010585STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010586
Sean Christopherson434a1e92018-03-20 12:17:18 -070010587static struct kvm *vmx_vm_alloc(void)
10588{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010589 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010590 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010591}
10592
10593static void vmx_vm_free(struct kvm *kvm)
10594{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010595 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010596}
10597
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010598static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010599{
10600 struct vcpu_vmx *vmx = to_vmx(vcpu);
10601 int cpu;
10602
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010603 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010604 return;
10605
10606 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010607 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010608 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010609 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010610 put_cpu();
10611}
10612
Jim Mattson2f1fe812016-07-08 15:36:06 -070010613/*
10614 * Ensure that the current vmcs of the logical processor is the
10615 * vmcs01 of the vcpu before calling free_nested().
10616 */
10617static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10618{
10619 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010620
Christoffer Dallec7660c2017-12-04 21:35:23 +010010621 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010622 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010623 free_nested(vmx);
10624 vcpu_put(vcpu);
10625}
10626
Avi Kivity6aa8b732006-12-10 02:21:36 -080010627static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10628{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010629 struct vcpu_vmx *vmx = to_vmx(vcpu);
10630
Kai Huang843e4332015-01-28 10:54:28 +080010631 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010632 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010633 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010634 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010635 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010636 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010637 kfree(vmx->guest_msrs);
10638 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010639 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010640}
10641
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010642static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010643{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010644 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010645 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010646 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010647 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010648
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010649 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010650 return ERR_PTR(-ENOMEM);
10651
Wanpeng Li991e7a02015-09-16 17:30:05 +080010652 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010653
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010654 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10655 if (err)
10656 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010657
Peter Feiner4e595162016-07-07 14:49:58 -070010658 err = -ENOMEM;
10659
10660 /*
10661 * If PML is turned on, failure on enabling PML just results in failure
10662 * of creating the vcpu, therefore we can simplify PML logic (by
10663 * avoiding dealing with cases, such as enabling PML partially on vcpus
10664 * for the guest, etc.
10665 */
10666 if (enable_pml) {
10667 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10668 if (!vmx->pml_pg)
10669 goto uninit_vcpu;
10670 }
10671
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010672 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010673 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10674 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010675
Peter Feiner4e595162016-07-07 14:49:58 -070010676 if (!vmx->guest_msrs)
10677 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010678
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010679 err = alloc_loaded_vmcs(&vmx->vmcs01);
10680 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010681 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010682
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010683 msr_bitmap = vmx->vmcs01.msr_bitmap;
10684 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10685 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10686 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10687 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10688 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10689 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10690 vmx->msr_bitmap_mode = 0;
10691
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010692 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010693 cpu = get_cpu();
10694 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010695 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010696 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010697 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010698 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010699 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010700 err = alloc_apic_access_page(kvm);
10701 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010702 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010703 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010704
Sean Christophersone90008d2018-03-05 12:04:37 -080010705 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010706 err = init_rmode_identity_map(kvm);
10707 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010708 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010709 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010710
Wanpeng Li5c614b32015-10-13 09:18:36 -070010711 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010712 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10713 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010714 vmx->nested.vpid02 = allocate_vpid();
10715 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010716
Wincy Van705699a2015-02-03 23:58:17 +080010717 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010718 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010719
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010720 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10721
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010722 /*
10723 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10724 * or POSTED_INTR_WAKEUP_VECTOR.
10725 */
10726 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10727 vmx->pi_desc.sn = 1;
10728
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010729 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010730
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010731free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010732 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010733 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010734free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010735 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010736free_pml:
10737 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010738uninit_vcpu:
10739 kvm_vcpu_uninit(&vmx->vcpu);
10740free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010741 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010742 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010743 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010744}
10745
Wanpeng Lib31c1142018-03-12 04:53:04 -070010746static int vmx_vm_init(struct kvm *kvm)
10747{
Tianyu Lan877ad952018-07-19 08:40:23 +000010748 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10749
Wanpeng Lib31c1142018-03-12 04:53:04 -070010750 if (!ple_gap)
10751 kvm->arch.pause_in_guest = true;
10752 return 0;
10753}
10754
Yang, Sheng002c7f72007-07-31 14:23:01 +030010755static void __init vmx_check_processor_compat(void *rtn)
10756{
10757 struct vmcs_config vmcs_conf;
10758
10759 *(int *)rtn = 0;
10760 if (setup_vmcs_config(&vmcs_conf) < 0)
10761 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010762 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010763 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10764 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10765 smp_processor_id());
10766 *(int *)rtn = -EIO;
10767 }
10768}
10769
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010770static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010771{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010772 u8 cache;
10773 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010774
Sheng Yang522c68c2009-04-27 20:35:43 +080010775 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010776 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010777 * 2. EPT with VT-d:
10778 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010779 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010780 * b. VT-d with snooping control feature: snooping control feature of
10781 * VT-d engine can guarantee the cache correctness. Just set it
10782 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010783 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010784 * consistent with host MTRR
10785 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010786 if (is_mmio) {
10787 cache = MTRR_TYPE_UNCACHABLE;
10788 goto exit;
10789 }
10790
10791 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010792 ipat = VMX_EPT_IPAT_BIT;
10793 cache = MTRR_TYPE_WRBACK;
10794 goto exit;
10795 }
10796
10797 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10798 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010799 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010800 cache = MTRR_TYPE_WRBACK;
10801 else
10802 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010803 goto exit;
10804 }
10805
Xiao Guangrongff536042015-06-15 16:55:22 +080010806 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010807
10808exit:
10809 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010810}
10811
Sheng Yang17cc3932010-01-05 19:02:27 +080010812static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010813{
Sheng Yang878403b2010-01-05 19:02:29 +080010814 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10815 return PT_DIRECTORY_LEVEL;
10816 else
10817 /* For shadow and EPT supported 1GB page */
10818 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010819}
10820
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010821static void vmcs_set_secondary_exec_control(u32 new_ctl)
10822{
10823 /*
10824 * These bits in the secondary execution controls field
10825 * are dynamic, the others are mostly based on the hypervisor
10826 * architecture and the guest's CPUID. Do not touch the
10827 * dynamic bits.
10828 */
10829 u32 mask =
10830 SECONDARY_EXEC_SHADOW_VMCS |
10831 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010832 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10833 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010834
10835 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10836
10837 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10838 (new_ctl & ~mask) | (cur_ctl & mask));
10839}
10840
David Matlack8322ebb2016-11-29 18:14:09 -080010841/*
10842 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10843 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10844 */
10845static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10846{
10847 struct vcpu_vmx *vmx = to_vmx(vcpu);
10848 struct kvm_cpuid_entry2 *entry;
10849
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010850 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10851 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010852
10853#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10854 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010855 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010856} while (0)
10857
10858 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10859 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10860 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10861 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10862 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10863 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10864 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10865 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10866 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10867 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10868 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10869 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10870 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10871 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10872 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10873
10874 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10875 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10876 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10877 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10878 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010879 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010880
10881#undef cr4_fixed1_update
10882}
10883
Sheng Yang0e851882009-12-18 16:48:46 +080010884static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10885{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010886 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010887
Paolo Bonzini80154d72017-08-24 13:55:35 +020010888 if (cpu_has_secondary_exec_ctrls()) {
10889 vmx_compute_secondary_exec_control(vmx);
10890 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010891 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010892
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010893 if (nested_vmx_allowed(vcpu))
10894 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10895 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10896 else
10897 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10898 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010899
10900 if (nested_vmx_allowed(vcpu))
10901 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010902}
10903
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010904static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10905{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010906 if (func == 1 && nested)
10907 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010908}
10909
Yang Zhang25d92082013-08-06 12:00:32 +030010910static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10911 struct x86_exception *fault)
10912{
Jan Kiszka533558b2014-01-04 18:47:20 +010010913 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010914 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010915 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010916 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010917
Bandan Dasc5f983f2017-05-05 15:25:14 -040010918 if (vmx->nested.pml_full) {
10919 exit_reason = EXIT_REASON_PML_FULL;
10920 vmx->nested.pml_full = false;
10921 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10922 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010923 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010924 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010925 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010926
10927 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010928 vmcs12->guest_physical_address = fault->address;
10929}
10930
Peter Feiner995f00a2017-06-30 17:26:32 -070010931static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10932{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010933 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010934}
10935
Nadav Har'El155a97a2013-08-05 11:07:16 +030010936/* Callbacks for nested_ept_init_mmu_context: */
10937
10938static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10939{
10940 /* return the page table to be shadowed - in our case, EPT12 */
10941 return get_vmcs12(vcpu)->ept_pointer;
10942}
10943
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010944static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010945{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010946 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010947 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010948 return 1;
10949
Paolo Bonziniad896af2013-10-02 16:56:14 +020010950 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010951 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010952 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010953 nested_ept_ad_enabled(vcpu),
10954 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010955 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10956 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10957 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10958
10959 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010960 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010961}
10962
10963static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10964{
10965 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10966}
10967
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010968static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10969 u16 error_code)
10970{
10971 bool inequality, bit;
10972
10973 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10974 inequality =
10975 (error_code & vmcs12->page_fault_error_code_mask) !=
10976 vmcs12->page_fault_error_code_match;
10977 return inequality ^ bit;
10978}
10979
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010980static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10981 struct x86_exception *fault)
10982{
10983 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10984
10985 WARN_ON(!is_guest_mode(vcpu));
10986
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010987 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10988 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010989 vmcs12->vm_exit_intr_error_code = fault->error_code;
10990 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10991 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10992 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10993 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010994 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010995 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010996 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010997}
10998
Paolo Bonzinic9923842017-12-13 14:16:30 +010010999static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11000 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011001
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011002static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011003{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011004 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011005 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011006 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011007 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011008
11009 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011010 /*
11011 * Translate L1 physical address to host physical
11012 * address for vmcs02. Keep the page pinned, so this
11013 * physical address remains valid. We keep a reference
11014 * to it so we can release it later.
11015 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011016 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011017 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011018 vmx->nested.apic_access_page = NULL;
11019 }
11020 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011021 /*
11022 * If translation failed, no matter: This feature asks
11023 * to exit when accessing the given address, and if it
11024 * can never be accessed, this feature won't do
11025 * anything anyway.
11026 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011027 if (!is_error_page(page)) {
11028 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011029 hpa = page_to_phys(vmx->nested.apic_access_page);
11030 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11031 } else {
11032 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11033 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11034 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011035 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011036
11037 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011038 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011039 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011040 vmx->nested.virtual_apic_page = NULL;
11041 }
11042 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011043
11044 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011045 * If translation failed, VM entry will fail because
11046 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11047 * Failing the vm entry is _not_ what the processor
11048 * does but it's basically the only possibility we
11049 * have. We could still enter the guest if CR8 load
11050 * exits are enabled, CR8 store exits are enabled, and
11051 * virtualize APIC access is disabled; in this case
11052 * the processor would never use the TPR shadow and we
11053 * could simply clear the bit from the execution
11054 * control. But such a configuration is useless, so
11055 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011056 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011057 if (!is_error_page(page)) {
11058 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011059 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11060 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11061 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011062 }
11063
Wincy Van705699a2015-02-03 23:58:17 +080011064 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011065 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11066 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011067 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011068 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011069 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011070 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11071 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011072 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011073 vmx->nested.pi_desc_page = page;
11074 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011075 vmx->nested.pi_desc =
11076 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11077 (unsigned long)(vmcs12->posted_intr_desc_addr &
11078 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011079 vmcs_write64(POSTED_INTR_DESC_ADDR,
11080 page_to_phys(vmx->nested.pi_desc_page) +
11081 (unsigned long)(vmcs12->posted_intr_desc_addr &
11082 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011083 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011084 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011085 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11086 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011087 else
11088 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11089 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011090}
11091
Jan Kiszkaf41245002014-03-07 20:03:13 +010011092static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11093{
11094 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11095 struct vcpu_vmx *vmx = to_vmx(vcpu);
11096
11097 if (vcpu->arch.virtual_tsc_khz == 0)
11098 return;
11099
11100 /* Make sure short timeouts reliably trigger an immediate vmexit.
11101 * hrtimer_start does not guarantee this. */
11102 if (preemption_timeout <= 1) {
11103 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11104 return;
11105 }
11106
11107 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11108 preemption_timeout *= 1000000;
11109 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11110 hrtimer_start(&vmx->nested.preemption_timer,
11111 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11112}
11113
Jim Mattson56a20512017-07-06 16:33:06 -070011114static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11115 struct vmcs12 *vmcs12)
11116{
11117 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11118 return 0;
11119
11120 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11121 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11122 return -EINVAL;
11123
11124 return 0;
11125}
11126
Wincy Van3af18d92015-02-03 23:49:31 +080011127static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11128 struct vmcs12 *vmcs12)
11129{
Wincy Van3af18d92015-02-03 23:49:31 +080011130 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11131 return 0;
11132
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011133 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011134 return -EINVAL;
11135
11136 return 0;
11137}
11138
Jim Mattson712b12d2017-08-24 13:24:47 -070011139static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11140 struct vmcs12 *vmcs12)
11141{
11142 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11143 return 0;
11144
11145 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11146 return -EINVAL;
11147
11148 return 0;
11149}
11150
Wincy Van3af18d92015-02-03 23:49:31 +080011151/*
11152 * Merge L0's and L1's MSR bitmap, return false to indicate that
11153 * we do not use the hardware.
11154 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011155static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11156 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011157{
Wincy Van82f0dd42015-02-03 23:57:18 +080011158 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011159 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011160 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011161 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011162 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011163 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011164 *
11165 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11166 * ensures that we do not accidentally generate an L02 MSR bitmap
11167 * from the L12 MSR bitmap that is too permissive.
11168 * 2. That L1 or L2s have actually used the MSR. This avoids
11169 * unnecessarily merging of the bitmap if the MSR is unused. This
11170 * works properly because we only update the L01 MSR bitmap lazily.
11171 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11172 * updated to reflect this when L1 (or its L2s) actually write to
11173 * the MSR.
11174 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011175 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11176 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011177
Paolo Bonzinic9923842017-12-13 14:16:30 +010011178 /* Nothing to do if the MSR bitmap is not in use. */
11179 if (!cpu_has_vmx_msr_bitmap() ||
11180 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11181 return false;
11182
Ashok Raj15d45072018-02-01 22:59:43 +010011183 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011184 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011185 return false;
11186
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011187 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11188 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011189 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011190
Radim Krčmářd048c092016-08-08 20:16:22 +020011191 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011192 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11193 /*
11194 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11195 * just lets the processor take the value from the virtual-APIC page;
11196 * take those 256 bits directly from the L1 bitmap.
11197 */
11198 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11199 unsigned word = msr / BITS_PER_LONG;
11200 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11201 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011202 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011203 } else {
11204 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11205 unsigned word = msr / BITS_PER_LONG;
11206 msr_bitmap_l0[word] = ~0;
11207 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11208 }
11209 }
11210
11211 nested_vmx_disable_intercept_for_msr(
11212 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011213 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011214 MSR_TYPE_W);
11215
11216 if (nested_cpu_has_vid(vmcs12)) {
11217 nested_vmx_disable_intercept_for_msr(
11218 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011219 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011220 MSR_TYPE_W);
11221 nested_vmx_disable_intercept_for_msr(
11222 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011223 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011224 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011225 }
Ashok Raj15d45072018-02-01 22:59:43 +010011226
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011227 if (spec_ctrl)
11228 nested_vmx_disable_intercept_for_msr(
11229 msr_bitmap_l1, msr_bitmap_l0,
11230 MSR_IA32_SPEC_CTRL,
11231 MSR_TYPE_R | MSR_TYPE_W);
11232
Ashok Raj15d45072018-02-01 22:59:43 +010011233 if (pred_cmd)
11234 nested_vmx_disable_intercept_for_msr(
11235 msr_bitmap_l1, msr_bitmap_l0,
11236 MSR_IA32_PRED_CMD,
11237 MSR_TYPE_W);
11238
Wincy Vanf2b93282015-02-03 23:56:03 +080011239 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011240 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011241
11242 return true;
11243}
11244
Liran Alon61ada742018-06-23 02:35:08 +030011245static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11246 struct vmcs12 *vmcs12)
11247{
11248 struct vmcs12 *shadow;
11249 struct page *page;
11250
11251 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11252 vmcs12->vmcs_link_pointer == -1ull)
11253 return;
11254
11255 shadow = get_shadow_vmcs12(vcpu);
11256 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11257
11258 memcpy(shadow, kmap(page), VMCS12_SIZE);
11259
11260 kunmap(page);
11261 kvm_release_page_clean(page);
11262}
11263
11264static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11265 struct vmcs12 *vmcs12)
11266{
11267 struct vcpu_vmx *vmx = to_vmx(vcpu);
11268
11269 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11270 vmcs12->vmcs_link_pointer == -1ull)
11271 return;
11272
11273 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11274 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11275}
11276
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011277static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11278 struct vmcs12 *vmcs12)
11279{
11280 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11281 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11282 return -EINVAL;
11283 else
11284 return 0;
11285}
11286
Wincy Vanf2b93282015-02-03 23:56:03 +080011287static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11288 struct vmcs12 *vmcs12)
11289{
Wincy Van82f0dd42015-02-03 23:57:18 +080011290 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011291 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011292 !nested_cpu_has_vid(vmcs12) &&
11293 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011294 return 0;
11295
11296 /*
11297 * If virtualize x2apic mode is enabled,
11298 * virtualize apic access must be disabled.
11299 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011300 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11301 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011302 return -EINVAL;
11303
Wincy Van608406e2015-02-03 23:57:51 +080011304 /*
11305 * If virtual interrupt delivery is enabled,
11306 * we must exit on external interrupts.
11307 */
11308 if (nested_cpu_has_vid(vmcs12) &&
11309 !nested_exit_on_intr(vcpu))
11310 return -EINVAL;
11311
Wincy Van705699a2015-02-03 23:58:17 +080011312 /*
11313 * bits 15:8 should be zero in posted_intr_nv,
11314 * the descriptor address has been already checked
11315 * in nested_get_vmcs12_pages.
11316 */
11317 if (nested_cpu_has_posted_intr(vmcs12) &&
11318 (!nested_cpu_has_vid(vmcs12) ||
11319 !nested_exit_intr_ack_set(vcpu) ||
11320 vmcs12->posted_intr_nv & 0xff00))
11321 return -EINVAL;
11322
Wincy Vanf2b93282015-02-03 23:56:03 +080011323 /* tpr shadow is needed by all apicv features. */
11324 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11325 return -EINVAL;
11326
11327 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011328}
11329
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011330static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11331 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011332 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011333{
Liran Alone2536742018-06-23 02:35:02 +030011334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011335 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011336 u64 count, addr;
11337
Liran Alone2536742018-06-23 02:35:02 +030011338 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11339 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011340 WARN_ON(1);
11341 return -EINVAL;
11342 }
11343 if (count == 0)
11344 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011345 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011346 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11347 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011348 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011349 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11350 addr_field, maxphyaddr, count, addr);
11351 return -EINVAL;
11352 }
11353 return 0;
11354}
11355
11356static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11357 struct vmcs12 *vmcs12)
11358{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011359 if (vmcs12->vm_exit_msr_load_count == 0 &&
11360 vmcs12->vm_exit_msr_store_count == 0 &&
11361 vmcs12->vm_entry_msr_load_count == 0)
11362 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011363 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011364 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011365 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011366 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011367 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011368 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011369 return -EINVAL;
11370 return 0;
11371}
11372
Bandan Dasc5f983f2017-05-05 15:25:14 -040011373static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11374 struct vmcs12 *vmcs12)
11375{
11376 u64 address = vmcs12->pml_address;
11377 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11378
11379 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11380 if (!nested_cpu_has_ept(vmcs12) ||
11381 !IS_ALIGNED(address, 4096) ||
11382 address >> maxphyaddr)
11383 return -EINVAL;
11384 }
11385
11386 return 0;
11387}
11388
Liran Alona8a7c022018-06-23 02:35:06 +030011389static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11390 struct vmcs12 *vmcs12)
11391{
11392 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11393 return 0;
11394
11395 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11396 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11397 return -EINVAL;
11398
11399 return 0;
11400}
11401
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011402static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11403 struct vmx_msr_entry *e)
11404{
11405 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011406 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011407 return -EINVAL;
11408 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11409 e->index == MSR_IA32_UCODE_REV)
11410 return -EINVAL;
11411 if (e->reserved != 0)
11412 return -EINVAL;
11413 return 0;
11414}
11415
11416static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11417 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011418{
11419 if (e->index == MSR_FS_BASE ||
11420 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011421 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11422 nested_vmx_msr_check_common(vcpu, e))
11423 return -EINVAL;
11424 return 0;
11425}
11426
11427static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11428 struct vmx_msr_entry *e)
11429{
11430 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11431 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011432 return -EINVAL;
11433 return 0;
11434}
11435
11436/*
11437 * Load guest's/host's msr at nested entry/exit.
11438 * return 0 for success, entry index for failure.
11439 */
11440static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11441{
11442 u32 i;
11443 struct vmx_msr_entry e;
11444 struct msr_data msr;
11445
11446 msr.host_initiated = false;
11447 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011448 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11449 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011450 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011451 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11452 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011453 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011454 }
11455 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011456 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011457 "%s check failed (%u, 0x%x, 0x%x)\n",
11458 __func__, i, e.index, e.reserved);
11459 goto fail;
11460 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011461 msr.index = e.index;
11462 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011463 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011464 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011465 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11466 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011467 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011468 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011469 }
11470 return 0;
11471fail:
11472 return i + 1;
11473}
11474
11475static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11476{
11477 u32 i;
11478 struct vmx_msr_entry e;
11479
11480 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011481 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011482 if (kvm_vcpu_read_guest(vcpu,
11483 gpa + i * sizeof(e),
11484 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011485 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011486 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11487 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011488 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011489 }
11490 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011491 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011492 "%s check failed (%u, 0x%x, 0x%x)\n",
11493 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011494 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011495 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011496 msr_info.host_initiated = false;
11497 msr_info.index = e.index;
11498 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011499 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011500 "%s cannot read MSR (%u, 0x%x)\n",
11501 __func__, i, e.index);
11502 return -EINVAL;
11503 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011504 if (kvm_vcpu_write_guest(vcpu,
11505 gpa + i * sizeof(e) +
11506 offsetof(struct vmx_msr_entry, value),
11507 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011508 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011509 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011510 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011511 return -EINVAL;
11512 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011513 }
11514 return 0;
11515}
11516
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011517static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11518{
11519 unsigned long invalid_mask;
11520
11521 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11522 return (val & invalid_mask) == 0;
11523}
11524
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011525/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011526 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11527 * emulating VM entry into a guest with EPT enabled.
11528 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11529 * is assigned to entry_failure_code on failure.
11530 */
11531static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011532 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011533{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011534 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011535 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011536 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11537 return 1;
11538 }
11539
11540 /*
11541 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11542 * must not be dereferenced.
11543 */
11544 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11545 !nested_ept) {
11546 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11547 *entry_failure_code = ENTRY_FAIL_PDPTE;
11548 return 1;
11549 }
11550 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011551 }
11552
Junaid Shahid50c28f22018-06-27 14:59:11 -070011553 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011554 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011555
11556 vcpu->arch.cr3 = cr3;
11557 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11558
11559 kvm_init_mmu(vcpu, false);
11560
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011561 return 0;
11562}
11563
Jim Mattson6514dc32018-04-26 16:09:12 -070011564static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011565{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011566 struct vcpu_vmx *vmx = to_vmx(vcpu);
11567
11568 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11569 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11570 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11571 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11572 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11573 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11574 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11575 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11576 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11577 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11578 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11579 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11580 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11581 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11582 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11583 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11584 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11585 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11586 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11587 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11588 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11589 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11590 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11591 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11592 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11593 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11594 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11595 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11596 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11597 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11598 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011599
11600 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11601 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11602 vmcs12->guest_pending_dbg_exceptions);
11603 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11604 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11605
11606 if (nested_cpu_has_xsaves(vmcs12))
11607 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11608 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11609
11610 if (cpu_has_vmx_posted_intr())
11611 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11612
11613 /*
11614 * Whether page-faults are trapped is determined by a combination of
11615 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11616 * If enable_ept, L0 doesn't care about page faults and we should
11617 * set all of these to L1's desires. However, if !enable_ept, L0 does
11618 * care about (at least some) page faults, and because it is not easy
11619 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11620 * to exit on each and every L2 page fault. This is done by setting
11621 * MASK=MATCH=0 and (see below) EB.PF=1.
11622 * Note that below we don't need special code to set EB.PF beyond the
11623 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11624 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11625 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11626 */
11627 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11628 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11629 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11630 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11631
11632 /* All VMFUNCs are currently emulated through L0 vmexits. */
11633 if (cpu_has_vmx_vmfunc())
11634 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11635
11636 if (cpu_has_vmx_apicv()) {
11637 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11638 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11639 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11640 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11641 }
11642
11643 /*
11644 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11645 * Some constant fields are set here by vmx_set_constant_host_state().
11646 * Other fields are different per CPU, and will be set later when
11647 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11648 */
11649 vmx_set_constant_host_state(vmx);
11650
11651 /*
11652 * Set the MSR load/store lists to match L0's settings.
11653 */
11654 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11655 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11656 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11657 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11658 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11659
11660 set_cr4_guest_host_mask(vmx);
11661
11662 if (vmx_mpx_supported())
11663 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11664
11665 if (enable_vpid) {
11666 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11667 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11668 else
11669 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11670 }
11671
11672 /*
11673 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11674 */
11675 if (enable_ept) {
11676 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11677 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11678 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11679 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11680 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011681
11682 if (cpu_has_vmx_msr_bitmap())
11683 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011684}
11685
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011686/*
11687 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11688 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011689 * 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 +030011690 * guest in a way that will both be appropriate to L1's requests, and our
11691 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11692 * function also has additional necessary side-effects, like setting various
11693 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011694 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11695 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011696 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011697static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011698 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011699{
11700 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011701 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011702
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011703 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011704 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011705 vmx->nested.dirty_vmcs12 = false;
11706 }
11707
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011708 /*
11709 * First, the fields that are shadowed. This must be kept in sync
11710 * with vmx_shadow_fields.h.
11711 */
11712
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011713 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011714 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011715 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011716 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11717 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011718
11719 /*
11720 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11721 * HOST_FS_BASE, HOST_GS_BASE.
11722 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011723
Jim Mattson6514dc32018-04-26 16:09:12 -070011724 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011725 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011726 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11727 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11728 } else {
11729 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11730 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11731 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011732 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011733 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11734 vmcs12->vm_entry_intr_info_field);
11735 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11736 vmcs12->vm_entry_exception_error_code);
11737 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11738 vmcs12->vm_entry_instruction_len);
11739 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11740 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011741 vmx->loaded_vmcs->nmi_known_unmasked =
11742 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011743 } else {
11744 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11745 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011746 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011747
Jan Kiszkaf41245002014-03-07 20:03:13 +010011748 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011749
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011750 /* Preemption timer setting is only taken from vmcs01. */
11751 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11752 exec_control |= vmcs_config.pin_based_exec_ctrl;
11753 if (vmx->hv_deadline_tsc == -1)
11754 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11755
11756 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011757 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011758 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11759 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011760 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011761 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011762 }
Wincy Van705699a2015-02-03 23:58:17 +080011763
Jan Kiszkaf41245002014-03-07 20:03:13 +010011764 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011765
Jan Kiszkaf41245002014-03-07 20:03:13 +010011766 vmx->nested.preemption_timer_expired = false;
11767 if (nested_cpu_has_preemption_timer(vmcs12))
11768 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011769
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011770 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011771 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011772
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011773 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011774 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011775 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011776 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011777 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011778 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011779 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11780 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011781 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011782 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11783 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11784 ~SECONDARY_EXEC_ENABLE_PML;
11785 exec_control |= vmcs12_exec_ctrl;
11786 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011787
Liran Alon32c7acf2018-06-23 02:35:11 +030011788 /* VMCS shadowing for L2 is emulated for now */
11789 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11790
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011791 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011792 vmcs_write16(GUEST_INTR_STATUS,
11793 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011794
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011795 /*
11796 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11797 * nested_get_vmcs12_pages will either fix it up or
11798 * remove the VM execution control.
11799 */
11800 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11801 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11802
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011803 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11804 }
11805
Jim Mattson83bafef2016-10-04 10:48:38 -070011806 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011807 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11808 * entry, but only if the current (host) sp changed from the value
11809 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11810 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11811 * here we just force the write to happen on entry.
11812 */
11813 vmx->host_rsp = 0;
11814
11815 exec_control = vmx_exec_control(vmx); /* L0's desires */
11816 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11817 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11818 exec_control &= ~CPU_BASED_TPR_SHADOW;
11819 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011820
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011821 /*
11822 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11823 * nested_get_vmcs12_pages can't fix it up, the illegal value
11824 * will result in a VM entry failure.
11825 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011826 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011827 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011828 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011829 } else {
11830#ifdef CONFIG_X86_64
11831 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11832 CPU_BASED_CR8_STORE_EXITING;
11833#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011834 }
11835
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011836 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011837 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11838 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011839 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011840 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11841 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11842
11843 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11844
11845 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11846 * bitwise-or of what L1 wants to trap for L2, and what we want to
11847 * trap. Note that CR0.TS also needs updating - we do this later.
11848 */
11849 update_exception_bitmap(vcpu);
11850 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11851 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11852
Nadav Har'El8049d652013-08-05 11:07:06 +030011853 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11854 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11855 * bits are further modified by vmx_set_efer() below.
11856 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011857 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011858
11859 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11860 * emulated by vmx_set_efer(), below.
11861 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011862 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011863 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11864 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011865 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11866
Jim Mattson6514dc32018-04-26 16:09:12 -070011867 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011868 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011869 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011870 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011871 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011872 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011873 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011874
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011875 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11876
Peter Feinerc95ba922016-08-17 09:36:47 -070011877 if (kvm_has_tsc_control)
11878 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011879
11880 if (enable_vpid) {
11881 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011882 * There is no direct mapping between vpid02 and vpid12, the
11883 * vpid02 is per-vCPU for L0 and reused while the value of
11884 * vpid12 is changed w/ one invvpid during nested vmentry.
11885 * The vpid12 is allocated by L1 for L2, so it will not
11886 * influence global bitmap(for vpid01 and vpid02 allocation)
11887 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011888 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011889 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011890 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11891 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011892 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011893 }
11894 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011895 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011896 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011897 }
11898
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011899 if (enable_pml) {
11900 /*
11901 * Conceptually we want to copy the PML address and index from
11902 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11903 * since we always flush the log on each vmexit, this happens
11904 * to be equivalent to simply resetting the fields in vmcs02.
11905 */
11906 ASSERT(vmx->pml_pg);
11907 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11908 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11909 }
11910
Nadav Har'El155a97a2013-08-05 11:07:16 +030011911 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011912 if (nested_ept_init_mmu_context(vcpu)) {
11913 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11914 return 1;
11915 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011916 } else if (nested_cpu_has2(vmcs12,
11917 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011918 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011919 }
11920
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011921 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011922 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11923 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011924 * The CR0_READ_SHADOW is what L2 should have expected to read given
11925 * the specifications by L1; It's not enough to take
11926 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11927 * have more bits than L1 expected.
11928 */
11929 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11930 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11931
11932 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11933 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11934
Jim Mattson6514dc32018-04-26 16:09:12 -070011935 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011936 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011937 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11938 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11939 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11940 else
11941 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11942 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11943 vmx_set_efer(vcpu, vcpu->arch.efer);
11944
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011945 /*
11946 * Guest state is invalid and unrestricted guest is disabled,
11947 * which means L1 attempted VMEntry to L2 with invalid state.
11948 * Fail the VMEntry.
11949 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011950 if (vmx->emulation_required) {
11951 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011952 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011953 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011954
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011955 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011956 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011957 entry_failure_code))
11958 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011959
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011960 if (!enable_ept)
11961 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11962
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011963 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11964 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011965 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011966}
11967
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011968static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11969{
11970 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11971 nested_cpu_has_virtual_nmis(vmcs12))
11972 return -EINVAL;
11973
11974 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11975 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11976 return -EINVAL;
11977
11978 return 0;
11979}
11980
Jim Mattsonca0bde22016-11-30 12:03:46 -080011981static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11982{
11983 struct vcpu_vmx *vmx = to_vmx(vcpu);
11984
11985 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11986 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11987 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11988
Jim Mattson56a20512017-07-06 16:33:06 -070011989 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11990 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11991
Jim Mattsonca0bde22016-11-30 12:03:46 -080011992 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11993 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11994
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011995 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11996 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11997
Jim Mattson712b12d2017-08-24 13:24:47 -070011998 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11999 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12000
Jim Mattsonca0bde22016-11-30 12:03:46 -080012001 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12002 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12003
12004 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12005 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12006
Bandan Dasc5f983f2017-05-05 15:25:14 -040012007 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12008 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12009
Liran Alona8a7c022018-06-23 02:35:06 +030012010 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12011 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12012
Jim Mattsonca0bde22016-11-30 12:03:46 -080012013 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012014 vmx->nested.msrs.procbased_ctls_low,
12015 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012016 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12017 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012018 vmx->nested.msrs.secondary_ctls_low,
12019 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012020 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012021 vmx->nested.msrs.pinbased_ctls_low,
12022 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012023 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012024 vmx->nested.msrs.exit_ctls_low,
12025 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012026 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012027 vmx->nested.msrs.entry_ctls_low,
12028 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012029 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12030
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012031 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012032 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12033
Bandan Das41ab9372017-08-03 15:54:43 -040012034 if (nested_cpu_has_vmfunc(vmcs12)) {
12035 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012036 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012037 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12038
12039 if (nested_cpu_has_eptp_switching(vmcs12)) {
12040 if (!nested_cpu_has_ept(vmcs12) ||
12041 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12042 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12043 }
12044 }
Bandan Das27c42a12017-08-03 15:54:42 -040012045
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012046 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12047 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12048
Jim Mattsonca0bde22016-11-30 12:03:46 -080012049 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12050 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12051 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12052 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12053
Marc Orr04473782018-06-20 17:21:29 -070012054 /*
12055 * From the Intel SDM, volume 3:
12056 * Fields relevant to VM-entry event injection must be set properly.
12057 * These fields are the VM-entry interruption-information field, the
12058 * VM-entry exception error code, and the VM-entry instruction length.
12059 */
12060 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12061 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12062 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12063 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12064 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12065 bool should_have_error_code;
12066 bool urg = nested_cpu_has2(vmcs12,
12067 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12068 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12069
12070 /* VM-entry interruption-info field: interruption type */
12071 if (intr_type == INTR_TYPE_RESERVED ||
12072 (intr_type == INTR_TYPE_OTHER_EVENT &&
12073 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12074 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12075
12076 /* VM-entry interruption-info field: vector */
12077 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12078 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12079 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12080 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12081
12082 /* VM-entry interruption-info field: deliver error code */
12083 should_have_error_code =
12084 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12085 x86_exception_has_error_code(vector);
12086 if (has_error_code != should_have_error_code)
12087 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12088
12089 /* VM-entry exception error code */
12090 if (has_error_code &&
12091 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12092 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12093
12094 /* VM-entry interruption-info field: reserved bits */
12095 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12096 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12097
12098 /* VM-entry instruction length */
12099 switch (intr_type) {
12100 case INTR_TYPE_SOFT_EXCEPTION:
12101 case INTR_TYPE_SOFT_INTR:
12102 case INTR_TYPE_PRIV_SW_EXCEPTION:
12103 if ((vmcs12->vm_entry_instruction_len > 15) ||
12104 (vmcs12->vm_entry_instruction_len == 0 &&
12105 !nested_cpu_has_zero_length_injection(vcpu)))
12106 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12107 }
12108 }
12109
Jim Mattsonca0bde22016-11-30 12:03:46 -080012110 return 0;
12111}
12112
Liran Alonf145d902018-06-23 02:35:07 +030012113static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12114 struct vmcs12 *vmcs12)
12115{
12116 int r;
12117 struct page *page;
12118 struct vmcs12 *shadow;
12119
12120 if (vmcs12->vmcs_link_pointer == -1ull)
12121 return 0;
12122
12123 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12124 return -EINVAL;
12125
12126 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12127 if (is_error_page(page))
12128 return -EINVAL;
12129
12130 r = 0;
12131 shadow = kmap(page);
12132 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12133 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12134 r = -EINVAL;
12135 kunmap(page);
12136 kvm_release_page_clean(page);
12137 return r;
12138}
12139
Jim Mattsonca0bde22016-11-30 12:03:46 -080012140static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12141 u32 *exit_qual)
12142{
12143 bool ia32e;
12144
12145 *exit_qual = ENTRY_FAIL_DEFAULT;
12146
12147 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12148 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12149 return 1;
12150
Liran Alonf145d902018-06-23 02:35:07 +030012151 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012152 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12153 return 1;
12154 }
12155
12156 /*
12157 * If the load IA32_EFER VM-entry control is 1, the following checks
12158 * are performed on the field for the IA32_EFER MSR:
12159 * - Bits reserved in the IA32_EFER MSR must be 0.
12160 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12161 * the IA-32e mode guest VM-exit control. It must also be identical
12162 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12163 * CR0.PG) is 1.
12164 */
12165 if (to_vmx(vcpu)->nested.nested_run_pending &&
12166 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12167 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12168 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12169 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12170 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12171 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12172 return 1;
12173 }
12174
12175 /*
12176 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12177 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12178 * the values of the LMA and LME bits in the field must each be that of
12179 * the host address-space size VM-exit control.
12180 */
12181 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12182 ia32e = (vmcs12->vm_exit_controls &
12183 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12184 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12185 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12186 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12187 return 1;
12188 }
12189
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012190 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12191 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12192 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12193 return 1;
12194
Jim Mattsonca0bde22016-11-30 12:03:46 -080012195 return 0;
12196}
12197
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012198/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012199 * If exit_qual is NULL, this is being called from state restore (either RSM
12200 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012201 */
12202static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012203{
12204 struct vcpu_vmx *vmx = to_vmx(vcpu);
12205 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012206 bool from_vmentry = !!exit_qual;
12207 u32 dummy_exit_qual;
12208 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012209
Jim Mattson858e25c2016-11-30 12:03:47 -080012210 enter_guest_mode(vcpu);
12211
12212 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12213 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12214
Jim Mattsonde3a0022017-11-27 17:22:25 -060012215 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012216 vmx_segment_cache_clear(vmx);
12217
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012218 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12219 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12220
12221 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012222 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012223 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012224
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012225 if (from_vmentry) {
12226 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012227
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012228 r = EXIT_REASON_MSR_LOAD_FAIL;
12229 *exit_qual = nested_vmx_load_msr(vcpu,
12230 vmcs12->vm_entry_msr_load_addr,
12231 vmcs12->vm_entry_msr_load_count);
12232 if (*exit_qual)
12233 goto fail;
12234 } else {
12235 /*
12236 * The MMU is not initialized to point at the right entities yet and
12237 * "get pages" would need to read data from the guest (i.e. we will
12238 * need to perform gpa to hpa translation). Request a call
12239 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12240 * have already been set at vmentry time and should not be reset.
12241 */
12242 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12243 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012244
Jim Mattson858e25c2016-11-30 12:03:47 -080012245 /*
12246 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12247 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12248 * returned as far as L1 is concerned. It will only return (and set
12249 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12250 */
12251 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012252
12253fail:
12254 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12255 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12256 leave_guest_mode(vcpu);
12257 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012258 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012259}
12260
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012261/*
12262 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12263 * for running an L2 nested guest.
12264 */
12265static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12266{
12267 struct vmcs12 *vmcs12;
12268 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012269 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012270 u32 exit_qual;
12271 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012272
Kyle Hueyeb277562016-11-29 12:40:39 -080012273 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012274 return 1;
12275
Kyle Hueyeb277562016-11-29 12:40:39 -080012276 if (!nested_vmx_check_vmcs12(vcpu))
12277 goto out;
12278
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012279 vmcs12 = get_vmcs12(vcpu);
12280
Liran Alona6192d42018-06-23 02:35:04 +030012281 /*
12282 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12283 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12284 * rather than RFLAGS.ZF, and no error number is stored to the
12285 * VM-instruction error field.
12286 */
12287 if (vmcs12->hdr.shadow_vmcs) {
12288 nested_vmx_failInvalid(vcpu);
12289 goto out;
12290 }
12291
Abel Gordon012f83c2013-04-18 14:39:25 +030012292 if (enable_shadow_vmcs)
12293 copy_shadow_to_vmcs12(vmx);
12294
Nadav Har'El7c177932011-05-25 23:12:04 +030012295 /*
12296 * The nested entry process starts with enforcing various prerequisites
12297 * on vmcs12 as required by the Intel SDM, and act appropriately when
12298 * they fail: As the SDM explains, some conditions should cause the
12299 * instruction to fail, while others will cause the instruction to seem
12300 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12301 * To speed up the normal (success) code path, we should avoid checking
12302 * for misconfigurations which will anyway be caught by the processor
12303 * when using the merged vmcs02.
12304 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012305 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12306 nested_vmx_failValid(vcpu,
12307 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12308 goto out;
12309 }
12310
Nadav Har'El7c177932011-05-25 23:12:04 +030012311 if (vmcs12->launch_state == launch) {
12312 nested_vmx_failValid(vcpu,
12313 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12314 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012315 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012316 }
12317
Jim Mattsonca0bde22016-11-30 12:03:46 -080012318 ret = check_vmentry_prereqs(vcpu, vmcs12);
12319 if (ret) {
12320 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012321 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012322 }
12323
Nadav Har'El7c177932011-05-25 23:12:04 +030012324 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012325 * After this point, the trap flag no longer triggers a singlestep trap
12326 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12327 * This is not 100% correct; for performance reasons, we delegate most
12328 * of the checks on host state to the processor. If those fail,
12329 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012330 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012331 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012332
Jim Mattsonca0bde22016-11-30 12:03:46 -080012333 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12334 if (ret) {
12335 nested_vmx_entry_failure(vcpu, vmcs12,
12336 EXIT_REASON_INVALID_STATE, exit_qual);
12337 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012338 }
12339
12340 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012341 * We're finally done with prerequisite checking, and can start with
12342 * the nested entry.
12343 */
12344
Jim Mattson6514dc32018-04-26 16:09:12 -070012345 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012346 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012347 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012348 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012349 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012350 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012351 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012352
Chao Gao135a06c2018-02-11 10:06:30 +080012353 /*
Liran Alon61ada742018-06-23 02:35:08 +030012354 * Must happen outside of enter_vmx_non_root_mode() as it will
12355 * also be used as part of restoring nVMX state for
12356 * snapshot restore (migration).
12357 *
12358 * In this flow, it is assumed that vmcs12 cache was
12359 * trasferred as part of captured nVMX state and should
12360 * therefore not be read from guest memory (which may not
12361 * exist on destination host yet).
12362 */
12363 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12364
12365 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012366 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12367 * by event injection, halt vcpu.
12368 */
12369 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012370 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12371 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012372 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012373 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012374 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012375
12376out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012377 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012378}
12379
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012380/*
12381 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12382 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12383 * This function returns the new value we should put in vmcs12.guest_cr0.
12384 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12385 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12386 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12387 * didn't trap the bit, because if L1 did, so would L0).
12388 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12389 * been modified by L2, and L1 knows it. So just leave the old value of
12390 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12391 * isn't relevant, because if L0 traps this bit it can set it to anything.
12392 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12393 * changed these bits, and therefore they need to be updated, but L0
12394 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12395 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12396 */
12397static inline unsigned long
12398vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12399{
12400 return
12401 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12402 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12403 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12404 vcpu->arch.cr0_guest_owned_bits));
12405}
12406
12407static inline unsigned long
12408vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12409{
12410 return
12411 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12412 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12413 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12414 vcpu->arch.cr4_guest_owned_bits));
12415}
12416
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012417static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12418 struct vmcs12 *vmcs12)
12419{
12420 u32 idt_vectoring;
12421 unsigned int nr;
12422
Wanpeng Li664f8e22017-08-24 03:35:09 -070012423 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012424 nr = vcpu->arch.exception.nr;
12425 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12426
12427 if (kvm_exception_is_soft(nr)) {
12428 vmcs12->vm_exit_instruction_len =
12429 vcpu->arch.event_exit_inst_len;
12430 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12431 } else
12432 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12433
12434 if (vcpu->arch.exception.has_error_code) {
12435 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12436 vmcs12->idt_vectoring_error_code =
12437 vcpu->arch.exception.error_code;
12438 }
12439
12440 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012441 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012442 vmcs12->idt_vectoring_info_field =
12443 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012444 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012445 nr = vcpu->arch.interrupt.nr;
12446 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12447
12448 if (vcpu->arch.interrupt.soft) {
12449 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12450 vmcs12->vm_entry_instruction_len =
12451 vcpu->arch.event_exit_inst_len;
12452 } else
12453 idt_vectoring |= INTR_TYPE_EXT_INTR;
12454
12455 vmcs12->idt_vectoring_info_field = idt_vectoring;
12456 }
12457}
12458
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012459static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12460{
12461 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012462 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012463 bool block_nested_events =
12464 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012465
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012466 if (vcpu->arch.exception.pending &&
12467 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012468 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012469 return -EBUSY;
12470 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012471 return 0;
12472 }
12473
Jan Kiszkaf41245002014-03-07 20:03:13 +010012474 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12475 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012476 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012477 return -EBUSY;
12478 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12479 return 0;
12480 }
12481
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012482 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012483 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012484 return -EBUSY;
12485 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12486 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12487 INTR_INFO_VALID_MASK, 0);
12488 /*
12489 * The NMI-triggered VM exit counts as injection:
12490 * clear this one and block further NMIs.
12491 */
12492 vcpu->arch.nmi_pending = 0;
12493 vmx_set_nmi_mask(vcpu, true);
12494 return 0;
12495 }
12496
12497 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12498 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012499 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012500 return -EBUSY;
12501 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012502 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012503 }
12504
David Hildenbrand6342c502017-01-25 11:58:58 +010012505 vmx_complete_nested_posted_interrupt(vcpu);
12506 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012507}
12508
Jan Kiszkaf41245002014-03-07 20:03:13 +010012509static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12510{
12511 ktime_t remaining =
12512 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12513 u64 value;
12514
12515 if (ktime_to_ns(remaining) <= 0)
12516 return 0;
12517
12518 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12519 do_div(value, 1000000);
12520 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12521}
12522
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012523/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012524 * Update the guest state fields of vmcs12 to reflect changes that
12525 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12526 * VM-entry controls is also updated, since this is really a guest
12527 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012528 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012529static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012530{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012531 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12532 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12533
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012534 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12535 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12536 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12537
12538 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12539 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12540 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12541 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12542 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12543 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12544 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12545 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12546 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12547 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12548 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12549 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12550 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12551 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12552 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12553 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12554 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12555 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12556 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12557 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12558 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12559 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12560 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12561 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12562 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12563 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12564 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12565 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12566 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12567 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12568 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12569 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12570 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12571 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12572 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12573 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12574
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012575 vmcs12->guest_interruptibility_info =
12576 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12577 vmcs12->guest_pending_dbg_exceptions =
12578 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012579 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12580 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12581 else
12582 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012583
Jan Kiszkaf41245002014-03-07 20:03:13 +010012584 if (nested_cpu_has_preemption_timer(vmcs12)) {
12585 if (vmcs12->vm_exit_controls &
12586 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12587 vmcs12->vmx_preemption_timer_value =
12588 vmx_get_preemption_timer_value(vcpu);
12589 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12590 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012591
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012592 /*
12593 * In some cases (usually, nested EPT), L2 is allowed to change its
12594 * own CR3 without exiting. If it has changed it, we must keep it.
12595 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12596 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12597 *
12598 * Additionally, restore L2's PDPTR to vmcs12.
12599 */
12600 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012601 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012602 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12603 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12604 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12605 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12606 }
12607
Jim Mattsond281e132017-06-01 12:44:46 -070012608 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012609
Wincy Van608406e2015-02-03 23:57:51 +080012610 if (nested_cpu_has_vid(vmcs12))
12611 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12612
Jan Kiszkac18911a2013-03-13 16:06:41 +010012613 vmcs12->vm_entry_controls =
12614 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012615 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012616
Jan Kiszka2996fca2014-06-16 13:59:43 +020012617 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12618 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12619 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12620 }
12621
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012622 /* TODO: These cannot have changed unless we have MSR bitmaps and
12623 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012624 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012625 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012626 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12627 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012628 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12629 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12630 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012631 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012632 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012633}
12634
12635/*
12636 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12637 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12638 * and this function updates it to reflect the changes to the guest state while
12639 * L2 was running (and perhaps made some exits which were handled directly by L0
12640 * without going back to L1), and to reflect the exit reason.
12641 * Note that we do not have to copy here all VMCS fields, just those that
12642 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12643 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12644 * which already writes to vmcs12 directly.
12645 */
12646static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12647 u32 exit_reason, u32 exit_intr_info,
12648 unsigned long exit_qualification)
12649{
12650 /* update guest state fields: */
12651 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012652
12653 /* update exit information fields: */
12654
Jan Kiszka533558b2014-01-04 18:47:20 +010012655 vmcs12->vm_exit_reason = exit_reason;
12656 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012657 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012658
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012659 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012660 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12661 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12662
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012663 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012664 vmcs12->launch_state = 1;
12665
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012666 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12667 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012668 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012669
12670 /*
12671 * Transfer the event that L0 or L1 may wanted to inject into
12672 * L2 to IDT_VECTORING_INFO_FIELD.
12673 */
12674 vmcs12_save_pending_event(vcpu, vmcs12);
12675 }
12676
12677 /*
12678 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12679 * preserved above and would only end up incorrectly in L1.
12680 */
12681 vcpu->arch.nmi_injected = false;
12682 kvm_clear_exception_queue(vcpu);
12683 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012684}
12685
Wanpeng Li5af41572017-11-05 16:54:49 -080012686static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12687 struct vmcs12 *vmcs12)
12688{
12689 u32 entry_failure_code;
12690
12691 nested_ept_uninit_mmu_context(vcpu);
12692
12693 /*
12694 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12695 * couldn't have changed.
12696 */
12697 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12698 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12699
12700 if (!enable_ept)
12701 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12702}
12703
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012704/*
12705 * A part of what we need to when the nested L2 guest exits and we want to
12706 * run its L1 parent, is to reset L1's guest state to the host state specified
12707 * in vmcs12.
12708 * This function is to be called not only on normal nested exit, but also on
12709 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12710 * Failures During or After Loading Guest State").
12711 * This function should be called when the active VMCS is L1's (vmcs01).
12712 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012713static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12714 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012715{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012716 struct kvm_segment seg;
12717
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012718 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12719 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012720 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012721 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12722 else
12723 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12724 vmx_set_efer(vcpu, vcpu->arch.efer);
12725
12726 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12727 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012728 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012729 /*
12730 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012731 * actually changed, because vmx_set_cr0 refers to efer set above.
12732 *
12733 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12734 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012735 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012736 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012737 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012738
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012739 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012740 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012741 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012742
Wanpeng Li5af41572017-11-05 16:54:49 -080012743 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012744
Liran Alon6f1e03b2018-05-22 17:16:14 +030012745 /*
12746 * If vmcs01 don't use VPID, CPU flushes TLB on every
12747 * VMEntry/VMExit. Thus, no need to flush TLB.
12748 *
12749 * If vmcs12 uses VPID, TLB entries populated by L2 are
12750 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12751 * with vmx->vpid. Thus, no need to flush TLB.
12752 *
12753 * Therefore, flush TLB only in case vmcs01 uses VPID and
12754 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12755 * are both tagged with vmx->vpid.
12756 */
12757 if (enable_vpid &&
12758 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012759 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012760 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012761
12762 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12763 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12764 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12765 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12766 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012767 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12768 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012769
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012770 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12771 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12772 vmcs_write64(GUEST_BNDCFGS, 0);
12773
Jan Kiszka44811c02013-08-04 17:17:27 +020012774 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012775 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012776 vcpu->arch.pat = vmcs12->host_ia32_pat;
12777 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012778 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12779 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12780 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012781
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012782 /* Set L1 segment info according to Intel SDM
12783 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12784 seg = (struct kvm_segment) {
12785 .base = 0,
12786 .limit = 0xFFFFFFFF,
12787 .selector = vmcs12->host_cs_selector,
12788 .type = 11,
12789 .present = 1,
12790 .s = 1,
12791 .g = 1
12792 };
12793 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12794 seg.l = 1;
12795 else
12796 seg.db = 1;
12797 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12798 seg = (struct kvm_segment) {
12799 .base = 0,
12800 .limit = 0xFFFFFFFF,
12801 .type = 3,
12802 .present = 1,
12803 .s = 1,
12804 .db = 1,
12805 .g = 1
12806 };
12807 seg.selector = vmcs12->host_ds_selector;
12808 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12809 seg.selector = vmcs12->host_es_selector;
12810 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12811 seg.selector = vmcs12->host_ss_selector;
12812 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12813 seg.selector = vmcs12->host_fs_selector;
12814 seg.base = vmcs12->host_fs_base;
12815 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12816 seg.selector = vmcs12->host_gs_selector;
12817 seg.base = vmcs12->host_gs_base;
12818 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12819 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012820 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012821 .limit = 0x67,
12822 .selector = vmcs12->host_tr_selector,
12823 .type = 11,
12824 .present = 1
12825 };
12826 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12827
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012828 kvm_set_dr(vcpu, 7, 0x400);
12829 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012830
Wincy Van3af18d92015-02-03 23:49:31 +080012831 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012832 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012833
Wincy Vanff651cb2014-12-11 08:52:58 +030012834 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12835 vmcs12->vm_exit_msr_load_count))
12836 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012837}
12838
12839/*
12840 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12841 * and modify vmcs12 to make it see what it would expect to see there if
12842 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12843 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012844static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12845 u32 exit_intr_info,
12846 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012847{
12848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012849 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12850
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012851 /* trying to cancel vmlaunch/vmresume is a bug */
12852 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12853
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012854 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012855 * The only expected VM-instruction error is "VM entry with
12856 * invalid control field(s)." Anything else indicates a
12857 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012858 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012859 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12860 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12861
12862 leave_guest_mode(vcpu);
12863
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012864 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12865 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12866
Jim Mattson4f350c62017-09-14 16:31:44 -070012867 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012868 if (exit_reason == -1)
12869 sync_vmcs12(vcpu, vmcs12);
12870 else
12871 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12872 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012873
Liran Alon61ada742018-06-23 02:35:08 +030012874 /*
12875 * Must happen outside of sync_vmcs12() as it will
12876 * also be used to capture vmcs12 cache as part of
12877 * capturing nVMX state for snapshot (migration).
12878 *
12879 * Otherwise, this flush will dirty guest memory at a
12880 * point it is already assumed by user-space to be
12881 * immutable.
12882 */
12883 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12884
Jim Mattson4f350c62017-09-14 16:31:44 -070012885 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12886 vmcs12->vm_exit_msr_store_count))
12887 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012888 }
12889
Jim Mattson4f350c62017-09-14 16:31:44 -070012890 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012891 vm_entry_controls_reset_shadow(vmx);
12892 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012893 vmx_segment_cache_clear(vmx);
12894
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012895 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012896 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12897 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012898 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012899 if (vmx->hv_deadline_tsc == -1)
12900 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12901 PIN_BASED_VMX_PREEMPTION_TIMER);
12902 else
12903 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12904 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012905 if (kvm_has_tsc_control)
12906 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012907
Jim Mattson8d860bb2018-05-09 16:56:05 -040012908 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12909 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12910 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012911 } else if (!nested_cpu_has_ept(vmcs12) &&
12912 nested_cpu_has2(vmcs12,
12913 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012914 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012915 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012916
12917 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12918 vmx->host_rsp = 0;
12919
12920 /* Unpin physical memory we referred to in vmcs02 */
12921 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012922 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012923 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012924 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012925 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012926 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012927 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012928 }
Wincy Van705699a2015-02-03 23:58:17 +080012929 if (vmx->nested.pi_desc_page) {
12930 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012931 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012932 vmx->nested.pi_desc_page = NULL;
12933 vmx->nested.pi_desc = NULL;
12934 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012935
12936 /*
Tang Chen38b99172014-09-24 15:57:54 +080012937 * We are now running in L2, mmu_notifier will force to reload the
12938 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12939 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012940 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012941
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012942 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012943 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012944
12945 /* in case we halted in L2 */
12946 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012947
12948 if (likely(!vmx->fail)) {
12949 /*
12950 * TODO: SDM says that with acknowledge interrupt on
12951 * exit, bit 31 of the VM-exit interrupt information
12952 * (valid interrupt) is always set to 1 on
12953 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12954 * need kvm_cpu_has_interrupt(). See the commit
12955 * message for details.
12956 */
12957 if (nested_exit_intr_ack_set(vcpu) &&
12958 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12959 kvm_cpu_has_interrupt(vcpu)) {
12960 int irq = kvm_cpu_get_interrupt(vcpu);
12961 WARN_ON(irq < 0);
12962 vmcs12->vm_exit_intr_info = irq |
12963 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12964 }
12965
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012966 if (exit_reason != -1)
12967 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12968 vmcs12->exit_qualification,
12969 vmcs12->idt_vectoring_info_field,
12970 vmcs12->vm_exit_intr_info,
12971 vmcs12->vm_exit_intr_error_code,
12972 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012973
12974 load_vmcs12_host_state(vcpu, vmcs12);
12975
12976 return;
12977 }
12978
12979 /*
12980 * After an early L2 VM-entry failure, we're now back
12981 * in L1 which thinks it just finished a VMLAUNCH or
12982 * VMRESUME instruction, so we need to set the failure
12983 * flag and the VM-instruction error field of the VMCS
12984 * accordingly.
12985 */
12986 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012987
12988 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12989
Jim Mattson4f350c62017-09-14 16:31:44 -070012990 /*
12991 * The emulated instruction was already skipped in
12992 * nested_vmx_run, but the updated RIP was never
12993 * written back to the vmcs01.
12994 */
12995 skip_emulated_instruction(vcpu);
12996 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012997}
12998
Nadav Har'El7c177932011-05-25 23:12:04 +030012999/*
Jan Kiszka42124922014-01-04 18:47:19 +010013000 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13001 */
13002static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13003{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013004 if (is_guest_mode(vcpu)) {
13005 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013006 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013007 }
Jan Kiszka42124922014-01-04 18:47:19 +010013008 free_nested(to_vmx(vcpu));
13009}
13010
13011/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013012 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13013 * 23.7 "VM-entry failures during or after loading guest state" (this also
13014 * lists the acceptable exit-reason and exit-qualification parameters).
13015 * It should only be called before L2 actually succeeded to run, and when
13016 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13017 */
13018static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13019 struct vmcs12 *vmcs12,
13020 u32 reason, unsigned long qualification)
13021{
13022 load_vmcs12_host_state(vcpu, vmcs12);
13023 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13024 vmcs12->exit_qualification = qualification;
13025 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013026 if (enable_shadow_vmcs)
13027 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013028}
13029
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013030static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13031 struct x86_instruction_info *info,
13032 enum x86_intercept_stage stage)
13033{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013034 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13035 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13036
13037 /*
13038 * RDPID causes #UD if disabled through secondary execution controls.
13039 * Because it is marked as EmulateOnUD, we need to intercept it here.
13040 */
13041 if (info->intercept == x86_intercept_rdtscp &&
13042 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13043 ctxt->exception.vector = UD_VECTOR;
13044 ctxt->exception.error_code_valid = false;
13045 return X86EMUL_PROPAGATE_FAULT;
13046 }
13047
13048 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013049 return X86EMUL_CONTINUE;
13050}
13051
Yunhong Jiang64672c92016-06-13 14:19:59 -070013052#ifdef CONFIG_X86_64
13053/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13054static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13055 u64 divisor, u64 *result)
13056{
13057 u64 low = a << shift, high = a >> (64 - shift);
13058
13059 /* To avoid the overflow on divq */
13060 if (high >= divisor)
13061 return 1;
13062
13063 /* Low hold the result, high hold rem which is discarded */
13064 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13065 "rm" (divisor), "0" (low), "1" (high));
13066 *result = low;
13067
13068 return 0;
13069}
13070
13071static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13072{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013073 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013074 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013075
13076 if (kvm_mwait_in_guest(vcpu->kvm))
13077 return -EOPNOTSUPP;
13078
13079 vmx = to_vmx(vcpu);
13080 tscl = rdtsc();
13081 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13082 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013083 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13084
13085 if (delta_tsc > lapic_timer_advance_cycles)
13086 delta_tsc -= lapic_timer_advance_cycles;
13087 else
13088 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013089
13090 /* Convert to host delta tsc if tsc scaling is enabled */
13091 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13092 u64_shl_div_u64(delta_tsc,
13093 kvm_tsc_scaling_ratio_frac_bits,
13094 vcpu->arch.tsc_scaling_ratio,
13095 &delta_tsc))
13096 return -ERANGE;
13097
13098 /*
13099 * If the delta tsc can't fit in the 32 bit after the multi shift,
13100 * we can't use the preemption timer.
13101 * It's possible that it fits on later vmentries, but checking
13102 * on every vmentry is costly so we just use an hrtimer.
13103 */
13104 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13105 return -ERANGE;
13106
13107 vmx->hv_deadline_tsc = tscl + delta_tsc;
13108 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13109 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013110
13111 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013112}
13113
13114static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13115{
13116 struct vcpu_vmx *vmx = to_vmx(vcpu);
13117 vmx->hv_deadline_tsc = -1;
13118 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13119 PIN_BASED_VMX_PREEMPTION_TIMER);
13120}
13121#endif
13122
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013123static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013124{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013125 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013126 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013127}
13128
Kai Huang843e4332015-01-28 10:54:28 +080013129static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13130 struct kvm_memory_slot *slot)
13131{
13132 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13133 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13134}
13135
13136static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13137 struct kvm_memory_slot *slot)
13138{
13139 kvm_mmu_slot_set_dirty(kvm, slot);
13140}
13141
13142static void vmx_flush_log_dirty(struct kvm *kvm)
13143{
13144 kvm_flush_pml_buffers(kvm);
13145}
13146
Bandan Dasc5f983f2017-05-05 15:25:14 -040013147static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13148{
13149 struct vmcs12 *vmcs12;
13150 struct vcpu_vmx *vmx = to_vmx(vcpu);
13151 gpa_t gpa;
13152 struct page *page = NULL;
13153 u64 *pml_address;
13154
13155 if (is_guest_mode(vcpu)) {
13156 WARN_ON_ONCE(vmx->nested.pml_full);
13157
13158 /*
13159 * Check if PML is enabled for the nested guest.
13160 * Whether eptp bit 6 is set is already checked
13161 * as part of A/D emulation.
13162 */
13163 vmcs12 = get_vmcs12(vcpu);
13164 if (!nested_cpu_has_pml(vmcs12))
13165 return 0;
13166
Dan Carpenter47698862017-05-10 22:43:17 +030013167 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013168 vmx->nested.pml_full = true;
13169 return 1;
13170 }
13171
13172 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13173
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013174 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13175 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013176 return 0;
13177
13178 pml_address = kmap(page);
13179 pml_address[vmcs12->guest_pml_index--] = gpa;
13180 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013181 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013182 }
13183
13184 return 0;
13185}
13186
Kai Huang843e4332015-01-28 10:54:28 +080013187static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13188 struct kvm_memory_slot *memslot,
13189 gfn_t offset, unsigned long mask)
13190{
13191 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13192}
13193
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013194static void __pi_post_block(struct kvm_vcpu *vcpu)
13195{
13196 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13197 struct pi_desc old, new;
13198 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013199
13200 do {
13201 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013202 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13203 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013204
13205 dest = cpu_physical_id(vcpu->cpu);
13206
13207 if (x2apic_enabled())
13208 new.ndst = dest;
13209 else
13210 new.ndst = (dest << 8) & 0xFF00;
13211
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013212 /* set 'NV' to 'notification vector' */
13213 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013214 } while (cmpxchg64(&pi_desc->control, old.control,
13215 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013216
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013217 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13218 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013219 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013220 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013221 vcpu->pre_pcpu = -1;
13222 }
13223}
13224
Feng Wuefc64402015-09-18 22:29:51 +080013225/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013226 * This routine does the following things for vCPU which is going
13227 * to be blocked if VT-d PI is enabled.
13228 * - Store the vCPU to the wakeup list, so when interrupts happen
13229 * we can find the right vCPU to wake up.
13230 * - Change the Posted-interrupt descriptor as below:
13231 * 'NDST' <-- vcpu->pre_pcpu
13232 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13233 * - If 'ON' is set during this process, which means at least one
13234 * interrupt is posted for this vCPU, we cannot block it, in
13235 * this case, return 1, otherwise, return 0.
13236 *
13237 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013238static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013239{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013240 unsigned int dest;
13241 struct pi_desc old, new;
13242 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13243
13244 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013245 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13246 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013247 return 0;
13248
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013249 WARN_ON(irqs_disabled());
13250 local_irq_disable();
13251 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13252 vcpu->pre_pcpu = vcpu->cpu;
13253 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13254 list_add_tail(&vcpu->blocked_vcpu_list,
13255 &per_cpu(blocked_vcpu_on_cpu,
13256 vcpu->pre_pcpu));
13257 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13258 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013259
13260 do {
13261 old.control = new.control = pi_desc->control;
13262
Feng Wubf9f6ac2015-09-18 22:29:55 +080013263 WARN((pi_desc->sn == 1),
13264 "Warning: SN field of posted-interrupts "
13265 "is set before blocking\n");
13266
13267 /*
13268 * Since vCPU can be preempted during this process,
13269 * vcpu->cpu could be different with pre_pcpu, we
13270 * need to set pre_pcpu as the destination of wakeup
13271 * notification event, then we can find the right vCPU
13272 * to wakeup in wakeup handler if interrupts happen
13273 * when the vCPU is in blocked state.
13274 */
13275 dest = cpu_physical_id(vcpu->pre_pcpu);
13276
13277 if (x2apic_enabled())
13278 new.ndst = dest;
13279 else
13280 new.ndst = (dest << 8) & 0xFF00;
13281
13282 /* set 'NV' to 'wakeup vector' */
13283 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013284 } while (cmpxchg64(&pi_desc->control, old.control,
13285 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013286
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013287 /* We should not block the vCPU if an interrupt is posted for it. */
13288 if (pi_test_on(pi_desc) == 1)
13289 __pi_post_block(vcpu);
13290
13291 local_irq_enable();
13292 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013293}
13294
Yunhong Jiangbc225122016-06-13 14:19:58 -070013295static int vmx_pre_block(struct kvm_vcpu *vcpu)
13296{
13297 if (pi_pre_block(vcpu))
13298 return 1;
13299
Yunhong Jiang64672c92016-06-13 14:19:59 -070013300 if (kvm_lapic_hv_timer_in_use(vcpu))
13301 kvm_lapic_switch_to_sw_timer(vcpu);
13302
Yunhong Jiangbc225122016-06-13 14:19:58 -070013303 return 0;
13304}
13305
13306static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013307{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013308 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013309 return;
13310
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013311 WARN_ON(irqs_disabled());
13312 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013313 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013314 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013315}
13316
Yunhong Jiangbc225122016-06-13 14:19:58 -070013317static void vmx_post_block(struct kvm_vcpu *vcpu)
13318{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013319 if (kvm_x86_ops->set_hv_timer)
13320 kvm_lapic_switch_to_hv_timer(vcpu);
13321
Yunhong Jiangbc225122016-06-13 14:19:58 -070013322 pi_post_block(vcpu);
13323}
13324
Feng Wubf9f6ac2015-09-18 22:29:55 +080013325/*
Feng Wuefc64402015-09-18 22:29:51 +080013326 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13327 *
13328 * @kvm: kvm
13329 * @host_irq: host irq of the interrupt
13330 * @guest_irq: gsi of the interrupt
13331 * @set: set or unset PI
13332 * returns 0 on success, < 0 on failure
13333 */
13334static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13335 uint32_t guest_irq, bool set)
13336{
13337 struct kvm_kernel_irq_routing_entry *e;
13338 struct kvm_irq_routing_table *irq_rt;
13339 struct kvm_lapic_irq irq;
13340 struct kvm_vcpu *vcpu;
13341 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013342 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013343
13344 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013345 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13346 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013347 return 0;
13348
13349 idx = srcu_read_lock(&kvm->irq_srcu);
13350 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013351 if (guest_irq >= irq_rt->nr_rt_entries ||
13352 hlist_empty(&irq_rt->map[guest_irq])) {
13353 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13354 guest_irq, irq_rt->nr_rt_entries);
13355 goto out;
13356 }
Feng Wuefc64402015-09-18 22:29:51 +080013357
13358 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13359 if (e->type != KVM_IRQ_ROUTING_MSI)
13360 continue;
13361 /*
13362 * VT-d PI cannot support posting multicast/broadcast
13363 * interrupts to a vCPU, we still use interrupt remapping
13364 * for these kind of interrupts.
13365 *
13366 * For lowest-priority interrupts, we only support
13367 * those with single CPU as the destination, e.g. user
13368 * configures the interrupts via /proc/irq or uses
13369 * irqbalance to make the interrupts single-CPU.
13370 *
13371 * We will support full lowest-priority interrupt later.
13372 */
13373
Radim Krčmář371313132016-07-12 22:09:27 +020013374 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013375 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13376 /*
13377 * Make sure the IRTE is in remapped mode if
13378 * we don't handle it in posted mode.
13379 */
13380 ret = irq_set_vcpu_affinity(host_irq, NULL);
13381 if (ret < 0) {
13382 printk(KERN_INFO
13383 "failed to back to remapped mode, irq: %u\n",
13384 host_irq);
13385 goto out;
13386 }
13387
Feng Wuefc64402015-09-18 22:29:51 +080013388 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013389 }
Feng Wuefc64402015-09-18 22:29:51 +080013390
13391 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13392 vcpu_info.vector = irq.vector;
13393
hu huajun2698d822018-04-11 15:16:40 +080013394 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013395 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13396
13397 if (set)
13398 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013399 else
Feng Wuefc64402015-09-18 22:29:51 +080013400 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013401
13402 if (ret < 0) {
13403 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13404 __func__);
13405 goto out;
13406 }
13407 }
13408
13409 ret = 0;
13410out:
13411 srcu_read_unlock(&kvm->irq_srcu, idx);
13412 return ret;
13413}
13414
Ashok Rajc45dcc72016-06-22 14:59:56 +080013415static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13416{
13417 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13418 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13419 FEATURE_CONTROL_LMCE;
13420 else
13421 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13422 ~FEATURE_CONTROL_LMCE;
13423}
13424
Ladi Prosek72d7b372017-10-11 16:54:41 +020013425static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13426{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013427 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13428 if (to_vmx(vcpu)->nested.nested_run_pending)
13429 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013430 return 1;
13431}
13432
Ladi Prosek0234bf82017-10-11 16:54:40 +020013433static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13434{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013435 struct vcpu_vmx *vmx = to_vmx(vcpu);
13436
13437 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13438 if (vmx->nested.smm.guest_mode)
13439 nested_vmx_vmexit(vcpu, -1, 0, 0);
13440
13441 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13442 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013443 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013444 return 0;
13445}
13446
13447static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13448{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013449 struct vcpu_vmx *vmx = to_vmx(vcpu);
13450 int ret;
13451
13452 if (vmx->nested.smm.vmxon) {
13453 vmx->nested.vmxon = true;
13454 vmx->nested.smm.vmxon = false;
13455 }
13456
13457 if (vmx->nested.smm.guest_mode) {
13458 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013459 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013460 vcpu->arch.hflags |= HF_SMM_MASK;
13461 if (ret)
13462 return ret;
13463
13464 vmx->nested.smm.guest_mode = false;
13465 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013466 return 0;
13467}
13468
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013469static int enable_smi_window(struct kvm_vcpu *vcpu)
13470{
13471 return 0;
13472}
13473
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013474static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13475 struct kvm_nested_state __user *user_kvm_nested_state,
13476 u32 user_data_size)
13477{
13478 struct vcpu_vmx *vmx;
13479 struct vmcs12 *vmcs12;
13480 struct kvm_nested_state kvm_state = {
13481 .flags = 0,
13482 .format = 0,
13483 .size = sizeof(kvm_state),
13484 .vmx.vmxon_pa = -1ull,
13485 .vmx.vmcs_pa = -1ull,
13486 };
13487
13488 if (!vcpu)
13489 return kvm_state.size + 2 * VMCS12_SIZE;
13490
13491 vmx = to_vmx(vcpu);
13492 vmcs12 = get_vmcs12(vcpu);
13493 if (nested_vmx_allowed(vcpu) &&
13494 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13495 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13496 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13497
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013498 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013499 kvm_state.size += VMCS12_SIZE;
13500
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013501 if (is_guest_mode(vcpu) &&
13502 nested_cpu_has_shadow_vmcs(vmcs12) &&
13503 vmcs12->vmcs_link_pointer != -1ull)
13504 kvm_state.size += VMCS12_SIZE;
13505 }
13506
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013507 if (vmx->nested.smm.vmxon)
13508 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13509
13510 if (vmx->nested.smm.guest_mode)
13511 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13512
13513 if (is_guest_mode(vcpu)) {
13514 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13515
13516 if (vmx->nested.nested_run_pending)
13517 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13518 }
13519 }
13520
13521 if (user_data_size < kvm_state.size)
13522 goto out;
13523
13524 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13525 return -EFAULT;
13526
13527 if (vmx->nested.current_vmptr == -1ull)
13528 goto out;
13529
13530 /*
13531 * When running L2, the authoritative vmcs12 state is in the
13532 * vmcs02. When running L1, the authoritative vmcs12 state is
13533 * in the shadow vmcs linked to vmcs01, unless
13534 * sync_shadow_vmcs is set, in which case, the authoritative
13535 * vmcs12 state is in the vmcs12 already.
13536 */
13537 if (is_guest_mode(vcpu))
13538 sync_vmcs12(vcpu, vmcs12);
13539 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13540 copy_shadow_to_vmcs12(vmx);
13541
13542 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13543 return -EFAULT;
13544
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013545 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13546 vmcs12->vmcs_link_pointer != -1ull) {
13547 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13548 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13549 return -EFAULT;
13550 }
13551
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013552out:
13553 return kvm_state.size;
13554}
13555
13556static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13557 struct kvm_nested_state __user *user_kvm_nested_state,
13558 struct kvm_nested_state *kvm_state)
13559{
13560 struct vcpu_vmx *vmx = to_vmx(vcpu);
13561 struct vmcs12 *vmcs12;
13562 u32 exit_qual;
13563 int ret;
13564
13565 if (kvm_state->format != 0)
13566 return -EINVAL;
13567
13568 if (!nested_vmx_allowed(vcpu))
13569 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13570
13571 if (kvm_state->vmx.vmxon_pa == -1ull) {
13572 if (kvm_state->vmx.smm.flags)
13573 return -EINVAL;
13574
13575 if (kvm_state->vmx.vmcs_pa != -1ull)
13576 return -EINVAL;
13577
13578 vmx_leave_nested(vcpu);
13579 return 0;
13580 }
13581
13582 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13583 return -EINVAL;
13584
13585 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13586 return -EINVAL;
13587
13588 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13589 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13590 return -EINVAL;
13591
13592 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13593 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13594 return -EINVAL;
13595
13596 if (kvm_state->vmx.smm.flags &
13597 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13598 return -EINVAL;
13599
13600 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13601 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13602 return -EINVAL;
13603
13604 vmx_leave_nested(vcpu);
13605 if (kvm_state->vmx.vmxon_pa == -1ull)
13606 return 0;
13607
13608 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13609 ret = enter_vmx_operation(vcpu);
13610 if (ret)
13611 return ret;
13612
13613 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13614
13615 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13616 vmx->nested.smm.vmxon = true;
13617 vmx->nested.vmxon = false;
13618
13619 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13620 vmx->nested.smm.guest_mode = true;
13621 }
13622
13623 vmcs12 = get_vmcs12(vcpu);
13624 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13625 return -EFAULT;
13626
Liran Alon392b2f22018-06-23 02:35:01 +030013627 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013628 return -EINVAL;
13629
13630 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13631 return 0;
13632
13633 vmx->nested.nested_run_pending =
13634 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13635
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013636 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13637 vmcs12->vmcs_link_pointer != -1ull) {
13638 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13639 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13640 return -EINVAL;
13641
13642 if (copy_from_user(shadow_vmcs12,
13643 user_kvm_nested_state->data + VMCS12_SIZE,
13644 sizeof(*vmcs12)))
13645 return -EFAULT;
13646
13647 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13648 !shadow_vmcs12->hdr.shadow_vmcs)
13649 return -EINVAL;
13650 }
13651
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013652 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13653 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13654 return -EINVAL;
13655
13656 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13657 vmx->nested.nested_run_pending = 1;
13658
13659 vmx->nested.dirty_vmcs12 = true;
13660 ret = enter_vmx_non_root_mode(vcpu, NULL);
13661 if (ret)
13662 return -EINVAL;
13663
13664 return 0;
13665}
13666
Kees Cook404f6aa2016-08-08 16:29:06 -070013667static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013668 .cpu_has_kvm_support = cpu_has_kvm_support,
13669 .disabled_by_bios = vmx_disabled_by_bios,
13670 .hardware_setup = hardware_setup,
13671 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013672 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013673 .hardware_enable = hardware_enable,
13674 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013675 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013676 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013677
Wanpeng Lib31c1142018-03-12 04:53:04 -070013678 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013679 .vm_alloc = vmx_vm_alloc,
13680 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013681
Avi Kivity6aa8b732006-12-10 02:21:36 -080013682 .vcpu_create = vmx_create_vcpu,
13683 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013684 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013685
Avi Kivity04d2cc72007-09-10 18:10:54 +030013686 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013687 .vcpu_load = vmx_vcpu_load,
13688 .vcpu_put = vmx_vcpu_put,
13689
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013690 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013691 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013692 .get_msr = vmx_get_msr,
13693 .set_msr = vmx_set_msr,
13694 .get_segment_base = vmx_get_segment_base,
13695 .get_segment = vmx_get_segment,
13696 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013697 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013698 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013699 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013700 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013701 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013702 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013703 .set_cr3 = vmx_set_cr3,
13704 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013705 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013706 .get_idt = vmx_get_idt,
13707 .set_idt = vmx_set_idt,
13708 .get_gdt = vmx_get_gdt,
13709 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013710 .get_dr6 = vmx_get_dr6,
13711 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013712 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013713 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013714 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013715 .get_rflags = vmx_get_rflags,
13716 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013717
Avi Kivity6aa8b732006-12-10 02:21:36 -080013718 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013719 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013720
Avi Kivity6aa8b732006-12-10 02:21:36 -080013721 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013722 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013723 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013724 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13725 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013726 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013727 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013728 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013729 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013730 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013731 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013732 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013733 .get_nmi_mask = vmx_get_nmi_mask,
13734 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013735 .enable_nmi_window = enable_nmi_window,
13736 .enable_irq_window = enable_irq_window,
13737 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013738 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013739 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013740 .get_enable_apicv = vmx_get_enable_apicv,
13741 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013742 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013743 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013744 .hwapic_irr_update = vmx_hwapic_irr_update,
13745 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013746 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13747 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013748
Izik Eiduscbc94022007-10-25 00:29:55 +020013749 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013750 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013751 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013752 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013753
Avi Kivity586f9602010-11-18 13:09:54 +020013754 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013755
Sheng Yang17cc3932010-01-05 19:02:27 +080013756 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013757
13758 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013759
13760 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013761 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013762
13763 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013764
13765 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013766
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013767 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013768 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013769
13770 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013771
13772 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013773 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013774 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013775 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013776 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013777
13778 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013779
13780 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013781
13782 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13783 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13784 .flush_log_dirty = vmx_flush_log_dirty,
13785 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013786 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013787
Feng Wubf9f6ac2015-09-18 22:29:55 +080013788 .pre_block = vmx_pre_block,
13789 .post_block = vmx_post_block,
13790
Wei Huang25462f72015-06-19 15:45:05 +020013791 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013792
13793 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013794
13795#ifdef CONFIG_X86_64
13796 .set_hv_timer = vmx_set_hv_timer,
13797 .cancel_hv_timer = vmx_cancel_hv_timer,
13798#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013799
13800 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013801
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013802 .get_nested_state = vmx_get_nested_state,
13803 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013804 .get_vmcs12_pages = nested_get_vmcs12_pages,
13805
Ladi Prosek72d7b372017-10-11 16:54:41 +020013806 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013807 .pre_enter_smm = vmx_pre_enter_smm,
13808 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013809 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013810};
13811
13812static int __init vmx_init(void)
13813{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013814 int r;
13815
13816#if IS_ENABLED(CONFIG_HYPERV)
13817 /*
13818 * Enlightened VMCS usage should be recommended and the host needs
13819 * to support eVMCS v1 or above. We can also disable eVMCS support
13820 * with module parameter.
13821 */
13822 if (enlightened_vmcs &&
13823 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13824 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13825 KVM_EVMCS_VERSION) {
13826 int cpu;
13827
13828 /* Check that we have assist pages on all online CPUs */
13829 for_each_online_cpu(cpu) {
13830 if (!hv_get_vp_assist_page(cpu)) {
13831 enlightened_vmcs = false;
13832 break;
13833 }
13834 }
13835
13836 if (enlightened_vmcs) {
13837 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13838 static_branch_enable(&enable_evmcs);
13839 }
13840 } else {
13841 enlightened_vmcs = false;
13842 }
13843#endif
13844
13845 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013846 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013847 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013848 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013849
Dave Young2965faa2015-09-09 15:38:55 -070013850#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013851 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13852 crash_vmclear_local_loaded_vmcss);
13853#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013854 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013855
He, Qingfdef3ad2007-04-30 09:45:24 +030013856 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013857}
13858
13859static void __exit vmx_exit(void)
13860{
Dave Young2965faa2015-09-09 15:38:55 -070013861#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013862 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013863 synchronize_rcu();
13864#endif
13865
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013866 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013867
13868#if IS_ENABLED(CONFIG_HYPERV)
13869 if (static_branch_unlikely(&enable_evmcs)) {
13870 int cpu;
13871 struct hv_vp_assist_page *vp_ap;
13872 /*
13873 * Reset everything to support using non-enlightened VMCS
13874 * access later (e.g. when we reload the module with
13875 * enlightened_vmcs=0)
13876 */
13877 for_each_online_cpu(cpu) {
13878 vp_ap = hv_get_vp_assist_page(cpu);
13879
13880 if (!vp_ap)
13881 continue;
13882
13883 vp_ap->current_nested_vmcs = 0;
13884 vp_ap->enlighten_vmentry = 0;
13885 }
13886
13887 static_branch_disable(&enable_evmcs);
13888 }
13889#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013890}
13891
13892module_init(vmx_init)
13893module_exit(vmx_exit)