blob: 4937e11f971a79664bee308b925eacd25c0f7829 [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/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700222 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
223 * and whose values change infrequently, but are not constant. I.e. this is
224 * used as a write-through cache of the corresponding VMCS fields.
225 */
226struct vmcs_host_state {
227 unsigned long cr3; /* May not match real cr3 */
228 unsigned long cr4; /* May not match real cr4 */
229
230 u16 fs_sel, gs_sel, ldt_sel;
231#ifdef CONFIG_X86_64
232 u16 ds_sel, es_sel;
233#endif
234};
235
236/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300237 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
238 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
239 * loaded on this CPU (so we can clear them if the CPU goes down).
240 */
241struct loaded_vmcs {
242 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700243 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300244 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200245 bool launched;
246 bool nmi_known_unmasked;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100247 /* Support for vnmi-less CPUs */
248 int soft_vnmi_blocked;
249 ktime_t entry_time;
250 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100251 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300252 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700253 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300254};
255
Avi Kivity26bb0982009-09-07 11:14:12 +0300256struct shared_msr_entry {
257 unsigned index;
258 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200259 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300260};
261
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300262/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300263 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
264 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
265 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
266 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
267 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
268 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600269 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300270 * underlying hardware which will be used to run L2.
271 * This structure is packed to ensure that its layout is identical across
272 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700273 *
274 * IMPORTANT: Changing the layout of existing fields in this structure
275 * will break save/restore compatibility with older kvm releases. When
276 * adding new fields, either use space in the reserved padding* arrays
277 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300278 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300279typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300280struct __packed vmcs12 {
281 /* According to the Intel spec, a VMCS region must start with the
282 * following two fields. Then follow implementation-specific data.
283 */
Liran Alon392b2f22018-06-23 02:35:01 +0300284 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300285 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300286
Nadav Har'El27d6c862011-05-25 23:06:59 +0300287 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
288 u32 padding[7]; /* room for future expansion */
289
Nadav Har'El22bd0352011-05-25 23:05:57 +0300290 u64 io_bitmap_a;
291 u64 io_bitmap_b;
292 u64 msr_bitmap;
293 u64 vm_exit_msr_store_addr;
294 u64 vm_exit_msr_load_addr;
295 u64 vm_entry_msr_load_addr;
296 u64 tsc_offset;
297 u64 virtual_apic_page_addr;
298 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800299 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300300 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800301 u64 eoi_exit_bitmap0;
302 u64 eoi_exit_bitmap1;
303 u64 eoi_exit_bitmap2;
304 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800305 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300306 u64 guest_physical_address;
307 u64 vmcs_link_pointer;
308 u64 guest_ia32_debugctl;
309 u64 guest_ia32_pat;
310 u64 guest_ia32_efer;
311 u64 guest_ia32_perf_global_ctrl;
312 u64 guest_pdptr0;
313 u64 guest_pdptr1;
314 u64 guest_pdptr2;
315 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100316 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300317 u64 host_ia32_pat;
318 u64 host_ia32_efer;
319 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700320 u64 vmread_bitmap;
321 u64 vmwrite_bitmap;
322 u64 vm_function_control;
323 u64 eptp_list_address;
324 u64 pml_address;
325 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300326 /*
327 * To allow migration of L1 (complete with its L2 guests) between
328 * machines of different natural widths (32 or 64 bit), we cannot have
329 * unsigned long fields with no explict size. We use u64 (aliased
330 * natural_width) instead. Luckily, x86 is little-endian.
331 */
332 natural_width cr0_guest_host_mask;
333 natural_width cr4_guest_host_mask;
334 natural_width cr0_read_shadow;
335 natural_width cr4_read_shadow;
336 natural_width cr3_target_value0;
337 natural_width cr3_target_value1;
338 natural_width cr3_target_value2;
339 natural_width cr3_target_value3;
340 natural_width exit_qualification;
341 natural_width guest_linear_address;
342 natural_width guest_cr0;
343 natural_width guest_cr3;
344 natural_width guest_cr4;
345 natural_width guest_es_base;
346 natural_width guest_cs_base;
347 natural_width guest_ss_base;
348 natural_width guest_ds_base;
349 natural_width guest_fs_base;
350 natural_width guest_gs_base;
351 natural_width guest_ldtr_base;
352 natural_width guest_tr_base;
353 natural_width guest_gdtr_base;
354 natural_width guest_idtr_base;
355 natural_width guest_dr7;
356 natural_width guest_rsp;
357 natural_width guest_rip;
358 natural_width guest_rflags;
359 natural_width guest_pending_dbg_exceptions;
360 natural_width guest_sysenter_esp;
361 natural_width guest_sysenter_eip;
362 natural_width host_cr0;
363 natural_width host_cr3;
364 natural_width host_cr4;
365 natural_width host_fs_base;
366 natural_width host_gs_base;
367 natural_width host_tr_base;
368 natural_width host_gdtr_base;
369 natural_width host_idtr_base;
370 natural_width host_ia32_sysenter_esp;
371 natural_width host_ia32_sysenter_eip;
372 natural_width host_rsp;
373 natural_width host_rip;
374 natural_width paddingl[8]; /* room for future expansion */
375 u32 pin_based_vm_exec_control;
376 u32 cpu_based_vm_exec_control;
377 u32 exception_bitmap;
378 u32 page_fault_error_code_mask;
379 u32 page_fault_error_code_match;
380 u32 cr3_target_count;
381 u32 vm_exit_controls;
382 u32 vm_exit_msr_store_count;
383 u32 vm_exit_msr_load_count;
384 u32 vm_entry_controls;
385 u32 vm_entry_msr_load_count;
386 u32 vm_entry_intr_info_field;
387 u32 vm_entry_exception_error_code;
388 u32 vm_entry_instruction_len;
389 u32 tpr_threshold;
390 u32 secondary_vm_exec_control;
391 u32 vm_instruction_error;
392 u32 vm_exit_reason;
393 u32 vm_exit_intr_info;
394 u32 vm_exit_intr_error_code;
395 u32 idt_vectoring_info_field;
396 u32 idt_vectoring_error_code;
397 u32 vm_exit_instruction_len;
398 u32 vmx_instruction_info;
399 u32 guest_es_limit;
400 u32 guest_cs_limit;
401 u32 guest_ss_limit;
402 u32 guest_ds_limit;
403 u32 guest_fs_limit;
404 u32 guest_gs_limit;
405 u32 guest_ldtr_limit;
406 u32 guest_tr_limit;
407 u32 guest_gdtr_limit;
408 u32 guest_idtr_limit;
409 u32 guest_es_ar_bytes;
410 u32 guest_cs_ar_bytes;
411 u32 guest_ss_ar_bytes;
412 u32 guest_ds_ar_bytes;
413 u32 guest_fs_ar_bytes;
414 u32 guest_gs_ar_bytes;
415 u32 guest_ldtr_ar_bytes;
416 u32 guest_tr_ar_bytes;
417 u32 guest_interruptibility_info;
418 u32 guest_activity_state;
419 u32 guest_sysenter_cs;
420 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100421 u32 vmx_preemption_timer_value;
422 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300423 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800424 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300425 u16 guest_es_selector;
426 u16 guest_cs_selector;
427 u16 guest_ss_selector;
428 u16 guest_ds_selector;
429 u16 guest_fs_selector;
430 u16 guest_gs_selector;
431 u16 guest_ldtr_selector;
432 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800433 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300434 u16 host_es_selector;
435 u16 host_cs_selector;
436 u16 host_ss_selector;
437 u16 host_ds_selector;
438 u16 host_fs_selector;
439 u16 host_gs_selector;
440 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700441 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300442};
443
444/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700445 * For save/restore compatibility, the vmcs12 field offsets must not change.
446 */
447#define CHECK_OFFSET(field, loc) \
448 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
449 "Offset of " #field " in struct vmcs12 has changed.")
450
451static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300452 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700453 CHECK_OFFSET(abort, 4);
454 CHECK_OFFSET(launch_state, 8);
455 CHECK_OFFSET(io_bitmap_a, 40);
456 CHECK_OFFSET(io_bitmap_b, 48);
457 CHECK_OFFSET(msr_bitmap, 56);
458 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
459 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
460 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
461 CHECK_OFFSET(tsc_offset, 88);
462 CHECK_OFFSET(virtual_apic_page_addr, 96);
463 CHECK_OFFSET(apic_access_addr, 104);
464 CHECK_OFFSET(posted_intr_desc_addr, 112);
465 CHECK_OFFSET(ept_pointer, 120);
466 CHECK_OFFSET(eoi_exit_bitmap0, 128);
467 CHECK_OFFSET(eoi_exit_bitmap1, 136);
468 CHECK_OFFSET(eoi_exit_bitmap2, 144);
469 CHECK_OFFSET(eoi_exit_bitmap3, 152);
470 CHECK_OFFSET(xss_exit_bitmap, 160);
471 CHECK_OFFSET(guest_physical_address, 168);
472 CHECK_OFFSET(vmcs_link_pointer, 176);
473 CHECK_OFFSET(guest_ia32_debugctl, 184);
474 CHECK_OFFSET(guest_ia32_pat, 192);
475 CHECK_OFFSET(guest_ia32_efer, 200);
476 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
477 CHECK_OFFSET(guest_pdptr0, 216);
478 CHECK_OFFSET(guest_pdptr1, 224);
479 CHECK_OFFSET(guest_pdptr2, 232);
480 CHECK_OFFSET(guest_pdptr3, 240);
481 CHECK_OFFSET(guest_bndcfgs, 248);
482 CHECK_OFFSET(host_ia32_pat, 256);
483 CHECK_OFFSET(host_ia32_efer, 264);
484 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
485 CHECK_OFFSET(vmread_bitmap, 280);
486 CHECK_OFFSET(vmwrite_bitmap, 288);
487 CHECK_OFFSET(vm_function_control, 296);
488 CHECK_OFFSET(eptp_list_address, 304);
489 CHECK_OFFSET(pml_address, 312);
490 CHECK_OFFSET(cr0_guest_host_mask, 344);
491 CHECK_OFFSET(cr4_guest_host_mask, 352);
492 CHECK_OFFSET(cr0_read_shadow, 360);
493 CHECK_OFFSET(cr4_read_shadow, 368);
494 CHECK_OFFSET(cr3_target_value0, 376);
495 CHECK_OFFSET(cr3_target_value1, 384);
496 CHECK_OFFSET(cr3_target_value2, 392);
497 CHECK_OFFSET(cr3_target_value3, 400);
498 CHECK_OFFSET(exit_qualification, 408);
499 CHECK_OFFSET(guest_linear_address, 416);
500 CHECK_OFFSET(guest_cr0, 424);
501 CHECK_OFFSET(guest_cr3, 432);
502 CHECK_OFFSET(guest_cr4, 440);
503 CHECK_OFFSET(guest_es_base, 448);
504 CHECK_OFFSET(guest_cs_base, 456);
505 CHECK_OFFSET(guest_ss_base, 464);
506 CHECK_OFFSET(guest_ds_base, 472);
507 CHECK_OFFSET(guest_fs_base, 480);
508 CHECK_OFFSET(guest_gs_base, 488);
509 CHECK_OFFSET(guest_ldtr_base, 496);
510 CHECK_OFFSET(guest_tr_base, 504);
511 CHECK_OFFSET(guest_gdtr_base, 512);
512 CHECK_OFFSET(guest_idtr_base, 520);
513 CHECK_OFFSET(guest_dr7, 528);
514 CHECK_OFFSET(guest_rsp, 536);
515 CHECK_OFFSET(guest_rip, 544);
516 CHECK_OFFSET(guest_rflags, 552);
517 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
518 CHECK_OFFSET(guest_sysenter_esp, 568);
519 CHECK_OFFSET(guest_sysenter_eip, 576);
520 CHECK_OFFSET(host_cr0, 584);
521 CHECK_OFFSET(host_cr3, 592);
522 CHECK_OFFSET(host_cr4, 600);
523 CHECK_OFFSET(host_fs_base, 608);
524 CHECK_OFFSET(host_gs_base, 616);
525 CHECK_OFFSET(host_tr_base, 624);
526 CHECK_OFFSET(host_gdtr_base, 632);
527 CHECK_OFFSET(host_idtr_base, 640);
528 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
529 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
530 CHECK_OFFSET(host_rsp, 664);
531 CHECK_OFFSET(host_rip, 672);
532 CHECK_OFFSET(pin_based_vm_exec_control, 744);
533 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
534 CHECK_OFFSET(exception_bitmap, 752);
535 CHECK_OFFSET(page_fault_error_code_mask, 756);
536 CHECK_OFFSET(page_fault_error_code_match, 760);
537 CHECK_OFFSET(cr3_target_count, 764);
538 CHECK_OFFSET(vm_exit_controls, 768);
539 CHECK_OFFSET(vm_exit_msr_store_count, 772);
540 CHECK_OFFSET(vm_exit_msr_load_count, 776);
541 CHECK_OFFSET(vm_entry_controls, 780);
542 CHECK_OFFSET(vm_entry_msr_load_count, 784);
543 CHECK_OFFSET(vm_entry_intr_info_field, 788);
544 CHECK_OFFSET(vm_entry_exception_error_code, 792);
545 CHECK_OFFSET(vm_entry_instruction_len, 796);
546 CHECK_OFFSET(tpr_threshold, 800);
547 CHECK_OFFSET(secondary_vm_exec_control, 804);
548 CHECK_OFFSET(vm_instruction_error, 808);
549 CHECK_OFFSET(vm_exit_reason, 812);
550 CHECK_OFFSET(vm_exit_intr_info, 816);
551 CHECK_OFFSET(vm_exit_intr_error_code, 820);
552 CHECK_OFFSET(idt_vectoring_info_field, 824);
553 CHECK_OFFSET(idt_vectoring_error_code, 828);
554 CHECK_OFFSET(vm_exit_instruction_len, 832);
555 CHECK_OFFSET(vmx_instruction_info, 836);
556 CHECK_OFFSET(guest_es_limit, 840);
557 CHECK_OFFSET(guest_cs_limit, 844);
558 CHECK_OFFSET(guest_ss_limit, 848);
559 CHECK_OFFSET(guest_ds_limit, 852);
560 CHECK_OFFSET(guest_fs_limit, 856);
561 CHECK_OFFSET(guest_gs_limit, 860);
562 CHECK_OFFSET(guest_ldtr_limit, 864);
563 CHECK_OFFSET(guest_tr_limit, 868);
564 CHECK_OFFSET(guest_gdtr_limit, 872);
565 CHECK_OFFSET(guest_idtr_limit, 876);
566 CHECK_OFFSET(guest_es_ar_bytes, 880);
567 CHECK_OFFSET(guest_cs_ar_bytes, 884);
568 CHECK_OFFSET(guest_ss_ar_bytes, 888);
569 CHECK_OFFSET(guest_ds_ar_bytes, 892);
570 CHECK_OFFSET(guest_fs_ar_bytes, 896);
571 CHECK_OFFSET(guest_gs_ar_bytes, 900);
572 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
573 CHECK_OFFSET(guest_tr_ar_bytes, 908);
574 CHECK_OFFSET(guest_interruptibility_info, 912);
575 CHECK_OFFSET(guest_activity_state, 916);
576 CHECK_OFFSET(guest_sysenter_cs, 920);
577 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
578 CHECK_OFFSET(vmx_preemption_timer_value, 928);
579 CHECK_OFFSET(virtual_processor_id, 960);
580 CHECK_OFFSET(posted_intr_nv, 962);
581 CHECK_OFFSET(guest_es_selector, 964);
582 CHECK_OFFSET(guest_cs_selector, 966);
583 CHECK_OFFSET(guest_ss_selector, 968);
584 CHECK_OFFSET(guest_ds_selector, 970);
585 CHECK_OFFSET(guest_fs_selector, 972);
586 CHECK_OFFSET(guest_gs_selector, 974);
587 CHECK_OFFSET(guest_ldtr_selector, 976);
588 CHECK_OFFSET(guest_tr_selector, 978);
589 CHECK_OFFSET(guest_intr_status, 980);
590 CHECK_OFFSET(host_es_selector, 982);
591 CHECK_OFFSET(host_cs_selector, 984);
592 CHECK_OFFSET(host_ss_selector, 986);
593 CHECK_OFFSET(host_ds_selector, 988);
594 CHECK_OFFSET(host_fs_selector, 990);
595 CHECK_OFFSET(host_gs_selector, 992);
596 CHECK_OFFSET(host_tr_selector, 994);
597 CHECK_OFFSET(guest_pml_index, 996);
598}
599
600/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300601 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
602 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
603 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700604 *
605 * IMPORTANT: Changing this value will break save/restore compatibility with
606 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300607 */
608#define VMCS12_REVISION 0x11e57ed0
609
610/*
611 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
612 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
613 * current implementation, 4K are reserved to avoid future complications.
614 */
615#define VMCS12_SIZE 0x1000
616
617/*
Jim Mattson5b157062017-12-22 12:11:12 -0800618 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
619 * supported VMCS12 field encoding.
620 */
621#define VMCS12_MAX_FIELD_INDEX 0x17
622
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100623struct nested_vmx_msrs {
624 /*
625 * We only store the "true" versions of the VMX capability MSRs. We
626 * generate the "non-true" versions by setting the must-be-1 bits
627 * according to the SDM.
628 */
629 u32 procbased_ctls_low;
630 u32 procbased_ctls_high;
631 u32 secondary_ctls_low;
632 u32 secondary_ctls_high;
633 u32 pinbased_ctls_low;
634 u32 pinbased_ctls_high;
635 u32 exit_ctls_low;
636 u32 exit_ctls_high;
637 u32 entry_ctls_low;
638 u32 entry_ctls_high;
639 u32 misc_low;
640 u32 misc_high;
641 u32 ept_caps;
642 u32 vpid_caps;
643 u64 basic;
644 u64 cr0_fixed0;
645 u64 cr0_fixed1;
646 u64 cr4_fixed0;
647 u64 cr4_fixed1;
648 u64 vmcs_enum;
649 u64 vmfunc_controls;
650};
651
Jim Mattson5b157062017-12-22 12:11:12 -0800652/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300653 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
654 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
655 */
656struct nested_vmx {
657 /* Has the level1 guest done vmxon? */
658 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400659 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400660 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300661
662 /* The guest-physical address of the current VMCS L1 keeps for L2 */
663 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700664 /*
665 * Cache of the guest's VMCS, existing outside of guest memory.
666 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700667 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700668 */
669 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300670 /*
Liran Alon61ada742018-06-23 02:35:08 +0300671 * Cache of the guest's shadow VMCS, existing outside of guest
672 * memory. Loaded from guest memory during VM entry. Flushed
673 * to guest memory during VM exit.
674 */
675 struct vmcs12 *cached_shadow_vmcs12;
676 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300677 * Indicates if the shadow vmcs must be updated with the
678 * data hold by vmcs12
679 */
680 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100681 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300682
Jim Mattson8d860bb2018-05-09 16:56:05 -0400683 bool change_vmcs01_virtual_apic_mode;
684
Nadav Har'El644d7112011-05-25 23:12:35 +0300685 /* L2 must run next, and mustn't decide to exit to L1. */
686 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600687
688 struct loaded_vmcs vmcs02;
689
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300690 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600691 * Guest pages referred to in the vmcs02 with host-physical
692 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300693 */
694 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800695 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800696 struct page *pi_desc_page;
697 struct pi_desc *pi_desc;
698 bool pi_pending;
699 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100700
701 struct hrtimer preemption_timer;
702 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200703
704 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
705 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800706
Wanpeng Li5c614b32015-10-13 09:18:36 -0700707 u16 vpid02;
708 u16 last_vpid;
709
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100710 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200711
712 /* SMM related state */
713 struct {
714 /* in VMX operation on SMM entry? */
715 bool vmxon;
716 /* in guest mode on SMM entry? */
717 bool guest_mode;
718 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300719};
720
Yang Zhang01e439b2013-04-11 19:25:12 +0800721#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800722#define POSTED_INTR_SN 1
723
Yang Zhang01e439b2013-04-11 19:25:12 +0800724/* Posted-Interrupt Descriptor */
725struct pi_desc {
726 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800727 union {
728 struct {
729 /* bit 256 - Outstanding Notification */
730 u16 on : 1,
731 /* bit 257 - Suppress Notification */
732 sn : 1,
733 /* bit 271:258 - Reserved */
734 rsvd_1 : 14;
735 /* bit 279:272 - Notification Vector */
736 u8 nv;
737 /* bit 287:280 - Reserved */
738 u8 rsvd_2;
739 /* bit 319:288 - Notification Destination */
740 u32 ndst;
741 };
742 u64 control;
743 };
744 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800745} __aligned(64);
746
Yang Zhanga20ed542013-04-11 19:25:15 +0800747static bool pi_test_and_set_on(struct pi_desc *pi_desc)
748{
749 return test_and_set_bit(POSTED_INTR_ON,
750 (unsigned long *)&pi_desc->control);
751}
752
753static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
754{
755 return test_and_clear_bit(POSTED_INTR_ON,
756 (unsigned long *)&pi_desc->control);
757}
758
759static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
760{
761 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
762}
763
Feng Wuebbfc762015-09-18 22:29:46 +0800764static inline void pi_clear_sn(struct pi_desc *pi_desc)
765{
766 return clear_bit(POSTED_INTR_SN,
767 (unsigned long *)&pi_desc->control);
768}
769
770static inline void pi_set_sn(struct pi_desc *pi_desc)
771{
772 return set_bit(POSTED_INTR_SN,
773 (unsigned long *)&pi_desc->control);
774}
775
Paolo Bonziniad361092016-09-20 16:15:05 +0200776static inline void pi_clear_on(struct pi_desc *pi_desc)
777{
778 clear_bit(POSTED_INTR_ON,
779 (unsigned long *)&pi_desc->control);
780}
781
Feng Wuebbfc762015-09-18 22:29:46 +0800782static inline int pi_test_on(struct pi_desc *pi_desc)
783{
784 return test_bit(POSTED_INTR_ON,
785 (unsigned long *)&pi_desc->control);
786}
787
788static inline int pi_test_sn(struct pi_desc *pi_desc)
789{
790 return test_bit(POSTED_INTR_SN,
791 (unsigned long *)&pi_desc->control);
792}
793
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400794struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300796 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300797 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100798 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300799 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200800 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200801 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300802 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400803 int nmsrs;
804 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800805 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400806#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300807 u64 msr_host_kernel_gs_base;
808 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400809#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100810
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100811 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100812 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100813
Gleb Natapov2961e8762013-11-25 15:37:13 +0200814 u32 vm_entry_controls_shadow;
815 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200816 u32 secondary_exec_control;
817
Nadav Har'Eld462b812011-05-24 15:26:10 +0300818 /*
819 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
820 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700821 * guest (L2), it points to a different VMCS. loaded_cpu_state points
822 * to the VMCS whose state is loaded into the CPU registers that only
823 * need to be switched when transitioning to/from the kernel; a NULL
824 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300825 */
826 struct loaded_vmcs vmcs01;
827 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700828 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300829 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300830 struct msr_autoload {
831 unsigned nr;
832 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
833 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
834 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700835
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400836 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300837 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300838 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300839 struct kvm_segment segs[8];
840 } rmode;
841 struct {
842 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300843 struct kvm_save_segment {
844 u16 selector;
845 unsigned long base;
846 u32 limit;
847 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300848 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300849 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800850 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300851 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200852
Andi Kleena0861c02009-06-08 17:37:09 +0800853 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800854
Yang Zhang01e439b2013-04-11 19:25:12 +0800855 /* Posted interrupt descriptor */
856 struct pi_desc pi_desc;
857
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300858 /* Support for a guest hypervisor (nested VMX) */
859 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200860
861 /* Dynamic PLE window. */
862 int ple_window;
863 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800864
865 /* Support for PML */
866#define PML_ENTITY_NUM 512
867 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800868
Yunhong Jiang64672c92016-06-13 14:19:59 -0700869 /* apic deadline value in host tsc */
870 u64 hv_deadline_tsc;
871
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800872 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800873
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800874 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800875
Wanpeng Li74c55932017-11-29 01:31:20 -0800876 unsigned long host_debugctlmsr;
877
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800878 /*
879 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
880 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
881 * in msr_ia32_feature_control_valid_bits.
882 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800883 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800884 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +0000885 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400886};
887
Avi Kivity2fb92db2011-04-27 19:42:18 +0300888enum segment_cache_field {
889 SEG_FIELD_SEL = 0,
890 SEG_FIELD_BASE = 1,
891 SEG_FIELD_LIMIT = 2,
892 SEG_FIELD_AR = 3,
893
894 SEG_FIELD_NR = 4
895};
896
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700897static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
898{
899 return container_of(kvm, struct kvm_vmx, kvm);
900}
901
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400902static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
903{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000904 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400905}
906
Feng Wuefc64402015-09-18 22:29:51 +0800907static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
908{
909 return &(to_vmx(vcpu)->pi_desc);
910}
911
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800912#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300913#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800914#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
915#define FIELD64(number, name) \
916 FIELD(number, name), \
917 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300918
Abel Gordon4607c2d2013-04-18 14:35:55 +0300919
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100920static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100921#define SHADOW_FIELD_RO(x) x,
922#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300923};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400924static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300925 ARRAY_SIZE(shadow_read_only_fields);
926
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100927static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100928#define SHADOW_FIELD_RW(x) x,
929#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300930};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400931static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300932 ARRAY_SIZE(shadow_read_write_fields);
933
Mathias Krause772e0312012-08-30 01:30:19 +0200934static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300935 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800936 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300937 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
938 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
939 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
940 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
941 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
942 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
943 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
944 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800945 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400946 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300947 FIELD(HOST_ES_SELECTOR, host_es_selector),
948 FIELD(HOST_CS_SELECTOR, host_cs_selector),
949 FIELD(HOST_SS_SELECTOR, host_ss_selector),
950 FIELD(HOST_DS_SELECTOR, host_ds_selector),
951 FIELD(HOST_FS_SELECTOR, host_fs_selector),
952 FIELD(HOST_GS_SELECTOR, host_gs_selector),
953 FIELD(HOST_TR_SELECTOR, host_tr_selector),
954 FIELD64(IO_BITMAP_A, io_bitmap_a),
955 FIELD64(IO_BITMAP_B, io_bitmap_b),
956 FIELD64(MSR_BITMAP, msr_bitmap),
957 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
958 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
959 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700960 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300961 FIELD64(TSC_OFFSET, tsc_offset),
962 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
963 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800964 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400965 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300966 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800967 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
968 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
969 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
970 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400971 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700972 FIELD64(VMREAD_BITMAP, vmread_bitmap),
973 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800974 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300975 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
976 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
977 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
978 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
979 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
980 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
981 FIELD64(GUEST_PDPTR0, guest_pdptr0),
982 FIELD64(GUEST_PDPTR1, guest_pdptr1),
983 FIELD64(GUEST_PDPTR2, guest_pdptr2),
984 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100985 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300986 FIELD64(HOST_IA32_PAT, host_ia32_pat),
987 FIELD64(HOST_IA32_EFER, host_ia32_efer),
988 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
989 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
990 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
991 FIELD(EXCEPTION_BITMAP, exception_bitmap),
992 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
993 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
994 FIELD(CR3_TARGET_COUNT, cr3_target_count),
995 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
996 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
997 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
998 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
999 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1000 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1001 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1002 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1003 FIELD(TPR_THRESHOLD, tpr_threshold),
1004 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1005 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1006 FIELD(VM_EXIT_REASON, vm_exit_reason),
1007 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1008 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1009 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1010 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1011 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1012 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1013 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1014 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1015 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1016 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1017 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1018 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1019 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1020 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1021 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1022 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1023 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1024 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1025 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1026 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1027 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1028 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1029 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1030 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1031 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1032 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1033 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1034 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001035 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001036 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1037 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1038 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1039 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1040 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1041 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1042 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1043 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1044 FIELD(EXIT_QUALIFICATION, exit_qualification),
1045 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1046 FIELD(GUEST_CR0, guest_cr0),
1047 FIELD(GUEST_CR3, guest_cr3),
1048 FIELD(GUEST_CR4, guest_cr4),
1049 FIELD(GUEST_ES_BASE, guest_es_base),
1050 FIELD(GUEST_CS_BASE, guest_cs_base),
1051 FIELD(GUEST_SS_BASE, guest_ss_base),
1052 FIELD(GUEST_DS_BASE, guest_ds_base),
1053 FIELD(GUEST_FS_BASE, guest_fs_base),
1054 FIELD(GUEST_GS_BASE, guest_gs_base),
1055 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1056 FIELD(GUEST_TR_BASE, guest_tr_base),
1057 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1058 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1059 FIELD(GUEST_DR7, guest_dr7),
1060 FIELD(GUEST_RSP, guest_rsp),
1061 FIELD(GUEST_RIP, guest_rip),
1062 FIELD(GUEST_RFLAGS, guest_rflags),
1063 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1064 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1065 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1066 FIELD(HOST_CR0, host_cr0),
1067 FIELD(HOST_CR3, host_cr3),
1068 FIELD(HOST_CR4, host_cr4),
1069 FIELD(HOST_FS_BASE, host_fs_base),
1070 FIELD(HOST_GS_BASE, host_gs_base),
1071 FIELD(HOST_TR_BASE, host_tr_base),
1072 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1073 FIELD(HOST_IDTR_BASE, host_idtr_base),
1074 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1075 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1076 FIELD(HOST_RSP, host_rsp),
1077 FIELD(HOST_RIP, host_rip),
1078};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001079
1080static inline short vmcs_field_to_offset(unsigned long field)
1081{
Dan Williams085331d2018-01-31 17:47:03 -08001082 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1083 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001084 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001085
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001086 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001087 return -ENOENT;
1088
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001089 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001090 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001091 return -ENOENT;
1092
Linus Torvalds15303ba2018-02-10 13:16:35 -08001093 index = array_index_nospec(index, size);
1094 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001095 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001096 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001097 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001098}
1099
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001100static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1101{
David Matlack4f2777b2016-07-13 17:16:37 -07001102 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001103}
1104
Liran Alon61ada742018-06-23 02:35:08 +03001105static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1106{
1107 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1108}
1109
Peter Feiner995f00a2017-06-30 17:26:32 -07001110static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001111static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001112static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001113static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001114static void vmx_set_segment(struct kvm_vcpu *vcpu,
1115 struct kvm_segment *var, int seg);
1116static void vmx_get_segment(struct kvm_vcpu *vcpu,
1117 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001118static bool guest_state_valid(struct kvm_vcpu *vcpu);
1119static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001120static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001121static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1122static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1123static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1124 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001125static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001126static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1127 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001128
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1130static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001131/*
1132 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1133 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1134 */
1135static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
Feng Wubf9f6ac2015-09-18 22:29:55 +08001137/*
1138 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1139 * can find which vCPU should be waken up.
1140 */
1141static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1142static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1143
Radim Krčmář23611332016-09-29 22:41:33 +02001144enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001145 VMX_VMREAD_BITMAP,
1146 VMX_VMWRITE_BITMAP,
1147 VMX_BITMAP_NR
1148};
1149
1150static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1151
Radim Krčmář23611332016-09-29 22:41:33 +02001152#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1153#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001154
Avi Kivity110312c2010-12-21 12:54:20 +02001155static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001156static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001157
Sheng Yang2384d2b2008-01-17 15:14:33 +08001158static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1159static DEFINE_SPINLOCK(vmx_vpid_lock);
1160
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001161static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162 int size;
1163 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001164 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001166 u32 pin_based_exec_ctrl;
1167 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001168 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001169 u32 vmexit_ctrl;
1170 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001171 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001172} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Hannes Ederefff9e52008-11-28 17:02:06 +01001174static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001175 u32 ept;
1176 u32 vpid;
1177} vmx_capability;
1178
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179#define VMX_SEGMENT_FIELD(seg) \
1180 [VCPU_SREG_##seg] = { \
1181 .selector = GUEST_##seg##_SELECTOR, \
1182 .base = GUEST_##seg##_BASE, \
1183 .limit = GUEST_##seg##_LIMIT, \
1184 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1185 }
1186
Mathias Krause772e0312012-08-30 01:30:19 +02001187static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 unsigned selector;
1189 unsigned base;
1190 unsigned limit;
1191 unsigned ar_bytes;
1192} kvm_vmx_segment_fields[] = {
1193 VMX_SEGMENT_FIELD(CS),
1194 VMX_SEGMENT_FIELD(DS),
1195 VMX_SEGMENT_FIELD(ES),
1196 VMX_SEGMENT_FIELD(FS),
1197 VMX_SEGMENT_FIELD(GS),
1198 VMX_SEGMENT_FIELD(SS),
1199 VMX_SEGMENT_FIELD(TR),
1200 VMX_SEGMENT_FIELD(LDTR),
1201};
1202
Avi Kivity26bb0982009-09-07 11:14:12 +03001203static u64 host_efer;
1204
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001205static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1206
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001207/*
Brian Gerst8c065852010-07-17 09:03:26 -04001208 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001209 * away by decrementing the array size.
1210 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001212#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001213 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001215 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001218DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1219
1220#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1221
1222#define KVM_EVMCS_VERSION 1
1223
1224#if IS_ENABLED(CONFIG_HYPERV)
1225static bool __read_mostly enlightened_vmcs = true;
1226module_param(enlightened_vmcs, bool, 0444);
1227
1228static inline void evmcs_write64(unsigned long field, u64 value)
1229{
1230 u16 clean_field;
1231 int offset = get_evmcs_offset(field, &clean_field);
1232
1233 if (offset < 0)
1234 return;
1235
1236 *(u64 *)((char *)current_evmcs + offset) = value;
1237
1238 current_evmcs->hv_clean_fields &= ~clean_field;
1239}
1240
1241static inline void evmcs_write32(unsigned long field, u32 value)
1242{
1243 u16 clean_field;
1244 int offset = get_evmcs_offset(field, &clean_field);
1245
1246 if (offset < 0)
1247 return;
1248
1249 *(u32 *)((char *)current_evmcs + offset) = value;
1250 current_evmcs->hv_clean_fields &= ~clean_field;
1251}
1252
1253static inline void evmcs_write16(unsigned long field, u16 value)
1254{
1255 u16 clean_field;
1256 int offset = get_evmcs_offset(field, &clean_field);
1257
1258 if (offset < 0)
1259 return;
1260
1261 *(u16 *)((char *)current_evmcs + offset) = value;
1262 current_evmcs->hv_clean_fields &= ~clean_field;
1263}
1264
1265static inline u64 evmcs_read64(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u64 *)((char *)current_evmcs + offset);
1273}
1274
1275static inline u32 evmcs_read32(unsigned long field)
1276{
1277 int offset = get_evmcs_offset(field, NULL);
1278
1279 if (offset < 0)
1280 return 0;
1281
1282 return *(u32 *)((char *)current_evmcs + offset);
1283}
1284
1285static inline u16 evmcs_read16(unsigned long field)
1286{
1287 int offset = get_evmcs_offset(field, NULL);
1288
1289 if (offset < 0)
1290 return 0;
1291
1292 return *(u16 *)((char *)current_evmcs + offset);
1293}
1294
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001295static inline void evmcs_touch_msr_bitmap(void)
1296{
1297 if (unlikely(!current_evmcs))
1298 return;
1299
1300 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1301 current_evmcs->hv_clean_fields &=
1302 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1303}
1304
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001305static void evmcs_load(u64 phys_addr)
1306{
1307 struct hv_vp_assist_page *vp_ap =
1308 hv_get_vp_assist_page(smp_processor_id());
1309
1310 vp_ap->current_nested_vmcs = phys_addr;
1311 vp_ap->enlighten_vmentry = 1;
1312}
1313
1314static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1315{
1316 /*
1317 * Enlightened VMCSv1 doesn't support these:
1318 *
1319 * POSTED_INTR_NV = 0x00000002,
1320 * GUEST_INTR_STATUS = 0x00000810,
1321 * APIC_ACCESS_ADDR = 0x00002014,
1322 * POSTED_INTR_DESC_ADDR = 0x00002016,
1323 * EOI_EXIT_BITMAP0 = 0x0000201c,
1324 * EOI_EXIT_BITMAP1 = 0x0000201e,
1325 * EOI_EXIT_BITMAP2 = 0x00002020,
1326 * EOI_EXIT_BITMAP3 = 0x00002022,
1327 */
1328 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1329 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1330 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1331 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1332 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1333 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1334 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1335
1336 /*
1337 * GUEST_PML_INDEX = 0x00000812,
1338 * PML_ADDRESS = 0x0000200e,
1339 */
1340 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1341
1342 /* VM_FUNCTION_CONTROL = 0x00002018, */
1343 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1344
1345 /*
1346 * EPTP_LIST_ADDRESS = 0x00002024,
1347 * VMREAD_BITMAP = 0x00002026,
1348 * VMWRITE_BITMAP = 0x00002028,
1349 */
1350 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1351
1352 /*
1353 * TSC_MULTIPLIER = 0x00002032,
1354 */
1355 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1356
1357 /*
1358 * PLE_GAP = 0x00004020,
1359 * PLE_WINDOW = 0x00004022,
1360 */
1361 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1362
1363 /*
1364 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1365 */
1366 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1367
1368 /*
1369 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1370 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1371 */
1372 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1373 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1374
1375 /*
1376 * Currently unsupported in KVM:
1377 * GUEST_IA32_RTIT_CTL = 0x00002814,
1378 */
1379}
Tianyu Lan877ad952018-07-19 08:40:23 +00001380
1381/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1382static void check_ept_pointer_match(struct kvm *kvm)
1383{
1384 struct kvm_vcpu *vcpu;
1385 u64 tmp_eptp = INVALID_PAGE;
1386 int i;
1387
1388 kvm_for_each_vcpu(i, vcpu, kvm) {
1389 if (!VALID_PAGE(tmp_eptp)) {
1390 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1391 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1392 to_kvm_vmx(kvm)->ept_pointers_match
1393 = EPT_POINTERS_MISMATCH;
1394 return;
1395 }
1396 }
1397
1398 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1399}
1400
1401static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1402{
1403 int ret;
1404
1405 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1406
1407 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1408 check_ept_pointer_match(kvm);
1409
1410 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1411 ret = -ENOTSUPP;
1412 goto out;
1413 }
1414
1415 ret = hyperv_flush_guest_mapping(
1416 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1417
1418out:
1419 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1420 return ret;
1421}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001422#else /* !IS_ENABLED(CONFIG_HYPERV) */
1423static inline void evmcs_write64(unsigned long field, u64 value) {}
1424static inline void evmcs_write32(unsigned long field, u32 value) {}
1425static inline void evmcs_write16(unsigned long field, u16 value) {}
1426static inline u64 evmcs_read64(unsigned long field) { return 0; }
1427static inline u32 evmcs_read32(unsigned long field) { return 0; }
1428static inline u16 evmcs_read16(unsigned long field) { return 0; }
1429static inline void evmcs_load(u64 phys_addr) {}
1430static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001431static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001432#endif /* IS_ENABLED(CONFIG_HYPERV) */
1433
Jan Kiszka5bb16012016-02-09 20:14:21 +01001434static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435{
1436 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1437 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001438 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1439}
1440
Jan Kiszka6f054852016-02-09 20:15:18 +01001441static inline bool is_debug(u32 intr_info)
1442{
1443 return is_exception_n(intr_info, DB_VECTOR);
1444}
1445
1446static inline bool is_breakpoint(u32 intr_info)
1447{
1448 return is_exception_n(intr_info, BP_VECTOR);
1449}
1450
Jan Kiszka5bb16012016-02-09 20:14:21 +01001451static inline bool is_page_fault(u32 intr_info)
1452{
1453 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454}
1455
Gui Jianfeng31299942010-03-15 17:29:09 +08001456static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001457{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001458 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001459}
1460
Gui Jianfeng31299942010-03-15 17:29:09 +08001461static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001462{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001463 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001464}
1465
Liran Alon9e869482018-03-12 13:12:51 +02001466static inline bool is_gp_fault(u32 intr_info)
1467{
1468 return is_exception_n(intr_info, GP_VECTOR);
1469}
1470
Gui Jianfeng31299942010-03-15 17:29:09 +08001471static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472{
1473 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1474 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1475}
1476
Gui Jianfeng31299942010-03-15 17:29:09 +08001477static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001478{
1479 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1480 INTR_INFO_VALID_MASK)) ==
1481 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1482}
1483
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001484/* Undocumented: icebp/int1 */
1485static inline bool is_icebp(u32 intr_info)
1486{
1487 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1488 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1489}
1490
Gui Jianfeng31299942010-03-15 17:29:09 +08001491static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001492{
Sheng Yang04547152009-04-01 15:52:31 +08001493 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001494}
1495
Gui Jianfeng31299942010-03-15 17:29:09 +08001496static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001497{
Sheng Yang04547152009-04-01 15:52:31 +08001498 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001499}
1500
Paolo Bonzini35754c92015-07-29 12:05:37 +02001501static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001502{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001503 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001504}
1505
Gui Jianfeng31299942010-03-15 17:29:09 +08001506static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001507{
Sheng Yang04547152009-04-01 15:52:31 +08001508 return vmcs_config.cpu_based_exec_ctrl &
1509 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001510}
1511
Avi Kivity774ead32007-12-26 13:57:04 +02001512static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001513{
Sheng Yang04547152009-04-01 15:52:31 +08001514 return vmcs_config.cpu_based_2nd_exec_ctrl &
1515 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1516}
1517
Yang Zhang8d146952013-01-25 10:18:50 +08001518static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1519{
1520 return vmcs_config.cpu_based_2nd_exec_ctrl &
1521 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1522}
1523
Yang Zhang83d4c282013-01-25 10:18:49 +08001524static inline bool cpu_has_vmx_apic_register_virt(void)
1525{
1526 return vmcs_config.cpu_based_2nd_exec_ctrl &
1527 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1528}
1529
Yang Zhangc7c9c562013-01-25 10:18:51 +08001530static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1531{
1532 return vmcs_config.cpu_based_2nd_exec_ctrl &
1533 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1534}
1535
Yunhong Jiang64672c92016-06-13 14:19:59 -07001536/*
1537 * Comment's format: document - errata name - stepping - processor name.
1538 * Refer from
1539 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1540 */
1541static u32 vmx_preemption_cpu_tfms[] = {
1542/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15430x000206E6,
1544/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1545/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1546/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15470x00020652,
1548/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15490x00020655,
1550/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1551/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1552/*
1553 * 320767.pdf - AAP86 - B1 -
1554 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1555 */
15560x000106E5,
1557/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15580x000106A0,
1559/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15600x000106A1,
1561/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15620x000106A4,
1563 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1564 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1565 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15660x000106A5,
1567};
1568
1569static inline bool cpu_has_broken_vmx_preemption_timer(void)
1570{
1571 u32 eax = cpuid_eax(0x00000001), i;
1572
1573 /* Clear the reserved bits */
1574 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001575 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001576 if (eax == vmx_preemption_cpu_tfms[i])
1577 return true;
1578
1579 return false;
1580}
1581
1582static inline bool cpu_has_vmx_preemption_timer(void)
1583{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001584 return vmcs_config.pin_based_exec_ctrl &
1585 PIN_BASED_VMX_PREEMPTION_TIMER;
1586}
1587
Yang Zhang01e439b2013-04-11 19:25:12 +08001588static inline bool cpu_has_vmx_posted_intr(void)
1589{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001590 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1591 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001592}
1593
1594static inline bool cpu_has_vmx_apicv(void)
1595{
1596 return cpu_has_vmx_apic_register_virt() &&
1597 cpu_has_vmx_virtual_intr_delivery() &&
1598 cpu_has_vmx_posted_intr();
1599}
1600
Sheng Yang04547152009-04-01 15:52:31 +08001601static inline bool cpu_has_vmx_flexpriority(void)
1602{
1603 return cpu_has_vmx_tpr_shadow() &&
1604 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001605}
1606
Marcelo Tosattie7997942009-06-11 12:07:40 -03001607static inline bool cpu_has_vmx_ept_execute_only(void)
1608{
Gui Jianfeng31299942010-03-15 17:29:09 +08001609 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001610}
1611
Marcelo Tosattie7997942009-06-11 12:07:40 -03001612static inline bool cpu_has_vmx_ept_2m_page(void)
1613{
Gui Jianfeng31299942010-03-15 17:29:09 +08001614 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001615}
1616
Sheng Yang878403b2010-01-05 19:02:29 +08001617static inline bool cpu_has_vmx_ept_1g_page(void)
1618{
Gui Jianfeng31299942010-03-15 17:29:09 +08001619 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001620}
1621
Sheng Yang4bc9b982010-06-02 14:05:24 +08001622static inline bool cpu_has_vmx_ept_4levels(void)
1623{
1624 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1625}
1626
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001627static inline bool cpu_has_vmx_ept_mt_wb(void)
1628{
1629 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1630}
1631
Yu Zhang855feb62017-08-24 20:27:55 +08001632static inline bool cpu_has_vmx_ept_5levels(void)
1633{
1634 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1635}
1636
Xudong Hao83c3a332012-05-28 19:33:35 +08001637static inline bool cpu_has_vmx_ept_ad_bits(void)
1638{
1639 return vmx_capability.ept & VMX_EPT_AD_BIT;
1640}
1641
Gui Jianfeng31299942010-03-15 17:29:09 +08001642static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001643{
Gui Jianfeng31299942010-03-15 17:29:09 +08001644 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001645}
1646
Gui Jianfeng31299942010-03-15 17:29:09 +08001647static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001648{
Gui Jianfeng31299942010-03-15 17:29:09 +08001649 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001650}
1651
Liran Aloncd9a4912018-05-22 17:16:15 +03001652static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1653{
1654 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1655}
1656
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001657static inline bool cpu_has_vmx_invvpid_single(void)
1658{
1659 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1660}
1661
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001662static inline bool cpu_has_vmx_invvpid_global(void)
1663{
1664 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1665}
1666
Wanpeng Li08d839c2017-03-23 05:30:08 -07001667static inline bool cpu_has_vmx_invvpid(void)
1668{
1669 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1670}
1671
Gui Jianfeng31299942010-03-15 17:29:09 +08001672static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001676}
1677
Gui Jianfeng31299942010-03-15 17:29:09 +08001678static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1682}
1683
Gui Jianfeng31299942010-03-15 17:29:09 +08001684static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1688}
1689
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001690static inline bool cpu_has_vmx_basic_inout(void)
1691{
1692 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1693}
1694
Paolo Bonzini35754c92015-07-29 12:05:37 +02001695static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001696{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001697 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001698}
1699
Gui Jianfeng31299942010-03-15 17:29:09 +08001700static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001701{
Sheng Yang04547152009-04-01 15:52:31 +08001702 return vmcs_config.cpu_based_2nd_exec_ctrl &
1703 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001704}
1705
Gui Jianfeng31299942010-03-15 17:29:09 +08001706static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001707{
1708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_RDTSCP;
1710}
1711
Mao, Junjiead756a12012-07-02 01:18:48 +00001712static inline bool cpu_has_vmx_invpcid(void)
1713{
1714 return vmcs_config.cpu_based_2nd_exec_ctrl &
1715 SECONDARY_EXEC_ENABLE_INVPCID;
1716}
1717
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001718static inline bool cpu_has_virtual_nmis(void)
1719{
1720 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1721}
1722
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001723static inline bool cpu_has_vmx_wbinvd_exit(void)
1724{
1725 return vmcs_config.cpu_based_2nd_exec_ctrl &
1726 SECONDARY_EXEC_WBINVD_EXITING;
1727}
1728
Abel Gordonabc4fc52013-04-18 14:35:25 +03001729static inline bool cpu_has_vmx_shadow_vmcs(void)
1730{
1731 u64 vmx_msr;
1732 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1733 /* check if the cpu supports writing r/o exit information fields */
1734 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1735 return false;
1736
1737 return vmcs_config.cpu_based_2nd_exec_ctrl &
1738 SECONDARY_EXEC_SHADOW_VMCS;
1739}
1740
Kai Huang843e4332015-01-28 10:54:28 +08001741static inline bool cpu_has_vmx_pml(void)
1742{
1743 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1744}
1745
Haozhong Zhang64903d62015-10-20 15:39:09 +08001746static inline bool cpu_has_vmx_tsc_scaling(void)
1747{
1748 return vmcs_config.cpu_based_2nd_exec_ctrl &
1749 SECONDARY_EXEC_TSC_SCALING;
1750}
1751
Bandan Das2a499e42017-08-03 15:54:41 -04001752static inline bool cpu_has_vmx_vmfunc(void)
1753{
1754 return vmcs_config.cpu_based_2nd_exec_ctrl &
1755 SECONDARY_EXEC_ENABLE_VMFUNC;
1756}
1757
Sean Christopherson64f7a112018-04-30 10:01:06 -07001758static bool vmx_umip_emulated(void)
1759{
1760 return vmcs_config.cpu_based_2nd_exec_ctrl &
1761 SECONDARY_EXEC_DESC;
1762}
1763
Sheng Yang04547152009-04-01 15:52:31 +08001764static inline bool report_flexpriority(void)
1765{
1766 return flexpriority_enabled;
1767}
1768
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001769static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1770{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001771 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001772}
1773
Jim Mattsonf4160e42018-05-29 09:11:33 -07001774/*
1775 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1776 * to modify any valid field of the VMCS, or are the VM-exit
1777 * information fields read-only?
1778 */
1779static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1780{
1781 return to_vmx(vcpu)->nested.msrs.misc_low &
1782 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1783}
1784
Marc Orr04473782018-06-20 17:21:29 -07001785static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1786{
1787 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1788}
1789
1790static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1791{
1792 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1793 CPU_BASED_MONITOR_TRAP_FLAG;
1794}
1795
Liran Alonfa97d7d2018-07-18 14:07:59 +02001796static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1797{
1798 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1799 SECONDARY_EXEC_SHADOW_VMCS;
1800}
1801
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001802static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1803{
1804 return vmcs12->cpu_based_vm_exec_control & bit;
1805}
1806
1807static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1808{
1809 return (vmcs12->cpu_based_vm_exec_control &
1810 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1811 (vmcs12->secondary_vm_exec_control & bit);
1812}
1813
Jan Kiszkaf41245002014-03-07 20:03:13 +01001814static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1815{
1816 return vmcs12->pin_based_vm_exec_control &
1817 PIN_BASED_VMX_PREEMPTION_TIMER;
1818}
1819
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001820static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1821{
1822 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1823}
1824
1825static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1826{
1827 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1828}
1829
Nadav Har'El155a97a2013-08-05 11:07:16 +03001830static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1831{
1832 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1833}
1834
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001835static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1836{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001837 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001838}
1839
Bandan Dasc5f983f2017-05-05 15:25:14 -04001840static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1841{
1842 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1843}
1844
Wincy Vanf2b93282015-02-03 23:56:03 +08001845static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1846{
1847 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1848}
1849
Wanpeng Li5c614b32015-10-13 09:18:36 -07001850static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1851{
1852 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1853}
1854
Wincy Van82f0dd42015-02-03 23:57:18 +08001855static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1856{
1857 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1858}
1859
Wincy Van608406e2015-02-03 23:57:51 +08001860static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1861{
1862 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1863}
1864
Wincy Van705699a2015-02-03 23:58:17 +08001865static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1866{
1867 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1868}
1869
Bandan Das27c42a12017-08-03 15:54:42 -04001870static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1871{
1872 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1873}
1874
Bandan Das41ab9372017-08-03 15:54:43 -04001875static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1876{
1877 return nested_cpu_has_vmfunc(vmcs12) &&
1878 (vmcs12->vm_function_control &
1879 VMX_VMFUNC_EPTP_SWITCHING);
1880}
1881
Liran Alonf792d272018-06-23 02:35:05 +03001882static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1883{
1884 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1885}
1886
Jim Mattsonef85b672016-12-12 11:01:37 -08001887static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001888{
1889 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001890 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001891}
1892
Jan Kiszka533558b2014-01-04 18:47:20 +01001893static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1894 u32 exit_intr_info,
1895 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001896static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1897 struct vmcs12 *vmcs12,
1898 u32 reason, unsigned long qualification);
1899
Rusty Russell8b9cf982007-07-30 16:31:43 +10001900static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001901{
1902 int i;
1903
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001904 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001905 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001906 return i;
1907 return -1;
1908}
1909
Sheng Yang2384d2b2008-01-17 15:14:33 +08001910static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1911{
1912 struct {
1913 u64 vpid : 16;
1914 u64 rsvd : 48;
1915 u64 gva;
1916 } operand = { vpid, 0, gva };
1917
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001918 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001919 /* CF==1 or ZF==1 --> rc = -1 */
1920 "; ja 1f ; ud2 ; 1:"
1921 : : "a"(&operand), "c"(ext) : "cc", "memory");
1922}
1923
Sheng Yang14394422008-04-28 12:24:45 +08001924static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1925{
1926 struct {
1927 u64 eptp, gpa;
1928 } operand = {eptp, gpa};
1929
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001930 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001931 /* CF==1 or ZF==1 --> rc = -1 */
1932 "; ja 1f ; ud2 ; 1:\n"
1933 : : "a" (&operand), "c" (ext) : "cc", "memory");
1934}
1935
Avi Kivity26bb0982009-09-07 11:14:12 +03001936static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001937{
1938 int i;
1939
Rusty Russell8b9cf982007-07-30 16:31:43 +10001940 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001941 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001942 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001943 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001944}
1945
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946static void vmcs_clear(struct vmcs *vmcs)
1947{
1948 u64 phys_addr = __pa(vmcs);
1949 u8 error;
1950
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001951 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001952 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 : "cc", "memory");
1954 if (error)
1955 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1956 vmcs, phys_addr);
1957}
1958
Nadav Har'Eld462b812011-05-24 15:26:10 +03001959static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1960{
1961 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001962 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1963 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001964 loaded_vmcs->cpu = -1;
1965 loaded_vmcs->launched = 0;
1966}
1967
Dongxiao Xu7725b892010-05-11 18:29:38 +08001968static void vmcs_load(struct vmcs *vmcs)
1969{
1970 u64 phys_addr = __pa(vmcs);
1971 u8 error;
1972
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001973 if (static_branch_unlikely(&enable_evmcs))
1974 return evmcs_load(phys_addr);
1975
Dongxiao Xu7725b892010-05-11 18:29:38 +08001976 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001977 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001978 : "cc", "memory");
1979 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001980 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001981 vmcs, phys_addr);
1982}
1983
Dave Young2965faa2015-09-09 15:38:55 -07001984#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001985/*
1986 * This bitmap is used to indicate whether the vmclear
1987 * operation is enabled on all cpus. All disabled by
1988 * default.
1989 */
1990static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1991
1992static inline void crash_enable_local_vmclear(int cpu)
1993{
1994 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1995}
1996
1997static inline void crash_disable_local_vmclear(int cpu)
1998{
1999 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2000}
2001
2002static inline int crash_local_vmclear_enabled(int cpu)
2003{
2004 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2005}
2006
2007static void crash_vmclear_local_loaded_vmcss(void)
2008{
2009 int cpu = raw_smp_processor_id();
2010 struct loaded_vmcs *v;
2011
2012 if (!crash_local_vmclear_enabled(cpu))
2013 return;
2014
2015 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2016 loaded_vmcss_on_cpu_link)
2017 vmcs_clear(v->vmcs);
2018}
2019#else
2020static inline void crash_enable_local_vmclear(int cpu) { }
2021static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002022#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002023
Nadav Har'Eld462b812011-05-24 15:26:10 +03002024static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002026 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002027 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028
Nadav Har'Eld462b812011-05-24 15:26:10 +03002029 if (loaded_vmcs->cpu != cpu)
2030 return; /* vcpu migration can race with cpu offline */
2031 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002033 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002034 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002035
2036 /*
2037 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2038 * is before setting loaded_vmcs->vcpu to -1 which is done in
2039 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2040 * then adds the vmcs into percpu list before it is deleted.
2041 */
2042 smp_wmb();
2043
Nadav Har'Eld462b812011-05-24 15:26:10 +03002044 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002045 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046}
2047
Nadav Har'Eld462b812011-05-24 15:26:10 +03002048static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002049{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002050 int cpu = loaded_vmcs->cpu;
2051
2052 if (cpu != -1)
2053 smp_call_function_single(cpu,
2054 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002055}
2056
Junaid Shahidfaff8752018-06-29 13:10:05 -07002057static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2058{
2059 if (vpid == 0)
2060 return true;
2061
2062 if (cpu_has_vmx_invvpid_individual_addr()) {
2063 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2064 return true;
2065 }
2066
2067 return false;
2068}
2069
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002070static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002071{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002072 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002073 return;
2074
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002075 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002076 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002077}
2078
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002079static inline void vpid_sync_vcpu_global(void)
2080{
2081 if (cpu_has_vmx_invvpid_global())
2082 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2083}
2084
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002085static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002086{
2087 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002088 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002089 else
2090 vpid_sync_vcpu_global();
2091}
2092
Sheng Yang14394422008-04-28 12:24:45 +08002093static inline void ept_sync_global(void)
2094{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002095 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002096}
2097
2098static inline void ept_sync_context(u64 eptp)
2099{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002100 if (cpu_has_vmx_invept_context())
2101 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2102 else
2103 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002104}
2105
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106static __always_inline void vmcs_check16(unsigned long field)
2107{
2108 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2109 "16-bit accessor invalid for 64-bit field");
2110 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2111 "16-bit accessor invalid for 64-bit high field");
2112 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2113 "16-bit accessor invalid for 32-bit high field");
2114 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2115 "16-bit accessor invalid for natural width field");
2116}
2117
2118static __always_inline void vmcs_check32(unsigned long field)
2119{
2120 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2121 "32-bit accessor invalid for 16-bit field");
2122 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2123 "32-bit accessor invalid for natural width field");
2124}
2125
2126static __always_inline void vmcs_check64(unsigned long field)
2127{
2128 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2129 "64-bit accessor invalid for 16-bit field");
2130 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2131 "64-bit accessor invalid for 64-bit high field");
2132 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2133 "64-bit accessor invalid for 32-bit field");
2134 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2135 "64-bit accessor invalid for natural width field");
2136}
2137
2138static __always_inline void vmcs_checkl(unsigned long field)
2139{
2140 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2141 "Natural width accessor invalid for 16-bit field");
2142 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2143 "Natural width accessor invalid for 64-bit field");
2144 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2145 "Natural width accessor invalid for 64-bit high field");
2146 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2147 "Natural width accessor invalid for 32-bit field");
2148}
2149
2150static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151{
Avi Kivity5e520e62011-05-15 10:13:12 -04002152 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153
Avi Kivity5e520e62011-05-15 10:13:12 -04002154 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2155 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 return value;
2157}
2158
Avi Kivity96304212011-05-15 10:13:13 -04002159static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002161 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002162 if (static_branch_unlikely(&enable_evmcs))
2163 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
2166
Avi Kivity96304212011-05-15 10:13:13 -04002167static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002169 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002170 if (static_branch_unlikely(&enable_evmcs))
2171 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002172 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173}
2174
Avi Kivity96304212011-05-15 10:13:13 -04002175static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002178 if (static_branch_unlikely(&enable_evmcs))
2179 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002180#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002181 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002183 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184#endif
2185}
2186
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002187static __always_inline unsigned long vmcs_readl(unsigned long field)
2188{
2189 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002190 if (static_branch_unlikely(&enable_evmcs))
2191 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192 return __vmcs_readl(field);
2193}
2194
Avi Kivitye52de1b2007-01-05 16:36:56 -08002195static noinline void vmwrite_error(unsigned long field, unsigned long value)
2196{
2197 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2198 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2199 dump_stack();
2200}
2201
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002202static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
2204 u8 error;
2205
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002206 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002207 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002208 if (unlikely(error))
2209 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210}
2211
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002212static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002214 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002215 if (static_branch_unlikely(&enable_evmcs))
2216 return evmcs_write16(field, value);
2217
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002218 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219}
2220
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002221static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002223 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002224 if (static_branch_unlikely(&enable_evmcs))
2225 return evmcs_write32(field, value);
2226
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002227 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228}
2229
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002230static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002232 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002233 if (static_branch_unlikely(&enable_evmcs))
2234 return evmcs_write64(field, value);
2235
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002236 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002237#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240#endif
2241}
2242
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002243static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002244{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002245 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002246 if (static_branch_unlikely(&enable_evmcs))
2247 return evmcs_write64(field, value);
2248
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002250}
2251
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002252static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002253{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002254 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2255 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002256 if (static_branch_unlikely(&enable_evmcs))
2257 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2258
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002259 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2260}
2261
2262static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2263{
2264 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2265 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002266 if (static_branch_unlikely(&enable_evmcs))
2267 return evmcs_write32(field, evmcs_read32(field) | mask);
2268
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002269 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002270}
2271
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002272static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2273{
2274 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2275}
2276
Gleb Natapov2961e8762013-11-25 15:37:13 +02002277static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2278{
2279 vmcs_write32(VM_ENTRY_CONTROLS, val);
2280 vmx->vm_entry_controls_shadow = val;
2281}
2282
2283static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2284{
2285 if (vmx->vm_entry_controls_shadow != val)
2286 vm_entry_controls_init(vmx, val);
2287}
2288
2289static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2290{
2291 return vmx->vm_entry_controls_shadow;
2292}
2293
2294
2295static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2296{
2297 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2298}
2299
2300static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2301{
2302 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2303}
2304
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002305static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2306{
2307 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2308}
2309
Gleb Natapov2961e8762013-11-25 15:37:13 +02002310static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2311{
2312 vmcs_write32(VM_EXIT_CONTROLS, val);
2313 vmx->vm_exit_controls_shadow = val;
2314}
2315
2316static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2317{
2318 if (vmx->vm_exit_controls_shadow != val)
2319 vm_exit_controls_init(vmx, val);
2320}
2321
2322static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2323{
2324 return vmx->vm_exit_controls_shadow;
2325}
2326
2327
2328static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2329{
2330 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2331}
2332
2333static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2334{
2335 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2336}
2337
Avi Kivity2fb92db2011-04-27 19:42:18 +03002338static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2339{
2340 vmx->segment_cache.bitmask = 0;
2341}
2342
2343static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2344 unsigned field)
2345{
2346 bool ret;
2347 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2348
2349 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2350 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2351 vmx->segment_cache.bitmask = 0;
2352 }
2353 ret = vmx->segment_cache.bitmask & mask;
2354 vmx->segment_cache.bitmask |= mask;
2355 return ret;
2356}
2357
2358static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2359{
2360 u16 *p = &vmx->segment_cache.seg[seg].selector;
2361
2362 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2363 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2364 return *p;
2365}
2366
2367static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2368{
2369 ulong *p = &vmx->segment_cache.seg[seg].base;
2370
2371 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2372 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2373 return *p;
2374}
2375
2376static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2377{
2378 u32 *p = &vmx->segment_cache.seg[seg].limit;
2379
2380 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2381 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2382 return *p;
2383}
2384
2385static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2386{
2387 u32 *p = &vmx->segment_cache.seg[seg].ar;
2388
2389 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2390 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2391 return *p;
2392}
2393
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002394static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2395{
2396 u32 eb;
2397
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002398 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002399 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002400 /*
2401 * Guest access to VMware backdoor ports could legitimately
2402 * trigger #GP because of TSS I/O permission bitmap.
2403 * We intercept those #GP and allow access to them anyway
2404 * as VMware does.
2405 */
2406 if (enable_vmware_backdoor)
2407 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002408 if ((vcpu->guest_debug &
2409 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2410 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2411 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002412 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002413 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002414 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002415 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002416
2417 /* When we are running a nested L2 guest and L1 specified for it a
2418 * certain exception bitmap, we must trap the same exceptions and pass
2419 * them to L1. When running L2, we will only handle the exceptions
2420 * specified above if L1 did not want them.
2421 */
2422 if (is_guest_mode(vcpu))
2423 eb |= get_vmcs12(vcpu)->exception_bitmap;
2424
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002425 vmcs_write32(EXCEPTION_BITMAP, eb);
2426}
2427
Ashok Raj15d45072018-02-01 22:59:43 +01002428/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002429 * Check if MSR is intercepted for currently loaded MSR bitmap.
2430 */
2431static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2432{
2433 unsigned long *msr_bitmap;
2434 int f = sizeof(unsigned long);
2435
2436 if (!cpu_has_vmx_msr_bitmap())
2437 return true;
2438
2439 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2440
2441 if (msr <= 0x1fff) {
2442 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2443 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2444 msr &= 0x1fff;
2445 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2446 }
2447
2448 return true;
2449}
2450
2451/*
Ashok Raj15d45072018-02-01 22:59:43 +01002452 * Check if MSR is intercepted for L01 MSR bitmap.
2453 */
2454static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2455{
2456 unsigned long *msr_bitmap;
2457 int f = sizeof(unsigned long);
2458
2459 if (!cpu_has_vmx_msr_bitmap())
2460 return true;
2461
2462 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2463
2464 if (msr <= 0x1fff) {
2465 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2466 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2467 msr &= 0x1fff;
2468 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2469 }
2470
2471 return true;
2472}
2473
Gleb Natapov2961e8762013-11-25 15:37:13 +02002474static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2475 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002476{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002477 vm_entry_controls_clearbit(vmx, entry);
2478 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002479}
2480
Avi Kivity61d2ef22010-04-28 16:40:38 +03002481static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2482{
2483 unsigned i;
2484 struct msr_autoload *m = &vmx->msr_autoload;
2485
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002486 switch (msr) {
2487 case MSR_EFER:
2488 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002489 clear_atomic_switch_msr_special(vmx,
2490 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002491 VM_EXIT_LOAD_IA32_EFER);
2492 return;
2493 }
2494 break;
2495 case MSR_CORE_PERF_GLOBAL_CTRL:
2496 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002497 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002498 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2499 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2500 return;
2501 }
2502 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002503 }
2504
Avi Kivity61d2ef22010-04-28 16:40:38 +03002505 for (i = 0; i < m->nr; ++i)
2506 if (m->guest[i].index == msr)
2507 break;
2508
2509 if (i == m->nr)
2510 return;
2511 --m->nr;
2512 m->guest[i] = m->guest[m->nr];
2513 m->host[i] = m->host[m->nr];
2514 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2515 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2516}
2517
Gleb Natapov2961e8762013-11-25 15:37:13 +02002518static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2519 unsigned long entry, unsigned long exit,
2520 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2521 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002522{
2523 vmcs_write64(guest_val_vmcs, guest_val);
2524 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002525 vm_entry_controls_setbit(vmx, entry);
2526 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002527}
2528
Avi Kivity61d2ef22010-04-28 16:40:38 +03002529static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2530 u64 guest_val, u64 host_val)
2531{
2532 unsigned i;
2533 struct msr_autoload *m = &vmx->msr_autoload;
2534
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002535 switch (msr) {
2536 case MSR_EFER:
2537 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002538 add_atomic_switch_msr_special(vmx,
2539 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002540 VM_EXIT_LOAD_IA32_EFER,
2541 GUEST_IA32_EFER,
2542 HOST_IA32_EFER,
2543 guest_val, host_val);
2544 return;
2545 }
2546 break;
2547 case MSR_CORE_PERF_GLOBAL_CTRL:
2548 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002549 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002550 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2551 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2552 GUEST_IA32_PERF_GLOBAL_CTRL,
2553 HOST_IA32_PERF_GLOBAL_CTRL,
2554 guest_val, host_val);
2555 return;
2556 }
2557 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002558 case MSR_IA32_PEBS_ENABLE:
2559 /* PEBS needs a quiescent period after being disabled (to write
2560 * a record). Disabling PEBS through VMX MSR swapping doesn't
2561 * provide that period, so a CPU could write host's record into
2562 * guest's memory.
2563 */
2564 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002565 }
2566
Avi Kivity61d2ef22010-04-28 16:40:38 +03002567 for (i = 0; i < m->nr; ++i)
2568 if (m->guest[i].index == msr)
2569 break;
2570
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002571 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002572 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002573 "Can't add msr %x\n", msr);
2574 return;
2575 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002576 ++m->nr;
2577 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2578 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2579 }
2580
2581 m->guest[i].index = msr;
2582 m->guest[i].value = guest_val;
2583 m->host[i].index = msr;
2584 m->host[i].value = host_val;
2585}
2586
Avi Kivity92c0d902009-10-29 11:00:16 +02002587static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002588{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002589 u64 guest_efer = vmx->vcpu.arch.efer;
2590 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002591
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002592 if (!enable_ept) {
2593 /*
2594 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2595 * host CPUID is more efficient than testing guest CPUID
2596 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2597 */
2598 if (boot_cpu_has(X86_FEATURE_SMEP))
2599 guest_efer |= EFER_NX;
2600 else if (!(guest_efer & EFER_NX))
2601 ignore_bits |= EFER_NX;
2602 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002603
Avi Kivity51c6cf62007-08-29 03:48:05 +03002604 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002605 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002606 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002607 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002608#ifdef CONFIG_X86_64
2609 ignore_bits |= EFER_LMA | EFER_LME;
2610 /* SCE is meaningful only in long mode on Intel */
2611 if (guest_efer & EFER_LMA)
2612 ignore_bits &= ~(u64)EFER_SCE;
2613#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002614
2615 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002616
2617 /*
2618 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2619 * On CPUs that support "load IA32_EFER", always switch EFER
2620 * atomically, since it's faster than switching it manually.
2621 */
2622 if (cpu_has_load_ia32_efer ||
2623 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002624 if (!(guest_efer & EFER_LMA))
2625 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002626 if (guest_efer != host_efer)
2627 add_atomic_switch_msr(vmx, MSR_EFER,
2628 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002629 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002630 } else {
2631 guest_efer &= ~ignore_bits;
2632 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002633
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002634 vmx->guest_msrs[efer_offset].data = guest_efer;
2635 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2636
2637 return true;
2638 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002639}
2640
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002641#ifdef CONFIG_X86_32
2642/*
2643 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2644 * VMCS rather than the segment table. KVM uses this helper to figure
2645 * out the current bases to poke them into the VMCS before entry.
2646 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002647static unsigned long segment_base(u16 selector)
2648{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002649 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002650 unsigned long v;
2651
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002652 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002653 return 0;
2654
Thomas Garnier45fc8752017-03-14 10:05:08 -07002655 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002656
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002657 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002658 u16 ldt_selector = kvm_read_ldt();
2659
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002660 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002661 return 0;
2662
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002663 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002664 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002665 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002666 return v;
2667}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002668#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002669
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002670static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002671{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002672 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002673 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002674#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002675 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002676#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002677 unsigned long fs_base, gs_base;
2678 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002679 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002680
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002681 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002682 return;
2683
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002684 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002685 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002686
Avi Kivity33ed6322007-05-02 16:54:03 +03002687 /*
2688 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2689 * allow segment selectors with cpl > 0 or ti == 1.
2690 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002691 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002692
2693#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002694 savesegment(ds, host_state->ds_sel);
2695 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002696
2697 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002698 if (likely(is_64bit_mm(current->mm))) {
2699 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002700 fs_sel = current->thread.fsindex;
2701 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002702 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002703 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002704 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002705 savesegment(fs, fs_sel);
2706 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002707 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002708 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002709 }
Sean Christophersone368b872018-07-23 12:32:41 -07002710
2711 if (is_long_mode(&vmx->vcpu))
2712 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2713#else
2714 savesegment(fs, fs_sel);
2715 savesegment(gs, gs_sel);
2716 fs_base = segment_base(fs_sel);
2717 gs_base = segment_base(gs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002718#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002719
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002720 if (unlikely(fs_sel != host_state->fs_sel)) {
2721 if (!(fs_sel & 7))
2722 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2723 else
2724 vmcs_write16(HOST_FS_SELECTOR, 0);
2725 host_state->fs_sel = fs_sel;
2726 }
2727 if (unlikely(gs_sel != host_state->gs_sel)) {
2728 if (!(gs_sel & 7))
2729 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2730 else
2731 vmcs_write16(HOST_GS_SELECTOR, 0);
2732 host_state->gs_sel = gs_sel;
2733 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002734
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002735 vmcs_writel(HOST_FS_BASE, fs_base);
Sean Christophersone368b872018-07-23 12:32:41 -07002736 vmcs_writel(HOST_GS_BASE, gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002737
Avi Kivity26bb0982009-09-07 11:14:12 +03002738 for (i = 0; i < vmx->save_nmsrs; ++i)
2739 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002740 vmx->guest_msrs[i].data,
2741 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002742}
2743
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002744static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002745{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002746 struct vmcs_host_state *host_state;
2747
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002748 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002749 return;
2750
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002751 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002752 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002753
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002754 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002755 vmx->loaded_cpu_state = NULL;
2756
Avi Kivityc8770e72010-11-11 12:37:26 +02002757#ifdef CONFIG_X86_64
2758 if (is_long_mode(&vmx->vcpu))
2759 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2760#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002761 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2762 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002763#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002764 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002765#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002766 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002767#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002768 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002769 if (host_state->fs_sel & 7)
2770 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002771#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002772 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2773 loadsegment(ds, host_state->ds_sel);
2774 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002775 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002776#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002777 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002778#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002779 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002780#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002781 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002782}
2783
Sean Christopherson678e3152018-07-23 12:32:43 -07002784#ifdef CONFIG_X86_64
2785static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002786{
Sean Christopherson678e3152018-07-23 12:32:43 -07002787 if (is_long_mode(&vmx->vcpu)) {
2788 preempt_disable();
2789 if (vmx->loaded_cpu_state)
2790 rdmsrl(MSR_KERNEL_GS_BASE,
2791 vmx->msr_guest_kernel_gs_base);
2792 preempt_enable();
2793 }
2794 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002795}
2796
Sean Christopherson678e3152018-07-23 12:32:43 -07002797static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2798{
2799 if (is_long_mode(&vmx->vcpu)) {
2800 preempt_disable();
2801 if (vmx->loaded_cpu_state)
2802 wrmsrl(MSR_KERNEL_GS_BASE, data);
2803 preempt_enable();
2804 }
2805 vmx->msr_guest_kernel_gs_base = data;
2806}
2807#endif
2808
Feng Wu28b835d2015-09-18 22:29:54 +08002809static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2810{
2811 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2812 struct pi_desc old, new;
2813 unsigned int dest;
2814
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002815 /*
2816 * In case of hot-plug or hot-unplug, we may have to undo
2817 * vmx_vcpu_pi_put even if there is no assigned device. And we
2818 * always keep PI.NDST up to date for simplicity: it makes the
2819 * code easier, and CPU migration is not a fast path.
2820 */
2821 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002822 return;
2823
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002824 /*
2825 * First handle the simple case where no cmpxchg is necessary; just
2826 * allow posting non-urgent interrupts.
2827 *
2828 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2829 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2830 * expects the VCPU to be on the blocked_vcpu_list that matches
2831 * PI.NDST.
2832 */
2833 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2834 vcpu->cpu == cpu) {
2835 pi_clear_sn(pi_desc);
2836 return;
2837 }
2838
2839 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002840 do {
2841 old.control = new.control = pi_desc->control;
2842
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002843 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002844
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002845 if (x2apic_enabled())
2846 new.ndst = dest;
2847 else
2848 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002849
Feng Wu28b835d2015-09-18 22:29:54 +08002850 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002851 } while (cmpxchg64(&pi_desc->control, old.control,
2852 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002853}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002854
Peter Feinerc95ba922016-08-17 09:36:47 -07002855static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2856{
2857 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2858 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2859}
2860
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861/*
2862 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2863 * vcpu mutex is already taken.
2864 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002865static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002867 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002868 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002870 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002871 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002872 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002873 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002874
2875 /*
2876 * Read loaded_vmcs->cpu should be before fetching
2877 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2878 * See the comments in __loaded_vmcs_clear().
2879 */
2880 smp_rmb();
2881
Nadav Har'Eld462b812011-05-24 15:26:10 +03002882 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2883 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002884 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002885 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002886 }
2887
2888 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2889 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2890 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002891 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002892 }
2893
2894 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002895 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002896 unsigned long sysenter_esp;
2897
2898 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002899
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 /*
2901 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002902 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002904 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002905 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002906 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002908 /*
2909 * VM exits change the host TR limit to 0x67 after a VM
2910 * exit. This is okay, since 0x67 covers everything except
2911 * the IO bitmap and have have code to handle the IO bitmap
2912 * being lost after a VM exit.
2913 */
2914 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2915
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2917 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002918
Nadav Har'Eld462b812011-05-24 15:26:10 +03002919 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 }
Feng Wu28b835d2015-09-18 22:29:54 +08002921
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002922 /* Setup TSC multiplier */
2923 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002924 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2925 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002926
Feng Wu28b835d2015-09-18 22:29:54 +08002927 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002928 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002929 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002930}
2931
2932static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2933{
2934 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2935
2936 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002937 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2938 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002939 return;
2940
2941 /* Set SN when the vCPU is preempted */
2942 if (vcpu->preempted)
2943 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944}
2945
2946static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2947{
Feng Wu28b835d2015-09-18 22:29:54 +08002948 vmx_vcpu_pi_put(vcpu);
2949
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002950 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951}
2952
Wanpeng Lif244dee2017-07-20 01:11:54 -07002953static bool emulation_required(struct kvm_vcpu *vcpu)
2954{
2955 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2956}
2957
Avi Kivityedcafe32009-12-30 18:07:40 +02002958static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2959
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002960/*
2961 * Return the cr0 value that a nested guest would read. This is a combination
2962 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2963 * its hypervisor (cr0_read_shadow).
2964 */
2965static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2966{
2967 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2968 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2969}
2970static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2971{
2972 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2973 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2974}
2975
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2977{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002978 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002979
Avi Kivity6de12732011-03-07 12:51:22 +02002980 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2981 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2982 rflags = vmcs_readl(GUEST_RFLAGS);
2983 if (to_vmx(vcpu)->rmode.vm86_active) {
2984 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2985 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2986 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2987 }
2988 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002989 }
Avi Kivity6de12732011-03-07 12:51:22 +02002990 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991}
2992
2993static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2994{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002995 unsigned long old_rflags = vmx_get_rflags(vcpu);
2996
Avi Kivity6de12732011-03-07 12:51:22 +02002997 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2998 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002999 if (to_vmx(vcpu)->rmode.vm86_active) {
3000 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003001 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003002 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003004
3005 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3006 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007}
3008
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003009static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003010{
3011 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3012 int ret = 0;
3013
3014 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003015 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003016 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003017 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003018
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003019 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003020}
3021
3022static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3023{
3024 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3025 u32 interruptibility = interruptibility_old;
3026
3027 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3028
Jan Kiszka48005f62010-02-19 19:38:07 +01003029 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003030 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003031 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003032 interruptibility |= GUEST_INTR_STATE_STI;
3033
3034 if ((interruptibility != interruptibility_old))
3035 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3036}
3037
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3039{
3040 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003042 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003044 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045
Glauber Costa2809f5d2009-05-12 16:21:05 -04003046 /* skipping an emulated instruction also counts */
3047 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048}
3049
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003050static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3051 unsigned long exit_qual)
3052{
3053 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3054 unsigned int nr = vcpu->arch.exception.nr;
3055 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3056
3057 if (vcpu->arch.exception.has_error_code) {
3058 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3059 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3060 }
3061
3062 if (kvm_exception_is_soft(nr))
3063 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3064 else
3065 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3066
3067 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3068 vmx_get_nmi_mask(vcpu))
3069 intr_info |= INTR_INFO_UNBLOCK_NMI;
3070
3071 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3072}
3073
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003074/*
3075 * KVM wants to inject page-faults which it got to the guest. This function
3076 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003077 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003078static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003079{
3080 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003081 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003082
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003083 if (nr == PF_VECTOR) {
3084 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003085 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003086 return 1;
3087 }
3088 /*
3089 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3090 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3091 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3092 * can be written only when inject_pending_event runs. This should be
3093 * conditional on a new capability---if the capability is disabled,
3094 * kvm_multiple_exception would write the ancillary information to
3095 * CR2 or DR6, for backwards ABI-compatibility.
3096 */
3097 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3098 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003099 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003100 return 1;
3101 }
3102 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003103 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003104 if (nr == DB_VECTOR)
3105 *exit_qual = vcpu->arch.dr6;
3106 else
3107 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003108 return 1;
3109 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003110 }
3111
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003112 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003113}
3114
Wanpeng Licaa057a2018-03-12 04:53:03 -07003115static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3116{
3117 /*
3118 * Ensure that we clear the HLT state in the VMCS. We don't need to
3119 * explicitly skip the instruction because if the HLT state is set,
3120 * then the instruction is already executing and RIP has already been
3121 * advanced.
3122 */
3123 if (kvm_hlt_in_guest(vcpu->kvm) &&
3124 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3125 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3126}
3127
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003128static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003129{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003130 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003131 unsigned nr = vcpu->arch.exception.nr;
3132 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003133 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003134 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003135
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003136 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003137 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003138 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3139 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003140
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003141 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003142 int inc_eip = 0;
3143 if (kvm_exception_is_soft(nr))
3144 inc_eip = vcpu->arch.event_exit_inst_len;
3145 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003146 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003147 return;
3148 }
3149
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003150 WARN_ON_ONCE(vmx->emulation_required);
3151
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003152 if (kvm_exception_is_soft(nr)) {
3153 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3154 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003155 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3156 } else
3157 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3158
3159 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003160
3161 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003162}
3163
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003164static bool vmx_rdtscp_supported(void)
3165{
3166 return cpu_has_vmx_rdtscp();
3167}
3168
Mao, Junjiead756a12012-07-02 01:18:48 +00003169static bool vmx_invpcid_supported(void)
3170{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003171 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003172}
3173
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174/*
Eddie Donga75beee2007-05-17 18:55:15 +03003175 * Swap MSR entry in host/guest MSR entry array.
3176 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003177static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003178{
Avi Kivity26bb0982009-09-07 11:14:12 +03003179 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003180
3181 tmp = vmx->guest_msrs[to];
3182 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3183 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003184}
3185
3186/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003187 * Set up the vmcs to automatically save and restore system
3188 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3189 * mode, as fiddling with msrs is very expensive.
3190 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003191static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003192{
Avi Kivity26bb0982009-09-07 11:14:12 +03003193 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003194
Eddie Donga75beee2007-05-17 18:55:15 +03003195 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003196#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003197 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003198 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003199 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003200 move_msr_up(vmx, index, save_nmsrs++);
3201 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003202 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003203 move_msr_up(vmx, index, save_nmsrs++);
3204 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003205 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003206 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003207 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003208 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003209 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003210 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003211 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003212 * if efer.sce is enabled.
3213 */
Brian Gerst8c065852010-07-17 09:03:26 -04003214 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003215 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003216 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003217 }
Eddie Donga75beee2007-05-17 18:55:15 +03003218#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003219 index = __find_msr_index(vmx, MSR_EFER);
3220 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003221 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003222
Avi Kivity26bb0982009-09-07 11:14:12 +03003223 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003224
Yang Zhang8d146952013-01-25 10:18:50 +08003225 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003226 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003227}
3228
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003229static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003231 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003233 if (is_guest_mode(vcpu) &&
3234 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3235 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3236
3237 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238}
3239
3240/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003241 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003243static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003245 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003246 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003247 * We're here if L1 chose not to trap WRMSR to TSC. According
3248 * to the spec, this should set L1's TSC; The offset that L1
3249 * set for L2 remains unchanged, and still needs to be added
3250 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003251 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003252 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003253 /* recalculate vmcs02.TSC_OFFSET: */
3254 vmcs12 = get_vmcs12(vcpu);
3255 vmcs_write64(TSC_OFFSET, offset +
3256 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3257 vmcs12->tsc_offset : 0));
3258 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003259 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3260 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003261 vmcs_write64(TSC_OFFSET, offset);
3262 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263}
3264
Nadav Har'El801d3422011-05-25 23:02:23 +03003265/*
3266 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3267 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3268 * all guests if the "nested" module option is off, and can also be disabled
3269 * for a single guest by disabling its VMX cpuid bit.
3270 */
3271static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3272{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003273 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003274}
3275
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003277 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3278 * returned for the various VMX controls MSRs when nested VMX is enabled.
3279 * The same values should also be used to verify that vmcs12 control fields are
3280 * valid during nested entry from L1 to L2.
3281 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3282 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3283 * bit in the high half is on if the corresponding bit in the control field
3284 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003285 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003286static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003287{
Paolo Bonzini13893092018-02-26 13:40:09 +01003288 if (!nested) {
3289 memset(msrs, 0, sizeof(*msrs));
3290 return;
3291 }
3292
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003293 /*
3294 * Note that as a general rule, the high half of the MSRs (bits in
3295 * the control fields which may be 1) should be initialized by the
3296 * intersection of the underlying hardware's MSR (i.e., features which
3297 * can be supported) and the list of features we want to expose -
3298 * because they are known to be properly supported in our code.
3299 * Also, usually, the low half of the MSRs (bits which must be 1) can
3300 * be set to 0, meaning that L1 may turn off any of these bits. The
3301 * reason is that if one of these bits is necessary, it will appear
3302 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3303 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003304 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003305 * These rules have exceptions below.
3306 */
3307
3308 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003309 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 msrs->pinbased_ctls_low,
3311 msrs->pinbased_ctls_high);
3312 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003313 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003315 PIN_BASED_EXT_INTR_MASK |
3316 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003317 PIN_BASED_VIRTUAL_NMIS |
3318 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003320 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003321 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003322
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003323 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003324 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->exit_ctls_low,
3326 msrs->exit_ctls_high);
3327 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003328 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003329
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003330 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003331#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003332 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003333#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003334 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003336 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003337 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003338 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3339
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003340 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003341 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003342
Jan Kiszka2996fca2014-06-16 13:59:43 +02003343 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003345
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003346 /* entry controls */
3347 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003348 msrs->entry_ctls_low,
3349 msrs->entry_ctls_high);
3350 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003351 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003352 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003353#ifdef CONFIG_X86_64
3354 VM_ENTRY_IA32E_MODE |
3355#endif
3356 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003357 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003358 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003359 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003360 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003361
Jan Kiszka2996fca2014-06-16 13:59:43 +02003362 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003363 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003364
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003365 /* cpu-based controls */
3366 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003367 msrs->procbased_ctls_low,
3368 msrs->procbased_ctls_high);
3369 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003370 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003371 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003372 CPU_BASED_VIRTUAL_INTR_PENDING |
3373 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003374 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3375 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3376 CPU_BASED_CR3_STORE_EXITING |
3377#ifdef CONFIG_X86_64
3378 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3379#endif
3380 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003381 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3382 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3383 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3384 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003385 /*
3386 * We can allow some features even when not supported by the
3387 * hardware. For example, L1 can specify an MSR bitmap - and we
3388 * can use it to avoid exits to L1 - even when L0 runs L2
3389 * without MSR bitmaps.
3390 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003392 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003393 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003394
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003395 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003396 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003397 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3398
Paolo Bonzini80154d72017-08-24 13:55:35 +02003399 /*
3400 * secondary cpu-based controls. Do not include those that
3401 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3402 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003403 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->secondary_ctls_low,
3405 msrs->secondary_ctls_high);
3406 msrs->secondary_ctls_low = 0;
3407 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003408 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003409 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003410 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003411 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003412 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003413 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003414 /*
3415 * We can emulate "VMCS shadowing," even if the hardware
3416 * doesn't support it.
3417 */
3418 msrs->secondary_ctls_high |=
3419 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003420
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003421 if (enable_ept) {
3422 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003423 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003424 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003425 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003426 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003427 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003428 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003429 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003430 msrs->ept_caps &= vmx_capability.ept;
3431 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003432 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3433 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003434 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003435 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003436 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003437 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003438 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003439 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003440
Bandan Das27c42a12017-08-03 15:54:42 -04003441 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003442 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003443 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003444 /*
3445 * Advertise EPTP switching unconditionally
3446 * since we emulate it
3447 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003448 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003449 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003450 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003451 }
3452
Paolo Bonzinief697a72016-03-18 16:58:38 +01003453 /*
3454 * Old versions of KVM use the single-context version without
3455 * checking for support, so declare that it is supported even
3456 * though it is treated as global context. The alternative is
3457 * not failing the single-context invvpid, and it is worse.
3458 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003459 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003460 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003461 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003462 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003463 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003464 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003465
Radim Krčmář0790ec12015-03-17 14:02:32 +01003466 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003468 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3469
Jan Kiszkac18911a2013-03-13 16:06:41 +01003470 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003471 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003472 msrs->misc_low,
3473 msrs->misc_high);
3474 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3475 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003476 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003477 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003478 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003479 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003480
3481 /*
3482 * This MSR reports some information about VMX support. We
3483 * should return information about the VMX we emulate for the
3484 * guest, and the VMCS structure we give it - not about the
3485 * VMX support of the underlying hardware.
3486 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003487 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003488 VMCS12_REVISION |
3489 VMX_BASIC_TRUE_CTLS |
3490 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3491 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3492
3493 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003494 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495
3496 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003497 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003498 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3499 * We picked the standard core2 setting.
3500 */
3501#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3502#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003503 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3504 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003505
3506 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003507 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3508 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003509
3510 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003511 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003512}
3513
David Matlack38991522016-11-29 18:14:08 -08003514/*
3515 * if fixed0[i] == 1: val[i] must be 1
3516 * if fixed1[i] == 0: val[i] must be 0
3517 */
3518static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3519{
3520 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003521}
3522
3523static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3524{
David Matlack38991522016-11-29 18:14:08 -08003525 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003526}
3527
3528static inline u64 vmx_control_msr(u32 low, u32 high)
3529{
3530 return low | ((u64)high << 32);
3531}
3532
David Matlack62cc6b9d2016-11-29 18:14:07 -08003533static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3534{
3535 superset &= mask;
3536 subset &= mask;
3537
3538 return (superset | subset) == superset;
3539}
3540
3541static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3542{
3543 const u64 feature_and_reserved =
3544 /* feature (except bit 48; see below) */
3545 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3546 /* reserved */
3547 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003549
3550 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3551 return -EINVAL;
3552
3553 /*
3554 * KVM does not emulate a version of VMX that constrains physical
3555 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3556 */
3557 if (data & BIT_ULL(48))
3558 return -EINVAL;
3559
3560 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3561 vmx_basic_vmcs_revision_id(data))
3562 return -EINVAL;
3563
3564 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3565 return -EINVAL;
3566
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003568 return 0;
3569}
3570
3571static int
3572vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3573{
3574 u64 supported;
3575 u32 *lowp, *highp;
3576
3577 switch (msr_index) {
3578 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003579 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3580 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003581 break;
3582 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 lowp = &vmx->nested.msrs.procbased_ctls_low;
3584 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003585 break;
3586 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 lowp = &vmx->nested.msrs.exit_ctls_low;
3588 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003589 break;
3590 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003591 lowp = &vmx->nested.msrs.entry_ctls_low;
3592 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003593 break;
3594 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 lowp = &vmx->nested.msrs.secondary_ctls_low;
3596 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003597 break;
3598 default:
3599 BUG();
3600 }
3601
3602 supported = vmx_control_msr(*lowp, *highp);
3603
3604 /* Check must-be-1 bits are still 1. */
3605 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3606 return -EINVAL;
3607
3608 /* Check must-be-0 bits are still 0. */
3609 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3610 return -EINVAL;
3611
3612 *lowp = data;
3613 *highp = data >> 32;
3614 return 0;
3615}
3616
3617static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3618{
3619 const u64 feature_and_reserved_bits =
3620 /* feature */
3621 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3622 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3623 /* reserved */
3624 GENMASK_ULL(13, 9) | BIT_ULL(31);
3625 u64 vmx_misc;
3626
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3628 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003629
3630 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3631 return -EINVAL;
3632
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003634 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3635 vmx_misc_preemption_timer_rate(data) !=
3636 vmx_misc_preemption_timer_rate(vmx_misc))
3637 return -EINVAL;
3638
3639 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3640 return -EINVAL;
3641
3642 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3643 return -EINVAL;
3644
3645 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3646 return -EINVAL;
3647
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003648 vmx->nested.msrs.misc_low = data;
3649 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003650
3651 /*
3652 * If L1 has read-only VM-exit information fields, use the
3653 * less permissive vmx_vmwrite_bitmap to specify write
3654 * permissions for the shadow VMCS.
3655 */
3656 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3657 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3658
David Matlack62cc6b9d2016-11-29 18:14:07 -08003659 return 0;
3660}
3661
3662static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3663{
3664 u64 vmx_ept_vpid_cap;
3665
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3667 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003668
3669 /* Every bit is either reserved or a feature bit. */
3670 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3671 return -EINVAL;
3672
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 vmx->nested.msrs.ept_caps = data;
3674 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003675 return 0;
3676}
3677
3678static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3679{
3680 u64 *msr;
3681
3682 switch (msr_index) {
3683 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003684 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003685 break;
3686 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003687 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003688 break;
3689 default:
3690 BUG();
3691 }
3692
3693 /*
3694 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3695 * must be 1 in the restored value.
3696 */
3697 if (!is_bitwise_subset(data, *msr, -1ULL))
3698 return -EINVAL;
3699
3700 *msr = data;
3701 return 0;
3702}
3703
3704/*
3705 * Called when userspace is restoring VMX MSRs.
3706 *
3707 * Returns 0 on success, non-0 otherwise.
3708 */
3709static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3710{
3711 struct vcpu_vmx *vmx = to_vmx(vcpu);
3712
Jim Mattsona943ac52018-05-29 09:11:32 -07003713 /*
3714 * Don't allow changes to the VMX capability MSRs while the vCPU
3715 * is in VMX operation.
3716 */
3717 if (vmx->nested.vmxon)
3718 return -EBUSY;
3719
David Matlack62cc6b9d2016-11-29 18:14:07 -08003720 switch (msr_index) {
3721 case MSR_IA32_VMX_BASIC:
3722 return vmx_restore_vmx_basic(vmx, data);
3723 case MSR_IA32_VMX_PINBASED_CTLS:
3724 case MSR_IA32_VMX_PROCBASED_CTLS:
3725 case MSR_IA32_VMX_EXIT_CTLS:
3726 case MSR_IA32_VMX_ENTRY_CTLS:
3727 /*
3728 * The "non-true" VMX capability MSRs are generated from the
3729 * "true" MSRs, so we do not support restoring them directly.
3730 *
3731 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3732 * should restore the "true" MSRs with the must-be-1 bits
3733 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3734 * DEFAULT SETTINGS".
3735 */
3736 return -EINVAL;
3737 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3738 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3739 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3740 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3741 case MSR_IA32_VMX_PROCBASED_CTLS2:
3742 return vmx_restore_control_msr(vmx, msr_index, data);
3743 case MSR_IA32_VMX_MISC:
3744 return vmx_restore_vmx_misc(vmx, data);
3745 case MSR_IA32_VMX_CR0_FIXED0:
3746 case MSR_IA32_VMX_CR4_FIXED0:
3747 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3748 case MSR_IA32_VMX_CR0_FIXED1:
3749 case MSR_IA32_VMX_CR4_FIXED1:
3750 /*
3751 * These MSRs are generated based on the vCPU's CPUID, so we
3752 * do not support restoring them directly.
3753 */
3754 return -EINVAL;
3755 case MSR_IA32_VMX_EPT_VPID_CAP:
3756 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3757 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003759 return 0;
3760 default:
3761 /*
3762 * The rest of the VMX capability MSRs do not support restore.
3763 */
3764 return -EINVAL;
3765 }
3766}
3767
Jan Kiszkacae50132014-01-04 18:47:22 +01003768/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003769static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003770{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003771 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003772 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003773 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003774 break;
3775 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3776 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003777 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 msrs->pinbased_ctls_low,
3779 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003780 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3781 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003782 break;
3783 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3784 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003785 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 msrs->procbased_ctls_low,
3787 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003788 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3789 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003790 break;
3791 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3792 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003793 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003794 msrs->exit_ctls_low,
3795 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003796 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3797 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003798 break;
3799 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3800 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003801 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 msrs->entry_ctls_low,
3803 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003804 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3805 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003806 break;
3807 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003808 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003809 msrs->misc_low,
3810 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003811 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003813 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003814 break;
3815 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003816 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003817 break;
3818 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003819 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003820 break;
3821 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003822 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003823 break;
3824 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003825 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003826 break;
3827 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003828 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003829 msrs->secondary_ctls_low,
3830 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003831 break;
3832 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003833 *pdata = msrs->ept_caps |
3834 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003835 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003836 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003837 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003838 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003839 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003840 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003841 }
3842
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003843 return 0;
3844}
3845
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003846static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3847 uint64_t val)
3848{
3849 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3850
3851 return !(val & ~valid_bits);
3852}
3853
Tom Lendacky801e4592018-02-21 13:39:51 -06003854static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3855{
Paolo Bonzini13893092018-02-26 13:40:09 +01003856 switch (msr->index) {
3857 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3858 if (!nested)
3859 return 1;
3860 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3861 default:
3862 return 1;
3863 }
3864
3865 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003866}
3867
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003868/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 * Reads an msr value (of 'msr_index') into 'pdata'.
3870 * Returns 0 on success, non-0 otherwise.
3871 * Assumes vcpu_load() was already called.
3872 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003873static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003875 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003876 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003878 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003879#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003881 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 break;
3883 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003884 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003886 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07003887 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03003888 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003889#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003890 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003891 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003892 case MSR_IA32_SPEC_CTRL:
3893 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003894 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3895 return 1;
3896
3897 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3898 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003899 case MSR_IA32_ARCH_CAPABILITIES:
3900 if (!msr_info->host_initiated &&
3901 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3902 return 1;
3903 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3904 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003906 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 break;
3908 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003909 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
3911 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003914 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003915 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003916 (!msr_info->host_initiated &&
3917 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003918 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003919 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003920 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003921 case MSR_IA32_MCG_EXT_CTL:
3922 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003923 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003924 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003925 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003926 msr_info->data = vcpu->arch.mcg_ext_ctl;
3927 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003928 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003929 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003930 break;
3931 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3932 if (!nested_vmx_allowed(vcpu))
3933 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003934 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3935 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003936 case MSR_IA32_XSS:
3937 if (!vmx_xsaves_supported())
3938 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003939 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003940 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003941 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003942 if (!msr_info->host_initiated &&
3943 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003944 return 1;
3945 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003947 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003948 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003949 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003950 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003952 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 }
3954
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955 return 0;
3956}
3957
Jan Kiszkacae50132014-01-04 18:47:22 +01003958static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3959
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960/*
3961 * Writes msr value into into the appropriate "register".
3962 * Returns 0 on success, non-0 otherwise.
3963 * Assumes vcpu_load() was already called.
3964 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003965static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003967 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003968 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003969 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003970 u32 msr_index = msr_info->index;
3971 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003972
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003974 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003975 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003976 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003977#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003979 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 vmcs_writel(GUEST_FS_BASE, data);
3981 break;
3982 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003983 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 vmcs_writel(GUEST_GS_BASE, data);
3985 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003986 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07003987 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03003988 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989#endif
3990 case MSR_IA32_SYSENTER_CS:
3991 vmcs_write32(GUEST_SYSENTER_CS, data);
3992 break;
3993 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003994 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 break;
3996 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003997 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003999 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004000 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004001 (!msr_info->host_initiated &&
4002 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004003 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004004 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004005 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004007 vmcs_write64(GUEST_BNDCFGS, data);
4008 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004009 case MSR_IA32_SPEC_CTRL:
4010 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004011 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4012 return 1;
4013
4014 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004015 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004016 return 1;
4017
4018 vmx->spec_ctrl = data;
4019
4020 if (!data)
4021 break;
4022
4023 /*
4024 * For non-nested:
4025 * When it's written (to non-zero) for the first time, pass
4026 * it through.
4027 *
4028 * For nested:
4029 * The handling of the MSR bitmap for L2 guests is done in
4030 * nested_vmx_merge_msr_bitmap. We should not touch the
4031 * vmcs02.msr_bitmap here since it gets completely overwritten
4032 * in the merging. We update the vmcs01 here for L1 as well
4033 * since it will end up touching the MSR anyway now.
4034 */
4035 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4036 MSR_IA32_SPEC_CTRL,
4037 MSR_TYPE_RW);
4038 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004039 case MSR_IA32_PRED_CMD:
4040 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004041 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4042 return 1;
4043
4044 if (data & ~PRED_CMD_IBPB)
4045 return 1;
4046
4047 if (!data)
4048 break;
4049
4050 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4051
4052 /*
4053 * For non-nested:
4054 * When it's written (to non-zero) for the first time, pass
4055 * it through.
4056 *
4057 * For nested:
4058 * The handling of the MSR bitmap for L2 guests is done in
4059 * nested_vmx_merge_msr_bitmap. We should not touch the
4060 * vmcs02.msr_bitmap here since it gets completely overwritten
4061 * in the merging.
4062 */
4063 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4064 MSR_TYPE_W);
4065 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004066 case MSR_IA32_ARCH_CAPABILITIES:
4067 if (!msr_info->host_initiated)
4068 return 1;
4069 vmx->arch_capabilities = data;
4070 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004071 case MSR_IA32_CR_PAT:
4072 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004073 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4074 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004075 vmcs_write64(GUEST_IA32_PAT, data);
4076 vcpu->arch.pat = data;
4077 break;
4078 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004079 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004080 break;
Will Auldba904632012-11-29 12:42:50 -08004081 case MSR_IA32_TSC_ADJUST:
4082 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004083 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004084 case MSR_IA32_MCG_EXT_CTL:
4085 if ((!msr_info->host_initiated &&
4086 !(to_vmx(vcpu)->msr_ia32_feature_control &
4087 FEATURE_CONTROL_LMCE)) ||
4088 (data & ~MCG_EXT_CTL_LMCE_EN))
4089 return 1;
4090 vcpu->arch.mcg_ext_ctl = data;
4091 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004092 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004093 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004094 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004095 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4096 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004097 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004098 if (msr_info->host_initiated && data == 0)
4099 vmx_leave_nested(vcpu);
4100 break;
4101 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004102 if (!msr_info->host_initiated)
4103 return 1; /* they are read-only */
4104 if (!nested_vmx_allowed(vcpu))
4105 return 1;
4106 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004107 case MSR_IA32_XSS:
4108 if (!vmx_xsaves_supported())
4109 return 1;
4110 /*
4111 * The only supported bit as of Skylake is bit 8, but
4112 * it is not supported on KVM.
4113 */
4114 if (data != 0)
4115 return 1;
4116 vcpu->arch.ia32_xss = data;
4117 if (vcpu->arch.ia32_xss != host_xss)
4118 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4119 vcpu->arch.ia32_xss, host_xss);
4120 else
4121 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4122 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004123 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004124 if (!msr_info->host_initiated &&
4125 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004126 return 1;
4127 /* Check reserved bit, higher 32 bits should be zero */
4128 if ((data >> 32) != 0)
4129 return 1;
4130 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004132 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004133 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004134 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004135 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004136 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4137 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004138 ret = kvm_set_shared_msr(msr->index, msr->data,
4139 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004140 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004141 if (ret)
4142 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004143 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004144 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004146 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 }
4148
Eddie Dong2cc51562007-05-21 07:28:09 +03004149 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150}
4151
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004152static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004154 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4155 switch (reg) {
4156 case VCPU_REGS_RSP:
4157 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4158 break;
4159 case VCPU_REGS_RIP:
4160 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4161 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004162 case VCPU_EXREG_PDPTR:
4163 if (enable_ept)
4164 ept_save_pdptrs(vcpu);
4165 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004166 default:
4167 break;
4168 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169}
4170
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171static __init int cpu_has_kvm_support(void)
4172{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004173 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174}
4175
4176static __init int vmx_disabled_by_bios(void)
4177{
4178 u64 msr;
4179
4180 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004181 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004182 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004183 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4184 && tboot_enabled())
4185 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004186 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004187 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004188 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004189 && !tboot_enabled()) {
4190 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004191 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004192 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004193 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004194 /* launched w/o TXT and VMX disabled */
4195 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4196 && !tboot_enabled())
4197 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004198 }
4199
4200 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201}
4202
Dongxiao Xu7725b892010-05-11 18:29:38 +08004203static void kvm_cpu_vmxon(u64 addr)
4204{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004205 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004206 intel_pt_handle_vmx(1);
4207
Dongxiao Xu7725b892010-05-11 18:29:38 +08004208 asm volatile (ASM_VMX_VMXON_RAX
4209 : : "a"(&addr), "m"(addr)
4210 : "memory", "cc");
4211}
4212
Radim Krčmář13a34e02014-08-28 15:13:03 +02004213static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214{
4215 int cpu = raw_smp_processor_id();
4216 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004217 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004218
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004219 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004220 return -EBUSY;
4221
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004222 /*
4223 * This can happen if we hot-added a CPU but failed to allocate
4224 * VP assist page for it.
4225 */
4226 if (static_branch_unlikely(&enable_evmcs) &&
4227 !hv_get_vp_assist_page(cpu))
4228 return -EFAULT;
4229
Nadav Har'Eld462b812011-05-24 15:26:10 +03004230 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004231 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4232 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004233
4234 /*
4235 * Now we can enable the vmclear operation in kdump
4236 * since the loaded_vmcss_on_cpu list on this cpu
4237 * has been initialized.
4238 *
4239 * Though the cpu is not in VMX operation now, there
4240 * is no problem to enable the vmclear operation
4241 * for the loaded_vmcss_on_cpu list is empty!
4242 */
4243 crash_enable_local_vmclear(cpu);
4244
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004246
4247 test_bits = FEATURE_CONTROL_LOCKED;
4248 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4249 if (tboot_enabled())
4250 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4251
4252 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004254 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4255 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004256 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004257 if (enable_ept)
4258 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004259
4260 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261}
4262
Nadav Har'Eld462b812011-05-24 15:26:10 +03004263static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004264{
4265 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004266 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004267
Nadav Har'Eld462b812011-05-24 15:26:10 +03004268 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4269 loaded_vmcss_on_cpu_link)
4270 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004271}
4272
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004273
4274/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4275 * tricks.
4276 */
4277static void kvm_cpu_vmxoff(void)
4278{
4279 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004280
4281 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004282 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004283}
4284
Radim Krčmář13a34e02014-08-28 15:13:03 +02004285static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004286{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004287 vmclear_local_loaded_vmcss();
4288 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004289}
4290
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004291static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004292 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293{
4294 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004295 u32 ctl = ctl_min | ctl_opt;
4296
4297 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4298
4299 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4300 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4301
4302 /* Ensure minimum (required) set of control bits are supported. */
4303 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004304 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004305
4306 *result = ctl;
4307 return 0;
4308}
4309
Avi Kivity110312c2010-12-21 12:54:20 +02004310static __init bool allow_1_setting(u32 msr, u32 ctl)
4311{
4312 u32 vmx_msr_low, vmx_msr_high;
4313
4314 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4315 return vmx_msr_high & ctl;
4316}
4317
Yang, Sheng002c7f72007-07-31 14:23:01 +03004318static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004319{
4320 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004321 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004322 u32 _pin_based_exec_control = 0;
4323 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004324 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004325 u32 _vmexit_control = 0;
4326 u32 _vmentry_control = 0;
4327
Paolo Bonzini13893092018-02-26 13:40:09 +01004328 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304329 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004330#ifdef CONFIG_X86_64
4331 CPU_BASED_CR8_LOAD_EXITING |
4332 CPU_BASED_CR8_STORE_EXITING |
4333#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004334 CPU_BASED_CR3_LOAD_EXITING |
4335 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004336 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004337 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004338 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004339 CPU_BASED_MWAIT_EXITING |
4340 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004341 CPU_BASED_INVLPG_EXITING |
4342 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004343
Sheng Yangf78e0e22007-10-29 09:40:42 +08004344 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004345 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004346 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004347 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4348 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004349 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004350#ifdef CONFIG_X86_64
4351 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4352 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4353 ~CPU_BASED_CR8_STORE_EXITING;
4354#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004355 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004356 min2 = 0;
4357 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004358 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004359 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004360 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004361 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004362 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004363 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004364 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004365 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004366 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004367 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004368 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004369 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004370 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004371 SECONDARY_EXEC_RDSEED_EXITING |
4372 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004373 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004374 SECONDARY_EXEC_TSC_SCALING |
4375 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004376 if (adjust_vmx_controls(min2, opt2,
4377 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004378 &_cpu_based_2nd_exec_control) < 0)
4379 return -EIO;
4380 }
4381#ifndef CONFIG_X86_64
4382 if (!(_cpu_based_2nd_exec_control &
4383 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4384 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4385#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004386
4387 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4388 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004389 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004390 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4391 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004392
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004393 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4394 &vmx_capability.ept, &vmx_capability.vpid);
4395
Sheng Yangd56f5462008-04-25 10:13:16 +08004396 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004397 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4398 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004399 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4400 CPU_BASED_CR3_STORE_EXITING |
4401 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004402 } else if (vmx_capability.ept) {
4403 vmx_capability.ept = 0;
4404 pr_warn_once("EPT CAP should not exist if not support "
4405 "1-setting enable EPT VM-execution control\n");
4406 }
4407 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4408 vmx_capability.vpid) {
4409 vmx_capability.vpid = 0;
4410 pr_warn_once("VPID CAP should not exist if not support "
4411 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004412 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004413
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004414 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004415#ifdef CONFIG_X86_64
4416 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4417#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004418 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004419 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004420 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4421 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004422 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004423
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004424 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4425 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4426 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004427 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4428 &_pin_based_exec_control) < 0)
4429 return -EIO;
4430
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004431 if (cpu_has_broken_vmx_preemption_timer())
4432 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004433 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004434 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004435 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4436
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004437 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004438 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004439 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4440 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004441 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004442
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004443 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004444
4445 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4446 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004447 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004448
4449#ifdef CONFIG_X86_64
4450 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4451 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004452 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004453#endif
4454
4455 /* Require Write-Back (WB) memory type for VMCS accesses. */
4456 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004457 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004458
Yang, Sheng002c7f72007-07-31 14:23:01 +03004459 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004460 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004461 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004462
Liran Alon2307af12018-06-29 22:59:04 +03004463 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004464
Yang, Sheng002c7f72007-07-31 14:23:01 +03004465 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4466 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004467 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004468 vmcs_conf->vmexit_ctrl = _vmexit_control;
4469 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004470
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004471 if (static_branch_unlikely(&enable_evmcs))
4472 evmcs_sanitize_exec_ctrls(vmcs_conf);
4473
Avi Kivity110312c2010-12-21 12:54:20 +02004474 cpu_has_load_ia32_efer =
4475 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4476 VM_ENTRY_LOAD_IA32_EFER)
4477 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4478 VM_EXIT_LOAD_IA32_EFER);
4479
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004480 cpu_has_load_perf_global_ctrl =
4481 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4482 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4483 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4484 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4485
4486 /*
4487 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004488 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004489 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4490 *
4491 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4492 *
4493 * AAK155 (model 26)
4494 * AAP115 (model 30)
4495 * AAT100 (model 37)
4496 * BC86,AAY89,BD102 (model 44)
4497 * BA97 (model 46)
4498 *
4499 */
4500 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4501 switch (boot_cpu_data.x86_model) {
4502 case 26:
4503 case 30:
4504 case 37:
4505 case 44:
4506 case 46:
4507 cpu_has_load_perf_global_ctrl = false;
4508 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4509 "does not work properly. Using workaround\n");
4510 break;
4511 default:
4512 break;
4513 }
4514 }
4515
Borislav Petkov782511b2016-04-04 22:25:03 +02004516 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004517 rdmsrl(MSR_IA32_XSS, host_xss);
4518
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004519 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004520}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004521
Liran Alon491a6032018-06-23 02:35:12 +03004522static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523{
4524 int node = cpu_to_node(cpu);
4525 struct page *pages;
4526 struct vmcs *vmcs;
4527
Vlastimil Babka96db8002015-09-08 15:03:50 -07004528 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 if (!pages)
4530 return NULL;
4531 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004532 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004533
4534 /* KVM supports Enlightened VMCS v1 only */
4535 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004536 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004537 else
Liran Alon392b2f22018-06-23 02:35:01 +03004538 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004539
Liran Alon491a6032018-06-23 02:35:12 +03004540 if (shadow)
4541 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004542 return vmcs;
4543}
4544
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545static void free_vmcs(struct vmcs *vmcs)
4546{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004547 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548}
4549
Nadav Har'Eld462b812011-05-24 15:26:10 +03004550/*
4551 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4552 */
4553static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4554{
4555 if (!loaded_vmcs->vmcs)
4556 return;
4557 loaded_vmcs_clear(loaded_vmcs);
4558 free_vmcs(loaded_vmcs->vmcs);
4559 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004560 if (loaded_vmcs->msr_bitmap)
4561 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004562 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004563}
4564
Liran Alon491a6032018-06-23 02:35:12 +03004565static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004566{
Liran Alon491a6032018-06-23 02:35:12 +03004567 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004568}
4569
4570static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4571{
Liran Alon491a6032018-06-23 02:35:12 +03004572 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004573 if (!loaded_vmcs->vmcs)
4574 return -ENOMEM;
4575
4576 loaded_vmcs->shadow_vmcs = NULL;
4577 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004578
4579 if (cpu_has_vmx_msr_bitmap()) {
4580 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4581 if (!loaded_vmcs->msr_bitmap)
4582 goto out_vmcs;
4583 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004584
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004585 if (IS_ENABLED(CONFIG_HYPERV) &&
4586 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004587 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4588 struct hv_enlightened_vmcs *evmcs =
4589 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4590
4591 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4592 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004593 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004594
4595 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4596
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004597 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004598
4599out_vmcs:
4600 free_loaded_vmcs(loaded_vmcs);
4601 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004602}
4603
Sam Ravnborg39959582007-06-01 00:47:13 -07004604static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605{
4606 int cpu;
4607
Zachary Amsden3230bb42009-09-29 11:38:37 -10004608 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004610 per_cpu(vmxarea, cpu) = NULL;
4611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612}
4613
Jim Mattsond37f4262017-12-22 12:12:16 -08004614enum vmcs_field_width {
4615 VMCS_FIELD_WIDTH_U16 = 0,
4616 VMCS_FIELD_WIDTH_U64 = 1,
4617 VMCS_FIELD_WIDTH_U32 = 2,
4618 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004619};
4620
Jim Mattsond37f4262017-12-22 12:12:16 -08004621static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004622{
4623 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004624 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004625 return (field >> 13) & 0x3 ;
4626}
4627
4628static inline int vmcs_field_readonly(unsigned long field)
4629{
4630 return (((field >> 10) & 0x3) == 1);
4631}
4632
Bandan Dasfe2b2012014-04-21 15:20:14 -04004633static void init_vmcs_shadow_fields(void)
4634{
4635 int i, j;
4636
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004637 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4638 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004639 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004640 (i + 1 == max_shadow_read_only_fields ||
4641 shadow_read_only_fields[i + 1] != field + 1))
4642 pr_err("Missing field from shadow_read_only_field %x\n",
4643 field + 1);
4644
4645 clear_bit(field, vmx_vmread_bitmap);
4646#ifdef CONFIG_X86_64
4647 if (field & 1)
4648 continue;
4649#endif
4650 if (j < i)
4651 shadow_read_only_fields[j] = field;
4652 j++;
4653 }
4654 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004655
4656 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004657 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004658 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004659 (i + 1 == max_shadow_read_write_fields ||
4660 shadow_read_write_fields[i + 1] != field + 1))
4661 pr_err("Missing field from shadow_read_write_field %x\n",
4662 field + 1);
4663
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004664 /*
4665 * PML and the preemption timer can be emulated, but the
4666 * processor cannot vmwrite to fields that don't exist
4667 * on bare metal.
4668 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004669 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004670 case GUEST_PML_INDEX:
4671 if (!cpu_has_vmx_pml())
4672 continue;
4673 break;
4674 case VMX_PREEMPTION_TIMER_VALUE:
4675 if (!cpu_has_vmx_preemption_timer())
4676 continue;
4677 break;
4678 case GUEST_INTR_STATUS:
4679 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004680 continue;
4681 break;
4682 default:
4683 break;
4684 }
4685
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004686 clear_bit(field, vmx_vmwrite_bitmap);
4687 clear_bit(field, vmx_vmread_bitmap);
4688#ifdef CONFIG_X86_64
4689 if (field & 1)
4690 continue;
4691#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004692 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004693 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004694 j++;
4695 }
4696 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004697}
4698
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699static __init int alloc_kvm_area(void)
4700{
4701 int cpu;
4702
Zachary Amsden3230bb42009-09-29 11:38:37 -10004703 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 struct vmcs *vmcs;
4705
Liran Alon491a6032018-06-23 02:35:12 +03004706 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707 if (!vmcs) {
4708 free_kvm_area();
4709 return -ENOMEM;
4710 }
4711
Liran Alon2307af12018-06-29 22:59:04 +03004712 /*
4713 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4714 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4715 * revision_id reported by MSR_IA32_VMX_BASIC.
4716 *
4717 * However, even though not explictly documented by
4718 * TLFS, VMXArea passed as VMXON argument should
4719 * still be marked with revision_id reported by
4720 * physical CPU.
4721 */
4722 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004723 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004724
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725 per_cpu(vmxarea, cpu) = vmcs;
4726 }
4727 return 0;
4728}
4729
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004730static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004731 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004733 if (!emulate_invalid_guest_state) {
4734 /*
4735 * CS and SS RPL should be equal during guest entry according
4736 * to VMX spec, but in reality it is not always so. Since vcpu
4737 * is in the middle of the transition from real mode to
4738 * protected mode it is safe to assume that RPL 0 is a good
4739 * default value.
4740 */
4741 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004742 save->selector &= ~SEGMENT_RPL_MASK;
4743 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004744 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004746 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747}
4748
4749static void enter_pmode(struct kvm_vcpu *vcpu)
4750{
4751 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004752 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753
Gleb Natapovd99e4152012-12-20 16:57:45 +02004754 /*
4755 * Update real mode segment cache. It may be not up-to-date if sement
4756 * register was written while vcpu was in a guest mode.
4757 */
4758 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4759 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4760 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4761 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4762 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4763 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4764
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004765 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766
Avi Kivity2fb92db2011-04-27 19:42:18 +03004767 vmx_segment_cache_clear(vmx);
4768
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004769 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770
4771 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004772 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4773 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774 vmcs_writel(GUEST_RFLAGS, flags);
4775
Rusty Russell66aee912007-07-17 23:34:16 +10004776 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4777 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778
4779 update_exception_bitmap(vcpu);
4780
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004781 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4782 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4783 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4784 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4785 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4786 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004787}
4788
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004789static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790{
Mathias Krause772e0312012-08-30 01:30:19 +02004791 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004792 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793
Gleb Natapovd99e4152012-12-20 16:57:45 +02004794 var.dpl = 0x3;
4795 if (seg == VCPU_SREG_CS)
4796 var.type = 0x3;
4797
4798 if (!emulate_invalid_guest_state) {
4799 var.selector = var.base >> 4;
4800 var.base = var.base & 0xffff0;
4801 var.limit = 0xffff;
4802 var.g = 0;
4803 var.db = 0;
4804 var.present = 1;
4805 var.s = 1;
4806 var.l = 0;
4807 var.unusable = 0;
4808 var.type = 0x3;
4809 var.avl = 0;
4810 if (save->base & 0xf)
4811 printk_once(KERN_WARNING "kvm: segment base is not "
4812 "paragraph aligned when entering "
4813 "protected mode (seg=%d)", seg);
4814 }
4815
4816 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004817 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004818 vmcs_write32(sf->limit, var.limit);
4819 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820}
4821
4822static void enter_rmode(struct kvm_vcpu *vcpu)
4823{
4824 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004825 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004826 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004828 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4829 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4830 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4831 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4832 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004833 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4834 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004835
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004836 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004837
Gleb Natapov776e58e2011-03-13 12:34:27 +02004838 /*
4839 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004840 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004841 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004842 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004843 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4844 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004845
Avi Kivity2fb92db2011-04-27 19:42:18 +03004846 vmx_segment_cache_clear(vmx);
4847
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004848 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4851
4852 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004853 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004854
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004855 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856
4857 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004858 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004859 update_exception_bitmap(vcpu);
4860
Gleb Natapovd99e4152012-12-20 16:57:45 +02004861 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4862 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4863 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4864 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4865 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4866 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004867
Eddie Dong8668a3c2007-10-10 14:26:45 +08004868 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869}
4870
Amit Shah401d10d2009-02-20 22:53:37 +05304871static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4872{
4873 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004874 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4875
4876 if (!msr)
4877 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304878
Avi Kivity44ea2b12009-09-06 15:55:37 +03004879 /*
Sean Christopherson678e3152018-07-23 12:32:43 -07004880 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
4881 * 64-bit mode as a 64-bit kernel may frequently access the
4882 * MSR. This means we need to manually save/restore the MSR
4883 * when switching between guest and host state, but only if
4884 * the guest is in 64-bit mode. Sync our cached value if the
4885 * guest is transitioning to 32-bit mode and the CPU contains
4886 * guest state, i.e. the cache is stale.
Avi Kivity44ea2b12009-09-06 15:55:37 +03004887 */
Sean Christopherson678e3152018-07-23 12:32:43 -07004888#ifdef CONFIG_X86_64
4889 if (!(efer & EFER_LMA))
4890 (void)vmx_read_guest_kernel_gs_base(vmx);
4891#endif
Avi Kivityf6801df2010-01-21 15:31:50 +02004892 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304893 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004894 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304895 msr->data = efer;
4896 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004897 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304898
4899 msr->data = efer & ~EFER_LME;
4900 }
4901 setup_msrs(vmx);
4902}
4903
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004904#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004905
4906static void enter_lmode(struct kvm_vcpu *vcpu)
4907{
4908 u32 guest_tr_ar;
4909
Avi Kivity2fb92db2011-04-27 19:42:18 +03004910 vmx_segment_cache_clear(to_vmx(vcpu));
4911
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004913 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004914 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4915 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004917 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4918 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919 }
Avi Kivityda38f432010-07-06 11:30:49 +03004920 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921}
4922
4923static void exit_lmode(struct kvm_vcpu *vcpu)
4924{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004925 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004926 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004927}
4928
4929#endif
4930
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004931static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4932 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004933{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004934 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004935 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4936 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004937 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004938 } else {
4939 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004940 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004941}
4942
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004943static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004944{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004945 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004946}
4947
Junaid Shahidfaff8752018-06-29 13:10:05 -07004948static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4949{
4950 int vpid = to_vmx(vcpu)->vpid;
4951
4952 if (!vpid_sync_vcpu_addr(vpid, addr))
4953 vpid_sync_context(vpid);
4954
4955 /*
4956 * If VPIDs are not supported or enabled, then the above is a no-op.
4957 * But we don't really need a TLB flush in that case anyway, because
4958 * each VM entry/exit includes an implicit flush when VPID is 0.
4959 */
4960}
4961
Avi Kivitye8467fd2009-12-29 18:43:06 +02004962static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4963{
4964 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4965
4966 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4967 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4968}
4969
Avi Kivityaff48ba2010-12-05 18:56:11 +02004970static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4971{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004972 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004973 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4974 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4975}
4976
Anthony Liguori25c4c272007-04-27 09:29:21 +03004977static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004978{
Avi Kivityfc78f512009-12-07 12:16:48 +02004979 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4980
4981 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4982 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004983}
4984
Sheng Yang14394422008-04-28 12:24:45 +08004985static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4986{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004987 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4988
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004989 if (!test_bit(VCPU_EXREG_PDPTR,
4990 (unsigned long *)&vcpu->arch.regs_dirty))
4991 return;
4992
Sheng Yang14394422008-04-28 12:24:45 +08004993 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004994 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4995 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4996 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4997 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004998 }
4999}
5000
Avi Kivity8f5d5492009-05-31 18:41:29 +03005001static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5002{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005003 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5004
Avi Kivity8f5d5492009-05-31 18:41:29 +03005005 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005006 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5007 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5008 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5009 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005010 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005011
5012 __set_bit(VCPU_EXREG_PDPTR,
5013 (unsigned long *)&vcpu->arch.regs_avail);
5014 __set_bit(VCPU_EXREG_PDPTR,
5015 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005016}
5017
David Matlack38991522016-11-29 18:14:08 -08005018static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5019{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005020 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5021 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005022 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5023
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005024 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005025 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5026 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5027 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5028
5029 return fixed_bits_valid(val, fixed0, fixed1);
5030}
5031
5032static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5033{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005034 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5035 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005036
5037 return fixed_bits_valid(val, fixed0, fixed1);
5038}
5039
5040static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5041{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005042 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5043 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005044
5045 return fixed_bits_valid(val, fixed0, fixed1);
5046}
5047
5048/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5049#define nested_guest_cr4_valid nested_cr4_valid
5050#define nested_host_cr4_valid nested_cr4_valid
5051
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005052static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005053
5054static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5055 unsigned long cr0,
5056 struct kvm_vcpu *vcpu)
5057{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005058 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5059 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005060 if (!(cr0 & X86_CR0_PG)) {
5061 /* From paging/starting to nonpaging */
5062 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005063 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005064 (CPU_BASED_CR3_LOAD_EXITING |
5065 CPU_BASED_CR3_STORE_EXITING));
5066 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005067 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005068 } else if (!is_paging(vcpu)) {
5069 /* From nonpaging to paging */
5070 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005071 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005072 ~(CPU_BASED_CR3_LOAD_EXITING |
5073 CPU_BASED_CR3_STORE_EXITING));
5074 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005075 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005076 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005077
5078 if (!(cr0 & X86_CR0_WP))
5079 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005080}
5081
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5083{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005084 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005085 unsigned long hw_cr0;
5086
Gleb Natapov50378782013-02-04 16:00:28 +02005087 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005088 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005089 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005090 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005091 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005092
Gleb Natapov218e7632013-01-21 15:36:45 +02005093 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5094 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095
Gleb Natapov218e7632013-01-21 15:36:45 +02005096 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5097 enter_rmode(vcpu);
5098 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005099
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005100#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005101 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005102 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005103 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005104 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005105 exit_lmode(vcpu);
5106 }
5107#endif
5108
Sean Christophersonb4d18512018-03-05 12:04:40 -08005109 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005110 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5111
Avi Kivity6aa8b732006-12-10 02:21:36 -08005112 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005113 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005114 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005115
5116 /* depends on vcpu->arch.cr0 to be set to a new value */
5117 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005118}
5119
Yu Zhang855feb62017-08-24 20:27:55 +08005120static int get_ept_level(struct kvm_vcpu *vcpu)
5121{
5122 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5123 return 5;
5124 return 4;
5125}
5126
Peter Feiner995f00a2017-06-30 17:26:32 -07005127static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005128{
Yu Zhang855feb62017-08-24 20:27:55 +08005129 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005130
Yu Zhang855feb62017-08-24 20:27:55 +08005131 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005132
Peter Feiner995f00a2017-06-30 17:26:32 -07005133 if (enable_ept_ad_bits &&
5134 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005135 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005136 eptp |= (root_hpa & PAGE_MASK);
5137
5138 return eptp;
5139}
5140
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5142{
Tianyu Lan877ad952018-07-19 08:40:23 +00005143 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005144 unsigned long guest_cr3;
5145 u64 eptp;
5146
5147 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005148 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005149 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005150 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005151
5152 if (kvm_x86_ops->tlb_remote_flush) {
5153 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5154 to_vmx(vcpu)->ept_pointer = eptp;
5155 to_kvm_vmx(kvm)->ept_pointers_match
5156 = EPT_POINTERS_CHECK;
5157 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5158 }
5159
Sean Christophersone90008d2018-03-05 12:04:37 -08005160 if (enable_unrestricted_guest || is_paging(vcpu) ||
5161 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005162 guest_cr3 = kvm_read_cr3(vcpu);
5163 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005164 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005165 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005166 }
5167
Sheng Yang14394422008-04-28 12:24:45 +08005168 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005169}
5170
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005171static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005173 /*
5174 * Pass through host's Machine Check Enable value to hw_cr4, which
5175 * is in force while we are in guest mode. Do not let guests control
5176 * this bit, even if host CR4.MCE == 0.
5177 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005178 unsigned long hw_cr4;
5179
5180 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5181 if (enable_unrestricted_guest)
5182 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5183 else if (to_vmx(vcpu)->rmode.vm86_active)
5184 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5185 else
5186 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005187
Sean Christopherson64f7a112018-04-30 10:01:06 -07005188 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5189 if (cr4 & X86_CR4_UMIP) {
5190 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005191 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005192 hw_cr4 &= ~X86_CR4_UMIP;
5193 } else if (!is_guest_mode(vcpu) ||
5194 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5195 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5196 SECONDARY_EXEC_DESC);
5197 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005198
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005199 if (cr4 & X86_CR4_VMXE) {
5200 /*
5201 * To use VMXON (and later other VMX instructions), a guest
5202 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5203 * So basically the check on whether to allow nested VMX
5204 * is here.
5205 */
5206 if (!nested_vmx_allowed(vcpu))
5207 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005208 }
David Matlack38991522016-11-29 18:14:08 -08005209
5210 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005211 return 1;
5212
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005213 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005214
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005215 if (!enable_unrestricted_guest) {
5216 if (enable_ept) {
5217 if (!is_paging(vcpu)) {
5218 hw_cr4 &= ~X86_CR4_PAE;
5219 hw_cr4 |= X86_CR4_PSE;
5220 } else if (!(cr4 & X86_CR4_PAE)) {
5221 hw_cr4 &= ~X86_CR4_PAE;
5222 }
5223 }
5224
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005225 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005226 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5227 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5228 * to be manually disabled when guest switches to non-paging
5229 * mode.
5230 *
5231 * If !enable_unrestricted_guest, the CPU is always running
5232 * with CR0.PG=1 and CR4 needs to be modified.
5233 * If enable_unrestricted_guest, the CPU automatically
5234 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005235 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005236 if (!is_paging(vcpu))
5237 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5238 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005239
Sheng Yang14394422008-04-28 12:24:45 +08005240 vmcs_writel(CR4_READ_SHADOW, cr4);
5241 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005242 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243}
5244
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245static void vmx_get_segment(struct kvm_vcpu *vcpu,
5246 struct kvm_segment *var, int seg)
5247{
Avi Kivitya9179492011-01-03 14:28:52 +02005248 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005249 u32 ar;
5250
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005251 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005252 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005253 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005254 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005255 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005256 var->base = vmx_read_guest_seg_base(vmx, seg);
5257 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5258 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005259 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005260 var->base = vmx_read_guest_seg_base(vmx, seg);
5261 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5262 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5263 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005264 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005265 var->type = ar & 15;
5266 var->s = (ar >> 4) & 1;
5267 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005268 /*
5269 * Some userspaces do not preserve unusable property. Since usable
5270 * segment has to be present according to VMX spec we can use present
5271 * property to amend userspace bug by making unusable segment always
5272 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5273 * segment as unusable.
5274 */
5275 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005276 var->avl = (ar >> 12) & 1;
5277 var->l = (ar >> 13) & 1;
5278 var->db = (ar >> 14) & 1;
5279 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005280}
5281
Avi Kivitya9179492011-01-03 14:28:52 +02005282static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5283{
Avi Kivitya9179492011-01-03 14:28:52 +02005284 struct kvm_segment s;
5285
5286 if (to_vmx(vcpu)->rmode.vm86_active) {
5287 vmx_get_segment(vcpu, &s, seg);
5288 return s.base;
5289 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005290 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005291}
5292
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005293static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005294{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005295 struct vcpu_vmx *vmx = to_vmx(vcpu);
5296
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005297 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005298 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005299 else {
5300 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005301 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005302 }
Avi Kivity69c73022011-03-07 15:26:44 +02005303}
5304
Avi Kivity653e3102007-05-07 10:55:37 +03005305static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005306{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307 u32 ar;
5308
Avi Kivityf0495f92012-06-07 17:06:10 +03005309 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310 ar = 1 << 16;
5311 else {
5312 ar = var->type & 15;
5313 ar |= (var->s & 1) << 4;
5314 ar |= (var->dpl & 3) << 5;
5315 ar |= (var->present & 1) << 7;
5316 ar |= (var->avl & 1) << 12;
5317 ar |= (var->l & 1) << 13;
5318 ar |= (var->db & 1) << 14;
5319 ar |= (var->g & 1) << 15;
5320 }
Avi Kivity653e3102007-05-07 10:55:37 +03005321
5322 return ar;
5323}
5324
5325static void vmx_set_segment(struct kvm_vcpu *vcpu,
5326 struct kvm_segment *var, int seg)
5327{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005328 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005329 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005330
Avi Kivity2fb92db2011-04-27 19:42:18 +03005331 vmx_segment_cache_clear(vmx);
5332
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005333 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5334 vmx->rmode.segs[seg] = *var;
5335 if (seg == VCPU_SREG_TR)
5336 vmcs_write16(sf->selector, var->selector);
5337 else if (var->s)
5338 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005339 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005340 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005341
Avi Kivity653e3102007-05-07 10:55:37 +03005342 vmcs_writel(sf->base, var->base);
5343 vmcs_write32(sf->limit, var->limit);
5344 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005345
5346 /*
5347 * Fix the "Accessed" bit in AR field of segment registers for older
5348 * qemu binaries.
5349 * IA32 arch specifies that at the time of processor reset the
5350 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005351 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005352 * state vmexit when "unrestricted guest" mode is turned on.
5353 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5354 * tree. Newer qemu binaries with that qemu fix would not need this
5355 * kvm hack.
5356 */
5357 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005358 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005359
Gleb Natapovf924d662012-12-12 19:10:55 +02005360 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005361
5362out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005363 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005364}
5365
Avi Kivity6aa8b732006-12-10 02:21:36 -08005366static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5367{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005368 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369
5370 *db = (ar >> 14) & 1;
5371 *l = (ar >> 13) & 1;
5372}
5373
Gleb Natapov89a27f42010-02-16 10:51:48 +02005374static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005375{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005376 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5377 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005378}
5379
Gleb Natapov89a27f42010-02-16 10:51:48 +02005380static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005381{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005382 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5383 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384}
5385
Gleb Natapov89a27f42010-02-16 10:51:48 +02005386static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005388 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5389 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390}
5391
Gleb Natapov89a27f42010-02-16 10:51:48 +02005392static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005393{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005394 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5395 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005396}
5397
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005398static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5399{
5400 struct kvm_segment var;
5401 u32 ar;
5402
5403 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005404 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005405 if (seg == VCPU_SREG_CS)
5406 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005407 ar = vmx_segment_access_rights(&var);
5408
5409 if (var.base != (var.selector << 4))
5410 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005411 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005412 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005413 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005414 return false;
5415
5416 return true;
5417}
5418
5419static bool code_segment_valid(struct kvm_vcpu *vcpu)
5420{
5421 struct kvm_segment cs;
5422 unsigned int cs_rpl;
5423
5424 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005425 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005426
Avi Kivity1872a3f2009-01-04 23:26:52 +02005427 if (cs.unusable)
5428 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005429 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005430 return false;
5431 if (!cs.s)
5432 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005433 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005434 if (cs.dpl > cs_rpl)
5435 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005436 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005437 if (cs.dpl != cs_rpl)
5438 return false;
5439 }
5440 if (!cs.present)
5441 return false;
5442
5443 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5444 return true;
5445}
5446
5447static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5448{
5449 struct kvm_segment ss;
5450 unsigned int ss_rpl;
5451
5452 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005453 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005454
Avi Kivity1872a3f2009-01-04 23:26:52 +02005455 if (ss.unusable)
5456 return true;
5457 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005458 return false;
5459 if (!ss.s)
5460 return false;
5461 if (ss.dpl != ss_rpl) /* DPL != RPL */
5462 return false;
5463 if (!ss.present)
5464 return false;
5465
5466 return true;
5467}
5468
5469static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5470{
5471 struct kvm_segment var;
5472 unsigned int rpl;
5473
5474 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005475 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005476
Avi Kivity1872a3f2009-01-04 23:26:52 +02005477 if (var.unusable)
5478 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005479 if (!var.s)
5480 return false;
5481 if (!var.present)
5482 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005483 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005484 if (var.dpl < rpl) /* DPL < RPL */
5485 return false;
5486 }
5487
5488 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5489 * rights flags
5490 */
5491 return true;
5492}
5493
5494static bool tr_valid(struct kvm_vcpu *vcpu)
5495{
5496 struct kvm_segment tr;
5497
5498 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5499
Avi Kivity1872a3f2009-01-04 23:26:52 +02005500 if (tr.unusable)
5501 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005502 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005503 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005504 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005505 return false;
5506 if (!tr.present)
5507 return false;
5508
5509 return true;
5510}
5511
5512static bool ldtr_valid(struct kvm_vcpu *vcpu)
5513{
5514 struct kvm_segment ldtr;
5515
5516 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5517
Avi Kivity1872a3f2009-01-04 23:26:52 +02005518 if (ldtr.unusable)
5519 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005520 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005521 return false;
5522 if (ldtr.type != 2)
5523 return false;
5524 if (!ldtr.present)
5525 return false;
5526
5527 return true;
5528}
5529
5530static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5531{
5532 struct kvm_segment cs, ss;
5533
5534 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5535 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5536
Nadav Amitb32a9912015-03-29 16:33:04 +03005537 return ((cs.selector & SEGMENT_RPL_MASK) ==
5538 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005539}
5540
5541/*
5542 * Check if guest state is valid. Returns true if valid, false if
5543 * not.
5544 * We assume that registers are always usable
5545 */
5546static bool guest_state_valid(struct kvm_vcpu *vcpu)
5547{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005548 if (enable_unrestricted_guest)
5549 return true;
5550
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005551 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005552 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005553 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5554 return false;
5555 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5556 return false;
5557 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5558 return false;
5559 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5560 return false;
5561 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5562 return false;
5563 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5564 return false;
5565 } else {
5566 /* protected mode guest state checks */
5567 if (!cs_ss_rpl_check(vcpu))
5568 return false;
5569 if (!code_segment_valid(vcpu))
5570 return false;
5571 if (!stack_segment_valid(vcpu))
5572 return false;
5573 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5574 return false;
5575 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5576 return false;
5577 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5578 return false;
5579 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5580 return false;
5581 if (!tr_valid(vcpu))
5582 return false;
5583 if (!ldtr_valid(vcpu))
5584 return false;
5585 }
5586 /* TODO:
5587 * - Add checks on RIP
5588 * - Add checks on RFLAGS
5589 */
5590
5591 return true;
5592}
5593
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005594static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5595{
5596 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5597}
5598
Mike Dayd77c26f2007-10-08 09:02:08 -04005599static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005600{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005601 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005602 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005603 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005605 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005606 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005607 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5608 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005609 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005610 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005611 r = kvm_write_guest_page(kvm, fn++, &data,
5612 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005613 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005614 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005615 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5616 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005617 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005618 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5619 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005620 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005621 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005622 r = kvm_write_guest_page(kvm, fn, &data,
5623 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5624 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005625out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005626 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005627 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005628}
5629
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005630static int init_rmode_identity_map(struct kvm *kvm)
5631{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005632 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005633 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005634 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005635 u32 tmp;
5636
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005637 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005638 mutex_lock(&kvm->slots_lock);
5639
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005640 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005641 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005642
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005643 if (!kvm_vmx->ept_identity_map_addr)
5644 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5645 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005646
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005647 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005648 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005649 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005650 goto out2;
5651
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005652 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005653 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5654 if (r < 0)
5655 goto out;
5656 /* Set up identity-mapping pagetable for EPT in real mode */
5657 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5658 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5659 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5660 r = kvm_write_guest_page(kvm, identity_map_pfn,
5661 &tmp, i * sizeof(tmp), sizeof(tmp));
5662 if (r < 0)
5663 goto out;
5664 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005665 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005666
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005667out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005668 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005669
5670out2:
5671 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005672 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005673}
5674
Avi Kivity6aa8b732006-12-10 02:21:36 -08005675static void seg_setup(int seg)
5676{
Mathias Krause772e0312012-08-30 01:30:19 +02005677 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005678 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005679
5680 vmcs_write16(sf->selector, 0);
5681 vmcs_writel(sf->base, 0);
5682 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005683 ar = 0x93;
5684 if (seg == VCPU_SREG_CS)
5685 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005686
5687 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688}
5689
Sheng Yangf78e0e22007-10-29 09:40:42 +08005690static int alloc_apic_access_page(struct kvm *kvm)
5691{
Xiao Guangrong44841412012-09-07 14:14:20 +08005692 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005693 int r = 0;
5694
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005695 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005696 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005697 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005698 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5699 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005700 if (r)
5701 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005702
Tang Chen73a6d942014-09-11 13:38:00 +08005703 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005704 if (is_error_page(page)) {
5705 r = -EFAULT;
5706 goto out;
5707 }
5708
Tang Chenc24ae0d2014-09-24 15:57:58 +08005709 /*
5710 * Do not pin the page in memory, so that memory hot-unplug
5711 * is able to migrate it.
5712 */
5713 put_page(page);
5714 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005715out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005716 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005717 return r;
5718}
5719
Wanpeng Li991e7a02015-09-16 17:30:05 +08005720static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005721{
5722 int vpid;
5723
Avi Kivity919818a2009-03-23 18:01:29 +02005724 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005725 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005726 spin_lock(&vmx_vpid_lock);
5727 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005728 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005729 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005730 else
5731 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005732 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005733 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005734}
5735
Wanpeng Li991e7a02015-09-16 17:30:05 +08005736static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005737{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005738 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005739 return;
5740 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005741 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005742 spin_unlock(&vmx_vpid_lock);
5743}
5744
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005745static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5746 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005747{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005748 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005749
5750 if (!cpu_has_vmx_msr_bitmap())
5751 return;
5752
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005753 if (static_branch_unlikely(&enable_evmcs))
5754 evmcs_touch_msr_bitmap();
5755
Sheng Yang25c5f222008-03-28 13:18:56 +08005756 /*
5757 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5758 * have the write-low and read-high bitmap offsets the wrong way round.
5759 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5760 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005761 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005762 if (type & MSR_TYPE_R)
5763 /* read-low */
5764 __clear_bit(msr, msr_bitmap + 0x000 / f);
5765
5766 if (type & MSR_TYPE_W)
5767 /* write-low */
5768 __clear_bit(msr, msr_bitmap + 0x800 / f);
5769
Sheng Yang25c5f222008-03-28 13:18:56 +08005770 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5771 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005772 if (type & MSR_TYPE_R)
5773 /* read-high */
5774 __clear_bit(msr, msr_bitmap + 0x400 / f);
5775
5776 if (type & MSR_TYPE_W)
5777 /* write-high */
5778 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5779
5780 }
5781}
5782
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005783static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5784 u32 msr, int type)
5785{
5786 int f = sizeof(unsigned long);
5787
5788 if (!cpu_has_vmx_msr_bitmap())
5789 return;
5790
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005791 if (static_branch_unlikely(&enable_evmcs))
5792 evmcs_touch_msr_bitmap();
5793
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005794 /*
5795 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5796 * have the write-low and read-high bitmap offsets the wrong way round.
5797 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5798 */
5799 if (msr <= 0x1fff) {
5800 if (type & MSR_TYPE_R)
5801 /* read-low */
5802 __set_bit(msr, msr_bitmap + 0x000 / f);
5803
5804 if (type & MSR_TYPE_W)
5805 /* write-low */
5806 __set_bit(msr, msr_bitmap + 0x800 / f);
5807
5808 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5809 msr &= 0x1fff;
5810 if (type & MSR_TYPE_R)
5811 /* read-high */
5812 __set_bit(msr, msr_bitmap + 0x400 / f);
5813
5814 if (type & MSR_TYPE_W)
5815 /* write-high */
5816 __set_bit(msr, msr_bitmap + 0xc00 / f);
5817
5818 }
5819}
5820
5821static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5822 u32 msr, int type, bool value)
5823{
5824 if (value)
5825 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5826 else
5827 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5828}
5829
Wincy Vanf2b93282015-02-03 23:56:03 +08005830/*
5831 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5832 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5833 */
5834static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5835 unsigned long *msr_bitmap_nested,
5836 u32 msr, int type)
5837{
5838 int f = sizeof(unsigned long);
5839
Wincy Vanf2b93282015-02-03 23:56:03 +08005840 /*
5841 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5842 * have the write-low and read-high bitmap offsets the wrong way round.
5843 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5844 */
5845 if (msr <= 0x1fff) {
5846 if (type & MSR_TYPE_R &&
5847 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5848 /* read-low */
5849 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5850
5851 if (type & MSR_TYPE_W &&
5852 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5853 /* write-low */
5854 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5855
5856 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5857 msr &= 0x1fff;
5858 if (type & MSR_TYPE_R &&
5859 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5860 /* read-high */
5861 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5862
5863 if (type & MSR_TYPE_W &&
5864 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5865 /* write-high */
5866 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5867
5868 }
5869}
5870
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005871static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005872{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005873 u8 mode = 0;
5874
5875 if (cpu_has_secondary_exec_ctrls() &&
5876 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5877 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5878 mode |= MSR_BITMAP_MODE_X2APIC;
5879 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5880 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5881 }
5882
5883 if (is_long_mode(vcpu))
5884 mode |= MSR_BITMAP_MODE_LM;
5885
5886 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005887}
5888
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005889#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5890
5891static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5892 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005893{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005894 int msr;
5895
5896 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5897 unsigned word = msr / BITS_PER_LONG;
5898 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5899 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005900 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005901
5902 if (mode & MSR_BITMAP_MODE_X2APIC) {
5903 /*
5904 * TPR reads and writes can be virtualized even if virtual interrupt
5905 * delivery is not in use.
5906 */
5907 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5908 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5909 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5910 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5911 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5912 }
5913 }
5914}
5915
5916static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5917{
5918 struct vcpu_vmx *vmx = to_vmx(vcpu);
5919 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5920 u8 mode = vmx_msr_bitmap_mode(vcpu);
5921 u8 changed = mode ^ vmx->msr_bitmap_mode;
5922
5923 if (!changed)
5924 return;
5925
5926 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5927 !(mode & MSR_BITMAP_MODE_LM));
5928
5929 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5930 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5931
5932 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005933}
5934
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005935static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005936{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005937 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005938}
5939
David Matlackc9f04402017-08-01 14:00:40 -07005940static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5941{
5942 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5943 gfn_t gfn;
5944
5945 /*
5946 * Don't need to mark the APIC access page dirty; it is never
5947 * written to by the CPU during APIC virtualization.
5948 */
5949
5950 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5951 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5952 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5953 }
5954
5955 if (nested_cpu_has_posted_intr(vmcs12)) {
5956 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5957 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5958 }
5959}
5960
5961
David Hildenbrand6342c502017-01-25 11:58:58 +01005962static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005963{
5964 struct vcpu_vmx *vmx = to_vmx(vcpu);
5965 int max_irr;
5966 void *vapic_page;
5967 u16 status;
5968
David Matlackc9f04402017-08-01 14:00:40 -07005969 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5970 return;
Wincy Van705699a2015-02-03 23:58:17 +08005971
David Matlackc9f04402017-08-01 14:00:40 -07005972 vmx->nested.pi_pending = false;
5973 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5974 return;
Wincy Van705699a2015-02-03 23:58:17 +08005975
David Matlackc9f04402017-08-01 14:00:40 -07005976 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5977 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005978 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005979 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5980 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005981 kunmap(vmx->nested.virtual_apic_page);
5982
5983 status = vmcs_read16(GUEST_INTR_STATUS);
5984 if ((u8)max_irr > ((u8)status & 0xff)) {
5985 status &= ~0xff;
5986 status |= (u8)max_irr;
5987 vmcs_write16(GUEST_INTR_STATUS, status);
5988 }
5989 }
David Matlackc9f04402017-08-01 14:00:40 -07005990
5991 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005992}
5993
Wincy Van06a55242017-04-28 13:13:59 +08005994static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5995 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005996{
5997#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005998 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5999
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006000 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006001 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006002 * The vector of interrupt to be delivered to vcpu had
6003 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006004 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006005 * Following cases will be reached in this block, and
6006 * we always send a notification event in all cases as
6007 * explained below.
6008 *
6009 * Case 1: vcpu keeps in non-root mode. Sending a
6010 * notification event posts the interrupt to vcpu.
6011 *
6012 * Case 2: vcpu exits to root mode and is still
6013 * runnable. PIR will be synced to vIRR before the
6014 * next vcpu entry. Sending a notification event in
6015 * this case has no effect, as vcpu is not in root
6016 * mode.
6017 *
6018 * Case 3: vcpu exits to root mode and is blocked.
6019 * vcpu_block() has already synced PIR to vIRR and
6020 * never blocks vcpu if vIRR is not cleared. Therefore,
6021 * a blocked vcpu here does not wait for any requested
6022 * interrupts in PIR, and sending a notification event
6023 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006024 */
Feng Wu28b835d2015-09-18 22:29:54 +08006025
Wincy Van06a55242017-04-28 13:13:59 +08006026 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006027 return true;
6028 }
6029#endif
6030 return false;
6031}
6032
Wincy Van705699a2015-02-03 23:58:17 +08006033static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6034 int vector)
6035{
6036 struct vcpu_vmx *vmx = to_vmx(vcpu);
6037
6038 if (is_guest_mode(vcpu) &&
6039 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006040 /*
6041 * If a posted intr is not recognized by hardware,
6042 * we will accomplish it in the next vmentry.
6043 */
6044 vmx->nested.pi_pending = true;
6045 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006046 /* the PIR and ON have been set by L1. */
6047 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6048 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006049 return 0;
6050 }
6051 return -1;
6052}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006053/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006054 * Send interrupt to vcpu via posted interrupt way.
6055 * 1. If target vcpu is running(non-root mode), send posted interrupt
6056 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6057 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6058 * interrupt from PIR in next vmentry.
6059 */
6060static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6061{
6062 struct vcpu_vmx *vmx = to_vmx(vcpu);
6063 int r;
6064
Wincy Van705699a2015-02-03 23:58:17 +08006065 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6066 if (!r)
6067 return;
6068
Yang Zhanga20ed542013-04-11 19:25:15 +08006069 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6070 return;
6071
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006072 /* If a previous notification has sent the IPI, nothing to do. */
6073 if (pi_test_and_set_on(&vmx->pi_desc))
6074 return;
6075
Wincy Van06a55242017-04-28 13:13:59 +08006076 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006077 kvm_vcpu_kick(vcpu);
6078}
6079
Avi Kivity6aa8b732006-12-10 02:21:36 -08006080/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006081 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6082 * will not change in the lifetime of the guest.
6083 * Note that host-state that does change is set elsewhere. E.g., host-state
6084 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6085 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006086static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006087{
6088 u32 low32, high32;
6089 unsigned long tmpl;
6090 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006091 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006092
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006093 cr0 = read_cr0();
6094 WARN_ON(cr0 & X86_CR0_TS);
6095 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006096
6097 /*
6098 * Save the most likely value for this task's CR3 in the VMCS.
6099 * We can't use __get_current_cr3_fast() because we're not atomic.
6100 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006101 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006102 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006103 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006104
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006105 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006106 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006107 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006108 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006109
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006110 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006111#ifdef CONFIG_X86_64
6112 /*
6113 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006114 * vmx_prepare_switch_to_host(), in case userspace uses
6115 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006116 */
6117 vmcs_write16(HOST_DS_SELECTOR, 0);
6118 vmcs_write16(HOST_ES_SELECTOR, 0);
6119#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006120 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6121 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006122#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006123 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6124 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6125
Juergen Gross87930012017-09-04 12:25:27 +02006126 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006127 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006128 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006129
Avi Kivity83287ea422012-09-16 15:10:57 +03006130 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006131
6132 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6133 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6134 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6135 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6136
6137 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6138 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6139 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6140 }
6141}
6142
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006143static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6144{
6145 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6146 if (enable_ept)
6147 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006148 if (is_guest_mode(&vmx->vcpu))
6149 vmx->vcpu.arch.cr4_guest_owned_bits &=
6150 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006151 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6152}
6153
Yang Zhang01e439b2013-04-11 19:25:12 +08006154static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6155{
6156 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6157
Andrey Smetanind62caab2015-11-10 15:36:33 +03006158 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006159 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006160
6161 if (!enable_vnmi)
6162 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6163
Yunhong Jiang64672c92016-06-13 14:19:59 -07006164 /* Enable the preemption timer dynamically */
6165 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006166 return pin_based_exec_ctrl;
6167}
6168
Andrey Smetanind62caab2015-11-10 15:36:33 +03006169static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6170{
6171 struct vcpu_vmx *vmx = to_vmx(vcpu);
6172
6173 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006174 if (cpu_has_secondary_exec_ctrls()) {
6175 if (kvm_vcpu_apicv_active(vcpu))
6176 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6177 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6178 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6179 else
6180 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6181 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6182 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6183 }
6184
6185 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006186 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006187}
6188
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006189static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6190{
6191 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006192
6193 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6194 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6195
Paolo Bonzini35754c92015-07-29 12:05:37 +02006196 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006197 exec_control &= ~CPU_BASED_TPR_SHADOW;
6198#ifdef CONFIG_X86_64
6199 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6200 CPU_BASED_CR8_LOAD_EXITING;
6201#endif
6202 }
6203 if (!enable_ept)
6204 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6205 CPU_BASED_CR3_LOAD_EXITING |
6206 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006207 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6208 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6209 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006210 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6211 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006212 return exec_control;
6213}
6214
Jim Mattson45ec3682017-08-23 16:32:04 -07006215static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006216{
Jim Mattson45ec3682017-08-23 16:32:04 -07006217 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006218 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006219}
6220
Jim Mattson75f4fc82017-08-23 16:32:03 -07006221static bool vmx_rdseed_supported(void)
6222{
6223 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006224 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006225}
6226
Paolo Bonzini80154d72017-08-24 13:55:35 +02006227static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006228{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006229 struct kvm_vcpu *vcpu = &vmx->vcpu;
6230
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006231 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006232
Paolo Bonzini80154d72017-08-24 13:55:35 +02006233 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006234 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6235 if (vmx->vpid == 0)
6236 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6237 if (!enable_ept) {
6238 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6239 enable_unrestricted_guest = 0;
6240 }
6241 if (!enable_unrestricted_guest)
6242 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006243 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006244 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006245 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006246 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6247 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006248 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006249
6250 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6251 * in vmx_set_cr4. */
6252 exec_control &= ~SECONDARY_EXEC_DESC;
6253
Abel Gordonabc4fc52013-04-18 14:35:25 +03006254 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6255 (handle_vmptrld).
6256 We can NOT enable shadow_vmcs here because we don't have yet
6257 a current VMCS12
6258 */
6259 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006260
6261 if (!enable_pml)
6262 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006263
Paolo Bonzini3db13482017-08-24 14:48:03 +02006264 if (vmx_xsaves_supported()) {
6265 /* Exposing XSAVES only when XSAVE is exposed */
6266 bool xsaves_enabled =
6267 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6268 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6269
6270 if (!xsaves_enabled)
6271 exec_control &= ~SECONDARY_EXEC_XSAVES;
6272
6273 if (nested) {
6274 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006275 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006276 SECONDARY_EXEC_XSAVES;
6277 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006278 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006279 ~SECONDARY_EXEC_XSAVES;
6280 }
6281 }
6282
Paolo Bonzini80154d72017-08-24 13:55:35 +02006283 if (vmx_rdtscp_supported()) {
6284 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6285 if (!rdtscp_enabled)
6286 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6287
6288 if (nested) {
6289 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006290 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006291 SECONDARY_EXEC_RDTSCP;
6292 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006293 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006294 ~SECONDARY_EXEC_RDTSCP;
6295 }
6296 }
6297
6298 if (vmx_invpcid_supported()) {
6299 /* Exposing INVPCID only when PCID is exposed */
6300 bool invpcid_enabled =
6301 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6302 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6303
6304 if (!invpcid_enabled) {
6305 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6306 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6307 }
6308
6309 if (nested) {
6310 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006311 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006312 SECONDARY_EXEC_ENABLE_INVPCID;
6313 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006314 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006315 ~SECONDARY_EXEC_ENABLE_INVPCID;
6316 }
6317 }
6318
Jim Mattson45ec3682017-08-23 16:32:04 -07006319 if (vmx_rdrand_supported()) {
6320 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6321 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006322 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006323
6324 if (nested) {
6325 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006326 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006327 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006328 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006329 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006330 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006331 }
6332 }
6333
Jim Mattson75f4fc82017-08-23 16:32:03 -07006334 if (vmx_rdseed_supported()) {
6335 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6336 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006337 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006338
6339 if (nested) {
6340 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006341 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006342 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006343 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006344 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006345 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006346 }
6347 }
6348
Paolo Bonzini80154d72017-08-24 13:55:35 +02006349 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006350}
6351
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006352static void ept_set_mmio_spte_mask(void)
6353{
6354 /*
6355 * EPT Misconfigurations can be generated if the value of bits 2:0
6356 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006357 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006358 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6359 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006360}
6361
Wanpeng Lif53cd632014-12-02 19:14:58 +08006362#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006363/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006364 * Sets up the vmcs for emulated real mode.
6365 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006366static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006367{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006369
Abel Gordon4607c2d2013-04-18 14:35:55 +03006370 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006371 /*
6372 * At vCPU creation, "VMWRITE to any supported field
6373 * in the VMCS" is supported, so use the more
6374 * permissive vmx_vmread_bitmap to specify both read
6375 * and write permissions for the shadow VMCS.
6376 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006377 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006378 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006379 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006380 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006381 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006382
Avi Kivity6aa8b732006-12-10 02:21:36 -08006383 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6384
Avi Kivity6aa8b732006-12-10 02:21:36 -08006385 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006386 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006387 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006388
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006389 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006390
Dan Williamsdfa169b2016-06-02 11:17:24 -07006391 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006392 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006393 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006394 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006395 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006396
Andrey Smetanind62caab2015-11-10 15:36:33 +03006397 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006398 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6399 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6400 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6401 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6402
6403 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006404
Li RongQing0bcf2612015-12-03 13:29:34 +08006405 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006406 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006407 }
6408
Wanpeng Lib31c1142018-03-12 04:53:04 -07006409 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006410 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006411 vmx->ple_window = ple_window;
6412 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006413 }
6414
Xiao Guangrongc3707952011-07-12 03:28:04 +08006415 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6416 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6418
Avi Kivity9581d442010-10-19 16:46:55 +02006419 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6420 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006421 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006422 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6423 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006424
Bandan Das2a499e42017-08-03 15:54:41 -04006425 if (cpu_has_vmx_vmfunc())
6426 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6427
Eddie Dong2cc51562007-05-21 07:28:09 +03006428 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6429 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006430 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006431 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006432 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006433
Radim Krčmář74545702015-04-27 15:11:25 +02006434 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6435 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006436
Paolo Bonzini03916db2014-07-24 14:21:57 +02006437 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006438 u32 index = vmx_msr_index[i];
6439 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006440 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006441
6442 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6443 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006444 if (wrmsr_safe(index, data_low, data_high) < 0)
6445 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006446 vmx->guest_msrs[j].index = i;
6447 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006448 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006449 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006450 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006451
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006452 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6453 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006454
6455 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006456
6457 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006458 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006459
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006460 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6461 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6462
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006463 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006464
Wanpeng Lif53cd632014-12-02 19:14:58 +08006465 if (vmx_xsaves_supported())
6466 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6467
Peter Feiner4e595162016-07-07 14:49:58 -07006468 if (enable_pml) {
6469 ASSERT(vmx->pml_pg);
6470 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6471 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6472 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006473}
6474
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006475static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006476{
6477 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006478 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006479 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006480
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006481 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006482 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006483
Wanpeng Li518e7b92018-02-28 14:03:31 +08006484 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006485 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006486 kvm_set_cr8(vcpu, 0);
6487
6488 if (!init_event) {
6489 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6490 MSR_IA32_APICBASE_ENABLE;
6491 if (kvm_vcpu_is_reset_bsp(vcpu))
6492 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6493 apic_base_msr.host_initiated = true;
6494 kvm_set_apic_base(vcpu, &apic_base_msr);
6495 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006496
Avi Kivity2fb92db2011-04-27 19:42:18 +03006497 vmx_segment_cache_clear(vmx);
6498
Avi Kivity5706be02008-08-20 15:07:31 +03006499 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006500 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006501 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006502
6503 seg_setup(VCPU_SREG_DS);
6504 seg_setup(VCPU_SREG_ES);
6505 seg_setup(VCPU_SREG_FS);
6506 seg_setup(VCPU_SREG_GS);
6507 seg_setup(VCPU_SREG_SS);
6508
6509 vmcs_write16(GUEST_TR_SELECTOR, 0);
6510 vmcs_writel(GUEST_TR_BASE, 0);
6511 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6512 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6513
6514 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6515 vmcs_writel(GUEST_LDTR_BASE, 0);
6516 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6517 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6518
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006519 if (!init_event) {
6520 vmcs_write32(GUEST_SYSENTER_CS, 0);
6521 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6522 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6523 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6524 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006525
Wanpeng Lic37c2872017-11-20 14:52:21 -08006526 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006527 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006528
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006529 vmcs_writel(GUEST_GDTR_BASE, 0);
6530 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6531
6532 vmcs_writel(GUEST_IDTR_BASE, 0);
6533 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6534
Anthony Liguori443381a2010-12-06 10:53:38 -06006535 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006536 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006537 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006538 if (kvm_mpx_supported())
6539 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006540
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006541 setup_msrs(vmx);
6542
Avi Kivity6aa8b732006-12-10 02:21:36 -08006543 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6544
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006545 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006546 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006547 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006548 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006549 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006550 vmcs_write32(TPR_THRESHOLD, 0);
6551 }
6552
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006553 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554
Sheng Yang2384d2b2008-01-17 15:14:33 +08006555 if (vmx->vpid != 0)
6556 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6557
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006558 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006559 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006560 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006561 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006562 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006563
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006564 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006565
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006566 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006567 if (init_event)
6568 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006569}
6570
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006571/*
6572 * In nested virtualization, check if L1 asked to exit on external interrupts.
6573 * For most existing hypervisors, this will always return true.
6574 */
6575static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6576{
6577 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6578 PIN_BASED_EXT_INTR_MASK;
6579}
6580
Bandan Das77b0f5d2014-04-19 18:17:45 -04006581/*
6582 * In nested virtualization, check if L1 has set
6583 * VM_EXIT_ACK_INTR_ON_EXIT
6584 */
6585static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6586{
6587 return get_vmcs12(vcpu)->vm_exit_controls &
6588 VM_EXIT_ACK_INTR_ON_EXIT;
6589}
6590
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006591static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6592{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006593 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006594}
6595
Jan Kiszkac9a79532014-03-07 20:03:15 +01006596static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006597{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006598 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6599 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006600}
6601
Jan Kiszkac9a79532014-03-07 20:03:15 +01006602static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006603{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006604 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006605 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006606 enable_irq_window(vcpu);
6607 return;
6608 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006609
Paolo Bonzini47c01522016-12-19 11:44:07 +01006610 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6611 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006612}
6613
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006614static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006615{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006616 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006617 uint32_t intr;
6618 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006619
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006620 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006621
Avi Kivityfa89a812008-09-01 15:57:51 +03006622 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006623 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006624 int inc_eip = 0;
6625 if (vcpu->arch.interrupt.soft)
6626 inc_eip = vcpu->arch.event_exit_inst_len;
6627 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006628 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006629 return;
6630 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006631 intr = irq | INTR_INFO_VALID_MASK;
6632 if (vcpu->arch.interrupt.soft) {
6633 intr |= INTR_TYPE_SOFT_INTR;
6634 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6635 vmx->vcpu.arch.event_exit_inst_len);
6636 } else
6637 intr |= INTR_TYPE_EXT_INTR;
6638 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006639
6640 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006641}
6642
Sheng Yangf08864b2008-05-15 18:23:25 +08006643static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6644{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006645 struct vcpu_vmx *vmx = to_vmx(vcpu);
6646
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006647 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006648 /*
6649 * Tracking the NMI-blocked state in software is built upon
6650 * finding the next open IRQ window. This, in turn, depends on
6651 * well-behaving guests: They have to keep IRQs disabled at
6652 * least as long as the NMI handler runs. Otherwise we may
6653 * cause NMI nesting, maybe breaking the guest. But as this is
6654 * highly unlikely, we can live with the residual risk.
6655 */
6656 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6657 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6658 }
6659
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006660 ++vcpu->stat.nmi_injections;
6661 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006662
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006663 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006664 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006665 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006666 return;
6667 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006668
Sheng Yangf08864b2008-05-15 18:23:25 +08006669 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6670 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006671
6672 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006673}
6674
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006675static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6676{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006677 struct vcpu_vmx *vmx = to_vmx(vcpu);
6678 bool masked;
6679
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006680 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006681 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006682 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006683 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006684 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6685 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6686 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006687}
6688
6689static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6690{
6691 struct vcpu_vmx *vmx = to_vmx(vcpu);
6692
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006693 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006694 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6695 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6696 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6697 }
6698 } else {
6699 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6700 if (masked)
6701 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6702 GUEST_INTR_STATE_NMI);
6703 else
6704 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6705 GUEST_INTR_STATE_NMI);
6706 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006707}
6708
Jan Kiszka2505dc92013-04-14 12:12:47 +02006709static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6710{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006711 if (to_vmx(vcpu)->nested.nested_run_pending)
6712 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006713
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006714 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006715 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6716 return 0;
6717
Jan Kiszka2505dc92013-04-14 12:12:47 +02006718 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6719 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6720 | GUEST_INTR_STATE_NMI));
6721}
6722
Gleb Natapov78646122009-03-23 12:12:11 +02006723static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6724{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006725 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6726 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006727 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6728 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006729}
6730
Izik Eiduscbc94022007-10-25 00:29:55 +02006731static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6732{
6733 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006734
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006735 if (enable_unrestricted_guest)
6736 return 0;
6737
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006738 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6739 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006740 if (ret)
6741 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006742 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006743 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006744}
6745
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006746static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6747{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006748 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006749 return 0;
6750}
6751
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006752static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006754 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006755 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006756 /*
6757 * Update instruction length as we may reinject the exception
6758 * from user space while in guest debugging mode.
6759 */
6760 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6761 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006762 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006763 return false;
6764 /* fall through */
6765 case DB_VECTOR:
6766 if (vcpu->guest_debug &
6767 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6768 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006769 /* fall through */
6770 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006771 case OF_VECTOR:
6772 case BR_VECTOR:
6773 case UD_VECTOR:
6774 case DF_VECTOR:
6775 case SS_VECTOR:
6776 case GP_VECTOR:
6777 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006778 return true;
6779 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006780 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006781 return false;
6782}
6783
6784static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6785 int vec, u32 err_code)
6786{
6787 /*
6788 * Instruction with address size override prefix opcode 0x67
6789 * Cause the #SS fault with 0 error code in VM86 mode.
6790 */
6791 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6792 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6793 if (vcpu->arch.halt_request) {
6794 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006795 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006796 }
6797 return 1;
6798 }
6799 return 0;
6800 }
6801
6802 /*
6803 * Forward all other exceptions that are valid in real mode.
6804 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6805 * the required debugging infrastructure rework.
6806 */
6807 kvm_queue_exception(vcpu, vec);
6808 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006809}
6810
Andi Kleena0861c02009-06-08 17:37:09 +08006811/*
6812 * Trigger machine check on the host. We assume all the MSRs are already set up
6813 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6814 * We pass a fake environment to the machine check handler because we want
6815 * the guest to be always treated like user space, no matter what context
6816 * it used internally.
6817 */
6818static void kvm_machine_check(void)
6819{
6820#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6821 struct pt_regs regs = {
6822 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6823 .flags = X86_EFLAGS_IF,
6824 };
6825
6826 do_machine_check(&regs, 0);
6827#endif
6828}
6829
Avi Kivity851ba692009-08-24 11:10:17 +03006830static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006831{
6832 /* already handled by vcpu_run */
6833 return 1;
6834}
6835
Avi Kivity851ba692009-08-24 11:10:17 +03006836static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837{
Avi Kivity1155f762007-11-22 11:30:47 +02006838 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006839 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006840 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006841 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842 u32 vect_info;
6843 enum emulation_result er;
6844
Avi Kivity1155f762007-11-22 11:30:47 +02006845 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006846 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006847
Andi Kleena0861c02009-06-08 17:37:09 +08006848 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006849 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006850
Jim Mattsonef85b672016-12-12 11:01:37 -08006851 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006852 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006853
Wanpeng Li082d06e2018-04-03 16:28:48 -07006854 if (is_invalid_opcode(intr_info))
6855 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006856
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006858 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006860
Liran Alon9e869482018-03-12 13:12:51 +02006861 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6862 WARN_ON_ONCE(!enable_vmware_backdoor);
6863 er = emulate_instruction(vcpu,
6864 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6865 if (er == EMULATE_USER_EXIT)
6866 return 0;
6867 else if (er != EMULATE_DONE)
6868 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6869 return 1;
6870 }
6871
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006872 /*
6873 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6874 * MMIO, it is better to report an internal error.
6875 * See the comments in vmx_handle_exit.
6876 */
6877 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6878 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6879 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6880 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006881 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006882 vcpu->run->internal.data[0] = vect_info;
6883 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006884 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006885 return 0;
6886 }
6887
Avi Kivity6aa8b732006-12-10 02:21:36 -08006888 if (is_page_fault(intr_info)) {
6889 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006890 /* EPT won't cause page fault directly */
6891 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006892 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893 }
6894
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006895 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006896
6897 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6898 return handle_rmode_exception(vcpu, ex_no, error_code);
6899
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006900 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006901 case AC_VECTOR:
6902 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6903 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006904 case DB_VECTOR:
6905 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6906 if (!(vcpu->guest_debug &
6907 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006908 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006909 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006910 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006911 skip_emulated_instruction(vcpu);
6912
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006913 kvm_queue_exception(vcpu, DB_VECTOR);
6914 return 1;
6915 }
6916 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6917 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6918 /* fall through */
6919 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006920 /*
6921 * Update instruction length as we may reinject #BP from
6922 * user space while in guest debugging mode. Reading it for
6923 * #DB as well causes no harm, it is not used in that case.
6924 */
6925 vmx->vcpu.arch.event_exit_inst_len =
6926 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006927 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006928 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006929 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6930 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006931 break;
6932 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006933 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6934 kvm_run->ex.exception = ex_no;
6935 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006936 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006937 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006938 return 0;
6939}
6940
Avi Kivity851ba692009-08-24 11:10:17 +03006941static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006942{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006943 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006944 return 1;
6945}
6946
Avi Kivity851ba692009-08-24 11:10:17 +03006947static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006948{
Avi Kivity851ba692009-08-24 11:10:17 +03006949 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006950 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006951 return 0;
6952}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953
Avi Kivity851ba692009-08-24 11:10:17 +03006954static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006955{
He, Qingbfdaab02007-09-12 14:18:28 +08006956 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006957 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006958 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959
He, Qingbfdaab02007-09-12 14:18:28 +08006960 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006961 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006962
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006963 ++vcpu->stat.io_exits;
6964
Sean Christopherson432baf62018-03-08 08:57:26 -08006965 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006966 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006967
6968 port = exit_qualification >> 16;
6969 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006970 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006971
Sean Christophersondca7f122018-03-08 08:57:27 -08006972 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006973}
6974
Ingo Molnar102d8322007-02-19 14:37:47 +02006975static void
6976vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6977{
6978 /*
6979 * Patch in the VMCALL instruction:
6980 */
6981 hypercall[0] = 0x0f;
6982 hypercall[1] = 0x01;
6983 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006984}
6985
Guo Chao0fa06072012-06-28 15:16:19 +08006986/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006987static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6988{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006989 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006990 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6991 unsigned long orig_val = val;
6992
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006993 /*
6994 * We get here when L2 changed cr0 in a way that did not change
6995 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006996 * but did change L0 shadowed bits. So we first calculate the
6997 * effective cr0 value that L1 would like to write into the
6998 * hardware. It consists of the L2-owned bits from the new
6999 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007000 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007001 val = (val & ~vmcs12->cr0_guest_host_mask) |
7002 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7003
David Matlack38991522016-11-29 18:14:08 -08007004 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007005 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007006
7007 if (kvm_set_cr0(vcpu, val))
7008 return 1;
7009 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007010 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007011 } else {
7012 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007013 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007014 return 1;
David Matlack38991522016-11-29 18:14:08 -08007015
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007016 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007017 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007018}
7019
7020static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7021{
7022 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007023 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7024 unsigned long orig_val = val;
7025
7026 /* analogously to handle_set_cr0 */
7027 val = (val & ~vmcs12->cr4_guest_host_mask) |
7028 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7029 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007030 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007031 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007032 return 0;
7033 } else
7034 return kvm_set_cr4(vcpu, val);
7035}
7036
Paolo Bonzini0367f202016-07-12 10:44:55 +02007037static int handle_desc(struct kvm_vcpu *vcpu)
7038{
7039 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7040 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7041}
7042
Avi Kivity851ba692009-08-24 11:10:17 +03007043static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007044{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007045 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046 int cr;
7047 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007048 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007049 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050
He, Qingbfdaab02007-09-12 14:18:28 +08007051 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052 cr = exit_qualification & 15;
7053 reg = (exit_qualification >> 8) & 15;
7054 switch ((exit_qualification >> 4) & 3) {
7055 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007056 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007057 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058 switch (cr) {
7059 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007060 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007061 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007063 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007064 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007065 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007066 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007067 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007068 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007069 case 8: {
7070 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007071 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007072 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007073 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007074 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007075 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007076 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007077 return ret;
7078 /*
7079 * TODO: we might be squashing a
7080 * KVM_GUESTDBG_SINGLESTEP-triggered
7081 * KVM_EXIT_DEBUG here.
7082 */
Avi Kivity851ba692009-08-24 11:10:17 +03007083 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007084 return 0;
7085 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007086 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007088 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007089 WARN_ONCE(1, "Guest should always own CR0.TS");
7090 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007091 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007092 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093 case 1: /*mov from cr*/
7094 switch (cr) {
7095 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007096 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007097 val = kvm_read_cr3(vcpu);
7098 kvm_register_write(vcpu, reg, val);
7099 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007100 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007101 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007102 val = kvm_get_cr8(vcpu);
7103 kvm_register_write(vcpu, reg, val);
7104 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007105 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007106 }
7107 break;
7108 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007109 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007110 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007111 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007112
Kyle Huey6affcbe2016-11-29 12:40:40 -08007113 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007114 default:
7115 break;
7116 }
Avi Kivity851ba692009-08-24 11:10:17 +03007117 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007118 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007119 (int)(exit_qualification >> 4) & 3, cr);
7120 return 0;
7121}
7122
Avi Kivity851ba692009-08-24 11:10:17 +03007123static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007124{
He, Qingbfdaab02007-09-12 14:18:28 +08007125 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007126 int dr, dr7, reg;
7127
7128 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7129 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7130
7131 /* First, if DR does not exist, trigger UD */
7132 if (!kvm_require_dr(vcpu, dr))
7133 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007134
Jan Kiszkaf2483412010-01-20 18:20:20 +01007135 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007136 if (!kvm_require_cpl(vcpu, 0))
7137 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007138 dr7 = vmcs_readl(GUEST_DR7);
7139 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007140 /*
7141 * As the vm-exit takes precedence over the debug trap, we
7142 * need to emulate the latter, either for the host or the
7143 * guest debugging itself.
7144 */
7145 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007146 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007147 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007148 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007149 vcpu->run->debug.arch.exception = DB_VECTOR;
7150 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007151 return 0;
7152 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007153 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007154 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007155 kvm_queue_exception(vcpu, DB_VECTOR);
7156 return 1;
7157 }
7158 }
7159
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007160 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007161 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7162 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007163
7164 /*
7165 * No more DR vmexits; force a reload of the debug registers
7166 * and reenter on this instruction. The next vmexit will
7167 * retrieve the full state of the debug registers.
7168 */
7169 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7170 return 1;
7171 }
7172
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007173 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7174 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007175 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007176
7177 if (kvm_get_dr(vcpu, dr, &val))
7178 return 1;
7179 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007180 } else
Nadav Amit57773922014-06-18 17:19:23 +03007181 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007182 return 1;
7183
Kyle Huey6affcbe2016-11-29 12:40:40 -08007184 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007185}
7186
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007187static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7188{
7189 return vcpu->arch.dr6;
7190}
7191
7192static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7193{
7194}
7195
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007196static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7197{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007198 get_debugreg(vcpu->arch.db[0], 0);
7199 get_debugreg(vcpu->arch.db[1], 1);
7200 get_debugreg(vcpu->arch.db[2], 2);
7201 get_debugreg(vcpu->arch.db[3], 3);
7202 get_debugreg(vcpu->arch.dr6, 6);
7203 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7204
7205 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007206 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007207}
7208
Gleb Natapov020df072010-04-13 10:05:23 +03007209static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7210{
7211 vmcs_writel(GUEST_DR7, val);
7212}
7213
Avi Kivity851ba692009-08-24 11:10:17 +03007214static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007215{
Kyle Huey6a908b62016-11-29 12:40:37 -08007216 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007217}
7218
Avi Kivity851ba692009-08-24 11:10:17 +03007219static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007220{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007221 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007222 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007223
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007224 msr_info.index = ecx;
7225 msr_info.host_initiated = false;
7226 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007227 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007228 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007229 return 1;
7230 }
7231
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007232 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007233
Avi Kivity6aa8b732006-12-10 02:21:36 -08007234 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007235 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7236 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007237 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007238}
7239
Avi Kivity851ba692009-08-24 11:10:17 +03007240static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241{
Will Auld8fe8ab42012-11-29 12:42:12 -08007242 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007243 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7244 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7245 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246
Will Auld8fe8ab42012-11-29 12:42:12 -08007247 msr.data = data;
7248 msr.index = ecx;
7249 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007250 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007251 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007252 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007253 return 1;
7254 }
7255
Avi Kivity59200272010-01-25 19:47:02 +02007256 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007258}
7259
Avi Kivity851ba692009-08-24 11:10:17 +03007260static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007261{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007262 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007263 return 1;
7264}
7265
Avi Kivity851ba692009-08-24 11:10:17 +03007266static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007267{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007268 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7269 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007270
Avi Kivity3842d132010-07-27 12:30:24 +03007271 kvm_make_request(KVM_REQ_EVENT, vcpu);
7272
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007273 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007274 return 1;
7275}
7276
Avi Kivity851ba692009-08-24 11:10:17 +03007277static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007278{
Avi Kivityd3bef152007-06-05 15:53:05 +03007279 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007280}
7281
Avi Kivity851ba692009-08-24 11:10:17 +03007282static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007283{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007284 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007285}
7286
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007287static int handle_invd(struct kvm_vcpu *vcpu)
7288{
Andre Przywara51d8b662010-12-21 11:12:02 +01007289 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007290}
7291
Avi Kivity851ba692009-08-24 11:10:17 +03007292static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007293{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007294 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007295
7296 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007297 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007298}
7299
Avi Kivityfee84b02011-11-10 14:57:25 +02007300static int handle_rdpmc(struct kvm_vcpu *vcpu)
7301{
7302 int err;
7303
7304 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007305 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007306}
7307
Avi Kivity851ba692009-08-24 11:10:17 +03007308static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007309{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007310 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007311}
7312
Dexuan Cui2acf9232010-06-10 11:27:12 +08007313static int handle_xsetbv(struct kvm_vcpu *vcpu)
7314{
7315 u64 new_bv = kvm_read_edx_eax(vcpu);
7316 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7317
7318 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007319 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007320 return 1;
7321}
7322
Wanpeng Lif53cd632014-12-02 19:14:58 +08007323static int handle_xsaves(struct kvm_vcpu *vcpu)
7324{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007325 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007326 WARN(1, "this should never happen\n");
7327 return 1;
7328}
7329
7330static int handle_xrstors(struct kvm_vcpu *vcpu)
7331{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007332 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007333 WARN(1, "this should never happen\n");
7334 return 1;
7335}
7336
Avi Kivity851ba692009-08-24 11:10:17 +03007337static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007338{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007339 if (likely(fasteoi)) {
7340 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7341 int access_type, offset;
7342
7343 access_type = exit_qualification & APIC_ACCESS_TYPE;
7344 offset = exit_qualification & APIC_ACCESS_OFFSET;
7345 /*
7346 * Sane guest uses MOV to write EOI, with written value
7347 * not cared. So make a short-circuit here by avoiding
7348 * heavy instruction emulation.
7349 */
7350 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7351 (offset == APIC_EOI)) {
7352 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007353 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007354 }
7355 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007356 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007357}
7358
Yang Zhangc7c9c562013-01-25 10:18:51 +08007359static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7360{
7361 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7362 int vector = exit_qualification & 0xff;
7363
7364 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7365 kvm_apic_set_eoi_accelerated(vcpu, vector);
7366 return 1;
7367}
7368
Yang Zhang83d4c282013-01-25 10:18:49 +08007369static int handle_apic_write(struct kvm_vcpu *vcpu)
7370{
7371 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7372 u32 offset = exit_qualification & 0xfff;
7373
7374 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7375 kvm_apic_write_nodecode(vcpu, offset);
7376 return 1;
7377}
7378
Avi Kivity851ba692009-08-24 11:10:17 +03007379static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007380{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007382 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007383 bool has_error_code = false;
7384 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007385 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007386 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007387
7388 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007389 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007390 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007391
7392 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7393
7394 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007395 if (reason == TASK_SWITCH_GATE && idt_v) {
7396 switch (type) {
7397 case INTR_TYPE_NMI_INTR:
7398 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007399 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007400 break;
7401 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007402 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007403 kvm_clear_interrupt_queue(vcpu);
7404 break;
7405 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007406 if (vmx->idt_vectoring_info &
7407 VECTORING_INFO_DELIVER_CODE_MASK) {
7408 has_error_code = true;
7409 error_code =
7410 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7411 }
7412 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007413 case INTR_TYPE_SOFT_EXCEPTION:
7414 kvm_clear_exception_queue(vcpu);
7415 break;
7416 default:
7417 break;
7418 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007419 }
Izik Eidus37817f22008-03-24 23:14:53 +02007420 tss_selector = exit_qualification;
7421
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007422 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7423 type != INTR_TYPE_EXT_INTR &&
7424 type != INTR_TYPE_NMI_INTR))
7425 skip_emulated_instruction(vcpu);
7426
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007427 if (kvm_task_switch(vcpu, tss_selector,
7428 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7429 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007430 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7431 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7432 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007433 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007434 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007435
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007436 /*
7437 * TODO: What about debug traps on tss switch?
7438 * Are we supposed to inject them and update dr6?
7439 */
7440
7441 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007442}
7443
Avi Kivity851ba692009-08-24 11:10:17 +03007444static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007445{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007446 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007447 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007448 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007449
Sheng Yangf9c617f2009-03-25 10:08:52 +08007450 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007451
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007452 /*
7453 * EPT violation happened while executing iret from NMI,
7454 * "blocked by NMI" bit has to be set before next VM entry.
7455 * There are errata that may cause this bit to not be set:
7456 * AAK134, BY25.
7457 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007458 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007459 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007460 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007461 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7462
Sheng Yang14394422008-04-28 12:24:45 +08007463 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007464 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007465
Junaid Shahid27959a42016-12-06 16:46:10 -08007466 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007467 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007468 ? PFERR_USER_MASK : 0;
7469 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007470 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007471 ? PFERR_WRITE_MASK : 0;
7472 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007473 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007474 ? PFERR_FETCH_MASK : 0;
7475 /* ept page table entry is present? */
7476 error_code |= (exit_qualification &
7477 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7478 EPT_VIOLATION_EXECUTABLE))
7479 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007480
Paolo Bonzinieebed242016-11-28 14:39:58 +01007481 error_code |= (exit_qualification & 0x100) != 0 ?
7482 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007483
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007484 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007485 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007486}
7487
Avi Kivity851ba692009-08-24 11:10:17 +03007488static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007489{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007490 gpa_t gpa;
7491
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007492 /*
7493 * A nested guest cannot optimize MMIO vmexits, because we have an
7494 * nGPA here instead of the required GPA.
7495 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007496 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007497 if (!is_guest_mode(vcpu) &&
7498 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007499 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007500 /*
7501 * Doing kvm_skip_emulated_instruction() depends on undefined
7502 * behavior: Intel's manual doesn't mandate
7503 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7504 * occurs and while on real hardware it was observed to be set,
7505 * other hypervisors (namely Hyper-V) don't set it, we end up
7506 * advancing IP with some random value. Disable fast mmio when
7507 * running nested and keep it for real hardware in hope that
7508 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7509 */
7510 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7511 return kvm_skip_emulated_instruction(vcpu);
7512 else
7513 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7514 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007515 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007516
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007517 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007518}
7519
Avi Kivity851ba692009-08-24 11:10:17 +03007520static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007521{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007522 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007523 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7524 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007525 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007526 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007527
7528 return 1;
7529}
7530
Mohammed Gamal80ced182009-09-01 12:48:18 +02007531static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007532{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007533 struct vcpu_vmx *vmx = to_vmx(vcpu);
7534 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007535 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007536 u32 cpu_exec_ctrl;
7537 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007538 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007539
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007540 /*
7541 * We should never reach the point where we are emulating L2
7542 * due to invalid guest state as that means we incorrectly
7543 * allowed a nested VMEntry with an invalid vmcs12.
7544 */
7545 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7546
Avi Kivity49e9d552010-09-19 14:34:08 +02007547 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7548 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007549
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007550 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007551 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007552 return handle_interrupt_window(&vmx->vcpu);
7553
Radim Krčmář72875d82017-04-26 22:32:19 +02007554 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007555 return 1;
7556
Liran Alon9b8ae632017-11-05 16:56:34 +02007557 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007558
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007559 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007560 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007561 ret = 0;
7562 goto out;
7563 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007564
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007565 if (err != EMULATE_DONE)
7566 goto emulation_error;
7567
7568 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7569 vcpu->arch.exception.pending)
7570 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007571
Gleb Natapov8d76c492013-05-08 18:38:44 +03007572 if (vcpu->arch.halt_request) {
7573 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007574 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007575 goto out;
7576 }
7577
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007578 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007579 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007580 if (need_resched())
7581 schedule();
7582 }
7583
Mohammed Gamal80ced182009-09-01 12:48:18 +02007584out:
7585 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007586
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007587emulation_error:
7588 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7589 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7590 vcpu->run->internal.ndata = 0;
7591 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007592}
7593
7594static void grow_ple_window(struct kvm_vcpu *vcpu)
7595{
7596 struct vcpu_vmx *vmx = to_vmx(vcpu);
7597 int old = vmx->ple_window;
7598
Babu Mogerc8e88712018-03-16 16:37:24 -04007599 vmx->ple_window = __grow_ple_window(old, ple_window,
7600 ple_window_grow,
7601 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007602
7603 if (vmx->ple_window != old)
7604 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007605
7606 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007607}
7608
7609static void shrink_ple_window(struct kvm_vcpu *vcpu)
7610{
7611 struct vcpu_vmx *vmx = to_vmx(vcpu);
7612 int old = vmx->ple_window;
7613
Babu Mogerc8e88712018-03-16 16:37:24 -04007614 vmx->ple_window = __shrink_ple_window(old, ple_window,
7615 ple_window_shrink,
7616 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007617
7618 if (vmx->ple_window != old)
7619 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007620
7621 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007622}
7623
7624/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007625 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7626 */
7627static void wakeup_handler(void)
7628{
7629 struct kvm_vcpu *vcpu;
7630 int cpu = smp_processor_id();
7631
7632 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7633 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7634 blocked_vcpu_list) {
7635 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7636
7637 if (pi_test_on(pi_desc) == 1)
7638 kvm_vcpu_kick(vcpu);
7639 }
7640 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7641}
7642
Peng Haoe01bca22018-04-07 05:47:32 +08007643static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007644{
7645 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7646 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7647 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7648 0ull, VMX_EPT_EXECUTABLE_MASK,
7649 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007650 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007651
7652 ept_set_mmio_spte_mask();
7653 kvm_enable_tdp();
7654}
7655
Tiejun Chenf2c76482014-10-28 10:14:47 +08007656static __init int hardware_setup(void)
7657{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007658 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007659 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007660
7661 rdmsrl_safe(MSR_EFER, &host_efer);
7662
7663 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7664 kvm_define_shared_msr(i, vmx_msr_index[i]);
7665
Radim Krčmář23611332016-09-29 22:41:33 +02007666 for (i = 0; i < VMX_BITMAP_NR; i++) {
7667 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7668 if (!vmx_bitmap[i])
7669 goto out;
7670 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007671
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007672 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7673 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7674
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007675 if (setup_vmcs_config(&vmcs_config) < 0) {
7676 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007677 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007678 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007679
7680 if (boot_cpu_has(X86_FEATURE_NX))
7681 kvm_enable_efer_bits(EFER_NX);
7682
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007683 if (boot_cpu_has(X86_FEATURE_MPX)) {
7684 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7685 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7686 }
7687
Wanpeng Li08d839c2017-03-23 05:30:08 -07007688 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7689 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007690 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007691
Tiejun Chenf2c76482014-10-28 10:14:47 +08007692 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007693 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007694 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007695 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007696 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007697
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007698 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007699 enable_ept_ad_bits = 0;
7700
Wanpeng Li8ad81822017-10-09 15:51:53 -07007701 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007702 enable_unrestricted_guest = 0;
7703
Paolo Bonziniad15a292015-01-30 16:18:49 +01007704 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705 flexpriority_enabled = 0;
7706
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007707 if (!cpu_has_virtual_nmis())
7708 enable_vnmi = 0;
7709
Paolo Bonziniad15a292015-01-30 16:18:49 +01007710 /*
7711 * set_apic_access_page_addr() is used to reload apic access
7712 * page upon invalidation. No need to do anything if not
7713 * using the APIC_ACCESS_ADDR VMCS field.
7714 */
7715 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007716 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007717
7718 if (!cpu_has_vmx_tpr_shadow())
7719 kvm_x86_ops->update_cr8_intercept = NULL;
7720
7721 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7722 kvm_disable_largepages();
7723
Tianyu Lan877ad952018-07-19 08:40:23 +00007724#if IS_ENABLED(CONFIG_HYPERV)
7725 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7726 && enable_ept)
7727 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7728#endif
7729
Wanpeng Li0f107682017-09-28 18:06:24 -07007730 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007731 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007732 ple_window = 0;
7733 ple_window_grow = 0;
7734 ple_window_max = 0;
7735 ple_window_shrink = 0;
7736 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007737
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007738 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007739 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007740 kvm_x86_ops->sync_pir_to_irr = NULL;
7741 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007742
Haozhong Zhang64903d62015-10-20 15:39:09 +08007743 if (cpu_has_vmx_tsc_scaling()) {
7744 kvm_has_tsc_control = true;
7745 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7746 kvm_tsc_scaling_ratio_frac_bits = 48;
7747 }
7748
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007749 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7750
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007751 if (enable_ept)
7752 vmx_enable_tdp();
7753 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007754 kvm_disable_tdp();
7755
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007756 if (!nested) {
7757 kvm_x86_ops->get_nested_state = NULL;
7758 kvm_x86_ops->set_nested_state = NULL;
7759 }
7760
Kai Huang843e4332015-01-28 10:54:28 +08007761 /*
7762 * Only enable PML when hardware supports PML feature, and both EPT
7763 * and EPT A/D bit features are enabled -- PML depends on them to work.
7764 */
7765 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7766 enable_pml = 0;
7767
7768 if (!enable_pml) {
7769 kvm_x86_ops->slot_enable_log_dirty = NULL;
7770 kvm_x86_ops->slot_disable_log_dirty = NULL;
7771 kvm_x86_ops->flush_log_dirty = NULL;
7772 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7773 }
7774
Yunhong Jiang64672c92016-06-13 14:19:59 -07007775 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7776 u64 vmx_msr;
7777
7778 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7779 cpu_preemption_timer_multi =
7780 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7781 } else {
7782 kvm_x86_ops->set_hv_timer = NULL;
7783 kvm_x86_ops->cancel_hv_timer = NULL;
7784 }
7785
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007786 if (!cpu_has_vmx_shadow_vmcs())
7787 enable_shadow_vmcs = 0;
7788 if (enable_shadow_vmcs)
7789 init_vmcs_shadow_fields();
7790
Feng Wubf9f6ac2015-09-18 22:29:55 +08007791 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007792 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007793
Ashok Rajc45dcc72016-06-22 14:59:56 +08007794 kvm_mce_cap_supported |= MCG_LMCE_P;
7795
Tiejun Chenf2c76482014-10-28 10:14:47 +08007796 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007797
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007798out:
Radim Krčmář23611332016-09-29 22:41:33 +02007799 for (i = 0; i < VMX_BITMAP_NR; i++)
7800 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007801
7802 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007803}
7804
7805static __exit void hardware_unsetup(void)
7806{
Radim Krčmář23611332016-09-29 22:41:33 +02007807 int i;
7808
7809 for (i = 0; i < VMX_BITMAP_NR; i++)
7810 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007811
Tiejun Chenf2c76482014-10-28 10:14:47 +08007812 free_kvm_area();
7813}
7814
Avi Kivity6aa8b732006-12-10 02:21:36 -08007815/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007816 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7817 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7818 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007819static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007820{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007821 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007822 grow_ple_window(vcpu);
7823
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007824 /*
7825 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7826 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7827 * never set PAUSE_EXITING and just set PLE if supported,
7828 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7829 */
7830 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007831 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007832}
7833
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007834static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007835{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007836 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007837}
7838
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007839static int handle_mwait(struct kvm_vcpu *vcpu)
7840{
7841 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7842 return handle_nop(vcpu);
7843}
7844
Jim Mattson45ec3682017-08-23 16:32:04 -07007845static int handle_invalid_op(struct kvm_vcpu *vcpu)
7846{
7847 kvm_queue_exception(vcpu, UD_VECTOR);
7848 return 1;
7849}
7850
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007851static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7852{
7853 return 1;
7854}
7855
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007856static int handle_monitor(struct kvm_vcpu *vcpu)
7857{
7858 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7859 return handle_nop(vcpu);
7860}
7861
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007862/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007863 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7864 * set the success or error code of an emulated VMX instruction, as specified
7865 * by Vol 2B, VMX Instruction Reference, "Conventions".
7866 */
7867static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7868{
7869 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7870 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7871 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7872}
7873
7874static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7875{
7876 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7877 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7878 X86_EFLAGS_SF | X86_EFLAGS_OF))
7879 | X86_EFLAGS_CF);
7880}
7881
Abel Gordon145c28d2013-04-18 14:36:55 +03007882static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007883 u32 vm_instruction_error)
7884{
7885 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7886 /*
7887 * failValid writes the error number to the current VMCS, which
7888 * can't be done there isn't a current VMCS.
7889 */
7890 nested_vmx_failInvalid(vcpu);
7891 return;
7892 }
7893 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7894 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7895 X86_EFLAGS_SF | X86_EFLAGS_OF))
7896 | X86_EFLAGS_ZF);
7897 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7898 /*
7899 * We don't need to force a shadow sync because
7900 * VM_INSTRUCTION_ERROR is not shadowed
7901 */
7902}
Abel Gordon145c28d2013-04-18 14:36:55 +03007903
Wincy Vanff651cb2014-12-11 08:52:58 +03007904static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7905{
7906 /* TODO: not to reset guest simply here. */
7907 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007908 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007909}
7910
Jan Kiszkaf41245002014-03-07 20:03:13 +01007911static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7912{
7913 struct vcpu_vmx *vmx =
7914 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7915
7916 vmx->nested.preemption_timer_expired = true;
7917 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7918 kvm_vcpu_kick(&vmx->vcpu);
7919
7920 return HRTIMER_NORESTART;
7921}
7922
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007923/*
Bandan Das19677e32014-05-06 02:19:15 -04007924 * Decode the memory-address operand of a vmx instruction, as recorded on an
7925 * exit caused by such an instruction (run by a guest hypervisor).
7926 * On success, returns 0. When the operand is invalid, returns 1 and throws
7927 * #UD or #GP.
7928 */
7929static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7930 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007931 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007932{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007933 gva_t off;
7934 bool exn;
7935 struct kvm_segment s;
7936
Bandan Das19677e32014-05-06 02:19:15 -04007937 /*
7938 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7939 * Execution", on an exit, vmx_instruction_info holds most of the
7940 * addressing components of the operand. Only the displacement part
7941 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7942 * For how an actual address is calculated from all these components,
7943 * refer to Vol. 1, "Operand Addressing".
7944 */
7945 int scaling = vmx_instruction_info & 3;
7946 int addr_size = (vmx_instruction_info >> 7) & 7;
7947 bool is_reg = vmx_instruction_info & (1u << 10);
7948 int seg_reg = (vmx_instruction_info >> 15) & 7;
7949 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7950 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7951 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7952 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7953
7954 if (is_reg) {
7955 kvm_queue_exception(vcpu, UD_VECTOR);
7956 return 1;
7957 }
7958
7959 /* Addr = segment_base + offset */
7960 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007961 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007962 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007963 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007964 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007965 off += kvm_register_read(vcpu, index_reg)<<scaling;
7966 vmx_get_segment(vcpu, &s, seg_reg);
7967 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007968
7969 if (addr_size == 1) /* 32 bit */
7970 *ret &= 0xffffffff;
7971
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007972 /* Checks for #GP/#SS exceptions. */
7973 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007974 if (is_long_mode(vcpu)) {
7975 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7976 * non-canonical form. This is the only check on the memory
7977 * destination for long mode!
7978 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007979 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007980 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007981 /* Protected mode: apply checks for segment validity in the
7982 * following order:
7983 * - segment type check (#GP(0) may be thrown)
7984 * - usability check (#GP(0)/#SS(0))
7985 * - limit check (#GP(0)/#SS(0))
7986 */
7987 if (wr)
7988 /* #GP(0) if the destination operand is located in a
7989 * read-only data segment or any code segment.
7990 */
7991 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7992 else
7993 /* #GP(0) if the source operand is located in an
7994 * execute-only code segment
7995 */
7996 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007997 if (exn) {
7998 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7999 return 1;
8000 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008001 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8002 */
8003 exn = (s.unusable != 0);
8004 /* Protected mode: #GP(0)/#SS(0) if the memory
8005 * operand is outside the segment limit.
8006 */
8007 exn = exn || (off + sizeof(u64) > s.limit);
8008 }
8009 if (exn) {
8010 kvm_queue_exception_e(vcpu,
8011 seg_reg == VCPU_SREG_SS ?
8012 SS_VECTOR : GP_VECTOR,
8013 0);
8014 return 1;
8015 }
8016
Bandan Das19677e32014-05-06 02:19:15 -04008017 return 0;
8018}
8019
Radim Krčmářcbf71272017-05-19 15:48:51 +02008020static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008021{
8022 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008023 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008024
8025 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008026 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008027 return 1;
8028
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008029 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008030 kvm_inject_page_fault(vcpu, &e);
8031 return 1;
8032 }
8033
Bandan Das3573e222014-05-06 02:19:16 -04008034 return 0;
8035}
8036
Liran Alonabfc52c2018-06-23 02:35:13 +03008037/*
8038 * Allocate a shadow VMCS and associate it with the currently loaded
8039 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8040 * VMCS is also VMCLEARed, so that it is ready for use.
8041 */
8042static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8043{
8044 struct vcpu_vmx *vmx = to_vmx(vcpu);
8045 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8046
8047 /*
8048 * We should allocate a shadow vmcs for vmcs01 only when L1
8049 * executes VMXON and free it when L1 executes VMXOFF.
8050 * As it is invalid to execute VMXON twice, we shouldn't reach
8051 * here when vmcs01 already have an allocated shadow vmcs.
8052 */
8053 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8054
8055 if (!loaded_vmcs->shadow_vmcs) {
8056 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8057 if (loaded_vmcs->shadow_vmcs)
8058 vmcs_clear(loaded_vmcs->shadow_vmcs);
8059 }
8060 return loaded_vmcs->shadow_vmcs;
8061}
8062
Jim Mattsone29acc52016-11-30 12:03:43 -08008063static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8064{
8065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008066 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008067
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008068 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8069 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008070 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008071
8072 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8073 if (!vmx->nested.cached_vmcs12)
8074 goto out_cached_vmcs12;
8075
Liran Alon61ada742018-06-23 02:35:08 +03008076 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8077 if (!vmx->nested.cached_shadow_vmcs12)
8078 goto out_cached_shadow_vmcs12;
8079
Liran Alonabfc52c2018-06-23 02:35:13 +03008080 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8081 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008082
Jim Mattsone29acc52016-11-30 12:03:43 -08008083 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8084 HRTIMER_MODE_REL_PINNED);
8085 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8086
8087 vmx->nested.vmxon = true;
8088 return 0;
8089
8090out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008091 kfree(vmx->nested.cached_shadow_vmcs12);
8092
8093out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008094 kfree(vmx->nested.cached_vmcs12);
8095
8096out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008097 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008098
Jim Mattsonde3a0022017-11-27 17:22:25 -06008099out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008100 return -ENOMEM;
8101}
8102
Bandan Das3573e222014-05-06 02:19:16 -04008103/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008104 * Emulate the VMXON instruction.
8105 * Currently, we just remember that VMX is active, and do not save or even
8106 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8107 * do not currently need to store anything in that guest-allocated memory
8108 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8109 * argument is different from the VMXON pointer (which the spec says they do).
8110 */
8111static int handle_vmon(struct kvm_vcpu *vcpu)
8112{
Jim Mattsone29acc52016-11-30 12:03:43 -08008113 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008114 gpa_t vmptr;
8115 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008116 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008117 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8118 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008119
Jim Mattson70f3aac2017-04-26 08:53:46 -07008120 /*
8121 * The Intel VMX Instruction Reference lists a bunch of bits that are
8122 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8123 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8124 * Otherwise, we should fail with #UD. But most faulting conditions
8125 * have already been checked by hardware, prior to the VM-exit for
8126 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8127 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008128 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008129 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008130 kvm_queue_exception(vcpu, UD_VECTOR);
8131 return 1;
8132 }
8133
Felix Wilhelm727ba742018-06-11 09:43:44 +02008134 /* CPL=0 must be checked manually. */
8135 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008136 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008137 return 1;
8138 }
8139
Abel Gordon145c28d2013-04-18 14:36:55 +03008140 if (vmx->nested.vmxon) {
8141 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008142 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008143 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008144
Haozhong Zhang3b840802016-06-22 14:59:54 +08008145 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008146 != VMXON_NEEDED_FEATURES) {
8147 kvm_inject_gp(vcpu, 0);
8148 return 1;
8149 }
8150
Radim Krčmářcbf71272017-05-19 15:48:51 +02008151 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008152 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008153
8154 /*
8155 * SDM 3: 24.11.5
8156 * The first 4 bytes of VMXON region contain the supported
8157 * VMCS revision identifier
8158 *
8159 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8160 * which replaces physical address width with 32
8161 */
8162 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8163 nested_vmx_failInvalid(vcpu);
8164 return kvm_skip_emulated_instruction(vcpu);
8165 }
8166
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008167 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8168 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008169 nested_vmx_failInvalid(vcpu);
8170 return kvm_skip_emulated_instruction(vcpu);
8171 }
8172 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8173 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008174 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008175 nested_vmx_failInvalid(vcpu);
8176 return kvm_skip_emulated_instruction(vcpu);
8177 }
8178 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008179 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008180
8181 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008182 ret = enter_vmx_operation(vcpu);
8183 if (ret)
8184 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008185
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008186 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008187 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008188}
8189
8190/*
8191 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8192 * for running VMX instructions (except VMXON, whose prerequisites are
8193 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008194 * Note that many of these exceptions have priority over VM exits, so they
8195 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008196 */
8197static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8198{
Jim Mattsone49fcb82018-07-27 13:44:45 -07008199 if (!to_vmx(vcpu)->nested.vmxon) {
8200 kvm_queue_exception(vcpu, UD_VECTOR);
8201 return 0;
8202 }
8203
Felix Wilhelm727ba742018-06-11 09:43:44 +02008204 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008205 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008206 return 0;
8207 }
8208
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008209 return 1;
8210}
8211
David Matlack8ca44e82017-08-01 14:00:39 -07008212static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8213{
8214 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8215 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8216}
8217
Abel Gordone7953d72013-04-18 14:37:55 +03008218static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8219{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008220 if (vmx->nested.current_vmptr == -1ull)
8221 return;
8222
Abel Gordon012f83c2013-04-18 14:39:25 +03008223 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008224 /* copy to memory all shadowed fields in case
8225 they were modified */
8226 copy_shadow_to_vmcs12(vmx);
8227 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008228 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008229 }
Wincy Van705699a2015-02-03 23:58:17 +08008230 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008231
8232 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008233 kvm_vcpu_write_guest_page(&vmx->vcpu,
8234 vmx->nested.current_vmptr >> PAGE_SHIFT,
8235 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008236
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008237 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008238}
8239
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008240/*
8241 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8242 * just stops using VMX.
8243 */
8244static void free_nested(struct vcpu_vmx *vmx)
8245{
Wanpeng Lib7455822017-11-22 14:04:00 -08008246 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008247 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008248
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008249 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008250 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008251 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008252 vmx->nested.posted_intr_nv = -1;
8253 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008254 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008255 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008256 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8257 free_vmcs(vmx->vmcs01.shadow_vmcs);
8258 vmx->vmcs01.shadow_vmcs = NULL;
8259 }
David Matlack4f2777b2016-07-13 17:16:37 -07008260 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008261 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008262 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008263 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008264 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008265 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008266 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008267 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008268 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008269 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008270 }
Wincy Van705699a2015-02-03 23:58:17 +08008271 if (vmx->nested.pi_desc_page) {
8272 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008273 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008274 vmx->nested.pi_desc_page = NULL;
8275 vmx->nested.pi_desc = NULL;
8276 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008277
Jim Mattsonde3a0022017-11-27 17:22:25 -06008278 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008279}
8280
8281/* Emulate the VMXOFF instruction */
8282static int handle_vmoff(struct kvm_vcpu *vcpu)
8283{
8284 if (!nested_vmx_check_permission(vcpu))
8285 return 1;
8286 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008287 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008288 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008289}
8290
Nadav Har'El27d6c862011-05-25 23:06:59 +03008291/* Emulate the VMCLEAR instruction */
8292static int handle_vmclear(struct kvm_vcpu *vcpu)
8293{
8294 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008295 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008296 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008297
8298 if (!nested_vmx_check_permission(vcpu))
8299 return 1;
8300
Radim Krčmářcbf71272017-05-19 15:48:51 +02008301 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008302 return 1;
8303
Radim Krčmářcbf71272017-05-19 15:48:51 +02008304 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8305 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8306 return kvm_skip_emulated_instruction(vcpu);
8307 }
8308
8309 if (vmptr == vmx->nested.vmxon_ptr) {
8310 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8311 return kvm_skip_emulated_instruction(vcpu);
8312 }
8313
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008314 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008315 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008316
Jim Mattson587d7e722017-03-02 12:41:48 -08008317 kvm_vcpu_write_guest(vcpu,
8318 vmptr + offsetof(struct vmcs12, launch_state),
8319 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008320
Nadav Har'El27d6c862011-05-25 23:06:59 +03008321 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008322 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008323}
8324
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008325static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8326
8327/* Emulate the VMLAUNCH instruction */
8328static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8329{
8330 return nested_vmx_run(vcpu, true);
8331}
8332
8333/* Emulate the VMRESUME instruction */
8334static int handle_vmresume(struct kvm_vcpu *vcpu)
8335{
8336
8337 return nested_vmx_run(vcpu, false);
8338}
8339
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340/*
8341 * Read a vmcs12 field. Since these can have varying lengths and we return
8342 * one type, we chose the biggest type (u64) and zero-extend the return value
8343 * to that size. Note that the caller, handle_vmread, might need to use only
8344 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8345 * 64-bit fields are to be returned).
8346 */
Liran Alone2536742018-06-23 02:35:02 +03008347static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008348 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008349{
8350 short offset = vmcs_field_to_offset(field);
8351 char *p;
8352
8353 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008354 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008355
Liran Alone2536742018-06-23 02:35:02 +03008356 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357
Jim Mattsond37f4262017-12-22 12:12:16 -08008358 switch (vmcs_field_width(field)) {
8359 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008360 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008361 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008362 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008363 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008364 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008365 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008366 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008367 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008368 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008369 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008370 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008371 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008372 WARN_ON(1);
8373 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008374 }
8375}
8376
Abel Gordon20b97fe2013-04-18 14:36:25 +03008377
Liran Alone2536742018-06-23 02:35:02 +03008378static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008379 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008380 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008381 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008382 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008383 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008384
Jim Mattsond37f4262017-12-22 12:12:16 -08008385 switch (vmcs_field_width(field)) {
8386 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008387 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008388 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008389 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008390 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008391 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008392 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008393 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008394 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008395 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008396 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008397 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008398 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008399 WARN_ON(1);
8400 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008401 }
8402
8403}
8404
Jim Mattsonf4160e42018-05-29 09:11:33 -07008405/*
8406 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8407 * they have been modified by the L1 guest. Note that the "read-only"
8408 * VM-exit information fields are actually writable if the vCPU is
8409 * configured to support "VMWRITE to any supported field in the VMCS."
8410 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008411static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8412{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008413 const u16 *fields[] = {
8414 shadow_read_write_fields,
8415 shadow_read_only_fields
8416 };
8417 const int max_fields[] = {
8418 max_shadow_read_write_fields,
8419 max_shadow_read_only_fields
8420 };
8421 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008422 unsigned long field;
8423 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008424 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008425
Jan Kiszka282da872014-10-08 18:05:39 +02008426 preempt_disable();
8427
Abel Gordon16f5b902013-04-18 14:38:25 +03008428 vmcs_load(shadow_vmcs);
8429
Jim Mattsonf4160e42018-05-29 09:11:33 -07008430 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8431 for (i = 0; i < max_fields[q]; i++) {
8432 field = fields[q][i];
8433 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008434 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008435 }
8436 /*
8437 * Skip the VM-exit information fields if they are read-only.
8438 */
8439 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8440 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008441 }
8442
8443 vmcs_clear(shadow_vmcs);
8444 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008445
8446 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008447}
8448
Abel Gordonc3114422013-04-18 14:38:55 +03008449static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8450{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008451 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008452 shadow_read_write_fields,
8453 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008454 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008455 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008456 max_shadow_read_write_fields,
8457 max_shadow_read_only_fields
8458 };
8459 int i, q;
8460 unsigned long field;
8461 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008462 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008463
8464 vmcs_load(shadow_vmcs);
8465
Mathias Krausec2bae892013-06-26 20:36:21 +02008466 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008467 for (i = 0; i < max_fields[q]; i++) {
8468 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008469 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008470 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008471 }
8472 }
8473
8474 vmcs_clear(shadow_vmcs);
8475 vmcs_load(vmx->loaded_vmcs->vmcs);
8476}
8477
Nadav Har'El49f705c2011-05-25 23:08:30 +03008478/*
8479 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8480 * used before) all generate the same failure when it is missing.
8481 */
8482static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8483{
8484 struct vcpu_vmx *vmx = to_vmx(vcpu);
8485 if (vmx->nested.current_vmptr == -1ull) {
8486 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 return 0;
8488 }
8489 return 1;
8490}
8491
8492static int handle_vmread(struct kvm_vcpu *vcpu)
8493{
8494 unsigned long field;
8495 u64 field_value;
8496 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8497 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8498 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008499 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008500
Kyle Hueyeb277562016-11-29 12:40:39 -08008501 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008502 return 1;
8503
Kyle Huey6affcbe2016-11-29 12:40:40 -08008504 if (!nested_vmx_check_vmcs12(vcpu))
8505 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008506
Liran Alon6d894f42018-06-23 02:35:09 +03008507 if (!is_guest_mode(vcpu))
8508 vmcs12 = get_vmcs12(vcpu);
8509 else {
8510 /*
8511 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8512 * to shadowed-field sets the ALU flags for VMfailInvalid.
8513 */
8514 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8515 nested_vmx_failInvalid(vcpu);
8516 return kvm_skip_emulated_instruction(vcpu);
8517 }
8518 vmcs12 = get_shadow_vmcs12(vcpu);
8519 }
8520
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008522 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008523 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008524 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008525 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008526 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008527 }
8528 /*
8529 * Now copy part of this value to register or memory, as requested.
8530 * Note that the number of bits actually copied is 32 or 64 depending
8531 * on the guest's mode (32 or 64 bit), not on the given field's length.
8532 */
8533 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008534 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008535 field_value);
8536 } else {
8537 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008538 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008539 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008540 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008541 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8542 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543 }
8544
8545 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008546 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008547}
8548
8549
8550static int handle_vmwrite(struct kvm_vcpu *vcpu)
8551{
8552 unsigned long field;
8553 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008554 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008555 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8556 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008557
Nadav Har'El49f705c2011-05-25 23:08:30 +03008558 /* The value to write might be 32 or 64 bits, depending on L1's long
8559 * mode, and eventually we need to write that into a field of several
8560 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008561 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 * bits into the vmcs12 field.
8563 */
8564 u64 field_value = 0;
8565 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008566 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567
Kyle Hueyeb277562016-11-29 12:40:39 -08008568 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008569 return 1;
8570
Kyle Huey6affcbe2016-11-29 12:40:40 -08008571 if (!nested_vmx_check_vmcs12(vcpu))
8572 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008573
Nadav Har'El49f705c2011-05-25 23:08:30 +03008574 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008575 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008576 (((vmx_instruction_info) >> 3) & 0xf));
8577 else {
8578 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008579 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008580 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008581 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8582 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008583 kvm_inject_page_fault(vcpu, &e);
8584 return 1;
8585 }
8586 }
8587
8588
Nadav Amit27e6fb52014-06-18 17:19:26 +03008589 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008590 /*
8591 * If the vCPU supports "VMWRITE to any supported field in the
8592 * VMCS," then the "read-only" fields are actually read/write.
8593 */
8594 if (vmcs_field_readonly(field) &&
8595 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008596 nested_vmx_failValid(vcpu,
8597 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008598 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008599 }
8600
Liran Alon6d894f42018-06-23 02:35:09 +03008601 if (!is_guest_mode(vcpu))
8602 vmcs12 = get_vmcs12(vcpu);
8603 else {
8604 /*
8605 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8606 * to shadowed-field sets the ALU flags for VMfailInvalid.
8607 */
8608 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8609 nested_vmx_failInvalid(vcpu);
8610 return kvm_skip_emulated_instruction(vcpu);
8611 }
8612 vmcs12 = get_shadow_vmcs12(vcpu);
8613
8614 }
8615
8616 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008617 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008618 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008619 }
8620
Liran Alon6d894f42018-06-23 02:35:09 +03008621 /*
8622 * Do not track vmcs12 dirty-state if in guest-mode
8623 * as we actually dirty shadow vmcs12 instead of vmcs12.
8624 */
8625 if (!is_guest_mode(vcpu)) {
8626 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008627#define SHADOW_FIELD_RW(x) case x:
8628#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008629 /*
8630 * The fields that can be updated by L1 without a vmexit are
8631 * always updated in the vmcs02, the others go down the slow
8632 * path of prepare_vmcs02.
8633 */
8634 break;
8635 default:
8636 vmx->nested.dirty_vmcs12 = true;
8637 break;
8638 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008639 }
8640
Nadav Har'El49f705c2011-05-25 23:08:30 +03008641 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008642 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008643}
8644
Jim Mattsona8bc2842016-11-30 12:03:44 -08008645static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8646{
8647 vmx->nested.current_vmptr = vmptr;
8648 if (enable_shadow_vmcs) {
8649 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8650 SECONDARY_EXEC_SHADOW_VMCS);
8651 vmcs_write64(VMCS_LINK_POINTER,
8652 __pa(vmx->vmcs01.shadow_vmcs));
8653 vmx->nested.sync_shadow_vmcs = true;
8654 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008655 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008656}
8657
Nadav Har'El63846662011-05-25 23:07:29 +03008658/* Emulate the VMPTRLD instruction */
8659static int handle_vmptrld(struct kvm_vcpu *vcpu)
8660{
8661 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008662 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008663
8664 if (!nested_vmx_check_permission(vcpu))
8665 return 1;
8666
Radim Krčmářcbf71272017-05-19 15:48:51 +02008667 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008668 return 1;
8669
Radim Krčmářcbf71272017-05-19 15:48:51 +02008670 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8671 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8672 return kvm_skip_emulated_instruction(vcpu);
8673 }
8674
8675 if (vmptr == vmx->nested.vmxon_ptr) {
8676 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8677 return kvm_skip_emulated_instruction(vcpu);
8678 }
8679
Nadav Har'El63846662011-05-25 23:07:29 +03008680 if (vmx->nested.current_vmptr != vmptr) {
8681 struct vmcs12 *new_vmcs12;
8682 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008683 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8684 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008685 nested_vmx_failInvalid(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 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008689 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008690 (new_vmcs12->hdr.shadow_vmcs &&
8691 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008692 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008693 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008694 nested_vmx_failValid(vcpu,
8695 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008696 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008697 }
Nadav Har'El63846662011-05-25 23:07:29 +03008698
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008699 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008700 /*
8701 * Load VMCS12 from guest memory since it is not already
8702 * cached.
8703 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008704 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8705 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008706 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008707
Jim Mattsona8bc2842016-11-30 12:03:44 -08008708 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008709 }
8710
8711 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008712 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008713}
8714
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008715/* Emulate the VMPTRST instruction */
8716static int handle_vmptrst(struct kvm_vcpu *vcpu)
8717{
8718 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8719 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8720 gva_t vmcs_gva;
8721 struct x86_exception e;
8722
8723 if (!nested_vmx_check_permission(vcpu))
8724 return 1;
8725
8726 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008727 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008728 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008729 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008730 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8731 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8732 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008733 kvm_inject_page_fault(vcpu, &e);
8734 return 1;
8735 }
8736 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008737 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008738}
8739
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008740/* Emulate the INVEPT instruction */
8741static int handle_invept(struct kvm_vcpu *vcpu)
8742{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008743 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008744 u32 vmx_instruction_info, types;
8745 unsigned long type;
8746 gva_t gva;
8747 struct x86_exception e;
8748 struct {
8749 u64 eptp, gpa;
8750 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008751
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008752 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008753 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008754 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008755 kvm_queue_exception(vcpu, UD_VECTOR);
8756 return 1;
8757 }
8758
8759 if (!nested_vmx_check_permission(vcpu))
8760 return 1;
8761
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008762 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008763 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008764
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008765 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008766
Jim Mattson85c856b2016-10-26 08:38:38 -07008767 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008768 nested_vmx_failValid(vcpu,
8769 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008770 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008771 }
8772
8773 /* According to the Intel VMX instruction reference, the memory
8774 * operand is read even if it isn't needed (e.g., for type==global)
8775 */
8776 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008777 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008778 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008779 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008780 kvm_inject_page_fault(vcpu, &e);
8781 return 1;
8782 }
8783
8784 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008785 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008786 /*
8787 * TODO: track mappings and invalidate
8788 * single context requests appropriately
8789 */
8790 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008791 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008792 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008793 nested_vmx_succeed(vcpu);
8794 break;
8795 default:
8796 BUG_ON(1);
8797 break;
8798 }
8799
Kyle Huey6affcbe2016-11-29 12:40:40 -08008800 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008801}
8802
Petr Matouseka642fc32014-09-23 20:22:30 +02008803static int handle_invvpid(struct kvm_vcpu *vcpu)
8804{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008805 struct vcpu_vmx *vmx = to_vmx(vcpu);
8806 u32 vmx_instruction_info;
8807 unsigned long type, types;
8808 gva_t gva;
8809 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008810 struct {
8811 u64 vpid;
8812 u64 gla;
8813 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008814
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008815 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008816 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008817 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008818 kvm_queue_exception(vcpu, UD_VECTOR);
8819 return 1;
8820 }
8821
8822 if (!nested_vmx_check_permission(vcpu))
8823 return 1;
8824
8825 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8826 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8827
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008828 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008829 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008830
Jim Mattson85c856b2016-10-26 08:38:38 -07008831 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008832 nested_vmx_failValid(vcpu,
8833 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008834 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008835 }
8836
8837 /* according to the intel vmx instruction reference, the memory
8838 * operand is read even if it isn't needed (e.g., for type==global)
8839 */
8840 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8841 vmx_instruction_info, false, &gva))
8842 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008843 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008844 kvm_inject_page_fault(vcpu, &e);
8845 return 1;
8846 }
Jim Mattson40352602017-06-28 09:37:37 -07008847 if (operand.vpid >> 16) {
8848 nested_vmx_failValid(vcpu,
8849 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8850 return kvm_skip_emulated_instruction(vcpu);
8851 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008852
8853 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008854 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008855 if (!operand.vpid ||
8856 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008857 nested_vmx_failValid(vcpu,
8858 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8859 return kvm_skip_emulated_instruction(vcpu);
8860 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008861 if (cpu_has_vmx_invvpid_individual_addr() &&
8862 vmx->nested.vpid02) {
8863 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8864 vmx->nested.vpid02, operand.gla);
8865 } else
8866 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8867 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008868 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008869 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008870 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008871 nested_vmx_failValid(vcpu,
8872 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008873 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008874 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008875 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008876 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008877 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008878 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008879 break;
8880 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008881 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008882 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008883 }
8884
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008885 nested_vmx_succeed(vcpu);
8886
Kyle Huey6affcbe2016-11-29 12:40:40 -08008887 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008888}
8889
Junaid Shahideb4b2482018-06-27 14:59:14 -07008890static int handle_invpcid(struct kvm_vcpu *vcpu)
8891{
8892 u32 vmx_instruction_info;
8893 unsigned long type;
8894 bool pcid_enabled;
8895 gva_t gva;
8896 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07008897 unsigned i;
8898 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07008899 struct {
8900 u64 pcid;
8901 u64 gla;
8902 } operand;
8903
8904 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8905 kvm_queue_exception(vcpu, UD_VECTOR);
8906 return 1;
8907 }
8908
8909 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8910 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8911
8912 if (type > 3) {
8913 kvm_inject_gp(vcpu, 0);
8914 return 1;
8915 }
8916
8917 /* According to the Intel instruction reference, the memory operand
8918 * is read even if it isn't needed (e.g., for type==all)
8919 */
8920 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8921 vmx_instruction_info, false, &gva))
8922 return 1;
8923
8924 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8925 kvm_inject_page_fault(vcpu, &e);
8926 return 1;
8927 }
8928
8929 if (operand.pcid >> 12 != 0) {
8930 kvm_inject_gp(vcpu, 0);
8931 return 1;
8932 }
8933
8934 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8935
8936 switch (type) {
8937 case INVPCID_TYPE_INDIV_ADDR:
8938 if ((!pcid_enabled && (operand.pcid != 0)) ||
8939 is_noncanonical_address(operand.gla, vcpu)) {
8940 kvm_inject_gp(vcpu, 0);
8941 return 1;
8942 }
8943 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8944 return kvm_skip_emulated_instruction(vcpu);
8945
8946 case INVPCID_TYPE_SINGLE_CTXT:
8947 if (!pcid_enabled && (operand.pcid != 0)) {
8948 kvm_inject_gp(vcpu, 0);
8949 return 1;
8950 }
8951
8952 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8953 kvm_mmu_sync_roots(vcpu);
8954 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8955 }
8956
Junaid Shahidb94742c2018-06-27 14:59:20 -07008957 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8958 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8959 == operand.pcid)
8960 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07008961
Junaid Shahidb94742c2018-06-27 14:59:20 -07008962 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07008963 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07008964 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07008965 * given PCID, then nothing needs to be done here because a
8966 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008967 */
8968
8969 return kvm_skip_emulated_instruction(vcpu);
8970
8971 case INVPCID_TYPE_ALL_NON_GLOBAL:
8972 /*
8973 * Currently, KVM doesn't mark global entries in the shadow
8974 * page tables, so a non-global flush just degenerates to a
8975 * global flush. If needed, we could optimize this later by
8976 * keeping track of global entries in shadow page tables.
8977 */
8978
8979 /* fall-through */
8980 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8981 kvm_mmu_unload(vcpu);
8982 return kvm_skip_emulated_instruction(vcpu);
8983
8984 default:
8985 BUG(); /* We have already checked above that type <= 3 */
8986 }
8987}
8988
Kai Huang843e4332015-01-28 10:54:28 +08008989static int handle_pml_full(struct kvm_vcpu *vcpu)
8990{
8991 unsigned long exit_qualification;
8992
8993 trace_kvm_pml_full(vcpu->vcpu_id);
8994
8995 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8996
8997 /*
8998 * PML buffer FULL happened while executing iret from NMI,
8999 * "blocked by NMI" bit has to be set before next VM entry.
9000 */
9001 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009002 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009003 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9004 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9005 GUEST_INTR_STATE_NMI);
9006
9007 /*
9008 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9009 * here.., and there's no userspace involvement needed for PML.
9010 */
9011 return 1;
9012}
9013
Yunhong Jiang64672c92016-06-13 14:19:59 -07009014static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9015{
9016 kvm_lapic_expired_hv_timer(vcpu);
9017 return 1;
9018}
9019
Bandan Das41ab9372017-08-03 15:54:43 -04009020static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9021{
9022 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009023 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9024
9025 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009026 switch (address & VMX_EPTP_MT_MASK) {
9027 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009028 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009029 return false;
9030 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009031 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009032 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009033 return false;
9034 break;
9035 default:
9036 return false;
9037 }
9038
David Hildenbrandbb97a012017-08-10 23:15:28 +02009039 /* only 4 levels page-walk length are valid */
9040 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009041 return false;
9042
9043 /* Reserved bits should not be set */
9044 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9045 return false;
9046
9047 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009048 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009049 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009050 return false;
9051 }
9052
9053 return true;
9054}
9055
9056static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9057 struct vmcs12 *vmcs12)
9058{
9059 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9060 u64 address;
9061 bool accessed_dirty;
9062 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9063
9064 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9065 !nested_cpu_has_ept(vmcs12))
9066 return 1;
9067
9068 if (index >= VMFUNC_EPTP_ENTRIES)
9069 return 1;
9070
9071
9072 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9073 &address, index * 8, 8))
9074 return 1;
9075
David Hildenbrandbb97a012017-08-10 23:15:28 +02009076 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009077
9078 /*
9079 * If the (L2) guest does a vmfunc to the currently
9080 * active ept pointer, we don't have to do anything else
9081 */
9082 if (vmcs12->ept_pointer != address) {
9083 if (!valid_ept_address(vcpu, address))
9084 return 1;
9085
9086 kvm_mmu_unload(vcpu);
9087 mmu->ept_ad = accessed_dirty;
9088 mmu->base_role.ad_disabled = !accessed_dirty;
9089 vmcs12->ept_pointer = address;
9090 /*
9091 * TODO: Check what's the correct approach in case
9092 * mmu reload fails. Currently, we just let the next
9093 * reload potentially fail
9094 */
9095 kvm_mmu_reload(vcpu);
9096 }
9097
9098 return 0;
9099}
9100
Bandan Das2a499e42017-08-03 15:54:41 -04009101static int handle_vmfunc(struct kvm_vcpu *vcpu)
9102{
Bandan Das27c42a12017-08-03 15:54:42 -04009103 struct vcpu_vmx *vmx = to_vmx(vcpu);
9104 struct vmcs12 *vmcs12;
9105 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9106
9107 /*
9108 * VMFUNC is only supported for nested guests, but we always enable the
9109 * secondary control for simplicity; for non-nested mode, fake that we
9110 * didn't by injecting #UD.
9111 */
9112 if (!is_guest_mode(vcpu)) {
9113 kvm_queue_exception(vcpu, UD_VECTOR);
9114 return 1;
9115 }
9116
9117 vmcs12 = get_vmcs12(vcpu);
9118 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9119 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009120
9121 switch (function) {
9122 case 0:
9123 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9124 goto fail;
9125 break;
9126 default:
9127 goto fail;
9128 }
9129 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009130
9131fail:
9132 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9133 vmcs_read32(VM_EXIT_INTR_INFO),
9134 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009135 return 1;
9136}
9137
Nadav Har'El0140cae2011-05-25 23:06:28 +03009138/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009139 * The exit handlers return 1 if the exit was handled fully and guest execution
9140 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9141 * to be done to userspace and return 0.
9142 */
Mathias Krause772e0312012-08-30 01:30:19 +02009143static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009144 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9145 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009146 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009147 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009148 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009149 [EXIT_REASON_CR_ACCESS] = handle_cr,
9150 [EXIT_REASON_DR_ACCESS] = handle_dr,
9151 [EXIT_REASON_CPUID] = handle_cpuid,
9152 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9153 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9154 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9155 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009156 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009157 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009158 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009159 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009160 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009161 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009162 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009163 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009164 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009165 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009166 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009167 [EXIT_REASON_VMOFF] = handle_vmoff,
9168 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009169 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9170 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009171 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009172 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009173 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009174 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009175 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009176 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009177 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9178 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009179 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9180 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009181 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009182 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009183 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009184 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009185 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009186 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009187 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009188 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009189 [EXIT_REASON_XSAVES] = handle_xsaves,
9190 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009191 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009192 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009193 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009194 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009195};
9196
9197static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009198 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009199
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009200static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9201 struct vmcs12 *vmcs12)
9202{
9203 unsigned long exit_qualification;
9204 gpa_t bitmap, last_bitmap;
9205 unsigned int port;
9206 int size;
9207 u8 b;
9208
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009209 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009210 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009211
9212 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9213
9214 port = exit_qualification >> 16;
9215 size = (exit_qualification & 7) + 1;
9216
9217 last_bitmap = (gpa_t)-1;
9218 b = -1;
9219
9220 while (size > 0) {
9221 if (port < 0x8000)
9222 bitmap = vmcs12->io_bitmap_a;
9223 else if (port < 0x10000)
9224 bitmap = vmcs12->io_bitmap_b;
9225 else
Joe Perches1d804d02015-03-30 16:46:09 -07009226 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009227 bitmap += (port & 0x7fff) / 8;
9228
9229 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009230 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009231 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009232 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009233 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009234
9235 port++;
9236 size--;
9237 last_bitmap = bitmap;
9238 }
9239
Joe Perches1d804d02015-03-30 16:46:09 -07009240 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009241}
9242
Nadav Har'El644d7112011-05-25 23:12:35 +03009243/*
9244 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9245 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9246 * disinterest in the current event (read or write a specific MSR) by using an
9247 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9248 */
9249static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9250 struct vmcs12 *vmcs12, u32 exit_reason)
9251{
9252 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9253 gpa_t bitmap;
9254
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009255 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009256 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009257
9258 /*
9259 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9260 * for the four combinations of read/write and low/high MSR numbers.
9261 * First we need to figure out which of the four to use:
9262 */
9263 bitmap = vmcs12->msr_bitmap;
9264 if (exit_reason == EXIT_REASON_MSR_WRITE)
9265 bitmap += 2048;
9266 if (msr_index >= 0xc0000000) {
9267 msr_index -= 0xc0000000;
9268 bitmap += 1024;
9269 }
9270
9271 /* Then read the msr_index'th bit from this bitmap: */
9272 if (msr_index < 1024*8) {
9273 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009274 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009275 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009276 return 1 & (b >> (msr_index & 7));
9277 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009278 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009279}
9280
9281/*
9282 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9283 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9284 * intercept (via guest_host_mask etc.) the current event.
9285 */
9286static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9287 struct vmcs12 *vmcs12)
9288{
9289 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9290 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009291 int reg;
9292 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009293
9294 switch ((exit_qualification >> 4) & 3) {
9295 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009296 reg = (exit_qualification >> 8) & 15;
9297 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009298 switch (cr) {
9299 case 0:
9300 if (vmcs12->cr0_guest_host_mask &
9301 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009302 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009303 break;
9304 case 3:
9305 if ((vmcs12->cr3_target_count >= 1 &&
9306 vmcs12->cr3_target_value0 == val) ||
9307 (vmcs12->cr3_target_count >= 2 &&
9308 vmcs12->cr3_target_value1 == val) ||
9309 (vmcs12->cr3_target_count >= 3 &&
9310 vmcs12->cr3_target_value2 == val) ||
9311 (vmcs12->cr3_target_count >= 4 &&
9312 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009313 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009314 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009315 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009316 break;
9317 case 4:
9318 if (vmcs12->cr4_guest_host_mask &
9319 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009320 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009321 break;
9322 case 8:
9323 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009324 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009325 break;
9326 }
9327 break;
9328 case 2: /* clts */
9329 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9330 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009331 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009332 break;
9333 case 1: /* mov from cr */
9334 switch (cr) {
9335 case 3:
9336 if (vmcs12->cpu_based_vm_exec_control &
9337 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009338 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009339 break;
9340 case 8:
9341 if (vmcs12->cpu_based_vm_exec_control &
9342 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009343 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009344 break;
9345 }
9346 break;
9347 case 3: /* lmsw */
9348 /*
9349 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9350 * cr0. Other attempted changes are ignored, with no exit.
9351 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009352 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009353 if (vmcs12->cr0_guest_host_mask & 0xe &
9354 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009355 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009356 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9357 !(vmcs12->cr0_read_shadow & 0x1) &&
9358 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009359 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009360 break;
9361 }
Joe Perches1d804d02015-03-30 16:46:09 -07009362 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009363}
9364
Liran Alona7cde482018-06-23 02:35:10 +03009365static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9366 struct vmcs12 *vmcs12, gpa_t bitmap)
9367{
9368 u32 vmx_instruction_info;
9369 unsigned long field;
9370 u8 b;
9371
9372 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9373 return true;
9374
9375 /* Decode instruction info and find the field to access */
9376 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9377 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9378
9379 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9380 if (field >> 15)
9381 return true;
9382
9383 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9384 return true;
9385
9386 return 1 & (b >> (field & 7));
9387}
9388
Nadav Har'El644d7112011-05-25 23:12:35 +03009389/*
9390 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9391 * should handle it ourselves in L0 (and then continue L2). Only call this
9392 * when in is_guest_mode (L2).
9393 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009394static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009395{
Nadav Har'El644d7112011-05-25 23:12:35 +03009396 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9397 struct vcpu_vmx *vmx = to_vmx(vcpu);
9398 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9399
Jim Mattson4f350c62017-09-14 16:31:44 -07009400 if (vmx->nested.nested_run_pending)
9401 return false;
9402
9403 if (unlikely(vmx->fail)) {
9404 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9405 vmcs_read32(VM_INSTRUCTION_ERROR));
9406 return true;
9407 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009408
David Matlackc9f04402017-08-01 14:00:40 -07009409 /*
9410 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009411 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9412 * Page). The CPU may write to these pages via their host
9413 * physical address while L2 is running, bypassing any
9414 * address-translation-based dirty tracking (e.g. EPT write
9415 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009416 *
9417 * Mark them dirty on every exit from L2 to prevent them from
9418 * getting out of sync with dirty tracking.
9419 */
9420 nested_mark_vmcs12_pages_dirty(vcpu);
9421
Jim Mattson4f350c62017-09-14 16:31:44 -07009422 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9423 vmcs_readl(EXIT_QUALIFICATION),
9424 vmx->idt_vectoring_info,
9425 intr_info,
9426 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9427 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009428
9429 switch (exit_reason) {
9430 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009431 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009432 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009433 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009434 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009435 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009436 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009437 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009438 else if (is_debug(intr_info) &&
9439 vcpu->guest_debug &
9440 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9441 return false;
9442 else if (is_breakpoint(intr_info) &&
9443 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9444 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009445 return vmcs12->exception_bitmap &
9446 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9447 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009448 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009449 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009450 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009451 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009452 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009453 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009454 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009455 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009456 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009457 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009458 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009459 case EXIT_REASON_HLT:
9460 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9461 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009462 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009463 case EXIT_REASON_INVLPG:
9464 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9465 case EXIT_REASON_RDPMC:
9466 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009467 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009468 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009469 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009470 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009471 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009472 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009473 case EXIT_REASON_VMREAD:
9474 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9475 vmcs12->vmread_bitmap);
9476 case EXIT_REASON_VMWRITE:
9477 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9478 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009479 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9480 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009481 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009482 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009483 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009484 /*
9485 * VMX instructions trap unconditionally. This allows L1 to
9486 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9487 */
Joe Perches1d804d02015-03-30 16:46:09 -07009488 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009489 case EXIT_REASON_CR_ACCESS:
9490 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9491 case EXIT_REASON_DR_ACCESS:
9492 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9493 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009494 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009495 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9496 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009497 case EXIT_REASON_MSR_READ:
9498 case EXIT_REASON_MSR_WRITE:
9499 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9500 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009501 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009502 case EXIT_REASON_MWAIT_INSTRUCTION:
9503 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009504 case EXIT_REASON_MONITOR_TRAP_FLAG:
9505 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009506 case EXIT_REASON_MONITOR_INSTRUCTION:
9507 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9508 case EXIT_REASON_PAUSE_INSTRUCTION:
9509 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9510 nested_cpu_has2(vmcs12,
9511 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9512 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009513 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009514 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009515 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009516 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009517 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009518 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009519 /*
9520 * The controls for "virtualize APIC accesses," "APIC-
9521 * register virtualization," and "virtual-interrupt
9522 * delivery" only come from vmcs12.
9523 */
Joe Perches1d804d02015-03-30 16:46:09 -07009524 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009525 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009526 /*
9527 * L0 always deals with the EPT violation. If nested EPT is
9528 * used, and the nested mmu code discovers that the address is
9529 * missing in the guest EPT table (EPT12), the EPT violation
9530 * will be injected with nested_ept_inject_page_fault()
9531 */
Joe Perches1d804d02015-03-30 16:46:09 -07009532 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009533 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009534 /*
9535 * L2 never uses directly L1's EPT, but rather L0's own EPT
9536 * table (shadow on EPT) or a merged EPT table that L0 built
9537 * (EPT on EPT). So any problems with the structure of the
9538 * table is L0's fault.
9539 */
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009541 case EXIT_REASON_INVPCID:
9542 return
9543 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9544 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 case EXIT_REASON_WBINVD:
9546 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9547 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009548 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009549 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9550 /*
9551 * This should never happen, since it is not possible to
9552 * set XSS to a non-zero value---neither in L1 nor in L2.
9553 * If if it were, XSS would have to be checked against
9554 * the XSS exit bitmap in vmcs12.
9555 */
9556 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009557 case EXIT_REASON_PREEMPTION_TIMER:
9558 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009559 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009560 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009561 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009562 case EXIT_REASON_VMFUNC:
9563 /* VM functions are emulated through L2->L0 vmexits. */
9564 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009565 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009566 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009567 }
9568}
9569
Paolo Bonzini7313c692017-07-27 10:31:25 +02009570static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9571{
9572 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9573
9574 /*
9575 * At this point, the exit interruption info in exit_intr_info
9576 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9577 * we need to query the in-kernel LAPIC.
9578 */
9579 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9580 if ((exit_intr_info &
9581 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9582 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9583 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9584 vmcs12->vm_exit_intr_error_code =
9585 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9586 }
9587
9588 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9589 vmcs_readl(EXIT_QUALIFICATION));
9590 return 1;
9591}
9592
Avi Kivity586f9602010-11-18 13:09:54 +02009593static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9594{
9595 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9596 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9597}
9598
Kai Huanga3eaa862015-11-04 13:46:05 +08009599static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009600{
Kai Huanga3eaa862015-11-04 13:46:05 +08009601 if (vmx->pml_pg) {
9602 __free_page(vmx->pml_pg);
9603 vmx->pml_pg = NULL;
9604 }
Kai Huang843e4332015-01-28 10:54:28 +08009605}
9606
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009607static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009608{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009609 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009610 u64 *pml_buf;
9611 u16 pml_idx;
9612
9613 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9614
9615 /* Do nothing if PML buffer is empty */
9616 if (pml_idx == (PML_ENTITY_NUM - 1))
9617 return;
9618
9619 /* PML index always points to next available PML buffer entity */
9620 if (pml_idx >= PML_ENTITY_NUM)
9621 pml_idx = 0;
9622 else
9623 pml_idx++;
9624
9625 pml_buf = page_address(vmx->pml_pg);
9626 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9627 u64 gpa;
9628
9629 gpa = pml_buf[pml_idx];
9630 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009631 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009632 }
9633
9634 /* reset PML index */
9635 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9636}
9637
9638/*
9639 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9640 * Called before reporting dirty_bitmap to userspace.
9641 */
9642static void kvm_flush_pml_buffers(struct kvm *kvm)
9643{
9644 int i;
9645 struct kvm_vcpu *vcpu;
9646 /*
9647 * We only need to kick vcpu out of guest mode here, as PML buffer
9648 * is flushed at beginning of all VMEXITs, and it's obvious that only
9649 * vcpus running in guest are possible to have unflushed GPAs in PML
9650 * buffer.
9651 */
9652 kvm_for_each_vcpu(i, vcpu, kvm)
9653 kvm_vcpu_kick(vcpu);
9654}
9655
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009656static void vmx_dump_sel(char *name, uint32_t sel)
9657{
9658 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009659 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009660 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9661 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9662 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9663}
9664
9665static void vmx_dump_dtsel(char *name, uint32_t limit)
9666{
9667 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9668 name, vmcs_read32(limit),
9669 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9670}
9671
9672static void dump_vmcs(void)
9673{
9674 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9675 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9676 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9677 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9678 u32 secondary_exec_control = 0;
9679 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009680 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009681 int i, n;
9682
9683 if (cpu_has_secondary_exec_ctrls())
9684 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9685
9686 pr_err("*** Guest State ***\n");
9687 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9688 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9689 vmcs_readl(CR0_GUEST_HOST_MASK));
9690 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9691 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9692 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9693 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9694 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9695 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009696 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9697 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9698 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9699 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009700 }
9701 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9702 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9703 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9704 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9705 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9706 vmcs_readl(GUEST_SYSENTER_ESP),
9707 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9708 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9709 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9710 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9711 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9712 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9713 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9714 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9715 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9716 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9717 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9718 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9719 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009720 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9721 efer, vmcs_read64(GUEST_IA32_PAT));
9722 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9723 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009724 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009725 if (cpu_has_load_perf_global_ctrl &&
9726 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009727 pr_err("PerfGlobCtl = 0x%016llx\n",
9728 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009729 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009730 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009731 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9732 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9733 vmcs_read32(GUEST_ACTIVITY_STATE));
9734 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9735 pr_err("InterruptStatus = %04x\n",
9736 vmcs_read16(GUEST_INTR_STATUS));
9737
9738 pr_err("*** Host State ***\n");
9739 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9740 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9741 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9742 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9743 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9744 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9745 vmcs_read16(HOST_TR_SELECTOR));
9746 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9747 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9748 vmcs_readl(HOST_TR_BASE));
9749 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9750 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9751 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9752 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9753 vmcs_readl(HOST_CR4));
9754 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9755 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9756 vmcs_read32(HOST_IA32_SYSENTER_CS),
9757 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9758 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009759 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9760 vmcs_read64(HOST_IA32_EFER),
9761 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009762 if (cpu_has_load_perf_global_ctrl &&
9763 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009764 pr_err("PerfGlobCtl = 0x%016llx\n",
9765 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009766
9767 pr_err("*** Control State ***\n");
9768 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9769 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9770 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9771 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9772 vmcs_read32(EXCEPTION_BITMAP),
9773 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9774 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9775 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9776 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9777 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9778 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9779 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9780 vmcs_read32(VM_EXIT_INTR_INFO),
9781 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9782 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9783 pr_err(" reason=%08x qualification=%016lx\n",
9784 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9785 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9786 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9787 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009788 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009789 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009790 pr_err("TSC Multiplier = 0x%016llx\n",
9791 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009792 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9793 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9794 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9795 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9796 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009797 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009798 n = vmcs_read32(CR3_TARGET_COUNT);
9799 for (i = 0; i + 1 < n; i += 4)
9800 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9801 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9802 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9803 if (i < n)
9804 pr_err("CR3 target%u=%016lx\n",
9805 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9806 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9807 pr_err("PLE Gap=%08x Window=%08x\n",
9808 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9809 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9810 pr_err("Virtual processor ID = 0x%04x\n",
9811 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9812}
9813
Avi Kivity6aa8b732006-12-10 02:21:36 -08009814/*
9815 * The guest has exited. See if we can fix it or if we need userspace
9816 * assistance.
9817 */
Avi Kivity851ba692009-08-24 11:10:17 +03009818static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009819{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009820 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009821 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009822 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009823
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009824 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9825
Kai Huang843e4332015-01-28 10:54:28 +08009826 /*
9827 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9828 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9829 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9830 * mode as if vcpus is in root mode, the PML buffer must has been
9831 * flushed already.
9832 */
9833 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009834 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009835
Mohammed Gamal80ced182009-09-01 12:48:18 +02009836 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009837 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009838 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009839
Paolo Bonzini7313c692017-07-27 10:31:25 +02009840 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9841 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009842
Mohammed Gamal51207022010-05-31 22:40:54 +03009843 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009844 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009845 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9846 vcpu->run->fail_entry.hardware_entry_failure_reason
9847 = exit_reason;
9848 return 0;
9849 }
9850
Avi Kivity29bd8a72007-09-10 17:27:03 +03009851 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009852 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9853 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009854 = vmcs_read32(VM_INSTRUCTION_ERROR);
9855 return 0;
9856 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009857
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009858 /*
9859 * Note:
9860 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9861 * delivery event since it indicates guest is accessing MMIO.
9862 * The vm-exit can be triggered again after return to guest that
9863 * will cause infinite loop.
9864 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009865 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009866 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009867 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009868 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009869 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9870 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9871 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009872 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009873 vcpu->run->internal.data[0] = vectoring_info;
9874 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009875 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9876 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9877 vcpu->run->internal.ndata++;
9878 vcpu->run->internal.data[3] =
9879 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9880 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009881 return 0;
9882 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009883
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009884 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009885 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9886 if (vmx_interrupt_allowed(vcpu)) {
9887 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9888 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9889 vcpu->arch.nmi_pending) {
9890 /*
9891 * This CPU don't support us in finding the end of an
9892 * NMI-blocked window if the guest runs with IRQs
9893 * disabled. So we pull the trigger after 1 s of
9894 * futile waiting, but inform the user about this.
9895 */
9896 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9897 "state on VCPU %d after 1 s timeout\n",
9898 __func__, vcpu->vcpu_id);
9899 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9900 }
9901 }
9902
Avi Kivity6aa8b732006-12-10 02:21:36 -08009903 if (exit_reason < kvm_vmx_max_exit_handlers
9904 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009905 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009906 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009907 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9908 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009909 kvm_queue_exception(vcpu, UD_VECTOR);
9910 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009911 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009912}
9913
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009914static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009915{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009916 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9917
9918 if (is_guest_mode(vcpu) &&
9919 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9920 return;
9921
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009922 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009923 vmcs_write32(TPR_THRESHOLD, 0);
9924 return;
9925 }
9926
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009927 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009928}
9929
Jim Mattson8d860bb2018-05-09 16:56:05 -04009930static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009931{
9932 u32 sec_exec_control;
9933
Jim Mattson8d860bb2018-05-09 16:56:05 -04009934 if (!lapic_in_kernel(vcpu))
9935 return;
9936
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009937 /* Postpone execution until vmcs01 is the current VMCS. */
9938 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009939 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009940 return;
9941 }
9942
Paolo Bonzini35754c92015-07-29 12:05:37 +02009943 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009944 return;
9945
9946 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009947 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9948 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009949
Jim Mattson8d860bb2018-05-09 16:56:05 -04009950 switch (kvm_get_apic_mode(vcpu)) {
9951 case LAPIC_MODE_INVALID:
9952 WARN_ONCE(true, "Invalid local APIC state");
9953 case LAPIC_MODE_DISABLED:
9954 break;
9955 case LAPIC_MODE_XAPIC:
9956 if (flexpriority_enabled) {
9957 sec_exec_control |=
9958 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9959 vmx_flush_tlb(vcpu, true);
9960 }
9961 break;
9962 case LAPIC_MODE_X2APIC:
9963 if (cpu_has_vmx_virtualize_x2apic_mode())
9964 sec_exec_control |=
9965 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9966 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009967 }
9968 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9969
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009970 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009971}
9972
Tang Chen38b99172014-09-24 15:57:54 +08009973static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9974{
Jim Mattsonab5df312018-05-09 17:02:03 -04009975 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009976 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009977 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009978 }
Tang Chen38b99172014-09-24 15:57:54 +08009979}
9980
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009981static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009982{
9983 u16 status;
9984 u8 old;
9985
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009986 if (max_isr == -1)
9987 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009988
9989 status = vmcs_read16(GUEST_INTR_STATUS);
9990 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009991 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009992 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009993 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009994 vmcs_write16(GUEST_INTR_STATUS, status);
9995 }
9996}
9997
9998static void vmx_set_rvi(int vector)
9999{
10000 u16 status;
10001 u8 old;
10002
Wei Wang4114c272014-11-05 10:53:43 +080010003 if (vector == -1)
10004 vector = 0;
10005
Yang Zhangc7c9c562013-01-25 10:18:51 +080010006 status = vmcs_read16(GUEST_INTR_STATUS);
10007 old = (u8)status & 0xff;
10008 if ((u8)vector != old) {
10009 status &= ~0xff;
10010 status |= (u8)vector;
10011 vmcs_write16(GUEST_INTR_STATUS, status);
10012 }
10013}
10014
10015static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10016{
Liran Alon851c1a182017-12-24 18:12:56 +020010017 /*
10018 * When running L2, updating RVI is only relevant when
10019 * vmcs12 virtual-interrupt-delivery enabled.
10020 * However, it can be enabled only when L1 also
10021 * intercepts external-interrupts and in that case
10022 * we should not update vmcs02 RVI but instead intercept
10023 * interrupt. Therefore, do nothing when running L2.
10024 */
10025 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010026 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010027}
10028
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010029static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010030{
10031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010032 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010033 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010034
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010035 WARN_ON(!vcpu->arch.apicv_active);
10036 if (pi_test_on(&vmx->pi_desc)) {
10037 pi_clear_on(&vmx->pi_desc);
10038 /*
10039 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10040 * But on x86 this is just a compiler barrier anyway.
10041 */
10042 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010043 max_irr_updated =
10044 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10045
10046 /*
10047 * If we are running L2 and L1 has a new pending interrupt
10048 * which can be injected, we should re-evaluate
10049 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010050 * If L1 intercepts external-interrupts, we should
10051 * exit from L2 to L1. Otherwise, interrupt should be
10052 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010053 */
Liran Alon851c1a182017-12-24 18:12:56 +020010054 if (is_guest_mode(vcpu) && max_irr_updated) {
10055 if (nested_exit_on_intr(vcpu))
10056 kvm_vcpu_exiting_guest_mode(vcpu);
10057 else
10058 kvm_make_request(KVM_REQ_EVENT, vcpu);
10059 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010060 } else {
10061 max_irr = kvm_lapic_find_highest_irr(vcpu);
10062 }
10063 vmx_hwapic_irr_update(vcpu, max_irr);
10064 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010065}
10066
Andrey Smetanin63086302015-11-10 15:36:32 +030010067static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010068{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010069 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010070 return;
10071
Yang Zhangc7c9c562013-01-25 10:18:51 +080010072 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10073 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10074 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10075 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10076}
10077
Paolo Bonzini967235d2016-12-19 14:03:45 +010010078static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10079{
10080 struct vcpu_vmx *vmx = to_vmx(vcpu);
10081
10082 pi_clear_on(&vmx->pi_desc);
10083 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10084}
10085
Avi Kivity51aa01d2010-07-20 14:31:20 +030010086static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010087{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010088 u32 exit_intr_info = 0;
10089 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010090
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010091 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10092 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010093 return;
10094
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010095 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10096 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10097 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010098
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010099 /* if exit due to PF check for async PF */
10100 if (is_page_fault(exit_intr_info))
10101 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10102
Andi Kleena0861c02009-06-08 17:37:09 +080010103 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010104 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10105 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010106 kvm_machine_check();
10107
Gleb Natapov20f65982009-05-11 13:35:55 +030010108 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010109 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010110 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010111 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010112 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010113 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010114}
Gleb Natapov20f65982009-05-11 13:35:55 +030010115
Yang Zhanga547c6d2013-04-11 19:25:10 +080010116static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10117{
10118 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10119
Yang Zhanga547c6d2013-04-11 19:25:10 +080010120 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10121 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10122 unsigned int vector;
10123 unsigned long entry;
10124 gate_desc *desc;
10125 struct vcpu_vmx *vmx = to_vmx(vcpu);
10126#ifdef CONFIG_X86_64
10127 unsigned long tmp;
10128#endif
10129
10130 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10131 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010132 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010133 asm volatile(
10134#ifdef CONFIG_X86_64
10135 "mov %%" _ASM_SP ", %[sp]\n\t"
10136 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10137 "push $%c[ss]\n\t"
10138 "push %[sp]\n\t"
10139#endif
10140 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010141 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010142 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010143 :
10144#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010145 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010146#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010147 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010148 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010149 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010150 [ss]"i"(__KERNEL_DS),
10151 [cs]"i"(__KERNEL_CS)
10152 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010153 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010154}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010155STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010156
Tom Lendackybc226f02018-05-10 22:06:39 +020010157static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010158{
Tom Lendackybc226f02018-05-10 22:06:39 +020010159 switch (index) {
10160 case MSR_IA32_SMBASE:
10161 /*
10162 * We cannot do SMM unless we can run the guest in big
10163 * real mode.
10164 */
10165 return enable_unrestricted_guest || emulate_invalid_guest_state;
10166 case MSR_AMD64_VIRT_SPEC_CTRL:
10167 /* This is AMD only. */
10168 return false;
10169 default:
10170 return true;
10171 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010172}
10173
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010174static bool vmx_mpx_supported(void)
10175{
10176 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10177 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10178}
10179
Wanpeng Li55412b22014-12-02 19:21:30 +080010180static bool vmx_xsaves_supported(void)
10181{
10182 return vmcs_config.cpu_based_2nd_exec_ctrl &
10183 SECONDARY_EXEC_XSAVES;
10184}
10185
Avi Kivity51aa01d2010-07-20 14:31:20 +030010186static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10187{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010188 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010189 bool unblock_nmi;
10190 u8 vector;
10191 bool idtv_info_valid;
10192
10193 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010194
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010195 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010196 if (vmx->loaded_vmcs->nmi_known_unmasked)
10197 return;
10198 /*
10199 * Can't use vmx->exit_intr_info since we're not sure what
10200 * the exit reason is.
10201 */
10202 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10203 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10204 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10205 /*
10206 * SDM 3: 27.7.1.2 (September 2008)
10207 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10208 * a guest IRET fault.
10209 * SDM 3: 23.2.2 (September 2008)
10210 * Bit 12 is undefined in any of the following cases:
10211 * If the VM exit sets the valid bit in the IDT-vectoring
10212 * information field.
10213 * If the VM exit is due to a double fault.
10214 */
10215 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10216 vector != DF_VECTOR && !idtv_info_valid)
10217 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10218 GUEST_INTR_STATE_NMI);
10219 else
10220 vmx->loaded_vmcs->nmi_known_unmasked =
10221 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10222 & GUEST_INTR_STATE_NMI);
10223 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10224 vmx->loaded_vmcs->vnmi_blocked_time +=
10225 ktime_to_ns(ktime_sub(ktime_get(),
10226 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010227}
10228
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010229static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010230 u32 idt_vectoring_info,
10231 int instr_len_field,
10232 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010233{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010234 u8 vector;
10235 int type;
10236 bool idtv_info_valid;
10237
10238 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010239
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010240 vcpu->arch.nmi_injected = false;
10241 kvm_clear_exception_queue(vcpu);
10242 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010243
10244 if (!idtv_info_valid)
10245 return;
10246
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010247 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010248
Avi Kivity668f6122008-07-02 09:28:55 +030010249 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10250 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010251
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010252 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010253 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010254 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010255 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010256 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010257 * Clear bit "block by NMI" before VM entry if a NMI
10258 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010259 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010260 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010261 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010262 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010263 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010264 /* fall through */
10265 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010266 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010267 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010268 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010269 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010270 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010271 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010272 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010273 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010274 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010275 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010276 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010277 break;
10278 default:
10279 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010280 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010281}
10282
Avi Kivity83422e12010-07-20 14:43:23 +030010283static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10284{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010285 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010286 VM_EXIT_INSTRUCTION_LEN,
10287 IDT_VECTORING_ERROR_CODE);
10288}
10289
Avi Kivityb463a6f2010-07-20 15:06:17 +030010290static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10291{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010292 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010293 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10294 VM_ENTRY_INSTRUCTION_LEN,
10295 VM_ENTRY_EXCEPTION_ERROR_CODE);
10296
10297 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10298}
10299
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010300static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10301{
10302 int i, nr_msrs;
10303 struct perf_guest_switch_msr *msrs;
10304
10305 msrs = perf_guest_get_msrs(&nr_msrs);
10306
10307 if (!msrs)
10308 return;
10309
10310 for (i = 0; i < nr_msrs; i++)
10311 if (msrs[i].host == msrs[i].guest)
10312 clear_atomic_switch_msr(vmx, msrs[i].msr);
10313 else
10314 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10315 msrs[i].host);
10316}
10317
Jiang Biao33365e72016-11-03 15:03:37 +080010318static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010319{
10320 struct vcpu_vmx *vmx = to_vmx(vcpu);
10321 u64 tscl;
10322 u32 delta_tsc;
10323
10324 if (vmx->hv_deadline_tsc == -1)
10325 return;
10326
10327 tscl = rdtsc();
10328 if (vmx->hv_deadline_tsc > tscl)
10329 /* sure to be 32 bit only because checked on set_hv_timer */
10330 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10331 cpu_preemption_timer_multi);
10332 else
10333 delta_tsc = 0;
10334
10335 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10336}
10337
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010338static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010339{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010340 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010341 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010342
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010343 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010344 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010345 vmx->loaded_vmcs->soft_vnmi_blocked))
10346 vmx->loaded_vmcs->entry_time = ktime_get();
10347
Avi Kivity104f2262010-11-18 13:12:52 +020010348 /* Don't enter VMX if guest state is invalid, let the exit handler
10349 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010350 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010351 return;
10352
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010353 if (vmx->ple_window_dirty) {
10354 vmx->ple_window_dirty = false;
10355 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10356 }
10357
Abel Gordon012f83c2013-04-18 14:39:25 +030010358 if (vmx->nested.sync_shadow_vmcs) {
10359 copy_vmcs12_to_shadow(vmx);
10360 vmx->nested.sync_shadow_vmcs = false;
10361 }
10362
Avi Kivity104f2262010-11-18 13:12:52 +020010363 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10364 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10365 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10366 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10367
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010368 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010369 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010370 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010371 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010372 }
10373
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010374 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010375 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010376 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010377 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010378 }
10379
Avi Kivity104f2262010-11-18 13:12:52 +020010380 /* When single-stepping over STI and MOV SS, we must clear the
10381 * corresponding interruptibility bits in the guest state. Otherwise
10382 * vmentry fails as it then expects bit 14 (BS) in pending debug
10383 * exceptions being set, but that's not correct for the guest debugging
10384 * case. */
10385 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10386 vmx_set_interrupt_shadow(vcpu, 0);
10387
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010388 if (static_cpu_has(X86_FEATURE_PKU) &&
10389 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10390 vcpu->arch.pkru != vmx->host_pkru)
10391 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010392
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010393 atomic_switch_perf_msrs(vmx);
10394
Yunhong Jiang64672c92016-06-13 14:19:59 -070010395 vmx_arm_hv_timer(vcpu);
10396
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010397 /*
10398 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10399 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10400 * is no need to worry about the conditional branch over the wrmsr
10401 * being speculatively taken.
10402 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010403 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010404
Nadav Har'Eld462b812011-05-24 15:26:10 +030010405 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010406
10407 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10408 (unsigned long)&current_evmcs->host_rsp : 0;
10409
Avi Kivity104f2262010-11-18 13:12:52 +020010410 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010411 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010412 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10413 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10414 "push %%" _ASM_CX " \n\t"
10415 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010416 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010417 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010418 /* Avoid VMWRITE when Enlightened VMCS is in use */
10419 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10420 "jz 2f \n\t"
10421 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10422 "jmp 1f \n\t"
10423 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010424 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010425 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010426 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010427 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10428 "mov %%cr2, %%" _ASM_DX " \n\t"
10429 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010430 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010431 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010432 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010433 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010434 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010435 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010436 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10437 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10438 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10439 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10440 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10441 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010442#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010443 "mov %c[r8](%0), %%r8 \n\t"
10444 "mov %c[r9](%0), %%r9 \n\t"
10445 "mov %c[r10](%0), %%r10 \n\t"
10446 "mov %c[r11](%0), %%r11 \n\t"
10447 "mov %c[r12](%0), %%r12 \n\t"
10448 "mov %c[r13](%0), %%r13 \n\t"
10449 "mov %c[r14](%0), %%r14 \n\t"
10450 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010451#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010452 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010453
Avi Kivity6aa8b732006-12-10 02:21:36 -080010454 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010455 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010456 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010457 "jmp 2f \n\t"
10458 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10459 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010460 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010461 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010462 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010463 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010464 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10465 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10466 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10467 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10468 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10469 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10470 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010471#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010472 "mov %%r8, %c[r8](%0) \n\t"
10473 "mov %%r9, %c[r9](%0) \n\t"
10474 "mov %%r10, %c[r10](%0) \n\t"
10475 "mov %%r11, %c[r11](%0) \n\t"
10476 "mov %%r12, %c[r12](%0) \n\t"
10477 "mov %%r13, %c[r13](%0) \n\t"
10478 "mov %%r14, %c[r14](%0) \n\t"
10479 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010480 "xor %%r8d, %%r8d \n\t"
10481 "xor %%r9d, %%r9d \n\t"
10482 "xor %%r10d, %%r10d \n\t"
10483 "xor %%r11d, %%r11d \n\t"
10484 "xor %%r12d, %%r12d \n\t"
10485 "xor %%r13d, %%r13d \n\t"
10486 "xor %%r14d, %%r14d \n\t"
10487 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010488#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010489 "mov %%cr2, %%" _ASM_AX " \n\t"
10490 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010491
Jim Mattson0cb5b302018-01-03 14:31:38 -080010492 "xor %%eax, %%eax \n\t"
10493 "xor %%ebx, %%ebx \n\t"
10494 "xor %%esi, %%esi \n\t"
10495 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010496 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010497 ".pushsection .rodata \n\t"
10498 ".global vmx_return \n\t"
10499 "vmx_return: " _ASM_PTR " 2b \n\t"
10500 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010501 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010502 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010503 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010504 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010505 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10506 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10507 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10508 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10509 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10510 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10511 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010512#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010513 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10514 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10515 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10516 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10517 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10518 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10519 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10520 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010521#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010522 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10523 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010524 : "cc", "memory"
10525#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010526 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010527 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010528#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010529 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010530#endif
10531 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010532
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010533 /*
10534 * We do not use IBRS in the kernel. If this vCPU has used the
10535 * SPEC_CTRL MSR it may have left it on; save the value and
10536 * turn it off. This is much more efficient than blindly adding
10537 * it to the atomic save/restore list. Especially as the former
10538 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10539 *
10540 * For non-nested case:
10541 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10542 * save it.
10543 *
10544 * For nested case:
10545 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10546 * save it.
10547 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010548 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010549 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010550
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010551 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010552
David Woodhouse117cc7a2018-01-12 11:11:27 +000010553 /* Eliminate branch target predictions from guest mode */
10554 vmexit_fill_RSB();
10555
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010556 /* All fields are clean at this point */
10557 if (static_branch_unlikely(&enable_evmcs))
10558 current_evmcs->hv_clean_fields |=
10559 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10560
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010561 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010562 if (vmx->host_debugctlmsr)
10563 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010564
Avi Kivityaa67f602012-08-01 16:48:03 +030010565#ifndef CONFIG_X86_64
10566 /*
10567 * The sysexit path does not restore ds/es, so we must set them to
10568 * a reasonable value ourselves.
10569 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010570 * We can't defer this to vmx_prepare_switch_to_host() since that
10571 * function may be executed in interrupt context, which saves and
10572 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010573 */
10574 loadsegment(ds, __USER_DS);
10575 loadsegment(es, __USER_DS);
10576#endif
10577
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010578 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010579 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010580 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010581 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010582 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010583 vcpu->arch.regs_dirty = 0;
10584
Gleb Natapove0b890d2013-09-25 12:51:33 +030010585 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010586 * eager fpu is enabled if PKEY is supported and CR4 is switched
10587 * back on host, so it is safe to read guest PKRU from current
10588 * XSAVE.
10589 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010590 if (static_cpu_has(X86_FEATURE_PKU) &&
10591 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10592 vcpu->arch.pkru = __read_pkru();
10593 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010594 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010595 }
10596
Gleb Natapove0b890d2013-09-25 12:51:33 +030010597 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010598 vmx->idt_vectoring_info = 0;
10599
10600 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10601 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10602 return;
10603
10604 vmx->loaded_vmcs->launched = 1;
10605 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010606
Avi Kivity51aa01d2010-07-20 14:31:20 +030010607 vmx_complete_atomic_exit(vmx);
10608 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010609 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010610}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010611STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010612
Sean Christopherson434a1e92018-03-20 12:17:18 -070010613static struct kvm *vmx_vm_alloc(void)
10614{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010615 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010616 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010617}
10618
10619static void vmx_vm_free(struct kvm *kvm)
10620{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010621 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010622}
10623
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010624static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010625{
10626 struct vcpu_vmx *vmx = to_vmx(vcpu);
10627 int cpu;
10628
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010629 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010630 return;
10631
10632 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010633 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010634 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010635 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010636 put_cpu();
10637}
10638
Jim Mattson2f1fe812016-07-08 15:36:06 -070010639/*
10640 * Ensure that the current vmcs of the logical processor is the
10641 * vmcs01 of the vcpu before calling free_nested().
10642 */
10643static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10644{
10645 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010646
Christoffer Dallec7660c2017-12-04 21:35:23 +010010647 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010648 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010649 free_nested(vmx);
10650 vcpu_put(vcpu);
10651}
10652
Avi Kivity6aa8b732006-12-10 02:21:36 -080010653static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10654{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010655 struct vcpu_vmx *vmx = to_vmx(vcpu);
10656
Kai Huang843e4332015-01-28 10:54:28 +080010657 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010658 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010659 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010660 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010661 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010662 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010663 kfree(vmx->guest_msrs);
10664 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010665 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010666}
10667
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010668static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010669{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010670 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010671 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010672 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010673 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010674
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010675 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010676 return ERR_PTR(-ENOMEM);
10677
Wanpeng Li991e7a02015-09-16 17:30:05 +080010678 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010679
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010680 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10681 if (err)
10682 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010683
Peter Feiner4e595162016-07-07 14:49:58 -070010684 err = -ENOMEM;
10685
10686 /*
10687 * If PML is turned on, failure on enabling PML just results in failure
10688 * of creating the vcpu, therefore we can simplify PML logic (by
10689 * avoiding dealing with cases, such as enabling PML partially on vcpus
10690 * for the guest, etc.
10691 */
10692 if (enable_pml) {
10693 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10694 if (!vmx->pml_pg)
10695 goto uninit_vcpu;
10696 }
10697
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010698 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010699 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10700 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010701
Peter Feiner4e595162016-07-07 14:49:58 -070010702 if (!vmx->guest_msrs)
10703 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010704
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010705 err = alloc_loaded_vmcs(&vmx->vmcs01);
10706 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010707 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010708
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010709 msr_bitmap = vmx->vmcs01.msr_bitmap;
10710 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10711 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10712 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10713 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10714 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10715 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10716 vmx->msr_bitmap_mode = 0;
10717
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010718 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010719 cpu = get_cpu();
10720 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010721 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010722 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010723 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010724 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010725 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010726 err = alloc_apic_access_page(kvm);
10727 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010728 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010729 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010730
Sean Christophersone90008d2018-03-05 12:04:37 -080010731 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010732 err = init_rmode_identity_map(kvm);
10733 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010734 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010735 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010736
Wanpeng Li5c614b32015-10-13 09:18:36 -070010737 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010738 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10739 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010740 vmx->nested.vpid02 = allocate_vpid();
10741 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010742
Wincy Van705699a2015-02-03 23:58:17 +080010743 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010744 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010745
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010746 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10747
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010748 /*
10749 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10750 * or POSTED_INTR_WAKEUP_VECTOR.
10751 */
10752 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10753 vmx->pi_desc.sn = 1;
10754
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010755 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010756
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010757free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010758 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010759 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010760free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010761 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010762free_pml:
10763 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010764uninit_vcpu:
10765 kvm_vcpu_uninit(&vmx->vcpu);
10766free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010767 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010768 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010769 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010770}
10771
Wanpeng Lib31c1142018-03-12 04:53:04 -070010772static int vmx_vm_init(struct kvm *kvm)
10773{
Tianyu Lan877ad952018-07-19 08:40:23 +000010774 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10775
Wanpeng Lib31c1142018-03-12 04:53:04 -070010776 if (!ple_gap)
10777 kvm->arch.pause_in_guest = true;
10778 return 0;
10779}
10780
Yang, Sheng002c7f72007-07-31 14:23:01 +030010781static void __init vmx_check_processor_compat(void *rtn)
10782{
10783 struct vmcs_config vmcs_conf;
10784
10785 *(int *)rtn = 0;
10786 if (setup_vmcs_config(&vmcs_conf) < 0)
10787 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010788 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010789 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10790 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10791 smp_processor_id());
10792 *(int *)rtn = -EIO;
10793 }
10794}
10795
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010796static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010797{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010798 u8 cache;
10799 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010800
Sheng Yang522c68c2009-04-27 20:35:43 +080010801 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010802 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010803 * 2. EPT with VT-d:
10804 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010805 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010806 * b. VT-d with snooping control feature: snooping control feature of
10807 * VT-d engine can guarantee the cache correctness. Just set it
10808 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010809 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010810 * consistent with host MTRR
10811 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010812 if (is_mmio) {
10813 cache = MTRR_TYPE_UNCACHABLE;
10814 goto exit;
10815 }
10816
10817 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010818 ipat = VMX_EPT_IPAT_BIT;
10819 cache = MTRR_TYPE_WRBACK;
10820 goto exit;
10821 }
10822
10823 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10824 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010825 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010826 cache = MTRR_TYPE_WRBACK;
10827 else
10828 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010829 goto exit;
10830 }
10831
Xiao Guangrongff536042015-06-15 16:55:22 +080010832 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010833
10834exit:
10835 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010836}
10837
Sheng Yang17cc3932010-01-05 19:02:27 +080010838static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010839{
Sheng Yang878403b2010-01-05 19:02:29 +080010840 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10841 return PT_DIRECTORY_LEVEL;
10842 else
10843 /* For shadow and EPT supported 1GB page */
10844 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010845}
10846
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010847static void vmcs_set_secondary_exec_control(u32 new_ctl)
10848{
10849 /*
10850 * These bits in the secondary execution controls field
10851 * are dynamic, the others are mostly based on the hypervisor
10852 * architecture and the guest's CPUID. Do not touch the
10853 * dynamic bits.
10854 */
10855 u32 mask =
10856 SECONDARY_EXEC_SHADOW_VMCS |
10857 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010858 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10859 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010860
10861 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10862
10863 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10864 (new_ctl & ~mask) | (cur_ctl & mask));
10865}
10866
David Matlack8322ebb2016-11-29 18:14:09 -080010867/*
10868 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10869 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10870 */
10871static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10872{
10873 struct vcpu_vmx *vmx = to_vmx(vcpu);
10874 struct kvm_cpuid_entry2 *entry;
10875
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010876 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10877 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010878
10879#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10880 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010881 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010882} while (0)
10883
10884 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10885 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10886 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10887 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10888 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10889 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10890 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10891 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10892 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10893 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10894 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10895 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10896 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10897 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10898 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10899
10900 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10901 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10902 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10903 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10904 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010905 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010906
10907#undef cr4_fixed1_update
10908}
10909
Sheng Yang0e851882009-12-18 16:48:46 +080010910static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10911{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010912 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010913
Paolo Bonzini80154d72017-08-24 13:55:35 +020010914 if (cpu_has_secondary_exec_ctrls()) {
10915 vmx_compute_secondary_exec_control(vmx);
10916 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010917 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010918
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010919 if (nested_vmx_allowed(vcpu))
10920 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10921 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10922 else
10923 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10924 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010925
10926 if (nested_vmx_allowed(vcpu))
10927 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010928}
10929
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010930static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10931{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010932 if (func == 1 && nested)
10933 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010934}
10935
Yang Zhang25d92082013-08-06 12:00:32 +030010936static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10937 struct x86_exception *fault)
10938{
Jan Kiszka533558b2014-01-04 18:47:20 +010010939 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010941 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010942 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010943
Bandan Dasc5f983f2017-05-05 15:25:14 -040010944 if (vmx->nested.pml_full) {
10945 exit_reason = EXIT_REASON_PML_FULL;
10946 vmx->nested.pml_full = false;
10947 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10948 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010949 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010950 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010951 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010952
10953 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010954 vmcs12->guest_physical_address = fault->address;
10955}
10956
Peter Feiner995f00a2017-06-30 17:26:32 -070010957static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10958{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010959 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010960}
10961
Nadav Har'El155a97a2013-08-05 11:07:16 +030010962/* Callbacks for nested_ept_init_mmu_context: */
10963
10964static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10965{
10966 /* return the page table to be shadowed - in our case, EPT12 */
10967 return get_vmcs12(vcpu)->ept_pointer;
10968}
10969
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010970static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010971{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010972 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010973 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010974 return 1;
10975
Paolo Bonziniad896af2013-10-02 16:56:14 +020010976 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010977 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010978 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010979 nested_ept_ad_enabled(vcpu),
10980 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010981 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10982 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10983 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10984
10985 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010986 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010987}
10988
10989static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10990{
10991 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10992}
10993
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010994static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10995 u16 error_code)
10996{
10997 bool inequality, bit;
10998
10999 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11000 inequality =
11001 (error_code & vmcs12->page_fault_error_code_mask) !=
11002 vmcs12->page_fault_error_code_match;
11003 return inequality ^ bit;
11004}
11005
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011006static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11007 struct x86_exception *fault)
11008{
11009 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11010
11011 WARN_ON(!is_guest_mode(vcpu));
11012
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011013 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11014 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011015 vmcs12->vm_exit_intr_error_code = fault->error_code;
11016 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11017 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11018 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11019 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011020 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011021 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011022 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011023}
11024
Paolo Bonzinic9923842017-12-13 14:16:30 +010011025static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11026 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011027
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011028static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011029{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011030 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011031 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011032 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011033 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011034
11035 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011036 /*
11037 * Translate L1 physical address to host physical
11038 * address for vmcs02. Keep the page pinned, so this
11039 * physical address remains valid. We keep a reference
11040 * to it so we can release it later.
11041 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011042 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011043 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011044 vmx->nested.apic_access_page = NULL;
11045 }
11046 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011047 /*
11048 * If translation failed, no matter: This feature asks
11049 * to exit when accessing the given address, and if it
11050 * can never be accessed, this feature won't do
11051 * anything anyway.
11052 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011053 if (!is_error_page(page)) {
11054 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011055 hpa = page_to_phys(vmx->nested.apic_access_page);
11056 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11057 } else {
11058 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11059 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11060 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011061 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011062
11063 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011064 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011065 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011066 vmx->nested.virtual_apic_page = NULL;
11067 }
11068 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011069
11070 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011071 * If translation failed, VM entry will fail because
11072 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11073 * Failing the vm entry is _not_ what the processor
11074 * does but it's basically the only possibility we
11075 * have. We could still enter the guest if CR8 load
11076 * exits are enabled, CR8 store exits are enabled, and
11077 * virtualize APIC access is disabled; in this case
11078 * the processor would never use the TPR shadow and we
11079 * could simply clear the bit from the execution
11080 * control. But such a configuration is useless, so
11081 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011082 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011083 if (!is_error_page(page)) {
11084 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011085 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11086 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11087 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011088 }
11089
Wincy Van705699a2015-02-03 23:58:17 +080011090 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011091 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11092 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011093 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011094 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011095 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011096 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11097 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011098 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011099 vmx->nested.pi_desc_page = page;
11100 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011101 vmx->nested.pi_desc =
11102 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11103 (unsigned long)(vmcs12->posted_intr_desc_addr &
11104 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011105 vmcs_write64(POSTED_INTR_DESC_ADDR,
11106 page_to_phys(vmx->nested.pi_desc_page) +
11107 (unsigned long)(vmcs12->posted_intr_desc_addr &
11108 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011109 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011110 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011111 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11112 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011113 else
11114 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11115 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011116}
11117
Jan Kiszkaf41245002014-03-07 20:03:13 +010011118static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11119{
11120 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11121 struct vcpu_vmx *vmx = to_vmx(vcpu);
11122
11123 if (vcpu->arch.virtual_tsc_khz == 0)
11124 return;
11125
11126 /* Make sure short timeouts reliably trigger an immediate vmexit.
11127 * hrtimer_start does not guarantee this. */
11128 if (preemption_timeout <= 1) {
11129 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11130 return;
11131 }
11132
11133 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11134 preemption_timeout *= 1000000;
11135 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11136 hrtimer_start(&vmx->nested.preemption_timer,
11137 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11138}
11139
Jim Mattson56a20512017-07-06 16:33:06 -070011140static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11141 struct vmcs12 *vmcs12)
11142{
11143 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11144 return 0;
11145
11146 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11147 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11148 return -EINVAL;
11149
11150 return 0;
11151}
11152
Wincy Van3af18d92015-02-03 23:49:31 +080011153static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11154 struct vmcs12 *vmcs12)
11155{
Wincy Van3af18d92015-02-03 23:49:31 +080011156 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11157 return 0;
11158
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011159 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011160 return -EINVAL;
11161
11162 return 0;
11163}
11164
Jim Mattson712b12d2017-08-24 13:24:47 -070011165static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11166 struct vmcs12 *vmcs12)
11167{
11168 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11169 return 0;
11170
11171 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11172 return -EINVAL;
11173
11174 return 0;
11175}
11176
Wincy Van3af18d92015-02-03 23:49:31 +080011177/*
11178 * Merge L0's and L1's MSR bitmap, return false to indicate that
11179 * we do not use the hardware.
11180 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011181static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11182 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011183{
Wincy Van82f0dd42015-02-03 23:57:18 +080011184 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011185 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011186 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011187 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011188 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011189 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011190 *
11191 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11192 * ensures that we do not accidentally generate an L02 MSR bitmap
11193 * from the L12 MSR bitmap that is too permissive.
11194 * 2. That L1 or L2s have actually used the MSR. This avoids
11195 * unnecessarily merging of the bitmap if the MSR is unused. This
11196 * works properly because we only update the L01 MSR bitmap lazily.
11197 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11198 * updated to reflect this when L1 (or its L2s) actually write to
11199 * the MSR.
11200 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011201 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11202 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011203
Paolo Bonzinic9923842017-12-13 14:16:30 +010011204 /* Nothing to do if the MSR bitmap is not in use. */
11205 if (!cpu_has_vmx_msr_bitmap() ||
11206 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11207 return false;
11208
Ashok Raj15d45072018-02-01 22:59:43 +010011209 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011210 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011211 return false;
11212
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011213 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11214 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011215 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011216
Radim Krčmářd048c092016-08-08 20:16:22 +020011217 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011218 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11219 /*
11220 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11221 * just lets the processor take the value from the virtual-APIC page;
11222 * take those 256 bits directly from the L1 bitmap.
11223 */
11224 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11225 unsigned word = msr / BITS_PER_LONG;
11226 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11227 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011228 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011229 } else {
11230 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11231 unsigned word = msr / BITS_PER_LONG;
11232 msr_bitmap_l0[word] = ~0;
11233 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11234 }
11235 }
11236
11237 nested_vmx_disable_intercept_for_msr(
11238 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011239 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011240 MSR_TYPE_W);
11241
11242 if (nested_cpu_has_vid(vmcs12)) {
11243 nested_vmx_disable_intercept_for_msr(
11244 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011245 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011246 MSR_TYPE_W);
11247 nested_vmx_disable_intercept_for_msr(
11248 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011249 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011250 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011251 }
Ashok Raj15d45072018-02-01 22:59:43 +010011252
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011253 if (spec_ctrl)
11254 nested_vmx_disable_intercept_for_msr(
11255 msr_bitmap_l1, msr_bitmap_l0,
11256 MSR_IA32_SPEC_CTRL,
11257 MSR_TYPE_R | MSR_TYPE_W);
11258
Ashok Raj15d45072018-02-01 22:59:43 +010011259 if (pred_cmd)
11260 nested_vmx_disable_intercept_for_msr(
11261 msr_bitmap_l1, msr_bitmap_l0,
11262 MSR_IA32_PRED_CMD,
11263 MSR_TYPE_W);
11264
Wincy Vanf2b93282015-02-03 23:56:03 +080011265 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011266 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011267
11268 return true;
11269}
11270
Liran Alon61ada742018-06-23 02:35:08 +030011271static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11272 struct vmcs12 *vmcs12)
11273{
11274 struct vmcs12 *shadow;
11275 struct page *page;
11276
11277 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11278 vmcs12->vmcs_link_pointer == -1ull)
11279 return;
11280
11281 shadow = get_shadow_vmcs12(vcpu);
11282 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11283
11284 memcpy(shadow, kmap(page), VMCS12_SIZE);
11285
11286 kunmap(page);
11287 kvm_release_page_clean(page);
11288}
11289
11290static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11291 struct vmcs12 *vmcs12)
11292{
11293 struct vcpu_vmx *vmx = to_vmx(vcpu);
11294
11295 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11296 vmcs12->vmcs_link_pointer == -1ull)
11297 return;
11298
11299 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11300 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11301}
11302
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011303static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11304 struct vmcs12 *vmcs12)
11305{
11306 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11307 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11308 return -EINVAL;
11309 else
11310 return 0;
11311}
11312
Wincy Vanf2b93282015-02-03 23:56:03 +080011313static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11314 struct vmcs12 *vmcs12)
11315{
Wincy Van82f0dd42015-02-03 23:57:18 +080011316 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011317 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011318 !nested_cpu_has_vid(vmcs12) &&
11319 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011320 return 0;
11321
11322 /*
11323 * If virtualize x2apic mode is enabled,
11324 * virtualize apic access must be disabled.
11325 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011326 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11327 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011328 return -EINVAL;
11329
Wincy Van608406e2015-02-03 23:57:51 +080011330 /*
11331 * If virtual interrupt delivery is enabled,
11332 * we must exit on external interrupts.
11333 */
11334 if (nested_cpu_has_vid(vmcs12) &&
11335 !nested_exit_on_intr(vcpu))
11336 return -EINVAL;
11337
Wincy Van705699a2015-02-03 23:58:17 +080011338 /*
11339 * bits 15:8 should be zero in posted_intr_nv,
11340 * the descriptor address has been already checked
11341 * in nested_get_vmcs12_pages.
11342 */
11343 if (nested_cpu_has_posted_intr(vmcs12) &&
11344 (!nested_cpu_has_vid(vmcs12) ||
11345 !nested_exit_intr_ack_set(vcpu) ||
11346 vmcs12->posted_intr_nv & 0xff00))
11347 return -EINVAL;
11348
Wincy Vanf2b93282015-02-03 23:56:03 +080011349 /* tpr shadow is needed by all apicv features. */
11350 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11351 return -EINVAL;
11352
11353 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011354}
11355
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011356static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11357 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011358 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011359{
Liran Alone2536742018-06-23 02:35:02 +030011360 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011361 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011362 u64 count, addr;
11363
Liran Alone2536742018-06-23 02:35:02 +030011364 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11365 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011366 WARN_ON(1);
11367 return -EINVAL;
11368 }
11369 if (count == 0)
11370 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011371 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011372 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11373 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011374 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011375 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11376 addr_field, maxphyaddr, count, addr);
11377 return -EINVAL;
11378 }
11379 return 0;
11380}
11381
11382static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11383 struct vmcs12 *vmcs12)
11384{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011385 if (vmcs12->vm_exit_msr_load_count == 0 &&
11386 vmcs12->vm_exit_msr_store_count == 0 &&
11387 vmcs12->vm_entry_msr_load_count == 0)
11388 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011389 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011390 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011391 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011392 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011393 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011394 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011395 return -EINVAL;
11396 return 0;
11397}
11398
Bandan Dasc5f983f2017-05-05 15:25:14 -040011399static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11400 struct vmcs12 *vmcs12)
11401{
11402 u64 address = vmcs12->pml_address;
11403 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11404
11405 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11406 if (!nested_cpu_has_ept(vmcs12) ||
11407 !IS_ALIGNED(address, 4096) ||
11408 address >> maxphyaddr)
11409 return -EINVAL;
11410 }
11411
11412 return 0;
11413}
11414
Liran Alona8a7c022018-06-23 02:35:06 +030011415static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11416 struct vmcs12 *vmcs12)
11417{
11418 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11419 return 0;
11420
11421 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11422 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11423 return -EINVAL;
11424
11425 return 0;
11426}
11427
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011428static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11429 struct vmx_msr_entry *e)
11430{
11431 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011432 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011433 return -EINVAL;
11434 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11435 e->index == MSR_IA32_UCODE_REV)
11436 return -EINVAL;
11437 if (e->reserved != 0)
11438 return -EINVAL;
11439 return 0;
11440}
11441
11442static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11443 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011444{
11445 if (e->index == MSR_FS_BASE ||
11446 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011447 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11448 nested_vmx_msr_check_common(vcpu, e))
11449 return -EINVAL;
11450 return 0;
11451}
11452
11453static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11454 struct vmx_msr_entry *e)
11455{
11456 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11457 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011458 return -EINVAL;
11459 return 0;
11460}
11461
11462/*
11463 * Load guest's/host's msr at nested entry/exit.
11464 * return 0 for success, entry index for failure.
11465 */
11466static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11467{
11468 u32 i;
11469 struct vmx_msr_entry e;
11470 struct msr_data msr;
11471
11472 msr.host_initiated = false;
11473 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011474 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11475 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011476 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011477 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11478 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011479 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011480 }
11481 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011482 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011483 "%s check failed (%u, 0x%x, 0x%x)\n",
11484 __func__, i, e.index, e.reserved);
11485 goto fail;
11486 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011487 msr.index = e.index;
11488 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011489 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011490 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011491 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11492 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011493 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011494 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011495 }
11496 return 0;
11497fail:
11498 return i + 1;
11499}
11500
11501static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11502{
11503 u32 i;
11504 struct vmx_msr_entry e;
11505
11506 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011507 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011508 if (kvm_vcpu_read_guest(vcpu,
11509 gpa + i * sizeof(e),
11510 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011511 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011512 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11513 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011514 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011515 }
11516 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011517 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011518 "%s check failed (%u, 0x%x, 0x%x)\n",
11519 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011520 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011521 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011522 msr_info.host_initiated = false;
11523 msr_info.index = e.index;
11524 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011525 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011526 "%s cannot read MSR (%u, 0x%x)\n",
11527 __func__, i, e.index);
11528 return -EINVAL;
11529 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011530 if (kvm_vcpu_write_guest(vcpu,
11531 gpa + i * sizeof(e) +
11532 offsetof(struct vmx_msr_entry, value),
11533 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011534 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011535 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011536 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011537 return -EINVAL;
11538 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011539 }
11540 return 0;
11541}
11542
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011543static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11544{
11545 unsigned long invalid_mask;
11546
11547 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11548 return (val & invalid_mask) == 0;
11549}
11550
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011551/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011552 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11553 * emulating VM entry into a guest with EPT enabled.
11554 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11555 * is assigned to entry_failure_code on failure.
11556 */
11557static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011558 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011559{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011560 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011561 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011562 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11563 return 1;
11564 }
11565
11566 /*
11567 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11568 * must not be dereferenced.
11569 */
11570 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11571 !nested_ept) {
11572 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11573 *entry_failure_code = ENTRY_FAIL_PDPTE;
11574 return 1;
11575 }
11576 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011577 }
11578
Junaid Shahid50c28f22018-06-27 14:59:11 -070011579 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011580 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011581
11582 vcpu->arch.cr3 = cr3;
11583 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11584
11585 kvm_init_mmu(vcpu, false);
11586
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011587 return 0;
11588}
11589
Jim Mattson6514dc32018-04-26 16:09:12 -070011590static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011591{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011592 struct vcpu_vmx *vmx = to_vmx(vcpu);
11593
11594 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11595 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11596 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11597 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11598 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11599 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11600 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11601 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11602 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11603 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11604 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11605 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11606 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11607 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11608 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11609 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11610 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11611 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11612 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11613 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11614 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11615 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11616 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11617 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11618 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11619 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11620 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11621 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11622 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11623 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11624 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011625
11626 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11627 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11628 vmcs12->guest_pending_dbg_exceptions);
11629 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11630 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11631
11632 if (nested_cpu_has_xsaves(vmcs12))
11633 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11634 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11635
11636 if (cpu_has_vmx_posted_intr())
11637 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11638
11639 /*
11640 * Whether page-faults are trapped is determined by a combination of
11641 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11642 * If enable_ept, L0 doesn't care about page faults and we should
11643 * set all of these to L1's desires. However, if !enable_ept, L0 does
11644 * care about (at least some) page faults, and because it is not easy
11645 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11646 * to exit on each and every L2 page fault. This is done by setting
11647 * MASK=MATCH=0 and (see below) EB.PF=1.
11648 * Note that below we don't need special code to set EB.PF beyond the
11649 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11650 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11651 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11652 */
11653 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11654 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11655 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11656 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11657
11658 /* All VMFUNCs are currently emulated through L0 vmexits. */
11659 if (cpu_has_vmx_vmfunc())
11660 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11661
11662 if (cpu_has_vmx_apicv()) {
11663 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11664 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11665 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11666 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11667 }
11668
11669 /*
11670 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11671 * Some constant fields are set here by vmx_set_constant_host_state().
11672 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011673 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
11674 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011675 */
11676 vmx_set_constant_host_state(vmx);
11677
11678 /*
11679 * Set the MSR load/store lists to match L0's settings.
11680 */
11681 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11682 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11683 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11684 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11685 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11686
11687 set_cr4_guest_host_mask(vmx);
11688
11689 if (vmx_mpx_supported())
11690 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11691
11692 if (enable_vpid) {
11693 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11694 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11695 else
11696 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11697 }
11698
11699 /*
11700 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11701 */
11702 if (enable_ept) {
11703 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11704 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11705 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11706 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11707 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011708
11709 if (cpu_has_vmx_msr_bitmap())
11710 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011711}
11712
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011713/*
11714 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11715 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011716 * 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 +030011717 * guest in a way that will both be appropriate to L1's requests, and our
11718 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11719 * function also has additional necessary side-effects, like setting various
11720 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011721 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11722 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011723 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011724static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011725 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011726{
11727 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011728 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011729
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011730 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011731 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011732 vmx->nested.dirty_vmcs12 = false;
11733 }
11734
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011735 /*
11736 * First, the fields that are shadowed. This must be kept in sync
11737 * with vmx_shadow_fields.h.
11738 */
11739
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011740 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011741 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011742 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011743 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11744 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011745
Jim Mattson6514dc32018-04-26 16:09:12 -070011746 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011747 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011748 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11749 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11750 } else {
11751 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11752 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11753 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011754 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011755 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11756 vmcs12->vm_entry_intr_info_field);
11757 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11758 vmcs12->vm_entry_exception_error_code);
11759 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11760 vmcs12->vm_entry_instruction_len);
11761 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11762 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011763 vmx->loaded_vmcs->nmi_known_unmasked =
11764 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011765 } else {
11766 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11767 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011768 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011769
Jan Kiszkaf41245002014-03-07 20:03:13 +010011770 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011771
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011772 /* Preemption timer setting is only taken from vmcs01. */
11773 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11774 exec_control |= vmcs_config.pin_based_exec_ctrl;
11775 if (vmx->hv_deadline_tsc == -1)
11776 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11777
11778 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011779 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011780 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11781 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011782 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011783 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011784 }
Wincy Van705699a2015-02-03 23:58:17 +080011785
Jan Kiszkaf41245002014-03-07 20:03:13 +010011786 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011787
Jan Kiszkaf41245002014-03-07 20:03:13 +010011788 vmx->nested.preemption_timer_expired = false;
11789 if (nested_cpu_has_preemption_timer(vmcs12))
11790 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011791
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011792 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011793 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011794
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011795 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011796 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011797 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011798 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011799 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011800 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011801 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11802 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011803 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011804 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11805 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11806 ~SECONDARY_EXEC_ENABLE_PML;
11807 exec_control |= vmcs12_exec_ctrl;
11808 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011809
Liran Alon32c7acf2018-06-23 02:35:11 +030011810 /* VMCS shadowing for L2 is emulated for now */
11811 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11812
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011813 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011814 vmcs_write16(GUEST_INTR_STATUS,
11815 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011816
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011817 /*
11818 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11819 * nested_get_vmcs12_pages will either fix it up or
11820 * remove the VM execution control.
11821 */
11822 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11823 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11824
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011825 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11826 }
11827
Jim Mattson83bafef2016-10-04 10:48:38 -070011828 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011829 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11830 * entry, but only if the current (host) sp changed from the value
11831 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11832 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11833 * here we just force the write to happen on entry.
11834 */
11835 vmx->host_rsp = 0;
11836
11837 exec_control = vmx_exec_control(vmx); /* L0's desires */
11838 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11839 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11840 exec_control &= ~CPU_BASED_TPR_SHADOW;
11841 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011842
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011843 /*
11844 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11845 * nested_get_vmcs12_pages can't fix it up, the illegal value
11846 * will result in a VM entry failure.
11847 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011848 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011849 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011850 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011851 } else {
11852#ifdef CONFIG_X86_64
11853 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11854 CPU_BASED_CR8_STORE_EXITING;
11855#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011856 }
11857
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011858 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011859 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11860 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011861 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011862 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11863 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11864
11865 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11866
11867 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11868 * bitwise-or of what L1 wants to trap for L2, and what we want to
11869 * trap. Note that CR0.TS also needs updating - we do this later.
11870 */
11871 update_exception_bitmap(vcpu);
11872 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11873 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11874
Nadav Har'El8049d652013-08-05 11:07:06 +030011875 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11876 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11877 * bits are further modified by vmx_set_efer() below.
11878 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011879 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011880
11881 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11882 * emulated by vmx_set_efer(), below.
11883 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011884 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011885 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11886 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011887 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11888
Jim Mattson6514dc32018-04-26 16:09:12 -070011889 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011890 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011891 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011892 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011893 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011894 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011895 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011896
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011897 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11898
Peter Feinerc95ba922016-08-17 09:36:47 -070011899 if (kvm_has_tsc_control)
11900 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011901
11902 if (enable_vpid) {
11903 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011904 * There is no direct mapping between vpid02 and vpid12, the
11905 * vpid02 is per-vCPU for L0 and reused while the value of
11906 * vpid12 is changed w/ one invvpid during nested vmentry.
11907 * The vpid12 is allocated by L1 for L2, so it will not
11908 * influence global bitmap(for vpid01 and vpid02 allocation)
11909 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011910 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011911 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011912 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11913 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011914 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011915 }
11916 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011917 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011918 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011919 }
11920
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011921 if (enable_pml) {
11922 /*
11923 * Conceptually we want to copy the PML address and index from
11924 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11925 * since we always flush the log on each vmexit, this happens
11926 * to be equivalent to simply resetting the fields in vmcs02.
11927 */
11928 ASSERT(vmx->pml_pg);
11929 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11930 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11931 }
11932
Nadav Har'El155a97a2013-08-05 11:07:16 +030011933 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011934 if (nested_ept_init_mmu_context(vcpu)) {
11935 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11936 return 1;
11937 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011938 } else if (nested_cpu_has2(vmcs12,
11939 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011940 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011941 }
11942
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011943 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011944 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11945 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011946 * The CR0_READ_SHADOW is what L2 should have expected to read given
11947 * the specifications by L1; It's not enough to take
11948 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11949 * have more bits than L1 expected.
11950 */
11951 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11952 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11953
11954 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11955 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11956
Jim Mattson6514dc32018-04-26 16:09:12 -070011957 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011958 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011959 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11960 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11961 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11962 else
11963 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11964 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11965 vmx_set_efer(vcpu, vcpu->arch.efer);
11966
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011967 /*
11968 * Guest state is invalid and unrestricted guest is disabled,
11969 * which means L1 attempted VMEntry to L2 with invalid state.
11970 * Fail the VMEntry.
11971 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011972 if (vmx->emulation_required) {
11973 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011974 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011975 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011976
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011977 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011978 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011979 entry_failure_code))
11980 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011981
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011982 if (!enable_ept)
11983 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11984
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011985 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11986 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011987 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011988}
11989
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011990static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11991{
11992 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11993 nested_cpu_has_virtual_nmis(vmcs12))
11994 return -EINVAL;
11995
11996 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11997 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11998 return -EINVAL;
11999
12000 return 0;
12001}
12002
Jim Mattsonca0bde22016-11-30 12:03:46 -080012003static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12004{
12005 struct vcpu_vmx *vmx = to_vmx(vcpu);
12006
12007 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12008 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12009 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12010
Jim Mattson56a20512017-07-06 16:33:06 -070012011 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12012 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12013
Jim Mattsonca0bde22016-11-30 12:03:46 -080012014 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12015 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12016
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012017 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12018 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12019
Jim Mattson712b12d2017-08-24 13:24:47 -070012020 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12021 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12022
Jim Mattsonca0bde22016-11-30 12:03:46 -080012023 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12024 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12025
12026 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12027 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12028
Bandan Dasc5f983f2017-05-05 15:25:14 -040012029 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12030 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12031
Liran Alona8a7c022018-06-23 02:35:06 +030012032 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12033 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12034
Jim Mattsonca0bde22016-11-30 12:03:46 -080012035 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012036 vmx->nested.msrs.procbased_ctls_low,
12037 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012038 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12039 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012040 vmx->nested.msrs.secondary_ctls_low,
12041 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012042 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012043 vmx->nested.msrs.pinbased_ctls_low,
12044 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012045 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012046 vmx->nested.msrs.exit_ctls_low,
12047 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012048 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012049 vmx->nested.msrs.entry_ctls_low,
12050 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012051 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12052
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012053 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012054 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12055
Bandan Das41ab9372017-08-03 15:54:43 -040012056 if (nested_cpu_has_vmfunc(vmcs12)) {
12057 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012058 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012059 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12060
12061 if (nested_cpu_has_eptp_switching(vmcs12)) {
12062 if (!nested_cpu_has_ept(vmcs12) ||
12063 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12064 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12065 }
12066 }
Bandan Das27c42a12017-08-03 15:54:42 -040012067
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012068 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12069 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12070
Jim Mattsonca0bde22016-11-30 12:03:46 -080012071 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12072 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12073 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12074 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12075
Marc Orr04473782018-06-20 17:21:29 -070012076 /*
12077 * From the Intel SDM, volume 3:
12078 * Fields relevant to VM-entry event injection must be set properly.
12079 * These fields are the VM-entry interruption-information field, the
12080 * VM-entry exception error code, and the VM-entry instruction length.
12081 */
12082 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12083 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12084 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12085 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12086 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12087 bool should_have_error_code;
12088 bool urg = nested_cpu_has2(vmcs12,
12089 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12090 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12091
12092 /* VM-entry interruption-info field: interruption type */
12093 if (intr_type == INTR_TYPE_RESERVED ||
12094 (intr_type == INTR_TYPE_OTHER_EVENT &&
12095 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12096 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12097
12098 /* VM-entry interruption-info field: vector */
12099 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12100 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12101 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12102 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12103
12104 /* VM-entry interruption-info field: deliver error code */
12105 should_have_error_code =
12106 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12107 x86_exception_has_error_code(vector);
12108 if (has_error_code != should_have_error_code)
12109 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12110
12111 /* VM-entry exception error code */
12112 if (has_error_code &&
12113 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12114 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12115
12116 /* VM-entry interruption-info field: reserved bits */
12117 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12118 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12119
12120 /* VM-entry instruction length */
12121 switch (intr_type) {
12122 case INTR_TYPE_SOFT_EXCEPTION:
12123 case INTR_TYPE_SOFT_INTR:
12124 case INTR_TYPE_PRIV_SW_EXCEPTION:
12125 if ((vmcs12->vm_entry_instruction_len > 15) ||
12126 (vmcs12->vm_entry_instruction_len == 0 &&
12127 !nested_cpu_has_zero_length_injection(vcpu)))
12128 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12129 }
12130 }
12131
Jim Mattsonca0bde22016-11-30 12:03:46 -080012132 return 0;
12133}
12134
Liran Alonf145d902018-06-23 02:35:07 +030012135static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12136 struct vmcs12 *vmcs12)
12137{
12138 int r;
12139 struct page *page;
12140 struct vmcs12 *shadow;
12141
12142 if (vmcs12->vmcs_link_pointer == -1ull)
12143 return 0;
12144
12145 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12146 return -EINVAL;
12147
12148 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12149 if (is_error_page(page))
12150 return -EINVAL;
12151
12152 r = 0;
12153 shadow = kmap(page);
12154 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12155 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12156 r = -EINVAL;
12157 kunmap(page);
12158 kvm_release_page_clean(page);
12159 return r;
12160}
12161
Jim Mattsonca0bde22016-11-30 12:03:46 -080012162static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12163 u32 *exit_qual)
12164{
12165 bool ia32e;
12166
12167 *exit_qual = ENTRY_FAIL_DEFAULT;
12168
12169 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12170 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12171 return 1;
12172
Liran Alonf145d902018-06-23 02:35:07 +030012173 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012174 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12175 return 1;
12176 }
12177
12178 /*
12179 * If the load IA32_EFER VM-entry control is 1, the following checks
12180 * are performed on the field for the IA32_EFER MSR:
12181 * - Bits reserved in the IA32_EFER MSR must be 0.
12182 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12183 * the IA-32e mode guest VM-exit control. It must also be identical
12184 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12185 * CR0.PG) is 1.
12186 */
12187 if (to_vmx(vcpu)->nested.nested_run_pending &&
12188 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12189 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12190 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12191 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12192 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12193 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12194 return 1;
12195 }
12196
12197 /*
12198 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12199 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12200 * the values of the LMA and LME bits in the field must each be that of
12201 * the host address-space size VM-exit control.
12202 */
12203 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12204 ia32e = (vmcs12->vm_exit_controls &
12205 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12206 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12207 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12208 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12209 return 1;
12210 }
12211
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012212 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12213 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12214 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12215 return 1;
12216
Jim Mattsonca0bde22016-11-30 12:03:46 -080012217 return 0;
12218}
12219
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012220/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012221 * If exit_qual is NULL, this is being called from state restore (either RSM
12222 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012223 */
12224static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012225{
12226 struct vcpu_vmx *vmx = to_vmx(vcpu);
12227 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012228 bool from_vmentry = !!exit_qual;
12229 u32 dummy_exit_qual;
12230 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012231
Jim Mattson858e25c2016-11-30 12:03:47 -080012232 enter_guest_mode(vcpu);
12233
12234 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12235 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12236
Jim Mattsonde3a0022017-11-27 17:22:25 -060012237 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012238 vmx_segment_cache_clear(vmx);
12239
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012240 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12241 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12242
12243 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012244 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012245 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012246
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012247 if (from_vmentry) {
12248 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012249
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012250 r = EXIT_REASON_MSR_LOAD_FAIL;
12251 *exit_qual = nested_vmx_load_msr(vcpu,
12252 vmcs12->vm_entry_msr_load_addr,
12253 vmcs12->vm_entry_msr_load_count);
12254 if (*exit_qual)
12255 goto fail;
12256 } else {
12257 /*
12258 * The MMU is not initialized to point at the right entities yet and
12259 * "get pages" would need to read data from the guest (i.e. we will
12260 * need to perform gpa to hpa translation). Request a call
12261 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12262 * have already been set at vmentry time and should not be reset.
12263 */
12264 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12265 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012266
Jim Mattson858e25c2016-11-30 12:03:47 -080012267 /*
12268 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12269 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12270 * returned as far as L1 is concerned. It will only return (and set
12271 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12272 */
12273 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012274
12275fail:
12276 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12277 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12278 leave_guest_mode(vcpu);
12279 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012280 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012281}
12282
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012283/*
12284 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12285 * for running an L2 nested guest.
12286 */
12287static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12288{
12289 struct vmcs12 *vmcs12;
12290 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012291 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012292 u32 exit_qual;
12293 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012294
Kyle Hueyeb277562016-11-29 12:40:39 -080012295 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012296 return 1;
12297
Kyle Hueyeb277562016-11-29 12:40:39 -080012298 if (!nested_vmx_check_vmcs12(vcpu))
12299 goto out;
12300
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012301 vmcs12 = get_vmcs12(vcpu);
12302
Liran Alona6192d42018-06-23 02:35:04 +030012303 /*
12304 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12305 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12306 * rather than RFLAGS.ZF, and no error number is stored to the
12307 * VM-instruction error field.
12308 */
12309 if (vmcs12->hdr.shadow_vmcs) {
12310 nested_vmx_failInvalid(vcpu);
12311 goto out;
12312 }
12313
Abel Gordon012f83c2013-04-18 14:39:25 +030012314 if (enable_shadow_vmcs)
12315 copy_shadow_to_vmcs12(vmx);
12316
Nadav Har'El7c177932011-05-25 23:12:04 +030012317 /*
12318 * The nested entry process starts with enforcing various prerequisites
12319 * on vmcs12 as required by the Intel SDM, and act appropriately when
12320 * they fail: As the SDM explains, some conditions should cause the
12321 * instruction to fail, while others will cause the instruction to seem
12322 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12323 * To speed up the normal (success) code path, we should avoid checking
12324 * for misconfigurations which will anyway be caught by the processor
12325 * when using the merged vmcs02.
12326 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012327 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12328 nested_vmx_failValid(vcpu,
12329 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12330 goto out;
12331 }
12332
Nadav Har'El7c177932011-05-25 23:12:04 +030012333 if (vmcs12->launch_state == launch) {
12334 nested_vmx_failValid(vcpu,
12335 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12336 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012337 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012338 }
12339
Jim Mattsonca0bde22016-11-30 12:03:46 -080012340 ret = check_vmentry_prereqs(vcpu, vmcs12);
12341 if (ret) {
12342 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012343 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012344 }
12345
Nadav Har'El7c177932011-05-25 23:12:04 +030012346 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012347 * After this point, the trap flag no longer triggers a singlestep trap
12348 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12349 * This is not 100% correct; for performance reasons, we delegate most
12350 * of the checks on host state to the processor. If those fail,
12351 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012352 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012353 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012354
Jim Mattsonca0bde22016-11-30 12:03:46 -080012355 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12356 if (ret) {
12357 nested_vmx_entry_failure(vcpu, vmcs12,
12358 EXIT_REASON_INVALID_STATE, exit_qual);
12359 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012360 }
12361
12362 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012363 * We're finally done with prerequisite checking, and can start with
12364 * the nested entry.
12365 */
12366
Jim Mattson6514dc32018-04-26 16:09:12 -070012367 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012368 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012369 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012370 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012371 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012372 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012373 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012374
Chao Gao135a06c2018-02-11 10:06:30 +080012375 /*
Liran Alon61ada742018-06-23 02:35:08 +030012376 * Must happen outside of enter_vmx_non_root_mode() as it will
12377 * also be used as part of restoring nVMX state for
12378 * snapshot restore (migration).
12379 *
12380 * In this flow, it is assumed that vmcs12 cache was
12381 * trasferred as part of captured nVMX state and should
12382 * therefore not be read from guest memory (which may not
12383 * exist on destination host yet).
12384 */
12385 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12386
12387 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012388 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12389 * by event injection, halt vcpu.
12390 */
12391 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012392 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12393 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012394 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012395 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012396 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012397
12398out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012399 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012400}
12401
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012402/*
12403 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12404 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12405 * This function returns the new value we should put in vmcs12.guest_cr0.
12406 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12407 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12408 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12409 * didn't trap the bit, because if L1 did, so would L0).
12410 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12411 * been modified by L2, and L1 knows it. So just leave the old value of
12412 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12413 * isn't relevant, because if L0 traps this bit it can set it to anything.
12414 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12415 * changed these bits, and therefore they need to be updated, but L0
12416 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12417 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12418 */
12419static inline unsigned long
12420vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12421{
12422 return
12423 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12424 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12425 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12426 vcpu->arch.cr0_guest_owned_bits));
12427}
12428
12429static inline unsigned long
12430vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12431{
12432 return
12433 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12434 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12435 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12436 vcpu->arch.cr4_guest_owned_bits));
12437}
12438
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012439static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12440 struct vmcs12 *vmcs12)
12441{
12442 u32 idt_vectoring;
12443 unsigned int nr;
12444
Wanpeng Li664f8e22017-08-24 03:35:09 -070012445 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012446 nr = vcpu->arch.exception.nr;
12447 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12448
12449 if (kvm_exception_is_soft(nr)) {
12450 vmcs12->vm_exit_instruction_len =
12451 vcpu->arch.event_exit_inst_len;
12452 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12453 } else
12454 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12455
12456 if (vcpu->arch.exception.has_error_code) {
12457 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12458 vmcs12->idt_vectoring_error_code =
12459 vcpu->arch.exception.error_code;
12460 }
12461
12462 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012463 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012464 vmcs12->idt_vectoring_info_field =
12465 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012466 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012467 nr = vcpu->arch.interrupt.nr;
12468 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12469
12470 if (vcpu->arch.interrupt.soft) {
12471 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12472 vmcs12->vm_entry_instruction_len =
12473 vcpu->arch.event_exit_inst_len;
12474 } else
12475 idt_vectoring |= INTR_TYPE_EXT_INTR;
12476
12477 vmcs12->idt_vectoring_info_field = idt_vectoring;
12478 }
12479}
12480
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012481static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12482{
12483 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012484 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012485 bool block_nested_events =
12486 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012487
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012488 if (vcpu->arch.exception.pending &&
12489 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012490 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012491 return -EBUSY;
12492 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012493 return 0;
12494 }
12495
Jan Kiszkaf41245002014-03-07 20:03:13 +010012496 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12497 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012498 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012499 return -EBUSY;
12500 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12501 return 0;
12502 }
12503
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012504 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012505 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012506 return -EBUSY;
12507 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12508 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12509 INTR_INFO_VALID_MASK, 0);
12510 /*
12511 * The NMI-triggered VM exit counts as injection:
12512 * clear this one and block further NMIs.
12513 */
12514 vcpu->arch.nmi_pending = 0;
12515 vmx_set_nmi_mask(vcpu, true);
12516 return 0;
12517 }
12518
12519 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12520 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012521 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012522 return -EBUSY;
12523 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012524 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012525 }
12526
David Hildenbrand6342c502017-01-25 11:58:58 +010012527 vmx_complete_nested_posted_interrupt(vcpu);
12528 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012529}
12530
Jan Kiszkaf41245002014-03-07 20:03:13 +010012531static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12532{
12533 ktime_t remaining =
12534 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12535 u64 value;
12536
12537 if (ktime_to_ns(remaining) <= 0)
12538 return 0;
12539
12540 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12541 do_div(value, 1000000);
12542 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12543}
12544
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012545/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012546 * Update the guest state fields of vmcs12 to reflect changes that
12547 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12548 * VM-entry controls is also updated, since this is really a guest
12549 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012550 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012551static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012552{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012553 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12554 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12555
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012556 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12557 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12558 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12559
12560 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12561 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12562 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12563 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12564 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12565 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12566 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12567 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12568 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12569 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12570 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12571 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12572 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12573 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12574 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12575 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12576 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12577 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12578 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12579 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12580 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12581 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12582 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12583 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12584 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12585 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12586 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12587 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12588 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12589 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12590 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12591 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12592 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12593 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12594 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12595 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12596
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012597 vmcs12->guest_interruptibility_info =
12598 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12599 vmcs12->guest_pending_dbg_exceptions =
12600 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012601 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12602 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12603 else
12604 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012605
Jan Kiszkaf41245002014-03-07 20:03:13 +010012606 if (nested_cpu_has_preemption_timer(vmcs12)) {
12607 if (vmcs12->vm_exit_controls &
12608 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12609 vmcs12->vmx_preemption_timer_value =
12610 vmx_get_preemption_timer_value(vcpu);
12611 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12612 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012613
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012614 /*
12615 * In some cases (usually, nested EPT), L2 is allowed to change its
12616 * own CR3 without exiting. If it has changed it, we must keep it.
12617 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12618 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12619 *
12620 * Additionally, restore L2's PDPTR to vmcs12.
12621 */
12622 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012623 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012624 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12625 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12626 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12627 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12628 }
12629
Jim Mattsond281e132017-06-01 12:44:46 -070012630 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012631
Wincy Van608406e2015-02-03 23:57:51 +080012632 if (nested_cpu_has_vid(vmcs12))
12633 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12634
Jan Kiszkac18911a2013-03-13 16:06:41 +010012635 vmcs12->vm_entry_controls =
12636 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012637 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012638
Jan Kiszka2996fca2014-06-16 13:59:43 +020012639 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12640 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12641 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12642 }
12643
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012644 /* TODO: These cannot have changed unless we have MSR bitmaps and
12645 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012646 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012647 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012648 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12649 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012650 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12651 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12652 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012653 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012654 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012655}
12656
12657/*
12658 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12659 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12660 * and this function updates it to reflect the changes to the guest state while
12661 * L2 was running (and perhaps made some exits which were handled directly by L0
12662 * without going back to L1), and to reflect the exit reason.
12663 * Note that we do not have to copy here all VMCS fields, just those that
12664 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12665 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12666 * which already writes to vmcs12 directly.
12667 */
12668static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12669 u32 exit_reason, u32 exit_intr_info,
12670 unsigned long exit_qualification)
12671{
12672 /* update guest state fields: */
12673 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012674
12675 /* update exit information fields: */
12676
Jan Kiszka533558b2014-01-04 18:47:20 +010012677 vmcs12->vm_exit_reason = exit_reason;
12678 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012679 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012680
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012681 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012682 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12683 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12684
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012685 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012686 vmcs12->launch_state = 1;
12687
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012688 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12689 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012690 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012691
12692 /*
12693 * Transfer the event that L0 or L1 may wanted to inject into
12694 * L2 to IDT_VECTORING_INFO_FIELD.
12695 */
12696 vmcs12_save_pending_event(vcpu, vmcs12);
12697 }
12698
12699 /*
12700 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12701 * preserved above and would only end up incorrectly in L1.
12702 */
12703 vcpu->arch.nmi_injected = false;
12704 kvm_clear_exception_queue(vcpu);
12705 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012706}
12707
Wanpeng Li5af41572017-11-05 16:54:49 -080012708static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12709 struct vmcs12 *vmcs12)
12710{
12711 u32 entry_failure_code;
12712
12713 nested_ept_uninit_mmu_context(vcpu);
12714
12715 /*
12716 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12717 * couldn't have changed.
12718 */
12719 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12720 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12721
12722 if (!enable_ept)
12723 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12724}
12725
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012726/*
12727 * A part of what we need to when the nested L2 guest exits and we want to
12728 * run its L1 parent, is to reset L1's guest state to the host state specified
12729 * in vmcs12.
12730 * This function is to be called not only on normal nested exit, but also on
12731 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12732 * Failures During or After Loading Guest State").
12733 * This function should be called when the active VMCS is L1's (vmcs01).
12734 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012735static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12736 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012737{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012738 struct kvm_segment seg;
12739
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012740 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12741 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012742 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012743 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12744 else
12745 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12746 vmx_set_efer(vcpu, vcpu->arch.efer);
12747
12748 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12749 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012750 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012751 /*
12752 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012753 * actually changed, because vmx_set_cr0 refers to efer set above.
12754 *
12755 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12756 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012757 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012758 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012759 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012760
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012761 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012762 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012763 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012764
Wanpeng Li5af41572017-11-05 16:54:49 -080012765 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012766
Liran Alon6f1e03b2018-05-22 17:16:14 +030012767 /*
12768 * If vmcs01 don't use VPID, CPU flushes TLB on every
12769 * VMEntry/VMExit. Thus, no need to flush TLB.
12770 *
12771 * If vmcs12 uses VPID, TLB entries populated by L2 are
12772 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12773 * with vmx->vpid. Thus, no need to flush TLB.
12774 *
12775 * Therefore, flush TLB only in case vmcs01 uses VPID and
12776 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12777 * are both tagged with vmx->vpid.
12778 */
12779 if (enable_vpid &&
12780 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012781 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012782 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012783
12784 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12785 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12786 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12787 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12788 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012789 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12790 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012791
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012792 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12793 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12794 vmcs_write64(GUEST_BNDCFGS, 0);
12795
Jan Kiszka44811c02013-08-04 17:17:27 +020012796 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012797 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012798 vcpu->arch.pat = vmcs12->host_ia32_pat;
12799 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012800 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12801 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12802 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012803
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012804 /* Set L1 segment info according to Intel SDM
12805 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12806 seg = (struct kvm_segment) {
12807 .base = 0,
12808 .limit = 0xFFFFFFFF,
12809 .selector = vmcs12->host_cs_selector,
12810 .type = 11,
12811 .present = 1,
12812 .s = 1,
12813 .g = 1
12814 };
12815 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12816 seg.l = 1;
12817 else
12818 seg.db = 1;
12819 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12820 seg = (struct kvm_segment) {
12821 .base = 0,
12822 .limit = 0xFFFFFFFF,
12823 .type = 3,
12824 .present = 1,
12825 .s = 1,
12826 .db = 1,
12827 .g = 1
12828 };
12829 seg.selector = vmcs12->host_ds_selector;
12830 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12831 seg.selector = vmcs12->host_es_selector;
12832 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12833 seg.selector = vmcs12->host_ss_selector;
12834 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12835 seg.selector = vmcs12->host_fs_selector;
12836 seg.base = vmcs12->host_fs_base;
12837 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12838 seg.selector = vmcs12->host_gs_selector;
12839 seg.base = vmcs12->host_gs_base;
12840 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12841 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012842 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012843 .limit = 0x67,
12844 .selector = vmcs12->host_tr_selector,
12845 .type = 11,
12846 .present = 1
12847 };
12848 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12849
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012850 kvm_set_dr(vcpu, 7, 0x400);
12851 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012852
Wincy Van3af18d92015-02-03 23:49:31 +080012853 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012854 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012855
Wincy Vanff651cb2014-12-11 08:52:58 +030012856 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12857 vmcs12->vm_exit_msr_load_count))
12858 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012859}
12860
12861/*
12862 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12863 * and modify vmcs12 to make it see what it would expect to see there if
12864 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12865 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012866static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12867 u32 exit_intr_info,
12868 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012869{
12870 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012871 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12872
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012873 /* trying to cancel vmlaunch/vmresume is a bug */
12874 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12875
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012876 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012877 * The only expected VM-instruction error is "VM entry with
12878 * invalid control field(s)." Anything else indicates a
12879 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012880 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012881 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12882 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12883
12884 leave_guest_mode(vcpu);
12885
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012886 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12887 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12888
Jim Mattson4f350c62017-09-14 16:31:44 -070012889 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012890 if (exit_reason == -1)
12891 sync_vmcs12(vcpu, vmcs12);
12892 else
12893 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12894 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012895
Liran Alon61ada742018-06-23 02:35:08 +030012896 /*
12897 * Must happen outside of sync_vmcs12() as it will
12898 * also be used to capture vmcs12 cache as part of
12899 * capturing nVMX state for snapshot (migration).
12900 *
12901 * Otherwise, this flush will dirty guest memory at a
12902 * point it is already assumed by user-space to be
12903 * immutable.
12904 */
12905 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12906
Jim Mattson4f350c62017-09-14 16:31:44 -070012907 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12908 vmcs12->vm_exit_msr_store_count))
12909 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012910 }
12911
Jim Mattson4f350c62017-09-14 16:31:44 -070012912 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012913 vm_entry_controls_reset_shadow(vmx);
12914 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012915 vmx_segment_cache_clear(vmx);
12916
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012917 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012918 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12919 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012920 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012921 if (vmx->hv_deadline_tsc == -1)
12922 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12923 PIN_BASED_VMX_PREEMPTION_TIMER);
12924 else
12925 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12926 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012927 if (kvm_has_tsc_control)
12928 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012929
Jim Mattson8d860bb2018-05-09 16:56:05 -040012930 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12931 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12932 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012933 } else if (!nested_cpu_has_ept(vmcs12) &&
12934 nested_cpu_has2(vmcs12,
12935 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012936 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012937 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012938
12939 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12940 vmx->host_rsp = 0;
12941
12942 /* Unpin physical memory we referred to in vmcs02 */
12943 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012944 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012945 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012946 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012947 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012948 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012949 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012950 }
Wincy Van705699a2015-02-03 23:58:17 +080012951 if (vmx->nested.pi_desc_page) {
12952 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012953 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012954 vmx->nested.pi_desc_page = NULL;
12955 vmx->nested.pi_desc = NULL;
12956 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012957
12958 /*
Tang Chen38b99172014-09-24 15:57:54 +080012959 * We are now running in L2, mmu_notifier will force to reload the
12960 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12961 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012962 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012963
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012964 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012965 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012966
12967 /* in case we halted in L2 */
12968 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012969
12970 if (likely(!vmx->fail)) {
12971 /*
12972 * TODO: SDM says that with acknowledge interrupt on
12973 * exit, bit 31 of the VM-exit interrupt information
12974 * (valid interrupt) is always set to 1 on
12975 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12976 * need kvm_cpu_has_interrupt(). See the commit
12977 * message for details.
12978 */
12979 if (nested_exit_intr_ack_set(vcpu) &&
12980 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12981 kvm_cpu_has_interrupt(vcpu)) {
12982 int irq = kvm_cpu_get_interrupt(vcpu);
12983 WARN_ON(irq < 0);
12984 vmcs12->vm_exit_intr_info = irq |
12985 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12986 }
12987
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012988 if (exit_reason != -1)
12989 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12990 vmcs12->exit_qualification,
12991 vmcs12->idt_vectoring_info_field,
12992 vmcs12->vm_exit_intr_info,
12993 vmcs12->vm_exit_intr_error_code,
12994 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012995
12996 load_vmcs12_host_state(vcpu, vmcs12);
12997
12998 return;
12999 }
13000
13001 /*
13002 * After an early L2 VM-entry failure, we're now back
13003 * in L1 which thinks it just finished a VMLAUNCH or
13004 * VMRESUME instruction, so we need to set the failure
13005 * flag and the VM-instruction error field of the VMCS
13006 * accordingly.
13007 */
13008 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013009
13010 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13011
Jim Mattson4f350c62017-09-14 16:31:44 -070013012 /*
13013 * The emulated instruction was already skipped in
13014 * nested_vmx_run, but the updated RIP was never
13015 * written back to the vmcs01.
13016 */
13017 skip_emulated_instruction(vcpu);
13018 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013019}
13020
Nadav Har'El7c177932011-05-25 23:12:04 +030013021/*
Jan Kiszka42124922014-01-04 18:47:19 +010013022 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13023 */
13024static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13025{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013026 if (is_guest_mode(vcpu)) {
13027 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013028 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013029 }
Jan Kiszka42124922014-01-04 18:47:19 +010013030 free_nested(to_vmx(vcpu));
13031}
13032
13033/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013034 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13035 * 23.7 "VM-entry failures during or after loading guest state" (this also
13036 * lists the acceptable exit-reason and exit-qualification parameters).
13037 * It should only be called before L2 actually succeeded to run, and when
13038 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13039 */
13040static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13041 struct vmcs12 *vmcs12,
13042 u32 reason, unsigned long qualification)
13043{
13044 load_vmcs12_host_state(vcpu, vmcs12);
13045 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13046 vmcs12->exit_qualification = qualification;
13047 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013048 if (enable_shadow_vmcs)
13049 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013050}
13051
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013052static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13053 struct x86_instruction_info *info,
13054 enum x86_intercept_stage stage)
13055{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013056 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13057 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13058
13059 /*
13060 * RDPID causes #UD if disabled through secondary execution controls.
13061 * Because it is marked as EmulateOnUD, we need to intercept it here.
13062 */
13063 if (info->intercept == x86_intercept_rdtscp &&
13064 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13065 ctxt->exception.vector = UD_VECTOR;
13066 ctxt->exception.error_code_valid = false;
13067 return X86EMUL_PROPAGATE_FAULT;
13068 }
13069
13070 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013071 return X86EMUL_CONTINUE;
13072}
13073
Yunhong Jiang64672c92016-06-13 14:19:59 -070013074#ifdef CONFIG_X86_64
13075/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13076static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13077 u64 divisor, u64 *result)
13078{
13079 u64 low = a << shift, high = a >> (64 - shift);
13080
13081 /* To avoid the overflow on divq */
13082 if (high >= divisor)
13083 return 1;
13084
13085 /* Low hold the result, high hold rem which is discarded */
13086 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13087 "rm" (divisor), "0" (low), "1" (high));
13088 *result = low;
13089
13090 return 0;
13091}
13092
13093static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13094{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013095 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013096 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013097
13098 if (kvm_mwait_in_guest(vcpu->kvm))
13099 return -EOPNOTSUPP;
13100
13101 vmx = to_vmx(vcpu);
13102 tscl = rdtsc();
13103 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13104 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013105 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13106
13107 if (delta_tsc > lapic_timer_advance_cycles)
13108 delta_tsc -= lapic_timer_advance_cycles;
13109 else
13110 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013111
13112 /* Convert to host delta tsc if tsc scaling is enabled */
13113 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13114 u64_shl_div_u64(delta_tsc,
13115 kvm_tsc_scaling_ratio_frac_bits,
13116 vcpu->arch.tsc_scaling_ratio,
13117 &delta_tsc))
13118 return -ERANGE;
13119
13120 /*
13121 * If the delta tsc can't fit in the 32 bit after the multi shift,
13122 * we can't use the preemption timer.
13123 * It's possible that it fits on later vmentries, but checking
13124 * on every vmentry is costly so we just use an hrtimer.
13125 */
13126 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13127 return -ERANGE;
13128
13129 vmx->hv_deadline_tsc = tscl + delta_tsc;
13130 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13131 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013132
13133 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013134}
13135
13136static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13137{
13138 struct vcpu_vmx *vmx = to_vmx(vcpu);
13139 vmx->hv_deadline_tsc = -1;
13140 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13141 PIN_BASED_VMX_PREEMPTION_TIMER);
13142}
13143#endif
13144
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013145static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013146{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013147 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013148 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013149}
13150
Kai Huang843e4332015-01-28 10:54:28 +080013151static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13152 struct kvm_memory_slot *slot)
13153{
13154 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13155 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13156}
13157
13158static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13159 struct kvm_memory_slot *slot)
13160{
13161 kvm_mmu_slot_set_dirty(kvm, slot);
13162}
13163
13164static void vmx_flush_log_dirty(struct kvm *kvm)
13165{
13166 kvm_flush_pml_buffers(kvm);
13167}
13168
Bandan Dasc5f983f2017-05-05 15:25:14 -040013169static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13170{
13171 struct vmcs12 *vmcs12;
13172 struct vcpu_vmx *vmx = to_vmx(vcpu);
13173 gpa_t gpa;
13174 struct page *page = NULL;
13175 u64 *pml_address;
13176
13177 if (is_guest_mode(vcpu)) {
13178 WARN_ON_ONCE(vmx->nested.pml_full);
13179
13180 /*
13181 * Check if PML is enabled for the nested guest.
13182 * Whether eptp bit 6 is set is already checked
13183 * as part of A/D emulation.
13184 */
13185 vmcs12 = get_vmcs12(vcpu);
13186 if (!nested_cpu_has_pml(vmcs12))
13187 return 0;
13188
Dan Carpenter47698862017-05-10 22:43:17 +030013189 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013190 vmx->nested.pml_full = true;
13191 return 1;
13192 }
13193
13194 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13195
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013196 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13197 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013198 return 0;
13199
13200 pml_address = kmap(page);
13201 pml_address[vmcs12->guest_pml_index--] = gpa;
13202 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013203 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013204 }
13205
13206 return 0;
13207}
13208
Kai Huang843e4332015-01-28 10:54:28 +080013209static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13210 struct kvm_memory_slot *memslot,
13211 gfn_t offset, unsigned long mask)
13212{
13213 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13214}
13215
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013216static void __pi_post_block(struct kvm_vcpu *vcpu)
13217{
13218 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13219 struct pi_desc old, new;
13220 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013221
13222 do {
13223 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013224 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13225 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013226
13227 dest = cpu_physical_id(vcpu->cpu);
13228
13229 if (x2apic_enabled())
13230 new.ndst = dest;
13231 else
13232 new.ndst = (dest << 8) & 0xFF00;
13233
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013234 /* set 'NV' to 'notification vector' */
13235 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013236 } while (cmpxchg64(&pi_desc->control, old.control,
13237 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013238
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013239 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13240 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013241 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013242 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013243 vcpu->pre_pcpu = -1;
13244 }
13245}
13246
Feng Wuefc64402015-09-18 22:29:51 +080013247/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013248 * This routine does the following things for vCPU which is going
13249 * to be blocked if VT-d PI is enabled.
13250 * - Store the vCPU to the wakeup list, so when interrupts happen
13251 * we can find the right vCPU to wake up.
13252 * - Change the Posted-interrupt descriptor as below:
13253 * 'NDST' <-- vcpu->pre_pcpu
13254 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13255 * - If 'ON' is set during this process, which means at least one
13256 * interrupt is posted for this vCPU, we cannot block it, in
13257 * this case, return 1, otherwise, return 0.
13258 *
13259 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013260static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013261{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013262 unsigned int dest;
13263 struct pi_desc old, new;
13264 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13265
13266 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013267 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13268 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013269 return 0;
13270
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013271 WARN_ON(irqs_disabled());
13272 local_irq_disable();
13273 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13274 vcpu->pre_pcpu = vcpu->cpu;
13275 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13276 list_add_tail(&vcpu->blocked_vcpu_list,
13277 &per_cpu(blocked_vcpu_on_cpu,
13278 vcpu->pre_pcpu));
13279 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13280 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013281
13282 do {
13283 old.control = new.control = pi_desc->control;
13284
Feng Wubf9f6ac2015-09-18 22:29:55 +080013285 WARN((pi_desc->sn == 1),
13286 "Warning: SN field of posted-interrupts "
13287 "is set before blocking\n");
13288
13289 /*
13290 * Since vCPU can be preempted during this process,
13291 * vcpu->cpu could be different with pre_pcpu, we
13292 * need to set pre_pcpu as the destination of wakeup
13293 * notification event, then we can find the right vCPU
13294 * to wakeup in wakeup handler if interrupts happen
13295 * when the vCPU is in blocked state.
13296 */
13297 dest = cpu_physical_id(vcpu->pre_pcpu);
13298
13299 if (x2apic_enabled())
13300 new.ndst = dest;
13301 else
13302 new.ndst = (dest << 8) & 0xFF00;
13303
13304 /* set 'NV' to 'wakeup vector' */
13305 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013306 } while (cmpxchg64(&pi_desc->control, old.control,
13307 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013308
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013309 /* We should not block the vCPU if an interrupt is posted for it. */
13310 if (pi_test_on(pi_desc) == 1)
13311 __pi_post_block(vcpu);
13312
13313 local_irq_enable();
13314 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013315}
13316
Yunhong Jiangbc225122016-06-13 14:19:58 -070013317static int vmx_pre_block(struct kvm_vcpu *vcpu)
13318{
13319 if (pi_pre_block(vcpu))
13320 return 1;
13321
Yunhong Jiang64672c92016-06-13 14:19:59 -070013322 if (kvm_lapic_hv_timer_in_use(vcpu))
13323 kvm_lapic_switch_to_sw_timer(vcpu);
13324
Yunhong Jiangbc225122016-06-13 14:19:58 -070013325 return 0;
13326}
13327
13328static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013329{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013330 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013331 return;
13332
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013333 WARN_ON(irqs_disabled());
13334 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013335 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013336 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013337}
13338
Yunhong Jiangbc225122016-06-13 14:19:58 -070013339static void vmx_post_block(struct kvm_vcpu *vcpu)
13340{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013341 if (kvm_x86_ops->set_hv_timer)
13342 kvm_lapic_switch_to_hv_timer(vcpu);
13343
Yunhong Jiangbc225122016-06-13 14:19:58 -070013344 pi_post_block(vcpu);
13345}
13346
Feng Wubf9f6ac2015-09-18 22:29:55 +080013347/*
Feng Wuefc64402015-09-18 22:29:51 +080013348 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13349 *
13350 * @kvm: kvm
13351 * @host_irq: host irq of the interrupt
13352 * @guest_irq: gsi of the interrupt
13353 * @set: set or unset PI
13354 * returns 0 on success, < 0 on failure
13355 */
13356static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13357 uint32_t guest_irq, bool set)
13358{
13359 struct kvm_kernel_irq_routing_entry *e;
13360 struct kvm_irq_routing_table *irq_rt;
13361 struct kvm_lapic_irq irq;
13362 struct kvm_vcpu *vcpu;
13363 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013364 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013365
13366 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013367 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13368 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013369 return 0;
13370
13371 idx = srcu_read_lock(&kvm->irq_srcu);
13372 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013373 if (guest_irq >= irq_rt->nr_rt_entries ||
13374 hlist_empty(&irq_rt->map[guest_irq])) {
13375 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13376 guest_irq, irq_rt->nr_rt_entries);
13377 goto out;
13378 }
Feng Wuefc64402015-09-18 22:29:51 +080013379
13380 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13381 if (e->type != KVM_IRQ_ROUTING_MSI)
13382 continue;
13383 /*
13384 * VT-d PI cannot support posting multicast/broadcast
13385 * interrupts to a vCPU, we still use interrupt remapping
13386 * for these kind of interrupts.
13387 *
13388 * For lowest-priority interrupts, we only support
13389 * those with single CPU as the destination, e.g. user
13390 * configures the interrupts via /proc/irq or uses
13391 * irqbalance to make the interrupts single-CPU.
13392 *
13393 * We will support full lowest-priority interrupt later.
13394 */
13395
Radim Krčmář371313132016-07-12 22:09:27 +020013396 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013397 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13398 /*
13399 * Make sure the IRTE is in remapped mode if
13400 * we don't handle it in posted mode.
13401 */
13402 ret = irq_set_vcpu_affinity(host_irq, NULL);
13403 if (ret < 0) {
13404 printk(KERN_INFO
13405 "failed to back to remapped mode, irq: %u\n",
13406 host_irq);
13407 goto out;
13408 }
13409
Feng Wuefc64402015-09-18 22:29:51 +080013410 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013411 }
Feng Wuefc64402015-09-18 22:29:51 +080013412
13413 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13414 vcpu_info.vector = irq.vector;
13415
hu huajun2698d822018-04-11 15:16:40 +080013416 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013417 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13418
13419 if (set)
13420 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013421 else
Feng Wuefc64402015-09-18 22:29:51 +080013422 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013423
13424 if (ret < 0) {
13425 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13426 __func__);
13427 goto out;
13428 }
13429 }
13430
13431 ret = 0;
13432out:
13433 srcu_read_unlock(&kvm->irq_srcu, idx);
13434 return ret;
13435}
13436
Ashok Rajc45dcc72016-06-22 14:59:56 +080013437static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13438{
13439 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13440 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13441 FEATURE_CONTROL_LMCE;
13442 else
13443 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13444 ~FEATURE_CONTROL_LMCE;
13445}
13446
Ladi Prosek72d7b372017-10-11 16:54:41 +020013447static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13448{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013449 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13450 if (to_vmx(vcpu)->nested.nested_run_pending)
13451 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013452 return 1;
13453}
13454
Ladi Prosek0234bf82017-10-11 16:54:40 +020013455static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13456{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013457 struct vcpu_vmx *vmx = to_vmx(vcpu);
13458
13459 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13460 if (vmx->nested.smm.guest_mode)
13461 nested_vmx_vmexit(vcpu, -1, 0, 0);
13462
13463 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13464 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013465 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013466 return 0;
13467}
13468
13469static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13470{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013471 struct vcpu_vmx *vmx = to_vmx(vcpu);
13472 int ret;
13473
13474 if (vmx->nested.smm.vmxon) {
13475 vmx->nested.vmxon = true;
13476 vmx->nested.smm.vmxon = false;
13477 }
13478
13479 if (vmx->nested.smm.guest_mode) {
13480 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013481 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013482 vcpu->arch.hflags |= HF_SMM_MASK;
13483 if (ret)
13484 return ret;
13485
13486 vmx->nested.smm.guest_mode = false;
13487 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013488 return 0;
13489}
13490
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013491static int enable_smi_window(struct kvm_vcpu *vcpu)
13492{
13493 return 0;
13494}
13495
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013496static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13497 struct kvm_nested_state __user *user_kvm_nested_state,
13498 u32 user_data_size)
13499{
13500 struct vcpu_vmx *vmx;
13501 struct vmcs12 *vmcs12;
13502 struct kvm_nested_state kvm_state = {
13503 .flags = 0,
13504 .format = 0,
13505 .size = sizeof(kvm_state),
13506 .vmx.vmxon_pa = -1ull,
13507 .vmx.vmcs_pa = -1ull,
13508 };
13509
13510 if (!vcpu)
13511 return kvm_state.size + 2 * VMCS12_SIZE;
13512
13513 vmx = to_vmx(vcpu);
13514 vmcs12 = get_vmcs12(vcpu);
13515 if (nested_vmx_allowed(vcpu) &&
13516 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13517 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13518 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13519
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013520 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013521 kvm_state.size += VMCS12_SIZE;
13522
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013523 if (is_guest_mode(vcpu) &&
13524 nested_cpu_has_shadow_vmcs(vmcs12) &&
13525 vmcs12->vmcs_link_pointer != -1ull)
13526 kvm_state.size += VMCS12_SIZE;
13527 }
13528
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013529 if (vmx->nested.smm.vmxon)
13530 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13531
13532 if (vmx->nested.smm.guest_mode)
13533 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13534
13535 if (is_guest_mode(vcpu)) {
13536 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13537
13538 if (vmx->nested.nested_run_pending)
13539 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13540 }
13541 }
13542
13543 if (user_data_size < kvm_state.size)
13544 goto out;
13545
13546 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13547 return -EFAULT;
13548
13549 if (vmx->nested.current_vmptr == -1ull)
13550 goto out;
13551
13552 /*
13553 * When running L2, the authoritative vmcs12 state is in the
13554 * vmcs02. When running L1, the authoritative vmcs12 state is
13555 * in the shadow vmcs linked to vmcs01, unless
13556 * sync_shadow_vmcs is set, in which case, the authoritative
13557 * vmcs12 state is in the vmcs12 already.
13558 */
13559 if (is_guest_mode(vcpu))
13560 sync_vmcs12(vcpu, vmcs12);
13561 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13562 copy_shadow_to_vmcs12(vmx);
13563
13564 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13565 return -EFAULT;
13566
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013567 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13568 vmcs12->vmcs_link_pointer != -1ull) {
13569 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13570 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13571 return -EFAULT;
13572 }
13573
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013574out:
13575 return kvm_state.size;
13576}
13577
13578static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13579 struct kvm_nested_state __user *user_kvm_nested_state,
13580 struct kvm_nested_state *kvm_state)
13581{
13582 struct vcpu_vmx *vmx = to_vmx(vcpu);
13583 struct vmcs12 *vmcs12;
13584 u32 exit_qual;
13585 int ret;
13586
13587 if (kvm_state->format != 0)
13588 return -EINVAL;
13589
13590 if (!nested_vmx_allowed(vcpu))
13591 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13592
13593 if (kvm_state->vmx.vmxon_pa == -1ull) {
13594 if (kvm_state->vmx.smm.flags)
13595 return -EINVAL;
13596
13597 if (kvm_state->vmx.vmcs_pa != -1ull)
13598 return -EINVAL;
13599
13600 vmx_leave_nested(vcpu);
13601 return 0;
13602 }
13603
13604 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13605 return -EINVAL;
13606
13607 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13608 return -EINVAL;
13609
13610 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13611 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13612 return -EINVAL;
13613
13614 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13615 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13616 return -EINVAL;
13617
13618 if (kvm_state->vmx.smm.flags &
13619 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13620 return -EINVAL;
13621
13622 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13623 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13624 return -EINVAL;
13625
13626 vmx_leave_nested(vcpu);
13627 if (kvm_state->vmx.vmxon_pa == -1ull)
13628 return 0;
13629
13630 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13631 ret = enter_vmx_operation(vcpu);
13632 if (ret)
13633 return ret;
13634
13635 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13636
13637 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13638 vmx->nested.smm.vmxon = true;
13639 vmx->nested.vmxon = false;
13640
13641 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13642 vmx->nested.smm.guest_mode = true;
13643 }
13644
13645 vmcs12 = get_vmcs12(vcpu);
13646 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13647 return -EFAULT;
13648
Liran Alon392b2f22018-06-23 02:35:01 +030013649 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013650 return -EINVAL;
13651
13652 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13653 return 0;
13654
13655 vmx->nested.nested_run_pending =
13656 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13657
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013658 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13659 vmcs12->vmcs_link_pointer != -1ull) {
13660 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13661 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13662 return -EINVAL;
13663
13664 if (copy_from_user(shadow_vmcs12,
13665 user_kvm_nested_state->data + VMCS12_SIZE,
13666 sizeof(*vmcs12)))
13667 return -EFAULT;
13668
13669 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13670 !shadow_vmcs12->hdr.shadow_vmcs)
13671 return -EINVAL;
13672 }
13673
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013674 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13675 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13676 return -EINVAL;
13677
13678 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13679 vmx->nested.nested_run_pending = 1;
13680
13681 vmx->nested.dirty_vmcs12 = true;
13682 ret = enter_vmx_non_root_mode(vcpu, NULL);
13683 if (ret)
13684 return -EINVAL;
13685
13686 return 0;
13687}
13688
Kees Cook404f6aa2016-08-08 16:29:06 -070013689static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013690 .cpu_has_kvm_support = cpu_has_kvm_support,
13691 .disabled_by_bios = vmx_disabled_by_bios,
13692 .hardware_setup = hardware_setup,
13693 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013694 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013695 .hardware_enable = hardware_enable,
13696 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013697 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013698 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013699
Wanpeng Lib31c1142018-03-12 04:53:04 -070013700 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013701 .vm_alloc = vmx_vm_alloc,
13702 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013703
Avi Kivity6aa8b732006-12-10 02:21:36 -080013704 .vcpu_create = vmx_create_vcpu,
13705 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013706 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013707
Sean Christopherson6d6095b2018-07-23 12:32:44 -070013708 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013709 .vcpu_load = vmx_vcpu_load,
13710 .vcpu_put = vmx_vcpu_put,
13711
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013712 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013713 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013714 .get_msr = vmx_get_msr,
13715 .set_msr = vmx_set_msr,
13716 .get_segment_base = vmx_get_segment_base,
13717 .get_segment = vmx_get_segment,
13718 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013719 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013720 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013721 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013722 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013723 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013724 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013725 .set_cr3 = vmx_set_cr3,
13726 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013727 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013728 .get_idt = vmx_get_idt,
13729 .set_idt = vmx_set_idt,
13730 .get_gdt = vmx_get_gdt,
13731 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013732 .get_dr6 = vmx_get_dr6,
13733 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013734 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013735 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013736 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013737 .get_rflags = vmx_get_rflags,
13738 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013739
Avi Kivity6aa8b732006-12-10 02:21:36 -080013740 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013741 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013742
Avi Kivity6aa8b732006-12-10 02:21:36 -080013743 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013744 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013745 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013746 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13747 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013748 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013749 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013750 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013751 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013752 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013753 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013754 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013755 .get_nmi_mask = vmx_get_nmi_mask,
13756 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013757 .enable_nmi_window = enable_nmi_window,
13758 .enable_irq_window = enable_irq_window,
13759 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013760 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013761 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013762 .get_enable_apicv = vmx_get_enable_apicv,
13763 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013764 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013765 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013766 .hwapic_irr_update = vmx_hwapic_irr_update,
13767 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013768 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13769 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013770
Izik Eiduscbc94022007-10-25 00:29:55 +020013771 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013772 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013773 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013774 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013775
Avi Kivity586f9602010-11-18 13:09:54 +020013776 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013777
Sheng Yang17cc3932010-01-05 19:02:27 +080013778 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013779
13780 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013781
13782 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013783 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013784
13785 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013786
13787 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013788
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013789 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013790 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013791
13792 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013793
13794 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013795 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013796 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013797 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013798 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013799
13800 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013801
13802 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013803
13804 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13805 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13806 .flush_log_dirty = vmx_flush_log_dirty,
13807 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013808 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013809
Feng Wubf9f6ac2015-09-18 22:29:55 +080013810 .pre_block = vmx_pre_block,
13811 .post_block = vmx_post_block,
13812
Wei Huang25462f72015-06-19 15:45:05 +020013813 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013814
13815 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013816
13817#ifdef CONFIG_X86_64
13818 .set_hv_timer = vmx_set_hv_timer,
13819 .cancel_hv_timer = vmx_cancel_hv_timer,
13820#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013821
13822 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013823
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013824 .get_nested_state = vmx_get_nested_state,
13825 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013826 .get_vmcs12_pages = nested_get_vmcs12_pages,
13827
Ladi Prosek72d7b372017-10-11 16:54:41 +020013828 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013829 .pre_enter_smm = vmx_pre_enter_smm,
13830 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013831 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013832};
13833
13834static int __init vmx_init(void)
13835{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013836 int r;
13837
13838#if IS_ENABLED(CONFIG_HYPERV)
13839 /*
13840 * Enlightened VMCS usage should be recommended and the host needs
13841 * to support eVMCS v1 or above. We can also disable eVMCS support
13842 * with module parameter.
13843 */
13844 if (enlightened_vmcs &&
13845 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13846 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13847 KVM_EVMCS_VERSION) {
13848 int cpu;
13849
13850 /* Check that we have assist pages on all online CPUs */
13851 for_each_online_cpu(cpu) {
13852 if (!hv_get_vp_assist_page(cpu)) {
13853 enlightened_vmcs = false;
13854 break;
13855 }
13856 }
13857
13858 if (enlightened_vmcs) {
13859 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13860 static_branch_enable(&enable_evmcs);
13861 }
13862 } else {
13863 enlightened_vmcs = false;
13864 }
13865#endif
13866
13867 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013868 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013869 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013870 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013871
Dave Young2965faa2015-09-09 15:38:55 -070013872#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013873 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13874 crash_vmclear_local_loaded_vmcss);
13875#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013876 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013877
He, Qingfdef3ad2007-04-30 09:45:24 +030013878 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013879}
13880
13881static void __exit vmx_exit(void)
13882{
Dave Young2965faa2015-09-09 15:38:55 -070013883#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013884 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013885 synchronize_rcu();
13886#endif
13887
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013888 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013889
13890#if IS_ENABLED(CONFIG_HYPERV)
13891 if (static_branch_unlikely(&enable_evmcs)) {
13892 int cpu;
13893 struct hv_vp_assist_page *vp_ap;
13894 /*
13895 * Reset everything to support using non-enlightened VMCS
13896 * access later (e.g. when we reload the module with
13897 * enlightened_vmcs=0)
13898 */
13899 for_each_online_cpu(cpu) {
13900 vp_ap = hv_get_vp_assist_page(cpu);
13901
13902 if (!vp_ap)
13903 continue;
13904
13905 vp_ap->current_nested_vmcs = 0;
13906 vp_ap->enlighten_vmentry = 0;
13907 }
13908
13909 static_branch_disable(&enable_evmcs);
13910 }
13911#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013912}
13913
13914module_init(vmx_init)
13915module_exit(vmx_exit)